unplugin-https-reverse-proxy 4.0.4 → 4.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -6
- package/dist/{chunk-5R33PUJP.js → chunk-7CA3EIIX.js} +681 -223
- package/dist/{chunk-2P7SUCQ3.js → chunk-EY3HXBJ4.js} +0 -26
- package/dist/chunk-RKHJN6KY.js +53 -0
- package/dist/esbuild.cjs +781 -300
- package/dist/esbuild.js +3 -2
- package/dist/index.cjs +781 -300
- package/dist/index.js +3 -2
- package/dist/nuxt.cjs +779 -290
- package/dist/nuxt.js +5 -9
- package/dist/privileged-helper.cjs +668 -0
- package/dist/privileged-helper.d.cts +2 -0
- package/dist/privileged-helper.d.ts +2 -0
- package/dist/privileged-helper.js +641 -0
- package/dist/rollup.cjs +781 -300
- package/dist/rollup.js +3 -2
- package/dist/rspack.cjs +781 -300
- package/dist/rspack.js +3 -2
- package/dist/utils.cjs +0 -28
- package/dist/utils.d.cts +1 -3
- package/dist/utils.d.ts +1 -3
- package/dist/utils.js +1 -5
- package/dist/vite.cjs +781 -300
- package/dist/vite.js +3 -2
- package/dist/webpack.cjs +781 -300
- package/dist/webpack.js +3 -2
- package/package.json +1 -4
- package/src/caddy/caddyfile.ts +1 -0
- package/src/caddy/index.ts +183 -110
- package/src/caddy/privileged.ts +118 -0
- package/src/caddy/utils.ts +0 -43
- package/src/index.ts +1 -15
- package/src/nuxt.ts +1 -6
- package/src/privileged/bootstrap.ts +174 -0
- package/src/privileged/client.ts +287 -0
- package/src/privileged/entry.ts +45 -0
- package/src/privileged/hosts-file.ts +142 -0
- package/src/privileged/launch.ts +193 -0
- package/src/privileged/ports.ts +45 -0
- package/src/privileged/protocol.ts +111 -0
- package/src/privileged/server.ts +465 -0
- package/src/privileged/session.ts +114 -0
- package/src/privileged/validation.ts +38 -0
- package/src/privileged/windows-elevation.ts +80 -0
- package/src/privileged-helper.ts +109 -0
- package/src/utils.ts +0 -28
- package/src/host/index.ts +0 -52
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import process from 'node:process'
|
|
2
|
+
import { startHelper } from './privileged/bootstrap'
|
|
3
|
+
|
|
4
|
+
interface ParsedArgs {
|
|
5
|
+
socket: string
|
|
6
|
+
token: string
|
|
7
|
+
hosts?: string
|
|
8
|
+
caddyBin: string
|
|
9
|
+
caddyConfig: string
|
|
10
|
+
ownerUid?: number
|
|
11
|
+
ownerGid?: number
|
|
12
|
+
caddyStorage?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseArgs(argv: string[]): ParsedArgs {
|
|
16
|
+
let socket = ''
|
|
17
|
+
let token = ''
|
|
18
|
+
let hosts: string | undefined
|
|
19
|
+
let caddyBin = ''
|
|
20
|
+
let caddyConfig = ''
|
|
21
|
+
let ownerUid: number | undefined
|
|
22
|
+
let ownerGid: number | undefined
|
|
23
|
+
let caddyStorage: string | undefined
|
|
24
|
+
|
|
25
|
+
for (let index = 0; index < argv.length; index++) {
|
|
26
|
+
const current = argv[index]
|
|
27
|
+
if (current === '--socket')
|
|
28
|
+
socket = argv[++index] ?? ''
|
|
29
|
+
else if (current === '--token')
|
|
30
|
+
token = argv[++index] ?? ''
|
|
31
|
+
else if (current === '--hosts')
|
|
32
|
+
hosts = argv[++index] ?? undefined
|
|
33
|
+
else if (current === '--caddy')
|
|
34
|
+
caddyBin = argv[++index] ?? ''
|
|
35
|
+
else if (current === '--caddy-config')
|
|
36
|
+
caddyConfig = argv[++index] ?? ''
|
|
37
|
+
else if (current === '--owner-uid')
|
|
38
|
+
ownerUid = Number(argv[++index])
|
|
39
|
+
else if (current === '--owner-gid')
|
|
40
|
+
ownerGid = Number(argv[++index])
|
|
41
|
+
else if (current === '--caddy-storage')
|
|
42
|
+
caddyStorage = argv[++index] ?? undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!socket || !token)
|
|
46
|
+
throw new Error('usage: privileged-helper --socket <path> --token <token> --caddy <bin> --caddy-config <path>')
|
|
47
|
+
if (!caddyBin || !caddyConfig)
|
|
48
|
+
throw new Error('privileged-helper requires --caddy and --caddy-config')
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
socket,
|
|
52
|
+
token,
|
|
53
|
+
hosts,
|
|
54
|
+
caddyBin,
|
|
55
|
+
caddyConfig,
|
|
56
|
+
ownerUid: Number.isInteger(ownerUid) ? ownerUid : undefined,
|
|
57
|
+
ownerGid: Number.isInteger(ownerGid) ? ownerGid : undefined,
|
|
58
|
+
caddyStorage,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Entry point of the process that runs with elevated privileges.
|
|
64
|
+
*
|
|
65
|
+
* It owns no product logic. The Caddy binary and Caddyfile are fixed on the
|
|
66
|
+
* command line, so requests on the socket can only say *when* to act, never
|
|
67
|
+
* what* to execute.
|
|
68
|
+
*/
|
|
69
|
+
async function main(): Promise<void> {
|
|
70
|
+
const args = parseArgs(process.argv.slice(2))
|
|
71
|
+
|
|
72
|
+
const helper = await startHelper({
|
|
73
|
+
socketPath: args.socket,
|
|
74
|
+
token: args.token,
|
|
75
|
+
hostsPath: args.hosts,
|
|
76
|
+
caddyBin: args.caddyBin,
|
|
77
|
+
caddyConfigPath: args.caddyConfig,
|
|
78
|
+
ownerUid: args.ownerUid,
|
|
79
|
+
ownerGid: args.ownerGid,
|
|
80
|
+
caddyStoragePath: args.caddyStorage,
|
|
81
|
+
caddyEnv: {
|
|
82
|
+
HOME: process.env.HOME ?? '',
|
|
83
|
+
// Keep Caddy's storage next to the invoking user's home even though the
|
|
84
|
+
// process runs as root.
|
|
85
|
+
XDG_DATA_HOME: process.env.XDG_DATA_HOME ?? '',
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
// Ctrl+C is delivered to the whole foreground process group. The plugin also
|
|
90
|
+
// receives it and will drop the socket, which is the signal this helper acts
|
|
91
|
+
// on; exiting here would race that cleanup.
|
|
92
|
+
process.on('SIGINT', () => {})
|
|
93
|
+
|
|
94
|
+
const shutdown = (): void => {
|
|
95
|
+
void helper.runtime.dispose().finally(() => process.exit(0))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
process.on('SIGTERM', shutdown)
|
|
99
|
+
process.on('SIGHUP', shutdown)
|
|
100
|
+
|
|
101
|
+
// A closed stdout (the parent died) must not interrupt the cleanup path.
|
|
102
|
+
process.stdout.on('error', () => {})
|
|
103
|
+
process.stderr.on('error', () => {})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
main().catch((error: Error) => {
|
|
107
|
+
process.stderr.write(`[uhrp-helper] fatal: ${error.message}\n`)
|
|
108
|
+
process.exit(1)
|
|
109
|
+
})
|
package/src/utils.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import { execSync } from 'node:child_process'
|
|
2
|
-
import { chmod, readdir, stat } from 'node:fs/promises'
|
|
3
1
|
import { networkInterfaces } from 'node:os'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import process from 'node:process'
|
|
6
2
|
import { createConsola } from 'consola'
|
|
7
3
|
import c from 'picocolors'
|
|
8
4
|
import qrcode from 'qrcode-terminal'
|
|
@@ -11,15 +7,6 @@ export const consola = createConsola({
|
|
|
11
7
|
level: 3,
|
|
12
8
|
}).withTag('reverse-proxy')
|
|
13
9
|
|
|
14
|
-
export async function chmodRecursive(_path: string, mode: number): Promise<void> {
|
|
15
|
-
await chmod(_path, mode)
|
|
16
|
-
if ((await stat(_path)).isDirectory()) {
|
|
17
|
-
const children = await readdir(_path)
|
|
18
|
-
for (const child of children)
|
|
19
|
-
await chmodRecursive(path.join(_path, child), mode)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
10
|
export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
|
|
24
11
|
let called = false
|
|
25
12
|
return (...args: any[]) => {
|
|
@@ -30,21 +17,6 @@ export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parame
|
|
|
30
17
|
}
|
|
31
18
|
}
|
|
32
19
|
|
|
33
|
-
export function isAdmin(): boolean {
|
|
34
|
-
if (process.platform === 'win32') {
|
|
35
|
-
try {
|
|
36
|
-
execSync('net session', { stdio: 'ignore' })
|
|
37
|
-
return true
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
return false
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
return !!(process.getuid && process.getuid() === 0)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
20
|
export function getIpv4List(): string[] {
|
|
49
21
|
const interfaces = networkInterfaces()
|
|
50
22
|
|
package/src/host/index.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { exec } from 'node:child_process'
|
|
2
|
-
import { chmodSync } from 'node:fs'
|
|
3
|
-
import process from 'node:process'
|
|
4
|
-
import { promisify } from 'node:util'
|
|
5
|
-
|
|
6
|
-
import hostile from 'hostile'
|
|
7
|
-
import { consola } from '../utils'
|
|
8
|
-
|
|
9
|
-
const _setHost = promisify(hostile.set)
|
|
10
|
-
const _removeHost = promisify(hostile.remove)
|
|
11
|
-
|
|
12
|
-
function getPathOfSystemHostsPath(): string {
|
|
13
|
-
return process.platform === 'win32'
|
|
14
|
-
? `${process.env.windir || 'C:\\WINDOWS'}\\system32\\drivers\\etc\\hosts`
|
|
15
|
-
: '/etc/hosts'
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function syncHost(): Promise<boolean> {
|
|
19
|
-
return new Promise<boolean>((resolve, _reject) => {
|
|
20
|
-
exec(
|
|
21
|
-
process.platform === 'win32'
|
|
22
|
-
? 'ipconfig /flushdns'
|
|
23
|
-
: 'sudo -E killall -HUP mDNSResponder',
|
|
24
|
-
(error, _stdout, _stderr) => {
|
|
25
|
-
if (error) {
|
|
26
|
-
consola.error(`exec error: ${error}`)
|
|
27
|
-
return resolve(false)
|
|
28
|
-
}
|
|
29
|
-
return resolve(true)
|
|
30
|
-
},
|
|
31
|
-
)
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function addHost(ip: string, host: string): Promise<boolean> {
|
|
36
|
-
if (process.platform !== 'win32')
|
|
37
|
-
chmodSync(getPathOfSystemHostsPath(), 0o777)
|
|
38
|
-
await _setHost('127.0.0.1', 'localhost')
|
|
39
|
-
await _setHost(ip, host)
|
|
40
|
-
if (ip === 'localhost')
|
|
41
|
-
await _setHost('127.0.0.1', host)
|
|
42
|
-
return await syncHost()
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function removeHost(ip: string, host: string): Promise<boolean> {
|
|
46
|
-
if (process.platform !== 'win32')
|
|
47
|
-
chmodSync(getPathOfSystemHostsPath(), 0o777)
|
|
48
|
-
await _removeHost(ip, host)
|
|
49
|
-
if (ip === 'localhost')
|
|
50
|
-
await _removeHost('127.0.0.1', host)
|
|
51
|
-
return await syncHost()
|
|
52
|
-
}
|