unplugin-https-reverse-proxy 4.1.0-beta.8 → 4.1.0
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 +2 -2
- package/dist/{chunk-ICH7VY4H.js → chunk-U6J2RMIR.js} +211 -109
- package/dist/esbuild.cjs +223 -121
- package/dist/esbuild.js +1 -1
- package/dist/index.cjs +223 -121
- package/dist/index.js +1 -1
- package/dist/nuxt.cjs +221 -121
- package/dist/nuxt.js +1 -1
- package/dist/privileged-helper.cjs +154 -56
- package/dist/privileged-helper.js +144 -46
- package/dist/rollup.cjs +223 -121
- package/dist/rollup.js +1 -1
- package/dist/rspack.cjs +223 -121
- package/dist/rspack.js +1 -1
- package/dist/vite.cjs +223 -121
- package/dist/vite.js +1 -1
- package/dist/webpack.cjs +223 -121
- package/dist/webpack.js +1 -1
- package/package.json +1 -1
- package/src/caddy/caddyfile.ts +2 -0
- package/src/caddy/config.ts +57 -34
- package/src/caddy/constants.ts +30 -1
- package/src/caddy/index.ts +44 -49
- package/src/caddy/privileged.ts +3 -0
- package/src/index.ts +5 -0
- package/src/privileged/bootstrap.ts +27 -7
- package/src/privileged/client.ts +1 -1
- package/src/privileged/launch.ts +95 -11
- package/src/privileged/path-guard.ts +106 -0
- package/src/privileged/server.ts +36 -4
- package/src/privileged/session.ts +3 -0
- package/src/privileged/windows-elevation.ts +91 -33
- package/src/privileged-helper.ts +49 -9
package/dist/nuxt.cjs
CHANGED
|
@@ -44,10 +44,10 @@ var import_unplugin = require("unplugin");
|
|
|
44
44
|
|
|
45
45
|
// src/caddy/index.ts
|
|
46
46
|
var import_node_child_process3 = require("child_process");
|
|
47
|
-
var
|
|
47
|
+
var import_node_fs5 = require("fs");
|
|
48
48
|
var import_promises2 = require("fs/promises");
|
|
49
49
|
var import_node_os5 = require("os");
|
|
50
|
-
var
|
|
50
|
+
var import_node_path6 = require("path");
|
|
51
51
|
var import_node_process8 = __toESM(require("process"), 1);
|
|
52
52
|
var import_got_cjs = require("got-cjs");
|
|
53
53
|
var import_http_proxy_agent = require("http-proxy-agent");
|
|
@@ -12940,6 +12940,7 @@ var ProxyType = type({
|
|
|
12940
12940
|
});
|
|
12941
12941
|
var CaddyContext = type({
|
|
12942
12942
|
caddy_root: "string",
|
|
12943
|
+
admin_socket: "string",
|
|
12943
12944
|
proxies: ProxyType.array()
|
|
12944
12945
|
});
|
|
12945
12946
|
function validateTemplateContext(ctx) {
|
|
@@ -12952,6 +12953,7 @@ var caddyTemplate = `{
|
|
|
12952
12953
|
debug
|
|
12953
12954
|
auto_https disable_redirects ignore_loaded_certs
|
|
12954
12955
|
skip_install_trust
|
|
12956
|
+
admin unix/{{ admin_socket }}
|
|
12955
12957
|
}
|
|
12956
12958
|
|
|
12957
12959
|
:7601 {
|
|
@@ -13049,11 +13051,20 @@ function resolvePath(path) {
|
|
|
13049
13051
|
return (0, import_node_path.join)(import_node_os2.default.homedir() || "", path.slice(1));
|
|
13050
13052
|
return path;
|
|
13051
13053
|
}
|
|
13052
|
-
|
|
13054
|
+
function defaultTempDir() {
|
|
13055
|
+
const fromEnv = import_node_process.default.env.TEMP || import_node_process.default.env.TMPDIR;
|
|
13056
|
+
if (fromEnv)
|
|
13057
|
+
return resolvePath(fromEnv);
|
|
13058
|
+
if (import_node_process.default.platform === "linux")
|
|
13059
|
+
return (0, import_node_path.join)(import_node_process.default.env.XDG_CACHE_HOME || (0, import_node_path.join)(import_node_os2.default.homedir(), ".cache"), "uhrp");
|
|
13060
|
+
return "/tmp";
|
|
13061
|
+
}
|
|
13062
|
+
var TEMP_DIR = resolvePath(import_node_process.default.env.UHRP_CADDY_PATH || defaultTempDir());
|
|
13053
13063
|
var caddyPath = (0, import_node_path.join)(TEMP_DIR, `caddy${import_node_process.default.platform === "win32" ? ".exe" : ""}`);
|
|
13054
13064
|
var caddyFilePath = (0, import_node_path.join)(TEMP_DIR, "CADDYFILE");
|
|
13055
13065
|
var caddyConfigPath = (0, import_node_path.join)(TEMP_DIR, ".CADDYFILE.config.json");
|
|
13056
13066
|
var caddyLockFilePath = (0, import_node_path.join)(TEMP_DIR, "caddylock");
|
|
13067
|
+
var caddyAdminSocketPath = (0, import_node_path.join)(TEMP_DIR, "caddy-admin.sock");
|
|
13057
13068
|
var supportList = [
|
|
13058
13069
|
{
|
|
13059
13070
|
platform: "dragonfly",
|
|
@@ -13176,12 +13187,12 @@ function isAlive(pid) {
|
|
|
13176
13187
|
return false;
|
|
13177
13188
|
}
|
|
13178
13189
|
}
|
|
13179
|
-
function readConfig() {
|
|
13180
|
-
if (!(0, import_node_fs.existsSync)(
|
|
13190
|
+
function readConfig(path = caddyConfigPath) {
|
|
13191
|
+
if (!(0, import_node_fs.existsSync)(path))
|
|
13181
13192
|
return {};
|
|
13182
13193
|
let parsed2;
|
|
13183
13194
|
try {
|
|
13184
|
-
parsed2 = JSON.parse((0, import_node_fs.readFileSync)(
|
|
13195
|
+
parsed2 = JSON.parse((0, import_node_fs.readFileSync)(path, "utf-8"));
|
|
13185
13196
|
} catch {
|
|
13186
13197
|
return {};
|
|
13187
13198
|
}
|
|
@@ -13192,53 +13203,54 @@ function readConfig() {
|
|
|
13192
13203
|
}
|
|
13193
13204
|
return alive;
|
|
13194
13205
|
}
|
|
13195
|
-
function writeConfig(proxies) {
|
|
13206
|
+
function writeConfig(proxies, path = caddyConfigPath) {
|
|
13196
13207
|
const content = JSON.stringify(proxies, null, 2);
|
|
13197
|
-
const temp = `${
|
|
13208
|
+
const temp = `${path}.${import_node_process2.default.pid}.tmp`;
|
|
13198
13209
|
(0, import_node_fs.writeFileSync)(temp, content);
|
|
13199
|
-
(0, import_node_fs.renameSync)(temp,
|
|
13200
|
-
}
|
|
13201
|
-
async function mutateConfig(lock3, mutate) {
|
|
13202
|
-
const release = await lock3.acquire();
|
|
13203
|
-
try {
|
|
13204
|
-
const proxies = readConfig();
|
|
13205
|
-
mutate(proxies);
|
|
13206
|
-
writeConfig(proxies);
|
|
13207
|
-
return proxies;
|
|
13208
|
-
} finally {
|
|
13209
|
-
release();
|
|
13210
|
-
}
|
|
13210
|
+
(0, import_node_fs.renameSync)(temp, path);
|
|
13211
13211
|
}
|
|
13212
13212
|
function useCaddyConfig(options = {}) {
|
|
13213
13213
|
const lock3 = options.lock ?? createFileLock();
|
|
13214
|
+
const configPath = options.configPath ?? caddyConfigPath;
|
|
13215
|
+
async function mutate(apply) {
|
|
13216
|
+
var _a4;
|
|
13217
|
+
const release = await lock3.acquire();
|
|
13218
|
+
try {
|
|
13219
|
+
const proxies = readConfig(configPath);
|
|
13220
|
+
apply(proxies);
|
|
13221
|
+
writeConfig(proxies, configPath);
|
|
13222
|
+
await ((_a4 = options.renderCaddyfile) == null ? void 0 : _a4.call(options, proxies));
|
|
13223
|
+
return proxies;
|
|
13224
|
+
} finally {
|
|
13225
|
+
release();
|
|
13226
|
+
}
|
|
13227
|
+
}
|
|
13214
13228
|
return {
|
|
13215
|
-
configJsonRef: () => (0, import_node_fs.existsSync)(
|
|
13229
|
+
configJsonRef: () => (0, import_node_fs.existsSync)(configPath) ? readConfig(configPath) : void 0,
|
|
13216
13230
|
async writeCaddyConfig(_proxies, key) {
|
|
13217
|
-
await
|
|
13231
|
+
await mutate((proxies) => {
|
|
13218
13232
|
for (const p of _proxies)
|
|
13219
13233
|
proxies[routeKey(p, key)] = { ...p, base: normalizeBase(p.base), ownerPid: import_node_process2.default.pid };
|
|
13220
13234
|
});
|
|
13221
13235
|
},
|
|
13222
13236
|
async removeProxy(key) {
|
|
13223
|
-
|
|
13237
|
+
await mutate((current) => {
|
|
13224
13238
|
for (const route of Object.keys(current)) {
|
|
13225
13239
|
if (route.endsWith(`#${key}`))
|
|
13226
13240
|
delete current[route];
|
|
13227
13241
|
}
|
|
13228
13242
|
});
|
|
13229
|
-
if (Object.keys(proxies).length === 0)
|
|
13230
|
-
removeConfigFile();
|
|
13231
13243
|
},
|
|
13232
|
-
readProxies: () => readConfig(),
|
|
13244
|
+
readProxies: () => readConfig(configPath),
|
|
13233
13245
|
async cleanUp() {
|
|
13234
|
-
removeConfigFile();
|
|
13246
|
+
removeConfigFile(configPath);
|
|
13235
13247
|
}
|
|
13236
13248
|
};
|
|
13237
13249
|
}
|
|
13238
|
-
function removeConfigFile() {
|
|
13250
|
+
function removeConfigFile(path = caddyConfigPath) {
|
|
13239
13251
|
try {
|
|
13240
|
-
if ((0, import_node_fs.existsSync)(
|
|
13241
|
-
(0, import_node_fs.unlinkSync)(
|
|
13252
|
+
if ((0, import_node_fs.existsSync)(path))
|
|
13253
|
+
(0, import_node_fs.unlinkSync)(path);
|
|
13242
13254
|
} catch {
|
|
13243
13255
|
}
|
|
13244
13256
|
}
|
|
@@ -13284,7 +13296,7 @@ function isCaddyReloadCancellation(line) {
|
|
|
13284
13296
|
// src/caddy/privileged.ts
|
|
13285
13297
|
var import_node_buffer2 = require("buffer");
|
|
13286
13298
|
var import_node_events = require("events");
|
|
13287
|
-
var
|
|
13299
|
+
var import_node_path5 = require("path");
|
|
13288
13300
|
var import_node_process7 = __toESM(require("process"), 1);
|
|
13289
13301
|
var import_node_url = require("url");
|
|
13290
13302
|
|
|
@@ -13320,16 +13332,18 @@ function findUp(from, relative, exists2) {
|
|
|
13320
13332
|
|
|
13321
13333
|
// src/privileged/session.ts
|
|
13322
13334
|
var import_node_buffer = require("buffer");
|
|
13323
|
-
var
|
|
13335
|
+
var import_node_fs4 = require("fs");
|
|
13324
13336
|
var import_promises = require("fs/promises");
|
|
13325
13337
|
var import_node_os4 = require("os");
|
|
13326
|
-
var
|
|
13338
|
+
var import_node_path4 = require("path");
|
|
13327
13339
|
var lockfile2 = __toESM(require("proper-lockfile"), 1);
|
|
13328
13340
|
|
|
13329
13341
|
// src/privileged/launch.ts
|
|
13330
13342
|
var import_node_child_process2 = require("child_process");
|
|
13331
13343
|
var import_node_crypto = require("crypto");
|
|
13344
|
+
var import_node_fs3 = require("fs");
|
|
13332
13345
|
var import_node_os3 = require("os");
|
|
13346
|
+
var import_node_path3 = require("path");
|
|
13333
13347
|
var import_node_process6 = __toESM(require("process"), 1);
|
|
13334
13348
|
|
|
13335
13349
|
// src/privileged/client.ts
|
|
@@ -13443,7 +13457,7 @@ async function connectClient(options) {
|
|
|
13443
13457
|
} catch (error) {
|
|
13444
13458
|
lastError = error;
|
|
13445
13459
|
const code = lastError.code;
|
|
13446
|
-
if (code === "EACCES" || lastError instanceof HelperConnectionError)
|
|
13460
|
+
if (code === "EACCES" || code === "EPERM" || lastError instanceof HelperConnectionError)
|
|
13447
13461
|
throw lastError;
|
|
13448
13462
|
await new Promise((resolve2) => setTimeout(resolve2, retryIntervalMs));
|
|
13449
13463
|
}
|
|
@@ -13575,39 +13589,78 @@ function createClient(socket, initialBuffer, callTimeoutMs) {
|
|
|
13575
13589
|
// src/privileged/windows-elevation.ts
|
|
13576
13590
|
var import_node_child_process = require("child_process");
|
|
13577
13591
|
var import_node_process5 = __toESM(require("process"), 1);
|
|
13592
|
+
var ERROR_CANCELLED = 1223;
|
|
13578
13593
|
function quotePowerShell(value2) {
|
|
13579
13594
|
return `'${value2.replace(/'/g, "''")}'`;
|
|
13580
13595
|
}
|
|
13596
|
+
function quoteWindowsArg(arg) {
|
|
13597
|
+
if (arg === "")
|
|
13598
|
+
return '""';
|
|
13599
|
+
if (!/[\s"]/.test(arg))
|
|
13600
|
+
return arg;
|
|
13601
|
+
let quoted = '"';
|
|
13602
|
+
let backslashes = 0;
|
|
13603
|
+
for (const char of arg) {
|
|
13604
|
+
if (char === "\\") {
|
|
13605
|
+
backslashes++;
|
|
13606
|
+
continue;
|
|
13607
|
+
}
|
|
13608
|
+
quoted += char === '"' ? `${"\\".repeat(backslashes * 2 + 1)}"` : `${"\\".repeat(backslashes)}${char}`;
|
|
13609
|
+
backslashes = 0;
|
|
13610
|
+
}
|
|
13611
|
+
return `${quoted}${"\\".repeat(backslashes * 2)}"`;
|
|
13612
|
+
}
|
|
13581
13613
|
function buildElevationCommand(options) {
|
|
13582
|
-
const
|
|
13583
|
-
|
|
13584
|
-
"
|
|
13585
|
-
"-
|
|
13586
|
-
quotePowerShell(options.file)
|
|
13614
|
+
const lines = [
|
|
13615
|
+
// The message read back on failure is localized; keep it readable as UTF-8.
|
|
13616
|
+
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
|
|
13617
|
+
"$psi = New-Object System.Diagnostics.ProcessStartInfo",
|
|
13618
|
+
`$psi.FileName = ${quotePowerShell(options.file)}`,
|
|
13619
|
+
`$psi.Arguments = ${quotePowerShell(options.args.map(quoteWindowsArg).join(" "))}`
|
|
13587
13620
|
];
|
|
13588
|
-
if (argumentList)
|
|
13589
|
-
parts.push("-ArgumentList", argumentList);
|
|
13590
13621
|
if (options.cwd)
|
|
13591
|
-
|
|
13592
|
-
|
|
13593
|
-
|
|
13594
|
-
|
|
13622
|
+
lines.push(`$psi.WorkingDirectory = ${quotePowerShell(options.cwd)}`);
|
|
13623
|
+
lines.push(
|
|
13624
|
+
"$psi.Verb = 'runas'",
|
|
13625
|
+
"$psi.UseShellExecute = $true",
|
|
13626
|
+
"$psi.WindowStyle = 'Hidden'",
|
|
13627
|
+
"try { $p = [System.Diagnostics.Process]::Start($psi); if ($p) { [Console]::Out.Write($p.Id) } }",
|
|
13628
|
+
`catch { $e = $_.Exception; while ($e.InnerException) { $e = $e.InnerException }; if ($e.NativeErrorCode -eq ${ERROR_CANCELLED}) { exit ${ERROR_CANCELLED} }; [Console]::Error.Write($e.Message); exit 1 }`
|
|
13629
|
+
);
|
|
13630
|
+
return lines.join("\n");
|
|
13595
13631
|
}
|
|
13596
13632
|
function spawnElevatedOnWindows(options) {
|
|
13597
|
-
return new Promise((resolve2
|
|
13598
|
-
const script = buildElevationCommand(options);
|
|
13633
|
+
return new Promise((resolve2) => {
|
|
13599
13634
|
const child = (0, import_node_child_process.spawn)(
|
|
13600
13635
|
"powershell.exe",
|
|
13601
|
-
["-NoProfile", "-NonInteractive", "-Command",
|
|
13602
|
-
{ stdio: "ignore", windowsHide: true }
|
|
13636
|
+
["-NoProfile", "-NonInteractive", "-Command", buildElevationCommand(options)],
|
|
13637
|
+
{ stdio: ["ignore", "pipe", "pipe"], windowsHide: true }
|
|
13603
13638
|
);
|
|
13604
|
-
|
|
13605
|
-
|
|
13606
|
-
|
|
13607
|
-
|
|
13608
|
-
|
|
13609
|
-
|
|
13610
|
-
));
|
|
13639
|
+
let stdout = "";
|
|
13640
|
+
let stderr = "";
|
|
13641
|
+
child.stdout.on("data", (data) => {
|
|
13642
|
+
stdout += data.toString();
|
|
13643
|
+
});
|
|
13644
|
+
child.stderr.on("data", (data) => {
|
|
13645
|
+
stderr = (stderr + data.toString()).slice(-4096);
|
|
13646
|
+
});
|
|
13647
|
+
child.once("error", (error) => resolve2({
|
|
13648
|
+
ok: false,
|
|
13649
|
+
denied: false,
|
|
13650
|
+
message: `could not start PowerShell to request administrator permission: ${error.message}`
|
|
13651
|
+
}));
|
|
13652
|
+
child.once("close", (code) => {
|
|
13653
|
+
if (code === 0) {
|
|
13654
|
+
const pid = Number(stdout.trim());
|
|
13655
|
+
return resolve2({ ok: true, pid: Number.isSafeInteger(pid) && pid > 0 ? pid : void 0 });
|
|
13656
|
+
}
|
|
13657
|
+
if (code === ERROR_CANCELLED)
|
|
13658
|
+
return resolve2({ ok: false, denied: true, message: "administrator permission was cancelled" });
|
|
13659
|
+
resolve2({
|
|
13660
|
+
ok: false,
|
|
13661
|
+
denied: false,
|
|
13662
|
+
message: `could not start the privileged helper as administrator (PowerShell exited with ${code}).${stderr.trim() ? ` ${stderr.trim()}` : ""}`
|
|
13663
|
+
});
|
|
13611
13664
|
});
|
|
13612
13665
|
});
|
|
13613
13666
|
}
|
|
@@ -13734,11 +13787,22 @@ async function startPrivilegedHelper(options) {
|
|
|
13734
13787
|
"--caddy",
|
|
13735
13788
|
options.caddyBin,
|
|
13736
13789
|
"--caddy-config",
|
|
13737
|
-
options.caddyConfigPath
|
|
13790
|
+
options.caddyConfigPath,
|
|
13791
|
+
...options.caddyAdminSocket ? ["--caddy-admin", options.caddyAdminSocket] : []
|
|
13738
13792
|
];
|
|
13739
13793
|
let child;
|
|
13794
|
+
let helperExited;
|
|
13740
13795
|
if (!direct && (0, import_node_os3.platform)() === "win32") {
|
|
13741
|
-
|
|
13796
|
+
const errorFile = (0, import_node_path3.join)((0, import_node_os3.tmpdir)(), `uhrp-helper-${(0, import_node_crypto.randomBytes)(6).toString("hex")}.log`);
|
|
13797
|
+
const elevated = await spawnElevatedOnWindows({
|
|
13798
|
+
file: helperEntry,
|
|
13799
|
+
args: [...childArgv, "--error-file", errorFile],
|
|
13800
|
+
cwd: import_node_process6.default.cwd()
|
|
13801
|
+
});
|
|
13802
|
+
if (!elevated.ok)
|
|
13803
|
+
throw elevated.denied ? new PrivilegeDeniedError(elevated.message) : new Error(elevated.message);
|
|
13804
|
+
if (elevated.pid !== void 0)
|
|
13805
|
+
helperExited = watchWindowsHelper(elevated.pid, errorFile);
|
|
13742
13806
|
} else {
|
|
13743
13807
|
const command = direct ? helperEntry : sudoCommand;
|
|
13744
13808
|
const spawnArgs = direct ? childArgv : ["-n", "env", ...forwardedArgs, helperEntry, ...childArgv];
|
|
@@ -13746,20 +13810,36 @@ async function startPrivilegedHelper(options) {
|
|
|
13746
13810
|
stdio: ["ignore", "pipe", "pipe"]
|
|
13747
13811
|
});
|
|
13748
13812
|
(_a4 = child.stdout) == null ? void 0 : _a4.on("data", (data) => log == null ? void 0 : log(data.toString().trimEnd()));
|
|
13749
|
-
|
|
13750
|
-
|
|
13813
|
+
let stderr = "";
|
|
13814
|
+
(_b3 = child.stderr) == null ? void 0 : _b3.on("data", (data) => {
|
|
13815
|
+
stderr = (stderr + data.toString()).slice(-4096);
|
|
13816
|
+
log == null ? void 0 : log(data.toString().trimEnd());
|
|
13817
|
+
});
|
|
13818
|
+
const exited = new Promise((resolve2) => {
|
|
13819
|
+
child.once("error", (error) => {
|
|
13820
|
+
stderr = error.message;
|
|
13821
|
+
});
|
|
13822
|
+
child.once("close", (code) => resolve2(code));
|
|
13823
|
+
});
|
|
13751
13824
|
const earlyExit = await Promise.race([
|
|
13752
13825
|
exited,
|
|
13753
13826
|
new Promise((resolve2) => setTimeout(() => resolve2(void 0), 500))
|
|
13754
13827
|
]);
|
|
13755
13828
|
if (earlyExit !== void 0 && earlyExit !== 0) {
|
|
13756
|
-
|
|
13757
|
-
|
|
13758
|
-
)
|
|
13829
|
+
const detail = stderr.trim();
|
|
13830
|
+
let guidance = "";
|
|
13831
|
+
if (/\b(?:EACCES|EPERM)\b/.test(detail))
|
|
13832
|
+
guidance = " Check the path named above and its parent directories. Fix ownership or access manually; do not use chmod 777.";
|
|
13833
|
+
else if (!direct && /^sudo:/m.test(detail))
|
|
13834
|
+
guidance = " Retry from a terminal where sudo can request permission, or ask an administrator to grant access.";
|
|
13835
|
+
throw new Error(`privileged helper exited with code ${earlyExit}.${detail ? ` ${detail}${guidance}` : " No diagnostic output from the helper or sudo."}`);
|
|
13759
13836
|
}
|
|
13760
13837
|
}
|
|
13761
13838
|
try {
|
|
13762
|
-
const
|
|
13839
|
+
const connecting = connectClient({ socketPath, token, timeoutMs: connectTimeoutMs });
|
|
13840
|
+
connecting.catch(() => {
|
|
13841
|
+
});
|
|
13842
|
+
const client = await (helperExited ? Promise.race([connecting, helperExited.promise]) : connecting);
|
|
13763
13843
|
return {
|
|
13764
13844
|
client,
|
|
13765
13845
|
async close() {
|
|
@@ -13774,6 +13854,42 @@ async function startPrivilegedHelper(options) {
|
|
|
13774
13854
|
} catch {
|
|
13775
13855
|
}
|
|
13776
13856
|
throw error;
|
|
13857
|
+
} finally {
|
|
13858
|
+
helperExited == null ? void 0 : helperExited.stop();
|
|
13859
|
+
}
|
|
13860
|
+
}
|
|
13861
|
+
function watchWindowsHelper(pid, errorFile) {
|
|
13862
|
+
let timer;
|
|
13863
|
+
const promise = new Promise((_, reject2) => {
|
|
13864
|
+
timer = setInterval(() => {
|
|
13865
|
+
if (isProcessRunning(pid))
|
|
13866
|
+
return;
|
|
13867
|
+
clearInterval(timer);
|
|
13868
|
+
let detail = "";
|
|
13869
|
+
try {
|
|
13870
|
+
detail = (0, import_node_fs3.readFileSync)(errorFile, "utf8").trim().slice(-4096);
|
|
13871
|
+
} catch {
|
|
13872
|
+
}
|
|
13873
|
+
reject2(new Error(`privileged helper exited before accepting connections.${detail ? ` ${detail}` : " No diagnostic output from the helper."}`));
|
|
13874
|
+
}, 250);
|
|
13875
|
+
});
|
|
13876
|
+
return {
|
|
13877
|
+
promise,
|
|
13878
|
+
stop() {
|
|
13879
|
+
clearInterval(timer);
|
|
13880
|
+
try {
|
|
13881
|
+
(0, import_node_fs3.rmSync)(errorFile, { force: true });
|
|
13882
|
+
} catch {
|
|
13883
|
+
}
|
|
13884
|
+
}
|
|
13885
|
+
};
|
|
13886
|
+
}
|
|
13887
|
+
function isProcessRunning(pid) {
|
|
13888
|
+
try {
|
|
13889
|
+
import_node_process6.default.kill(pid, 0);
|
|
13890
|
+
return true;
|
|
13891
|
+
} catch (error) {
|
|
13892
|
+
return error.code !== "ESRCH";
|
|
13777
13893
|
}
|
|
13778
13894
|
}
|
|
13779
13895
|
async function connectExistingHelper(socketPath, token, timeoutMs = 1500) {
|
|
@@ -13786,12 +13902,12 @@ async function connectExistingHelper(socketPath, token, timeoutMs = 1500) {
|
|
|
13786
13902
|
|
|
13787
13903
|
// src/privileged/session.ts
|
|
13788
13904
|
function tokenPath(runtimeDir) {
|
|
13789
|
-
return (0,
|
|
13905
|
+
return (0, import_node_path4.join)(runtimeDir, "helper.token");
|
|
13790
13906
|
}
|
|
13791
13907
|
function sessionSocketPath(runtimeDir) {
|
|
13792
13908
|
if ((0, import_node_os4.platform)() === "win32")
|
|
13793
13909
|
return `\\\\.\\pipe\\uhrp-${import_node_buffer.Buffer.from(runtimeDir).toString("hex").slice(0, 40)}`;
|
|
13794
|
-
return (0,
|
|
13910
|
+
return (0, import_node_path4.join)(runtimeDir, "helper.sock");
|
|
13795
13911
|
}
|
|
13796
13912
|
async function openHelperSession(scope2) {
|
|
13797
13913
|
const socketPath = sessionSocketPath(scope2.runtimeDir);
|
|
@@ -13806,7 +13922,7 @@ async function openHelperSession(scope2) {
|
|
|
13806
13922
|
}
|
|
13807
13923
|
}
|
|
13808
13924
|
async function tryReuse(socketPath, scope2) {
|
|
13809
|
-
if (!(0,
|
|
13925
|
+
if (!(0, import_node_fs4.existsSync)(socketPath) && (0, import_node_os4.platform)() !== "win32")
|
|
13810
13926
|
return void 0;
|
|
13811
13927
|
const token = await readToken(scope2.runtimeDir);
|
|
13812
13928
|
if (!token)
|
|
@@ -13836,6 +13952,7 @@ async function startNew(socketPath, scope2) {
|
|
|
13836
13952
|
caddyStoragePath: scope2.caddyStoragePath,
|
|
13837
13953
|
caddyBin: scope2.caddyBin,
|
|
13838
13954
|
caddyConfigPath: scope2.caddyConfigPath,
|
|
13955
|
+
caddyAdminSocket: scope2.caddyAdminSocket,
|
|
13839
13956
|
reason: scope2.reason,
|
|
13840
13957
|
log: scope2.log
|
|
13841
13958
|
});
|
|
@@ -13849,7 +13966,7 @@ async function startNew(socketPath, scope2) {
|
|
|
13849
13966
|
};
|
|
13850
13967
|
}
|
|
13851
13968
|
async function acquireStartLock(runtimeDir) {
|
|
13852
|
-
const lockPath = (0,
|
|
13969
|
+
const lockPath = (0, import_node_path4.join)(runtimeDir, "helper.start.lock");
|
|
13853
13970
|
try {
|
|
13854
13971
|
await (0, import_promises.writeFile)(lockPath, "", { flag: "a" });
|
|
13855
13972
|
} catch {
|
|
@@ -13883,7 +14000,7 @@ async function writeToken(runtimeDir, token) {
|
|
|
13883
14000
|
// src/caddy/privileged.ts
|
|
13884
14001
|
var import_meta = {};
|
|
13885
14002
|
function resolvePrivilegedEnv() {
|
|
13886
|
-
const moduleDir = typeof __dirname === "string" ? __dirname : (0,
|
|
14003
|
+
const moduleDir = typeof __dirname === "string" ? __dirname : (0, import_node_path5.dirname)((0, import_node_url.fileURLToPath)(import_meta.url));
|
|
13887
14004
|
const resolved = resolveHelperEntry({ moduleDir });
|
|
13888
14005
|
return { helperEntry: resolved.entry, helperArgs: resolved.args };
|
|
13889
14006
|
}
|
|
@@ -13923,7 +14040,7 @@ var HelperCaddyProcess = class extends import_node_events.EventEmitter {
|
|
|
13923
14040
|
}
|
|
13924
14041
|
};
|
|
13925
14042
|
function caddyStoragePath() {
|
|
13926
|
-
return import_node_process7.default.platform === "win32" ? (0,
|
|
14043
|
+
return import_node_process7.default.platform === "win32" ? (0, import_node_path5.join)(import_node_process7.default.env.AppData ?? "", "Caddy") : (0, import_node_path5.join)(import_node_process7.default.env.HOME ?? "", "Library/Application Support/Caddy");
|
|
13927
14044
|
}
|
|
13928
14045
|
function openPrivilegedSession(runtimeDir, options) {
|
|
13929
14046
|
const env2 = resolvePrivilegedEnv();
|
|
@@ -13939,6 +14056,7 @@ function openPrivilegedSession(runtimeDir, options) {
|
|
|
13939
14056
|
// execute a path supplied over the socket.
|
|
13940
14057
|
caddyBin: options.caddyBin,
|
|
13941
14058
|
caddyConfigPath: options.caddyConfigPath,
|
|
14059
|
+
caddyAdminSocket: options.caddyAdminSocket,
|
|
13942
14060
|
reason: options.reason,
|
|
13943
14061
|
log: options.log
|
|
13944
14062
|
});
|
|
@@ -14066,11 +14184,11 @@ async function download() {
|
|
|
14066
14184
|
if (await testCaddy())
|
|
14067
14185
|
return caddyPath;
|
|
14068
14186
|
return new Promise((resolve2, reject2) => {
|
|
14069
|
-
if (!(0,
|
|
14070
|
-
(0,
|
|
14071
|
-
(0,
|
|
14187
|
+
if (!(0, import_node_fs5.existsSync)(TEMP_DIR)) {
|
|
14188
|
+
(0, import_node_fs5.mkdirSync)(TEMP_DIR, { recursive: true });
|
|
14189
|
+
(0, import_node_fs5.chmodSync)(TEMP_DIR, 448);
|
|
14072
14190
|
}
|
|
14073
|
-
const file = (0,
|
|
14191
|
+
const file = (0, import_node_fs5.createWriteStream)(caddyPath);
|
|
14074
14192
|
let _platform = (0, import_node_os5.platform)().toString();
|
|
14075
14193
|
_platform === "win32" && (_platform = "windows");
|
|
14076
14194
|
let _arch = import_node_process8.default.arch.toString();
|
|
@@ -14079,13 +14197,13 @@ async function download() {
|
|
|
14079
14197
|
if (!support)
|
|
14080
14198
|
return reject2(new Error("not support"));
|
|
14081
14199
|
const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}&p=github.com%2Fcaddyserver%2Fforwardproxy`;
|
|
14082
|
-
(0,
|
|
14200
|
+
(0, import_node_fs5.existsSync)(caddyPath) && (0, import_node_fs5.unlinkSync)(caddyPath);
|
|
14083
14201
|
const httpProxy = import_node_process8.default.env.HTTP_PROXY || import_node_process8.default.env.http_proxy;
|
|
14084
14202
|
const httpsProxy = import_node_process8.default.env.HTTPS_PROXY || import_node_process8.default.env.https_proxy;
|
|
14085
14203
|
const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
|
|
14086
14204
|
const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
|
|
14087
14205
|
const chmodCaddyOnce = once(() => {
|
|
14088
|
-
(0,
|
|
14206
|
+
(0, import_node_fs5.chmodSync)(caddyPath, 493);
|
|
14089
14207
|
});
|
|
14090
14208
|
import_got_cjs.got.stream(dowmloadLink, {
|
|
14091
14209
|
agent: {
|
|
@@ -14099,7 +14217,7 @@ async function download() {
|
|
|
14099
14217
|
logProgressOver();
|
|
14100
14218
|
if (import_node_process8.default.platform === "win32")
|
|
14101
14219
|
return resolve2(caddyPath);
|
|
14102
|
-
(0,
|
|
14220
|
+
(0, import_node_fs5.chmodSync)(caddyPath, 493);
|
|
14103
14221
|
resolve2(caddyPath);
|
|
14104
14222
|
}).on("error", (err) => {
|
|
14105
14223
|
reject2(err);
|
|
@@ -14108,10 +14226,10 @@ async function download() {
|
|
|
14108
14226
|
}
|
|
14109
14227
|
function testCaddy() {
|
|
14110
14228
|
return new Promise((resolve2) => {
|
|
14111
|
-
if (!(0,
|
|
14229
|
+
if (!(0, import_node_fs5.existsSync)(caddyPath))
|
|
14112
14230
|
return resolve2(false);
|
|
14113
14231
|
try {
|
|
14114
|
-
(0,
|
|
14232
|
+
(0, import_node_fs5.chmodSync)(caddyPath, 493);
|
|
14115
14233
|
} catch {
|
|
14116
14234
|
}
|
|
14117
14235
|
(0, import_node_child_process3.execFile)(caddyPath, [], { timeout: 1e4 }, (error, stdout) => {
|
|
@@ -14121,10 +14239,27 @@ function testCaddy() {
|
|
|
14121
14239
|
});
|
|
14122
14240
|
});
|
|
14123
14241
|
}
|
|
14242
|
+
function caddyRootPath() {
|
|
14243
|
+
return (0, import_node_path6.join)(
|
|
14244
|
+
import_node_process8.default.platform === "win32" ? `${import_node_process8.default.env.AppData}/Caddy` : `${import_node_process8.default.env.HOME}/Library/Application Support/Caddy`,
|
|
14245
|
+
"./pki/authorities/local"
|
|
14246
|
+
);
|
|
14247
|
+
}
|
|
14248
|
+
async function renderCaddyfile(proxies) {
|
|
14249
|
+
const ctx = validateTemplateContext({
|
|
14250
|
+
caddy_root: caddyRootPath(),
|
|
14251
|
+
admin_socket: caddyAdminSocketPath,
|
|
14252
|
+
proxies: Object.values(proxies)
|
|
14253
|
+
});
|
|
14254
|
+
const content = await genCaddyfile(ctx);
|
|
14255
|
+
const temp = `${caddyFilePath}.${import_node_process8.default.pid}.tmp`;
|
|
14256
|
+
await (0, import_promises2.writeFile)(temp, content);
|
|
14257
|
+
await (0, import_promises2.rename)(temp, caddyFilePath);
|
|
14258
|
+
}
|
|
14124
14259
|
var caddyConfig;
|
|
14125
14260
|
function getCaddyConfig() {
|
|
14126
14261
|
if (!caddyConfig) {
|
|
14127
|
-
caddyConfig = useCaddyConfig();
|
|
14262
|
+
caddyConfig = useCaddyConfig({ renderCaddyfile });
|
|
14128
14263
|
}
|
|
14129
14264
|
return caddyConfig;
|
|
14130
14265
|
}
|
|
@@ -14146,6 +14281,7 @@ var defaultDependencies = {
|
|
|
14146
14281
|
// helper must never execute something named in a request.
|
|
14147
14282
|
caddyBin: caddyPath,
|
|
14148
14283
|
caddyConfigPath: caddyFilePath,
|
|
14284
|
+
caddyAdminSocket: caddyAdminSocketPath,
|
|
14149
14285
|
reason
|
|
14150
14286
|
}),
|
|
14151
14287
|
config: () => getCaddyConfig()
|
|
@@ -14167,12 +14303,14 @@ var CaddyInstant = class {
|
|
|
14167
14303
|
async session(reason) {
|
|
14168
14304
|
if (this._session && !this._session.client.connected)
|
|
14169
14305
|
this._session = void 0;
|
|
14170
|
-
if (!this._session)
|
|
14306
|
+
if (!this._session) {
|
|
14307
|
+
await this.deps.config().writeCaddyConfig([], this.proxyKey);
|
|
14171
14308
|
this._session = await this.deps.openSession(TEMP_DIR, reason);
|
|
14309
|
+
}
|
|
14172
14310
|
return this._session;
|
|
14173
14311
|
}
|
|
14174
14312
|
async getCaddyfile() {
|
|
14175
|
-
if (!(0,
|
|
14313
|
+
if (!(0, import_node_fs5.existsSync)(caddyFilePath)) {
|
|
14176
14314
|
this.caddyfile = "";
|
|
14177
14315
|
return "";
|
|
14178
14316
|
}
|
|
@@ -14180,10 +14318,7 @@ var CaddyInstant = class {
|
|
|
14180
14318
|
return this.caddyfile;
|
|
14181
14319
|
}
|
|
14182
14320
|
get caddyRoot() {
|
|
14183
|
-
return (
|
|
14184
|
-
import_node_process8.default.platform === "win32" ? `${import_node_process8.default.env.AppData}/Caddy` : `${import_node_process8.default.env.HOME}/Library/Application Support/Caddy`,
|
|
14185
|
-
"./pki/authorities/local"
|
|
14186
|
-
);
|
|
14321
|
+
return caddyRootPath();
|
|
14187
14322
|
}
|
|
14188
14323
|
async writeCaddyfile() {
|
|
14189
14324
|
return await (0, import_promises2.writeFile)(caddyFilePath, this.caddyfile || "");
|
|
@@ -14227,40 +14362,6 @@ Run \`${caddyPath} trust\` manually if the browser warns about the certificate.`
|
|
|
14227
14362
|
if (!health.healthy)
|
|
14228
14363
|
throw new Error(formatTlsHealthFailure(targetHost, health));
|
|
14229
14364
|
}
|
|
14230
|
-
/**
|
|
14231
|
-
* Regenerates the Caddyfile from the config on disk.
|
|
14232
|
-
*
|
|
14233
|
-
* Reading the file rather than a cached view matters here: another project
|
|
14234
|
-
* may have added its route since this instance last looked.
|
|
14235
|
-
*/
|
|
14236
|
-
async updateCaddyfile() {
|
|
14237
|
-
const { readProxies } = this.deps.config();
|
|
14238
|
-
const proxies = Object.values(readProxies());
|
|
14239
|
-
const ctx = validateTemplateContext({
|
|
14240
|
-
caddy_root: this.caddyRoot,
|
|
14241
|
-
proxies
|
|
14242
|
-
});
|
|
14243
|
-
this.caddyfile = await genCaddyfile(ctx);
|
|
14244
|
-
await this.writeCaddyfile();
|
|
14245
|
-
}
|
|
14246
|
-
async stopCaddy() {
|
|
14247
|
-
var _a4;
|
|
14248
|
-
try {
|
|
14249
|
-
await import_got_cjs.got.post("http://127.0.0.1:2019/stop", {
|
|
14250
|
-
headers: {
|
|
14251
|
-
Origin: "http://localhost:2019"
|
|
14252
|
-
},
|
|
14253
|
-
retry: {
|
|
14254
|
-
limit: 0
|
|
14255
|
-
},
|
|
14256
|
-
timeout: {
|
|
14257
|
-
request: 2e3
|
|
14258
|
-
}
|
|
14259
|
-
});
|
|
14260
|
-
} catch {
|
|
14261
|
-
}
|
|
14262
|
-
(_a4 = this._caddyChild) == null ? void 0 : _a4.kill();
|
|
14263
|
-
}
|
|
14264
14365
|
/**
|
|
14265
14366
|
* @param source like `127.0.0.1:8080`
|
|
14266
14367
|
* @param target like `test.abc.com`. And if you provide a port ,it will be ignore.
|
|
@@ -14305,7 +14406,6 @@ Run \`${caddyPath} trust\` manually if the browser warns about the certificate.`
|
|
|
14305
14406
|
];
|
|
14306
14407
|
const { writeCaddyConfig } = this.deps.config();
|
|
14307
14408
|
await writeCaddyConfig(proxies, this.proxyKey);
|
|
14308
|
-
await this.updateCaddyfile();
|
|
14309
14409
|
await session.client.call("port.free", { port: 443 });
|
|
14310
14410
|
if (import_node_process8.default.platform === "win32" || !https)
|
|
14311
14411
|
await session.client.call("port.free", { port: 80 });
|