unplugin-https-reverse-proxy 4.1.0-beta.2 → 4.1.0-beta.3

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/dist/vite.cjs CHANGED
@@ -13678,16 +13678,28 @@ var import_node_process2 = __toESM(require("process"), 1);
13678
13678
  function resolveHelperEntry(options) {
13679
13679
  const { moduleDir, nodeBin = import_node_process2.default.execPath, exists: exists2 = import_node_fs2.existsSync } = options;
13680
13680
  for (const name of ["privileged-helper.js", "privileged-helper.cjs", "privileged-helper.mjs"]) {
13681
- const candidate = (0, import_node_path2.join)(moduleDir, name);
13682
- if (exists2(candidate))
13681
+ const candidate = findUp(moduleDir, name, exists2);
13682
+ if (candidate)
13683
13683
  return { entry: nodeBin, args: [candidate] };
13684
13684
  }
13685
- const source = (0, import_node_path2.join)(moduleDir, "privileged-helper.ts");
13686
- const tsxCli = (0, import_node_path2.join)(moduleDir, "../node_modules/tsx/dist/cli.mjs");
13687
- if (exists2(source) && exists2(tsxCli))
13685
+ const source = findUp(moduleDir, "privileged-helper.ts", exists2);
13686
+ const tsxCli = findUp(moduleDir, "node_modules/tsx/dist/cli.mjs", exists2);
13687
+ if (source && tsxCli)
13688
13688
  return { entry: nodeBin, args: [tsxCli, source] };
13689
13689
  throw new Error(`privileged helper entry not found in ${moduleDir}. Build the package first.`);
13690
13690
  }
13691
+ function findUp(from, relative, exists2) {
13692
+ let current = from;
13693
+ while (true) {
13694
+ const candidate = (0, import_node_path2.join)(current, relative);
13695
+ if (exists2(candidate))
13696
+ return candidate;
13697
+ const parent = (0, import_node_path2.dirname)(current);
13698
+ if (parent === current)
13699
+ return void 0;
13700
+ current = parent;
13701
+ }
13702
+ }
13691
13703
 
13692
13704
  // src/privileged/session.ts
13693
13705
  var import_node_buffer = require("buffer");
