unplugin-https-reverse-proxy 0.0.11 → 0.0.13
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 +5 -2
- package/dist/{chunk-UGVWTB7L.js → chunk-6KXUNICB.js} +1 -1
- package/dist/chunk-CQC5SSJR.js +37 -0
- package/dist/{chunk-5K6D2TTS.js → chunk-DH6VMKTP.js} +181 -70
- package/dist/{chunk-BFAT7WWW.js → chunk-RTEXFW4L.js} +1 -1
- package/dist/esbuild.cjs +200 -68
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +200 -68
- package/dist/index.js +2 -2
- package/dist/nuxt.cjs +200 -68
- package/dist/nuxt.js +4 -4
- package/dist/rollup.cjs +200 -68
- package/dist/rollup.js +2 -2
- package/dist/types.d.cts +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/utils.cjs +13 -2
- package/dist/utils.d.cts +2 -1
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +5 -3
- package/dist/vite.cjs +200 -68
- package/dist/vite.js +3 -3
- package/dist/webpack.cjs +200 -68
- package/dist/webpack.js +3 -3
- package/package.json +3 -3
- package/src/caddy/index.ts +69 -58
- package/src/caddy/utils.ts +41 -0
- package/src/index.ts +103 -21
- package/src/types.ts +1 -0
- package/src/utils.ts +10 -0
- package/dist/chunk-3HKPYCSE.js +0 -9
package/dist/index.cjs
CHANGED
|
@@ -3,8 +3,22 @@ var __create = Object.create;
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
8
22
|
var __export = (target, all) => {
|
|
9
23
|
for (var name in all)
|
|
10
24
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -44,6 +58,15 @@ var import_consola = require("consola");
|
|
|
44
58
|
var consola = (0, import_consola.createConsola)({
|
|
45
59
|
level: 3
|
|
46
60
|
}).withTag("reverse-proxy");
|
|
61
|
+
function once(fn) {
|
|
62
|
+
let called = false;
|
|
63
|
+
return (...args) => {
|
|
64
|
+
if (called)
|
|
65
|
+
return;
|
|
66
|
+
called = true;
|
|
67
|
+
return fn(...args);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
47
70
|
|
|
48
71
|
// src/caddy/index.ts
|
|
49
72
|
var import_node_os = require("os");
|
|
@@ -53,7 +76,7 @@ var import_node_child_process = require("child_process");
|
|
|
53
76
|
var import_got_cjs = require("got-cjs");
|
|
54
77
|
var import_http_proxy_agent = require("http-proxy-agent");
|
|
55
78
|
var import_https_proxy_agent = require("https-proxy-agent");
|
|
56
|
-
var
|
|
79
|
+
var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
|
|
57
80
|
var import_kill_port = __toESM(require("kill-port"), 1);
|
|
58
81
|
|
|
59
82
|
// src/caddy/constants.ts
|
|
@@ -175,6 +198,8 @@ var supportList = [
|
|
|
175
198
|
|
|
176
199
|
// src/caddy/utils.ts
|
|
177
200
|
var import_node_net = require("net");
|
|
201
|
+
var import_ora = __toESM(require("ora"), 1);
|
|
202
|
+
var spinner;
|
|
178
203
|
function tryPort(port) {
|
|
179
204
|
return new Promise((resolve, reject) => {
|
|
180
205
|
try {
|
|
@@ -193,8 +218,41 @@ function tryPort(port) {
|
|
|
193
218
|
}
|
|
194
219
|
});
|
|
195
220
|
}
|
|
221
|
+
function getNumber(number) {
|
|
222
|
+
if (number <= 0)
|
|
223
|
+
return "0%";
|
|
224
|
+
else if (number < 100)
|
|
225
|
+
return `${number.toFixed(2)}%`;
|
|
226
|
+
else
|
|
227
|
+
return "100%";
|
|
228
|
+
}
|
|
229
|
+
function logProgress(percent) {
|
|
230
|
+
if (!spinner) {
|
|
231
|
+
consola.log("");
|
|
232
|
+
spinner = (0, import_ora.default)({
|
|
233
|
+
text: "Start Download Caddy: 0%",
|
|
234
|
+
indent: 2
|
|
235
|
+
});
|
|
236
|
+
spinner.start();
|
|
237
|
+
} else {
|
|
238
|
+
spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function logProgressOver() {
|
|
242
|
+
if (!spinner) {
|
|
243
|
+
spinner = (0, import_ora.default)({
|
|
244
|
+
text: "Start Download Caddy: 100%",
|
|
245
|
+
indent: 2
|
|
246
|
+
});
|
|
247
|
+
spinner.start();
|
|
248
|
+
} else {
|
|
249
|
+
spinner.succeed();
|
|
250
|
+
consola.log("");
|
|
251
|
+
}
|
|
252
|
+
}
|
|
196
253
|
|
|
197
254
|
// src/caddy/index.ts
|
|
255
|
+
var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
|
|
198
256
|
async function download() {
|
|
199
257
|
if (await testCaddy())
|
|
200
258
|
return caddyPath;
|
|
@@ -218,7 +276,10 @@ async function download() {
|
|
|
218
276
|
http: httpAgent,
|
|
219
277
|
https: httpsAgent
|
|
220
278
|
}
|
|
279
|
+
}).on("downloadProgress", (progress) => {
|
|
280
|
+
logProgress(progress.percent);
|
|
221
281
|
}).pipe(file).on("finish", () => {
|
|
282
|
+
logProgressOver();
|
|
222
283
|
if (import_node_process2.default.platform === "win32")
|
|
223
284
|
return resolve(caddyPath);
|
|
224
285
|
(0, import_node_fs.chmodSync)(caddyPath, 493);
|
|
@@ -233,7 +294,7 @@ function testCaddy() {
|
|
|
233
294
|
if (!(0, import_node_fs.existsSync)(caddyPath))
|
|
234
295
|
return resolve(false);
|
|
235
296
|
(0, import_node_fs.chmodSync)(caddyPath, 493);
|
|
236
|
-
const child = import_node_process2.default.platform === "win32" ? (0, import_node_child_process.spawn)(caddyPath, []) : (0, import_node_child_process.spawn)("sudo", [caddyPath]);
|
|
297
|
+
const child = import_node_process2.default.platform === "win32" ? (0, import_node_child_process.spawn)(caddyPath, []) : (0, import_node_child_process.spawn)("sudo", ["-E", caddyPath]);
|
|
237
298
|
child.on("close", () => {
|
|
238
299
|
return resolve(false);
|
|
239
300
|
});
|
|
@@ -248,6 +309,8 @@ function testCaddy() {
|
|
|
248
309
|
var CaddyInstant = class {
|
|
249
310
|
constructor() {
|
|
250
311
|
this.inited = false;
|
|
312
|
+
this.stoped = false;
|
|
313
|
+
this.host = new Hosts();
|
|
251
314
|
testCaddy().then(() => {
|
|
252
315
|
this.inited = true;
|
|
253
316
|
});
|
|
@@ -256,52 +319,46 @@ var CaddyInstant = class {
|
|
|
256
319
|
await download();
|
|
257
320
|
this.inited = true;
|
|
258
321
|
}
|
|
259
|
-
|
|
322
|
+
updateHost(ip, host) {
|
|
260
323
|
["localhost", "0.0.0.0"].includes(ip) && (ip = "127.0.0.1");
|
|
324
|
+
this.host.add(ip, host);
|
|
261
325
|
return new Promise((resolve, reject) => {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
});
|
|
326
|
+
if (import_node_process2.default.platform === "win32") {
|
|
327
|
+
(0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err) => {
|
|
328
|
+
consola.error(err);
|
|
329
|
+
return resolve(false);
|
|
330
|
+
}).on("close", () => {
|
|
331
|
+
resolve(true);
|
|
332
|
+
});
|
|
333
|
+
} else {
|
|
334
|
+
(0, import_node_child_process.spawn)("sudo", ["-E", "killall", "-HUP", "mDNSResponder"]).on("error", (err) => {
|
|
335
|
+
consola.error(err);
|
|
336
|
+
return resolve(false);
|
|
337
|
+
}).on("close", () => {
|
|
338
|
+
resolve(true);
|
|
339
|
+
});
|
|
340
|
+
}
|
|
281
341
|
});
|
|
282
342
|
}
|
|
283
|
-
|
|
343
|
+
restoreHost(ip, host) {
|
|
284
344
|
["localhost", "0.0.0.0"].includes(ip) && (ip = "127.0.0.1");
|
|
345
|
+
this.host.remove(ip, host);
|
|
285
346
|
return new Promise((resolve, reject) => {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
});
|
|
347
|
+
if (import_node_process2.default.platform === "win32") {
|
|
348
|
+
(0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err) => {
|
|
349
|
+
consola.error(err);
|
|
350
|
+
return resolve(false);
|
|
351
|
+
}).on("close", () => {
|
|
352
|
+
resolve(true);
|
|
353
|
+
});
|
|
354
|
+
} else {
|
|
355
|
+
(0, import_node_child_process.spawn)("sudo", ["-E", "killall", "-HUP", "mDNSResponder"]).on("error", (err) => {
|
|
356
|
+
consola.error(err);
|
|
357
|
+
return resolve(false);
|
|
358
|
+
}).on("close", () => {
|
|
359
|
+
resolve(true);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
305
362
|
});
|
|
306
363
|
}
|
|
307
364
|
/**
|
|
@@ -318,14 +375,15 @@ var CaddyInstant = class {
|
|
|
318
375
|
} = options || {};
|
|
319
376
|
if (!this.inited)
|
|
320
377
|
await this.init();
|
|
321
|
-
if (!await this.updateHost(source.split(":")[0], target.split(":")[0]))
|
|
322
|
-
throw new Error("update host failed");
|
|
323
378
|
if (await tryPort(443))
|
|
324
379
|
await (0, import_kill_port.default)(443, "tcp");
|
|
325
380
|
if (import_node_process2.default.platform === "win32" && await tryPort(80))
|
|
326
381
|
await (0, import_kill_port.default)(80, "tcp");
|
|
382
|
+
if (!await this.updateHost(source.split(":")[0], target.split(":")[0]))
|
|
383
|
+
throw new Error("update host failed");
|
|
384
|
+
consola.success("update host success\n");
|
|
327
385
|
return new Promise((resolve, reject) => {
|
|
328
|
-
const child = import_node_process2.default.platform !== "win32" ? (0, import_node_child_process.spawn)("sudo", [caddyPath, "reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]) : (0, import_node_child_process.spawn)(caddyPath, ["reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]);
|
|
386
|
+
const child = import_node_process2.default.platform !== "win32" ? (0, import_node_child_process.spawn)("sudo", ["-E", caddyPath, "reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]) : (0, import_node_child_process.spawn)(caddyPath, ["reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]);
|
|
329
387
|
child.on("error", (err) => {
|
|
330
388
|
return reject(err);
|
|
331
389
|
});
|
|
@@ -333,7 +391,7 @@ var CaddyInstant = class {
|
|
|
333
391
|
const lines = data.toString().split("\n").map((line) => line.trim());
|
|
334
392
|
for (const line of lines) {
|
|
335
393
|
showCaddyLog && line && console.info(line);
|
|
336
|
-
if (line && JSON.parse(line).level === "error") {
|
|
394
|
+
if (line.includes("Error:") || line && JSON.parse(line).level === "error") {
|
|
337
395
|
consola.error(line);
|
|
338
396
|
return reject(line);
|
|
339
397
|
}
|
|
@@ -341,14 +399,21 @@ var CaddyInstant = class {
|
|
|
341
399
|
});
|
|
342
400
|
child.stdout.on("data", (_data) => {
|
|
343
401
|
consola.info(_data.toString());
|
|
344
|
-
resolve(async () => {
|
|
345
|
-
if (!restore)
|
|
402
|
+
resolve(async (callback) => {
|
|
403
|
+
if (!restore || this.stoped)
|
|
346
404
|
return;
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
405
|
+
try {
|
|
406
|
+
if (await this.restoreHost(source.split(":")[0], target.split(":")[0]))
|
|
407
|
+
consola.success("restore host success\n");
|
|
408
|
+
else
|
|
409
|
+
consola.fail("restore host failed\n");
|
|
410
|
+
} catch (e) {
|
|
411
|
+
consola.error(e);
|
|
350
412
|
consola.fail("restore host failed\n");
|
|
413
|
+
}
|
|
414
|
+
this.stoped = true;
|
|
351
415
|
child.kill();
|
|
416
|
+
callback && callback();
|
|
352
417
|
});
|
|
353
418
|
});
|
|
354
419
|
});
|
|
@@ -358,21 +423,66 @@ var CaddyInstant = class {
|
|
|
358
423
|
// src/index.ts
|
|
359
424
|
var config;
|
|
360
425
|
var caddy;
|
|
426
|
+
var _stop;
|
|
427
|
+
var _servce;
|
|
428
|
+
function registerExit(clear) {
|
|
429
|
+
import_node_process3.default.on("beforeExit", async (_code) => {
|
|
430
|
+
await clear();
|
|
431
|
+
});
|
|
432
|
+
import_node_process3.default.on("uncaughtException", async (err) => {
|
|
433
|
+
console.error("An uncaught error occurred!", err.stack);
|
|
434
|
+
await clear();
|
|
435
|
+
import_node_process3.default.nextTick(() => {
|
|
436
|
+
import_node_process3.default.exit(1);
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
import_node_process3.default.on("unhandledRejection", async (reason, promise) => {
|
|
440
|
+
console.error("Unhandled Rejection at:", promise, "reason:", reason);
|
|
441
|
+
await clear();
|
|
442
|
+
import_node_process3.default.nextTick(() => {
|
|
443
|
+
import_node_process3.default.exit(1);
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
import_node_process3.default.once("SIGINT", async () => {
|
|
447
|
+
await clear();
|
|
448
|
+
import_node_process3.default.emit("SIGINT");
|
|
449
|
+
});
|
|
450
|
+
import_node_process3.default.once("SIGTERM", async () => {
|
|
451
|
+
await clear();
|
|
452
|
+
import_node_process3.default.emit("SIGTERM");
|
|
453
|
+
});
|
|
454
|
+
}
|
|
361
455
|
var unpluginFactory = (options) => ({
|
|
362
456
|
name: "unplugin-https-reverse-proxy",
|
|
363
|
-
enforce: "
|
|
457
|
+
enforce: "pre",
|
|
364
458
|
vite: {
|
|
365
459
|
configResolved(_config) {
|
|
366
460
|
config = _config;
|
|
461
|
+
registerExit(async () => {
|
|
462
|
+
var _a;
|
|
463
|
+
try {
|
|
464
|
+
_servce && _servce.close();
|
|
465
|
+
(_a = _servce.httpServer) == null ? void 0 : _a.close(async () => {
|
|
466
|
+
_stop && await _stop();
|
|
467
|
+
});
|
|
468
|
+
_stop && await _stop();
|
|
469
|
+
} catch (e) {
|
|
470
|
+
consola.error(e);
|
|
471
|
+
}
|
|
472
|
+
});
|
|
367
473
|
},
|
|
368
474
|
configureServer(server) {
|
|
369
475
|
const {
|
|
476
|
+
enable = true,
|
|
370
477
|
target = ""
|
|
371
478
|
} = options;
|
|
479
|
+
if (!enable)
|
|
480
|
+
return;
|
|
372
481
|
if (!target) {
|
|
373
482
|
consola.fail("please provide target");
|
|
374
483
|
return;
|
|
375
484
|
}
|
|
485
|
+
_servce = server;
|
|
376
486
|
const _printUrls = server.printUrls;
|
|
377
487
|
server.printUrls = () => {
|
|
378
488
|
var _a;
|
|
@@ -385,51 +495,73 @@ var unpluginFactory = (options) => ({
|
|
|
385
495
|
}
|
|
386
496
|
const base = server.config.base || "/";
|
|
387
497
|
try {
|
|
388
|
-
|
|
389
|
-
|
|
498
|
+
if (caddy)
|
|
499
|
+
return;
|
|
500
|
+
caddy = new CaddyInstant();
|
|
501
|
+
caddy.run(source, target, {
|
|
390
502
|
base,
|
|
391
503
|
showCaddyLog: options.showCaddyLog
|
|
392
504
|
}).then((stop) => {
|
|
393
505
|
const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
|
|
394
506
|
consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`https://${target}${base}`)}`);
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
import_node_process3.default.exit();
|
|
399
|
-
});
|
|
507
|
+
!_stop && (_stop = once(stop));
|
|
508
|
+
}).catch((e) => {
|
|
509
|
+
throw e;
|
|
400
510
|
});
|
|
401
511
|
} catch (e) {
|
|
402
512
|
consola.error(e);
|
|
403
513
|
}
|
|
404
514
|
};
|
|
515
|
+
},
|
|
516
|
+
async buildEnd() {
|
|
517
|
+
_stop && await _stop();
|
|
405
518
|
}
|
|
406
519
|
},
|
|
407
520
|
webpack(compiler) {
|
|
408
521
|
const {
|
|
522
|
+
enable = true,
|
|
409
523
|
target = ""
|
|
410
524
|
} = options;
|
|
525
|
+
if (!enable)
|
|
526
|
+
return;
|
|
411
527
|
if (!target) {
|
|
412
528
|
consola.fail("please provide target");
|
|
413
529
|
return;
|
|
414
530
|
}
|
|
415
531
|
try {
|
|
416
|
-
|
|
417
|
-
|
|
532
|
+
registerExit(async () => {
|
|
533
|
+
try {
|
|
534
|
+
_stop && await _stop();
|
|
535
|
+
} catch (e) {
|
|
536
|
+
consola.error(e);
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
const devServer = __spreadValues(__spreadValues({
|
|
540
|
+
host: "127.0.0.1",
|
|
541
|
+
port: 8080
|
|
542
|
+
}, compiler.options.devServer), import_node_process3.default.VUE_CLI_SERVICE.projectOptions.devServer);
|
|
543
|
+
const source = `${devServer.host}:${devServer.port}`;
|
|
418
544
|
if (caddy)
|
|
419
545
|
return;
|
|
546
|
+
const _close = compiler.close.bind(compiler);
|
|
547
|
+
compiler.close = async (callback) => {
|
|
548
|
+
try {
|
|
549
|
+
_stop && await _stop(() => {
|
|
550
|
+
_close && _close(callback);
|
|
551
|
+
});
|
|
552
|
+
} catch (e) {
|
|
553
|
+
consola.error(e);
|
|
554
|
+
import_node_process3.default.exit(1);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
420
557
|
caddy = new CaddyInstant();
|
|
421
558
|
caddy.run(source, target, {
|
|
422
559
|
showCaddyLog: options.showCaddyLog
|
|
423
560
|
}).then((stop) => {
|
|
424
561
|
const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
|
|
425
|
-
compiler.hooks.done.tap("unplugin-https-reverse-proxy", (
|
|
426
|
-
if (stats.hasErrors())
|
|
427
|
-
return;
|
|
562
|
+
compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
|
|
428
563
|
consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`https://${target}`)}`);
|
|
429
|
-
|
|
430
|
-
import_node_process3.default.on("SIGINT", async () => {
|
|
431
|
-
await stop();
|
|
432
|
-
import_node_process3.default.exit();
|
|
564
|
+
!_stop && (_stop = once(stop));
|
|
433
565
|
});
|
|
434
566
|
}).catch((e) => {
|
|
435
567
|
throw e;
|