@@ -13986,18 +13998,42 @@ function spawnElevatedOnWindows(options) {
13986
13998
  function createHelperToken() {
13987
13999
  return (0, import_node_crypto.randomBytes)(24).toString("hex");
13988
14000
  }
13989
- function authenticateSudo(sudoCommand = "sudo") {
14001
+ function describePasswordReason(target, https) {
14002
+ const port = https ? "443" : "80";
14003
+ return [
14004
+ `reverse proxy needs administrator permission for ${target}`,
14005
+ ` \xB7 bind port ${port}${https ? ` for https://${target}` : ""}`,
14006
+ ` \xB7 add ${target} to the hosts file`,
14007
+ "",
14008
+ "This is the only prompt; the rest of the dev server keeps running as you.",
14009
+ "Press Ctrl+C at the prompt to skip the proxy and keep the dev server."
14010
+ ];
14011
+ }
14012
+ function authenticateSudo(sudoCommand = "sudo", options = {}) {
13990
14013
  if ((0, import_node_os3.platform)() === "win32")
13991
14014
  return { ok: true };
13992
14015
  if ((0, import_node_child_process2.spawnSync)(sudoCommand, ["-n", "true"], { stdio: "ignore" }).status === 0)
13993
14016
  return { ok: true };
14017
+ if (options.target) {
14018
+ for (const line of describePasswordReason(options.target, options.https ?? false))
14019
+ import_node_process5.default.stderr.write(`${line}
14020
+ `);
14021
+ import_node_process5.default.stderr.write("\n");
14022
+ }
13994
14023
  if ((0, import_node_child_process2.spawnSync)(sudoCommand, ["-v"], { stdio: "inherit" }).status === 0)
13995
14024
  return { ok: true };
13996
14025
  return {
13997
14026
  ok: false,
14027
+ denied: true,
13998
14028
  message: "administrator permission is required to edit the hosts file and bind ports 80/443"
13999
14029
  };
14000
14030
  }
14031
+ var PrivilegeDeniedError = class extends Error {
14032
+ constructor(message) {
14033
+ super(message);
14034
+ this.name = "PrivilegeDeniedError";
14035
+ }
14036
+ };
14001
14037
  async function startPrivilegedHelper(options) {
14002
14038
  var _a4, _b3;
14003
14039
  const {
@@ -14013,9 +14049,11 @@ async function startPrivilegedHelper(options) {
14013
14049
  connectTimeoutMs = 12e4
14014
14050
  } = options;
14015
14051
  if (!skipAuth && !direct) {
14016
- const auth = authenticateSudo(sudoCommand);
14017
- if (!auth.ok)
14018
- throw new Error(auth.message);
14052
+ const auth = authenticateSudo(sudoCommand, options.reason);
14053
+ if (!auth.ok) {
14054
+ const message = auth.message ?? "administrator permission is required";
14055
+ throw auth.denied ? new PrivilegeDeniedError(message) : new Error(message);
14056
+ }
14019
14057
  }
14020
14058
  const forwarded = collectForwardedEnv(env2);
14021
14059
  const forwardedArgs = Object.entries(forwarded).map(([key, value2]) => `${key}=${value2}`);
@@ -14120,6 +14158,7 @@ async function openHelperSession(scope2) {
14120
14158
  caddyStoragePath: scope2.caddyStoragePath,
14121
14159
  caddyBin: scope2.caddyBin,
14122
14160
  caddyConfigPath: scope2.caddyConfigPath,
14161
+ reason: scope2.reason,
14123
14162
  log: scope2.log
14124
14163
  });
14125
14164
  await writeToken(scope2.runtimeDir, token);
@@ -14206,6 +14245,7 @@ function openPrivilegedSession(runtimeDir, options) {
14206
14245
  // execute a path supplied over the socket.
14207
14246
  caddyBin: options.caddyBin,
14208
14247
  caddyConfigPath: options.caddyConfigPath,
14248
+ reason: options.reason,
14209
14249
  log: options.log
14210
14250
  });
14211
14251
  }
@@ -14406,12 +14446,13 @@ function splitHostPort(addr) {
14406
14446
  return { host: addr.slice(0, lastColon), port: addr.slice(lastColon + 1) };
14407
14447
  }
14408
14448
  var defaultDependencies = {
14409
- openSession: (runtimeDir) => openPrivilegedSession(runtimeDir, {
14449
+ openSession: (runtimeDir, reason) => openPrivilegedSession(runtimeDir, {
14410
14450
  log: (message) => consola.debug(message),
14411
14451
  // Both paths are fixed here, before any socket traffic: the privileged
14412
14452
  // helper must never execute something named in a request.
14413
14453
  caddyBin: caddyPath,
14414
- caddyConfigPath: caddyFilePath
14454
+ caddyConfigPath: caddyFilePath,
14455
+ reason
14415
14456
  })
14416
14457
  };
14417
14458
  var CaddyInstant = class {
@@ -14438,11 +14479,11 @@ var CaddyInstant = class {
14438
14479
  * reused across instances so a second dev server does not ask for a
14439
14480
  * password again.
14440
14481
  */
14441
- async session() {
14482
+ async session(reason) {
14442
14483
  if (this._session && !this._session.client.connected)
14443
14484
  this._session = void 0;
14444
14485
  if (!this._session)
14445
- this._session = await this.deps.openSession(TEMP_DIR);
14486
+ this._session = await this.deps.openSession(TEMP_DIR, reason);
14446
14487
  return this._session;
14447
14488
  }
14448
14489
  async getCaddyfile() {
@@ -14547,9 +14588,9 @@ Run \`${caddyPath} trust\` manually if the browser warns about the certificate.`
14547
14588
  this._target = target;
14548
14589
  if (!this.inited)
14549
14590
  await this.init();
14550
- const session = await this.session();
14551
14591
  const sourceHost = splitHostPort(source).host;
14552
14592
  const targetHost = splitHostPort(target).host;
14593
+ const session = await this.session({ target: targetHost, https });
14553
14594
  await session.client.call("hosts.add", { ip: sourceHost, host: targetHost });
14554
14595
  consola.success("update host success\n");
14555
14596
  const normalizeBase = (b) => {
@@ -14981,6 +15022,15 @@ var hmrSseBridgeClientScript = `
14981
15022
  })();
14982
15023
  `;
14983
15024
 
15025
+ // src/privilege-state.ts
15026
+ var denied = false;
15027
+ function markPrivilegeDenied() {
15028
+ denied = true;
15029
+ }
15030
+ function isPrivilegeDenied() {
15031
+ return denied;
15032
+ }
15033
+
14984
15034
  // src/index.ts
14985
15035
  var config;
14986
15036
  var caddy;
@@ -15020,16 +15070,27 @@ async function printForwardProxy() {
15020
15070
  }
15021
15071
  var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
15022
15072
  _printUrls == null ? void 0 : _printUrls();
15073
+ if (isPrivilegeDenied())
15074
+ return;
15023
15075
  if (caddy)
15024
15076
  return;
15025
- caddy = new CaddyInstant();
15026
- caddy.run(source, target, {
15077
+ const instance = new CaddyInstant();
15078
+ caddy = instance;
15079
+ instance.run(source, target, {
15027
15080
  base,
15028
15081
  ...options
15029
15082
  }).then(async () => {
15030
15083
  await printForwardProxy();
15031
15084
  consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
15032
15085
  }).catch((e) => {
15086
+ if (caddy === instance)
15087
+ caddy = void 0;
15088
+ if (e instanceof PrivilegeDeniedError) {
15089
+ markPrivilegeDenied();
15090
+ consola.warn("reverse proxy skipped: administrator permission was not granted.");
15091
+ consola.info("Restart the dev server to enable it again.");
15092
+ return;
15093
+ }
15033
15094
  throw e;
15034
15095
  });
15035
15096
  });
@@ -15109,6 +15170,8 @@ var unpluginFactory = (options) => ({
15109
15170
  ...(_a4 = import_node_process8.default.VUE_CLI_SERVICE) == null ? void 0 : _a4.projectOptions.devServer
15110
15171
  };
15111
15172
  const source = `${devServer.host}:${devServer.port}`;
15173
+ if (isPrivilegeDenied())
15174
+ return;
15112
15175
  if (caddy)
15113
15176
  return;
15114
15177
  let base = "/";
@@ -15130,8 +15193,9 @@ var unpluginFactory = (options) => ({
15130
15193
  } else {
15131
15194
  options.https = false;
15132
15195
  }
15133
- caddy = new CaddyInstant();
15134
- caddy.run(source, target, {
15196
+ const instance = new CaddyInstant();
15197
+ caddy = instance;
15198
+ instance.run(source, target, {
15135
15199
  base,
15136
15200
  ...options
15137
15201
  }).then(() => {
@@ -15140,6 +15204,14 @@ var unpluginFactory = (options) => ({
15140
15204
  consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
15141
15205
  });
15142
15206
  }).catch((e) => {
15207
+ if (caddy === instance)
15208
+ caddy = void 0;
15209
+ if (e instanceof PrivilegeDeniedError) {
15210
+ markPrivilegeDenied();
15211
+ consola.warn("reverse proxy skipped: administrator permission was not granted.");
15212
+ consola.info("Restart the dev server to enable it again.");
15213
+ return;
15214
+ }
15143
15215
  throw e;
15144
15216
  });
15145
15217
  },
@@ -15170,9 +15242,10 @@ var unpluginFactory = (options) => ({
15170
15242
  }))) == null ? void 0 : _b3.content) == null ? void 0 : _c2.server
15171
15243
  };
15172
15244
  const source = `${devServer.host}:${devServer.port}`;
15245
+ if (isPrivilegeDenied())
15246
+ return;
15173
15247
  if (caddy)
15174
15248
  return;
15175
- caddy = new CaddyInstant();
15176
15249
  let base = "/";
15177
15250
  const publicPath = (_d2 = compiler.options.output) == null ? void 0 : _d2.publicPath;
15178
15251
  if (typeof publicPath === "string" && publicPath) {
@@ -15182,13 +15255,23 @@ var unpluginFactory = (options) => ({
15182
15255
  base = `/${base}`;
15183
15256
  if (base !== "/" && !base.endsWith("/"))
15184
15257
  base = `${base}/`;
15185
- caddy.run(source, target, {
15258
+ const instance = new CaddyInstant();
15259
+ caddy = instance;
15260
+ instance.run(source, target, {
15186
15261
  base,
15187
15262
  ...options
15188
15263
  }).then(async () => {
15189
15264
  await printForwardProxy();
15190
15265
  consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
15191
15266
  }).catch((e) => {
15267
+ if (caddy === instance)
15268
+ caddy = void 0;
15269
+ if (e instanceof PrivilegeDeniedError) {
15270
+ markPrivilegeDenied();
15271
+ consola.warn("reverse proxy skipped: administrator permission was not granted.");
15272
+ consola.info("Restart the dev server to enable it again.");
15273
+ return;
15274
+ }
15192
15275
  throw e;
15193
15276
  });
15194
15277
  }
package/dist/vite.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-7CA3EIIX.js";
3
+ } from "./chunk-HBXMM25C.js";
4
4
  import "./chunk-EY3HXBJ4.js";
5
5
  import "./chunk-C5A7XLKL.js";
6
+ import "./chunk-WOMR3ZLM.js";
6
7
  import "./chunk-RKHJN6KY.js";
7
8
 
8
9
  // src/vite.ts
package/dist/webpack.cjs CHANGED
@@ -13678,16 +13678,28 @@ var import_node_process2 = __toESM(require("process"), 1);
13678
13678
  function resolveHelperEntry(options) {
13679
13679
  const { moduleDir, nodeBin = import_node_process2.default.execPath, exists: exists2 = import_node_fs2.existsSync } = options;
13680
13680
  for (const name of ["privileged-helper.js", "privileged-helper.cjs", "privileged-helper.mjs"]) {
13681
- const candidate = (0, import_node_path2.join)(moduleDir, name);
13682
- if (exists2(candidate))
13681
+ const candidate = findUp(moduleDir, name, exists2);
13682
+ if (candidate)
13683
13683
  return { entry: nodeBin, args: [candidate] };
13684
13684
  }
13685
- const source = (0, import_node_path2.join)(moduleDir, "privileged-helper.ts");
13686
- const tsxCli = (0, import_node_path2.join)(moduleDir, "../node_modules/tsx/dist/cli.mjs");
13687
- if (exists2(source) && exists2(tsxCli))
13685
+ const source = findUp(moduleDir, "privileged-helper.ts", exists2);
13686
+ const tsxCli = findUp(moduleDir, "node_modules/tsx/dist/cli.mjs", exists2);
13687
+ if (source && tsxCli)
13688
13688
  return { entry: nodeBin, args: [tsxCli, source] };
13689
13689
  throw new Error(`privileged helper entry not found in ${moduleDir}. Build the package first.`);
13690
13690
  }
13691
+ function findUp(from, relative, exists2) {
13692
+ let current = from;
13693
+ while (true) {
13694
+ const candidate = (0, import_node_path2.join)(current, relative);
13695
+ if (exists2(candidate))
13696
+ return candidate;
13697
+ const parent = (0, import_node_path2.dirname)(current);
13698
+ if (parent === current)
13699
+ return void 0;
13700
+ current = parent;
13701
+ }
13702
+ }
13691
13703
 
13692
13704
  // src/privileged/session.ts
13693
13705
  var import_node_buffer = require("buffer");
@@ -13986,18 +13998,42 @@ function spawnElevatedOnWindows(options) {
13986
13998
  function createHelperToken() {
13987
13999
  return (0, import_node_crypto.randomBytes)(24).toString("hex");
13988
14000
  }
13989
- function authenticateSudo(sudoCommand = "sudo") {
14001
+ function describePasswordReason(target, https) {
14002
+ const port = https ? "443" : "80";
14003
+ return [
14004
+ `reverse proxy needs administrator permission for ${target}`,
14005
+ ` \xB7 bind port ${port}${https ? ` for https://${target}` : ""}`,
14006
+ ` \xB7 add ${target} to the hosts file`,
14007
+ "",
14008
+ "This is the only prompt; the rest of the dev server keeps running as you.",
14009
+ "Press Ctrl+C at the prompt to skip the proxy and keep the dev server."
14010
+ ];
14011
+ }
14012
+ function authenticateSudo(sudoCommand = "sudo", options = {}) {
13990
14013
  if ((0, import_node_os3.platform)() === "win32")
13991
14014
  return { ok: true };
13992
14015
  if ((0, import_node_child_process2.spawnSync)(sudoCommand, ["-n", "true"], { stdio: "ignore" }).status === 0)
13993
14016
  return { ok: true };
14017
+ if (options.target) {
14018
+ for (const line of describePasswordReason(options.target, options.https ?? false))
14019
+ import_node_process5.default.stderr.write(`${line}
14020
+ `);
14021
+ import_node_process5.default.stderr.write("\n");
14022
+ }
13994
14023
  if ((0, import_node_child_process2.spawnSync)(sudoCommand, ["-v"], { stdio: "inherit" }).status === 0)
13995
14024
  return { ok: true };
13996
14025
  return {
13997
14026
  ok: false,
14027
+ denied: true,
13998
14028
  message: "administrator permission is required to edit the hosts file and bind ports 80/443"
13999
14029
  };
14000
14030
  }
14031
+ var PrivilegeDeniedError = class extends Error {
14032
+ constructor(message) {
14033
+ super(message);
14034
+ this.name = "PrivilegeDeniedError";
14035
+ }
14036
+ };
14001
14037
  async function startPrivilegedHelper(options) {
14002
14038
  var _a4, _b3;
14003
14039
  const {
@@ -14013,9 +14049,11 @@ async function startPrivilegedHelper(options) {
14013
14049
  connectTimeoutMs = 12e4
14014
14050
  } = options;
14015
14051
  if (!skipAuth && !direct) {
14016
- const auth = authenticateSudo(sudoCommand);
14017
- if (!auth.ok)
14018
- throw new Error(auth.message);
14052
+ const auth = authenticateSudo(sudoCommand, options.reason);
14053
+ if (!auth.ok) {
14054
+ const message = auth.message ?? "administrator permission is required";
14055
+ throw auth.denied ? new PrivilegeDeniedError(message) : new Error(message);
14056
+ }
14019
14057
  }
14020
14058
  const forwarded = collectForwardedEnv(env2);
14021
14059
  const forwardedArgs = Object.entries(forwarded).map(([key, value2]) => `${key}=${value2}`);
@@ -14120,6 +14158,7 @@ async function openHelperSession(scope2) {
14120
14158
  caddyStoragePath: scope2.caddyStoragePath,
14121
14159
  caddyBin: scope2.caddyBin,
14122
14160
  caddyConfigPath: scope2.caddyConfigPath,
14161
+ reason: scope2.reason,
14123
14162
  log: scope2.log
14124
14163
  });
14125
14164
  await writeToken(scope2.runtimeDir, token);
@@ -14206,6 +14245,7 @@ function openPrivilegedSession(runtimeDir, options) {
14206
14245
  // execute a path supplied over the socket.
14207
14246
  caddyBin: options.caddyBin,
14208
14247
  caddyConfigPath: options.caddyConfigPath,
14248
+ reason: options.reason,
14209
14249
  log: options.log
14210
14250
  });
14211
14251
  }
@@ -14406,12 +14446,13 @@ function splitHostPort(addr) {
14406
14446
  return { host: addr.slice(0, lastColon), port: addr.slice(lastColon + 1) };
14407
14447
  }
14408
14448
  var defaultDependencies = {
14409
- openSession: (runtimeDir) => openPrivilegedSession(runtimeDir, {
14449
+ openSession: (runtimeDir, reason) => openPrivilegedSession(runtimeDir, {
14410
14450
  log: (message) => consola.debug(message),
14411
14451
  // Both paths are fixed here, before any socket traffic: the privileged
14412
14452
  // helper must never execute something named in a request.
14413
14453
  caddyBin: caddyPath,
14414
- caddyConfigPath: caddyFilePath
14454
+ caddyConfigPath: caddyFilePath,
14455
+ reason
14415
14456
  })
14416
14457
  };
14417
14458
  var CaddyInstant = class {
@@ -14438,11 +14479,11 @@ var CaddyInstant = class {
14438
14479
  * reused across instances so a second dev server does not ask for a
14439
14480
  * password again.
14440
14481
  */
14441
- async session() {
14482
+ async session(reason) {
14442
14483
  if (this._session && !this._session.client.connected)
14443
14484
  this._session = void 0;
14444
14485
  if (!this._session)
14445
- this._session = await this.deps.openSession(TEMP_DIR);
14486
+ this._session = await this.deps.openSession(TEMP_DIR, reason);
14446
14487
  return this._session;
14447
14488
  }
14448
14489
  async getCaddyfile() {
@@ -14547,9 +14588,9 @@ Run \`${caddyPath} trust\` manually if the browser warns about the certificate.`
14547
14588
  this._target = target;
14548
14589
  if (!this.inited)
14549
14590
  await this.init();
14550
- const session = await this.session();
14551
14591
  const sourceHost = splitHostPort(source).host;
14552
14592
  const targetHost = splitHostPort(target).host;
14593
+ const session = await this.session({ target: targetHost, https });
14553
14594
  await session.client.call("hosts.add", { ip: sourceHost, host: targetHost });
14554
14595
  consola.success("update host success\n");
14555
14596
  const normalizeBase = (b) => {
@@ -14981,6 +15022,15 @@ var hmrSseBridgeClientScript = `
14981
15022
  })();
14982
15023
  `;
14983
15024
 
15025
+ // src/privilege-state.ts
15026
+ var denied = false;
15027
+ function markPrivilegeDenied() {
15028
+ denied = true;
15029
+ }
15030
+ function isPrivilegeDenied() {
15031
+ return denied;
15032
+ }
15033
+
14984
15034
  // src/index.ts
14985
15035
  var config;
14986
15036
  var caddy;
@@ -15020,16 +15070,27 @@ async function printForwardProxy() {
15020
15070
  }
15021
15071
  var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
15022
15072
  _printUrls == null ? void 0 : _printUrls();
15073
+ if (isPrivilegeDenied())
15074
+ return;
15023
15075
  if (caddy)
15024
15076
  return;
15025
- caddy = new CaddyInstant();
15026
- caddy.run(source, target, {
15077
+ const instance = new CaddyInstant();
15078
+ caddy = instance;
15079
+ instance.run(source, target, {
15027
15080
  base,
15028
15081
  ...options
15029
15082
  }).then(async () => {
15030
15083
  await printForwardProxy();
15031
15084
  consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
15032
15085
  }).catch((e) => {
15086
+ if (caddy === instance)
15087
+ caddy = void 0;
15088
+ if (e instanceof PrivilegeDeniedError) {
15089
+ markPrivilegeDenied();
15090
+ consola.warn("reverse proxy skipped: administrator permission was not granted.");
15091
+ consola.info("Restart the dev server to enable it again.");
15092
+ return;
15093
+ }
15033
15094
  throw e;
15034
15095
  });
15035
15096
  });
@@ -15109,6 +15170,8 @@ var unpluginFactory = (options) => ({
15109
15170
  ...(_a4 = import_node_process8.default.VUE_CLI_SERVICE) == null ? void 0 : _a4.projectOptions.devServer
15110
15171
  };
15111
15172
  const source = `${devServer.host}:${devServer.port}`;
15173
+ if (isPrivilegeDenied())
15174
+ return;
15112
15175
  if (caddy)
15113
15176
  return;
15114
15177
  let base = "/";
@@ -15130,8 +15193,9 @@ var unpluginFactory = (options) => ({
15130
15193
  } else {
15131
15194
  options.https = false;
15132
15195
  }
15133
- caddy = new CaddyInstant();
15134
- caddy.run(source, target, {
15196
+ const instance = new CaddyInstant();
15197
+ caddy = instance;
15198
+ instance.run(source, target, {
15135
15199
  base,
15136
15200
  ...options
15137
15201
  }).then(() => {
@@ -15140,6 +15204,14 @@ var unpluginFactory = (options) => ({
15140
15204
  consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
15141
15205
  });
15142
15206
  }).catch((e) => {
15207
+ if (caddy === instance)
15208
+ caddy = void 0;
15209
+ if (e instanceof PrivilegeDeniedError) {
15210
+ markPrivilegeDenied();
15211
+ consola.warn("reverse proxy skipped: administrator permission was not granted.");
15212
+ consola.info("Restart the dev server to enable it again.");
15213
+ return;
15214
+ }
15143
15215
  throw e;
15144
15216
  });
15145
15217
  },
@@ -15170,9 +15242,10 @@ var unpluginFactory = (options) => ({
15170
15242
  }))) == null ? void 0 : _b3.content) == null ? void 0 : _c2.server
15171
15243
  };
15172
15244
  const source = `${devServer.host}:${devServer.port}`;
15245
+ if (isPrivilegeDenied())
15246
+ return;
15173
15247
  if (caddy)
15174
15248
  return;
15175
- caddy = new CaddyInstant();
15176
15249
  let base = "/";
15177
15250
  const publicPath = (_d2 = compiler.options.output) == null ? void 0 : _d2.publicPath;
15178
15251
  if (typeof publicPath === "string" && publicPath) {
@@ -15182,13 +15255,23 @@ var unpluginFactory = (options) => ({
15182
15255
  base = `/${base}`;
15183
15256
  if (base !== "/" && !base.endsWith("/"))
15184
15257
  base = `${base}/`;
15185
- caddy.run(source, target, {
15258
+ const instance = new CaddyInstant();
15259
+ caddy = instance;
15260
+ instance.run(source, target, {
15186
15261
  base,
15187
15262
  ...options
15188
15263
  }).then(async () => {
15189
15264
  await printForwardProxy();
15190
15265
  consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
15191
15266
  }).catch((e) => {
15267
+ if (caddy === instance)
15268
+ caddy = void 0;
15269
+ if (e instanceof PrivilegeDeniedError) {
15270
+ markPrivilegeDenied();
15271
+ consola.warn("reverse proxy skipped: administrator permission was not granted.");
15272
+ consola.info("Restart the dev server to enable it again.");
15273
+ return;
15274
+ }
15192
15275
  throw e;
15193
15276
  });
15194
15277
  }
package/dist/webpack.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-7CA3EIIX.js";
3
+ } from "./chunk-HBXMM25C.js";
4
4
  import "./chunk-EY3HXBJ4.js";
5
5
  import "./chunk-C5A7XLKL.js";
6
+ import "./chunk-WOMR3ZLM.js";
6
7
  import "./chunk-RKHJN6KY.js";
7
8
 
8
9
  // src/webpack.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unplugin-https-reverse-proxy",
3
3
  "type": "module",
4
- "version": "4.1.0-beta.2",
4
+ "version": "4.1.0-beta.3",
5
5
  "description": "",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/zcf0508/unplugin-https-reverse-proxy#readme",
@@ -132,16 +132,17 @@ function splitHostPort(addr: string): { host: string, port: string } {
132
132
 
133
133
  /** Injection point for tests: opens the privileged helper without sudo. */
134
134
  export interface CaddyDependencies {
135
- openSession: (runtimeDir: string) => Promise<OpenedPrivilegedSession>
135
+ openSession: (runtimeDir: string, reason?: { target: string, https: boolean }) => Promise<OpenedPrivilegedSession>
136
136
  }
137
137
 
138
138
  const defaultDependencies: CaddyDependencies = {
139
- openSession: runtimeDir => openPrivilegedSession(runtimeDir, {
139
+ openSession: (runtimeDir, reason) => openPrivilegedSession(runtimeDir, {
140
140
  log: message => consola.debug(message),
141
141
  // Both paths are fixed here, before any socket traffic: the privileged
142
142
  // helper must never execute something named in a request.
143
143
  caddyBin: caddyPath,
144
144
  caddyConfigPath: caddyFilePath,
145
+ reason,
145
146
  }),
146
147
  }
147
148
 
@@ -182,14 +183,14 @@ export class CaddyInstant {
182
183
  * reused across instances so a second dev server does not ask for a
183
184
  * password again.
184
185
  */
185
- private async session(): Promise<OpenedPrivilegedSession> {
186
+ private async session(reason?: { target: string, https: boolean }): Promise<OpenedPrivilegedSession> {
186
187
  // A crashed helper leaves the cached client permanently disconnected; drop
187
188
  // it so the next call starts a fresh helper instead of failing forever.
188
189
  if (this._session && !this._session.client.connected)
189
190
  this._session = undefined
190
191
 
191
192
  if (!this._session)
192
- this._session = await this.deps.openSession(TEMP_DIR)
193
+ this._session = await this.deps.openSession(TEMP_DIR, reason)
193
194
  return this._session
194
195
  }
195
196
 
@@ -313,10 +314,10 @@ export class CaddyInstant {
313
314
  if (!this.inited)
314
315
  await this.init()
315
316
 
316
- const session = await this.session()
317
-
318
317
  const sourceHost = splitHostPort(source).host
319
318
  const targetHost = splitHostPort(target).host
319
+
320
+ const session = await this.session({ target: targetHost, https })
320
321
  await session.client.call('hosts.add', { ip: sourceHost, host: targetHost })
321
322
  consola.success('update host success\n')
322
323
 
@@ -82,6 +82,8 @@ export interface PrivilegedSessionOptions {
82
82
  caddyBin: string
83
83
  /** Caddyfile the helper may load. */
84
84
  caddyConfigPath: string
85
+ /** Shown before the password prompt so the request is not a surprise. */
86
+ reason?: { target: string, https: boolean }
85
87
  }
86
88
 
87
89
  /** Where Caddy keeps its PKI, per platform. */
@@ -113,6 +115,7 @@ export function openPrivilegedSession(
113
115
  // execute a path supplied over the socket.
114
116
  caddyBin: options.caddyBin,
115
117
  caddyConfigPath: options.caddyConfigPath,
118
+ reason: options.reason,
116
119
  log: options.log,
117
120
  })
118
121
  }