unplugin-https-reverse-proxy 1.0.0-beta.2 → 1.0.0-beta.20
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 +105 -2
- package/dist/chunk-2VGRJ3QS.js +45 -0
- package/dist/chunk-I6DP2OII.js +695 -0
- package/dist/esbuild.cjs +334 -160
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +334 -160
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -2
- package/dist/nuxt.cjs +270 -234
- package/dist/nuxt.js +24 -22
- package/dist/rollup.cjs +334 -160
- package/dist/rollup.js +2 -2
- package/dist/rspack.cjs +760 -0
- package/dist/rspack.d.cts +5 -0
- package/dist/rspack.d.ts +5 -0
- package/dist/rspack.js +10 -0
- package/dist/types.cjs +1 -1
- package/dist/utils.cjs +2 -2
- package/dist/utils.js +1 -1
- package/dist/vite.cjs +334 -160
- package/dist/vite.d.cts +1 -2
- package/dist/vite.d.ts +1 -2
- package/dist/vite.js +7 -4
- package/dist/webpack.cjs +334 -160
- package/dist/webpack.js +7 -4
- package/package.json +48 -28
- package/scripts/postinstall.cjs +308 -0
- package/src/caddy/constants.ts +14 -1
- package/src/caddy/index.ts +230 -76
- package/src/caddy/utils.ts +18 -0
- package/src/host/index.ts +9 -15
- package/src/index.ts +110 -83
- package/src/nuxt.ts +26 -19
- package/src/rspack.ts +5 -0
- package/dist/chunk-DDD5N75Y.js +0 -11
- package/dist/chunk-N6ZBBGYD.js +0 -516
- package/dist/chunk-RUJE3YFB.js +0 -63
- package/dist/chunk-VGLXVBHU.js +0 -11
- package/scripts/postinstall.ts +0 -19
|
@@ -0,0 +1,695 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chmodRecursive,
|
|
3
|
+
consola,
|
|
4
|
+
isAdmin,
|
|
5
|
+
once
|
|
6
|
+
} from "./chunk-2VGRJ3QS.js";
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import process4 from "node:process";
|
|
10
|
+
import { createUnplugin } from "unplugin";
|
|
11
|
+
import c from "picocolors";
|
|
12
|
+
|
|
13
|
+
// src/caddy/index.ts
|
|
14
|
+
import { platform } from "node:os";
|
|
15
|
+
import process3 from "node:process";
|
|
16
|
+
import { chmodSync as chmodSync2, createWriteStream, existsSync, mkdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { spawn } from "node:child_process";
|
|
18
|
+
import { readFile, unlink, writeFile } from "node:fs/promises";
|
|
19
|
+
import { got } from "got-cjs";
|
|
20
|
+
import { HttpProxyAgent } from "http-proxy-agent";
|
|
21
|
+
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
22
|
+
import * as lockfile from "proper-lockfile";
|
|
23
|
+
|
|
24
|
+
// src/host/index.ts
|
|
25
|
+
import process from "node:process";
|
|
26
|
+
import { exec } from "node:child_process";
|
|
27
|
+
import { chmodSync } from "node:fs";
|
|
28
|
+
import { promisify } from "node:util";
|
|
29
|
+
import hostile from "hostile";
|
|
30
|
+
var _setHost = promisify(hostile.set);
|
|
31
|
+
var _removeHost = promisify(hostile.remove);
|
|
32
|
+
function getPathOfSystemHostsPath() {
|
|
33
|
+
return process.platform === "win32" ? `${process.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
|
|
34
|
+
}
|
|
35
|
+
async function syncHost() {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
exec(
|
|
38
|
+
process.platform === "win32" ? "ipconfig /flushdns" : "sudo -E killall -HUP mDNSResponder",
|
|
39
|
+
(error, stdout, stderr) => {
|
|
40
|
+
if (error) {
|
|
41
|
+
consola.error(`exec error: ${error}`);
|
|
42
|
+
return resolve(false);
|
|
43
|
+
}
|
|
44
|
+
return resolve(true);
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async function addHost(ip, host) {
|
|
50
|
+
if (process.platform !== "win32")
|
|
51
|
+
chmodSync(getPathOfSystemHostsPath(), 511);
|
|
52
|
+
await _setHost("127.0.0.1", "localhost");
|
|
53
|
+
await _setHost(ip, host);
|
|
54
|
+
if (ip === "localhost")
|
|
55
|
+
await _setHost("127.0.0.1", host);
|
|
56
|
+
return await syncHost();
|
|
57
|
+
}
|
|
58
|
+
async function removeHost(ip, host) {
|
|
59
|
+
if (process.platform !== "win32")
|
|
60
|
+
chmodSync(getPathOfSystemHostsPath(), 511);
|
|
61
|
+
await _removeHost(ip, host);
|
|
62
|
+
if (ip === "localhost")
|
|
63
|
+
await _removeHost("127.0.0.1", host);
|
|
64
|
+
return await syncHost();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/caddy/constants.ts
|
|
68
|
+
import { join } from "node:path";
|
|
69
|
+
import os from "node:os";
|
|
70
|
+
import process2 from "node:process";
|
|
71
|
+
import dotenvFlow from "dotenv-flow";
|
|
72
|
+
dotenvFlow.config({
|
|
73
|
+
silent: true
|
|
74
|
+
});
|
|
75
|
+
function resolvePath(path) {
|
|
76
|
+
if (path.startsWith("~"))
|
|
77
|
+
return join(os.homedir() || "", path.slice(1));
|
|
78
|
+
return path;
|
|
79
|
+
}
|
|
80
|
+
var TEMP_DIR = resolvePath(process2.env.UHRP_CADDY_PATH || process2.env.TEMP || process2.env.TMPDIR || "/tmp");
|
|
81
|
+
var caddyPath = join(TEMP_DIR, `caddy${process2.platform === "win32" ? ".exe" : ""}`);
|
|
82
|
+
var caddyFilePath = join(TEMP_DIR, "CADDYFILE");
|
|
83
|
+
var caddyLockFilePath = join(TEMP_DIR, "caddylock");
|
|
84
|
+
var supportList = [
|
|
85
|
+
{
|
|
86
|
+
platform: "dragonfly",
|
|
87
|
+
arch: "amd64"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
platform: "freebsd",
|
|
91
|
+
arch: "amd64"
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
platform: "freebsd",
|
|
95
|
+
arch: "arm-6"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
platform: "freebsd",
|
|
99
|
+
arch: "arm-7"
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
platform: "freebsd",
|
|
103
|
+
arch: "arm64"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
platform: "linux",
|
|
107
|
+
arch: "amd64"
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
platform: "linux",
|
|
111
|
+
arch: "arm-5"
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
platform: "linux",
|
|
115
|
+
arch: "arm-6"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
platform: "linux",
|
|
119
|
+
arch: "arm-7"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
platform: "linux",
|
|
123
|
+
arch: "arm64"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
platform: "linux",
|
|
127
|
+
arch: "mips"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
platform: "linux",
|
|
131
|
+
arch: "mips64"
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
platform: "linux",
|
|
135
|
+
arch: "mips64le"
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
platform: "linux",
|
|
139
|
+
arch: "mipsle"
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
platform: "linux",
|
|
143
|
+
arch: "ppc64"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
platform: "linux",
|
|
147
|
+
arch: "ppc64le"
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
platform: "linux",
|
|
151
|
+
arch: "s390x"
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
platform: "darwin",
|
|
155
|
+
arch: "amd64"
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
platform: "darwin",
|
|
159
|
+
arch: "arm64"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
platform: "openbsd",
|
|
163
|
+
arch: "amd64"
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
platform: "openbsd",
|
|
167
|
+
arch: "arm-6"
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
platform: "openbsd",
|
|
171
|
+
arch: "arm-7"
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
platform: "openbsd",
|
|
175
|
+
arch: "arm64"
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
platform: "windows",
|
|
179
|
+
arch: "amd64"
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
platform: "windows",
|
|
183
|
+
arch: "arm-6"
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
platform: "windows",
|
|
187
|
+
arch: "arm-7"
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
platform: "windows",
|
|
191
|
+
arch: "arm64"
|
|
192
|
+
}
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
// src/caddy/utils.ts
|
|
196
|
+
import { createServer } from "node:net";
|
|
197
|
+
import { exec as exec2 } from "node:child_process";
|
|
198
|
+
import ora from "ora";
|
|
199
|
+
var spinner;
|
|
200
|
+
function tryPort(port) {
|
|
201
|
+
return new Promise((resolve, reject) => {
|
|
202
|
+
try {
|
|
203
|
+
const server = createServer().listen(port);
|
|
204
|
+
server.on("listening", () => {
|
|
205
|
+
server.close();
|
|
206
|
+
resolve(false);
|
|
207
|
+
});
|
|
208
|
+
server.on("error", (err) => {
|
|
209
|
+
if (err.code === "EADDRINUSE")
|
|
210
|
+
resolve(true);
|
|
211
|
+
server.close();
|
|
212
|
+
});
|
|
213
|
+
} catch (e) {
|
|
214
|
+
resolve(true);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
async function kill(port) {
|
|
219
|
+
return new Promise((resolve, reject) => {
|
|
220
|
+
exec2(`npx @maxbo/free-port ${port} -s`, (err, stdout, stderr) => {
|
|
221
|
+
if (err) {
|
|
222
|
+
reject(err);
|
|
223
|
+
} else {
|
|
224
|
+
consola.log(stdout);
|
|
225
|
+
resolve();
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
function getNumber(number) {
|
|
231
|
+
if (number <= 0)
|
|
232
|
+
return "0%";
|
|
233
|
+
else if (number < 100)
|
|
234
|
+
return `${number.toFixed(2)}%`;
|
|
235
|
+
else
|
|
236
|
+
return "100%";
|
|
237
|
+
}
|
|
238
|
+
function logProgress(percent) {
|
|
239
|
+
if (!spinner) {
|
|
240
|
+
consola.log("");
|
|
241
|
+
spinner = ora({
|
|
242
|
+
text: "Start Download Caddy: 0%",
|
|
243
|
+
indent: 2
|
|
244
|
+
});
|
|
245
|
+
spinner.start();
|
|
246
|
+
} else {
|
|
247
|
+
spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function logProgressOver() {
|
|
251
|
+
if (!spinner) {
|
|
252
|
+
spinner = ora({
|
|
253
|
+
text: "Start Download Caddy: 100%",
|
|
254
|
+
indent: 2
|
|
255
|
+
});
|
|
256
|
+
spinner.start();
|
|
257
|
+
} else {
|
|
258
|
+
spinner.succeed();
|
|
259
|
+
consola.log("");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/caddy/index.ts
|
|
264
|
+
async function download() {
|
|
265
|
+
if (await testCaddy())
|
|
266
|
+
return caddyPath;
|
|
267
|
+
return new Promise((resolve, reject) => {
|
|
268
|
+
if (!existsSync(TEMP_DIR)) {
|
|
269
|
+
mkdirSync(TEMP_DIR, { recursive: true });
|
|
270
|
+
chmodSync2(TEMP_DIR, 511);
|
|
271
|
+
}
|
|
272
|
+
const file = createWriteStream(caddyPath);
|
|
273
|
+
let _platform = platform().toString();
|
|
274
|
+
_platform === "win32" && (_platform = "windows");
|
|
275
|
+
let _arch = process3.arch.toString();
|
|
276
|
+
_arch === "x64" && (_arch = "amd64");
|
|
277
|
+
const support = supportList.find((item) => item.platform === _platform && item.arch === _arch);
|
|
278
|
+
if (!support)
|
|
279
|
+
return reject(new Error("not support"));
|
|
280
|
+
const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}`;
|
|
281
|
+
existsSync(caddyPath) && unlinkSync(caddyPath);
|
|
282
|
+
const httpProxy = process3.env.HTTP_PROXY || process3.env.http_proxy;
|
|
283
|
+
const httpsProxy = process3.env.HTTPS_PROXY || process3.env.https_proxy;
|
|
284
|
+
const httpAgent = httpProxy ? new HttpProxyAgent(httpProxy) : void 0;
|
|
285
|
+
const httpsAgent = httpsProxy ? new HttpsProxyAgent(httpsProxy) : void 0;
|
|
286
|
+
const chmodCaddyOnce = once(() => {
|
|
287
|
+
chmodSync2(caddyPath, 511);
|
|
288
|
+
});
|
|
289
|
+
got.stream(dowmloadLink, {
|
|
290
|
+
agent: {
|
|
291
|
+
http: httpAgent,
|
|
292
|
+
https: httpsAgent
|
|
293
|
+
}
|
|
294
|
+
}).on("downloadProgress", (progress) => {
|
|
295
|
+
logProgress(progress.percent);
|
|
296
|
+
chmodCaddyOnce();
|
|
297
|
+
}).pipe(file).on("finish", () => {
|
|
298
|
+
logProgressOver();
|
|
299
|
+
if (process3.platform === "win32")
|
|
300
|
+
return resolve(caddyPath);
|
|
301
|
+
chmodSync2(caddyPath, 511);
|
|
302
|
+
resolve(caddyPath);
|
|
303
|
+
}).on("error", (err) => {
|
|
304
|
+
reject(err);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
function testCaddy() {
|
|
309
|
+
return new Promise((resolve, reject) => {
|
|
310
|
+
if (!existsSync(caddyPath))
|
|
311
|
+
return resolve(false);
|
|
312
|
+
chmodSync2(caddyPath, 511);
|
|
313
|
+
const child = process3.platform === "win32" ? spawn(caddyPath, []) : spawn("sudo", ["-E", caddyPath]);
|
|
314
|
+
child.on("close", () => {
|
|
315
|
+
return resolve(false);
|
|
316
|
+
});
|
|
317
|
+
child.on("error", (err) => {
|
|
318
|
+
return reject(err);
|
|
319
|
+
});
|
|
320
|
+
child.stdout.on("data", (_data) => {
|
|
321
|
+
return resolve(true);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
var CaddyInstant = class {
|
|
326
|
+
constructor() {
|
|
327
|
+
this.inited = false;
|
|
328
|
+
this.stoped = false;
|
|
329
|
+
/** has locked */
|
|
330
|
+
this.locked = false;
|
|
331
|
+
testCaddy().then(async () => {
|
|
332
|
+
await this.getCaddyfile();
|
|
333
|
+
this.inited = true;
|
|
334
|
+
});
|
|
335
|
+
this.locked = this.checkLock();
|
|
336
|
+
}
|
|
337
|
+
checkLock() {
|
|
338
|
+
if (!existsSync(caddyLockFilePath))
|
|
339
|
+
writeFileSync(caddyLockFilePath, "");
|
|
340
|
+
return lockfile.checkSync(caddyLockFilePath);
|
|
341
|
+
}
|
|
342
|
+
async getCaddyfile() {
|
|
343
|
+
if (!existsSync(caddyFilePath))
|
|
344
|
+
await this.initCaddyfile();
|
|
345
|
+
this.caddyfile = await readFile(caddyFilePath, "utf-8");
|
|
346
|
+
return this.caddyfile;
|
|
347
|
+
}
|
|
348
|
+
async initCaddyfile() {
|
|
349
|
+
const contet = `
|
|
350
|
+
{
|
|
351
|
+
debug
|
|
352
|
+
auto_https disable_redirects
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
`;
|
|
356
|
+
await writeFile(caddyFilePath, contet);
|
|
357
|
+
this.caddyfile = contet;
|
|
358
|
+
}
|
|
359
|
+
async writeCaddyfile() {
|
|
360
|
+
return await writeFile(caddyFilePath, this.caddyfile || "");
|
|
361
|
+
}
|
|
362
|
+
async deleteCaddyfile() {
|
|
363
|
+
return await unlink(caddyFilePath);
|
|
364
|
+
}
|
|
365
|
+
async init() {
|
|
366
|
+
await download();
|
|
367
|
+
this.inited = true;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* @param source like `127.0.0.1:8080`
|
|
371
|
+
* @param target like `test.abc.com`. And if you provide a port ,it will be ignore.
|
|
372
|
+
* @returns
|
|
373
|
+
*/
|
|
374
|
+
async run(source, target, options) {
|
|
375
|
+
source = source.replace("0.0.0.0", "127.0.0.1");
|
|
376
|
+
const {
|
|
377
|
+
restore = true,
|
|
378
|
+
base = "/",
|
|
379
|
+
showCaddyLog = false,
|
|
380
|
+
https = false
|
|
381
|
+
} = options || {};
|
|
382
|
+
this._source = source;
|
|
383
|
+
this._target = target;
|
|
384
|
+
if (!this.inited)
|
|
385
|
+
await this.init();
|
|
386
|
+
if (!await addHost(source.split(":")[0], target.split(":")[0]))
|
|
387
|
+
throw new Error("update host failed");
|
|
388
|
+
consola.success("update host success\n");
|
|
389
|
+
await this.getCaddyfile();
|
|
390
|
+
if (!(this.caddyfile || "").includes(`${target.split(":")[0]}${https ? "" : ":80"}`)) {
|
|
391
|
+
this.caddyfile = `${this.caddyfile}
|
|
392
|
+
${target.split(":")[0]}${https ? "" : ":80"} {
|
|
393
|
+
${https ? "tls internal" : ""}
|
|
394
|
+
reverse_proxy http://${source} {
|
|
395
|
+
health_uri /
|
|
396
|
+
health_interval 2s
|
|
397
|
+
health_timeout 5s
|
|
398
|
+
fail_duration 2s
|
|
399
|
+
unhealthy_status 502 503 504 404
|
|
400
|
+
lb_try_duration 3s
|
|
401
|
+
lb_try_interval 300ms
|
|
402
|
+
|
|
403
|
+
@error status 502 503 504 404
|
|
404
|
+
handle_response @error {
|
|
405
|
+
respond * 503 {
|
|
406
|
+
body "Service Unavailable"
|
|
407
|
+
close
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
`;
|
|
414
|
+
await this.writeCaddyfile();
|
|
415
|
+
}
|
|
416
|
+
if (!existsSync(caddyLockFilePath))
|
|
417
|
+
writeFileSync(caddyLockFilePath, "");
|
|
418
|
+
if (!this.locked) {
|
|
419
|
+
if (await tryPort(443))
|
|
420
|
+
await kill(443);
|
|
421
|
+
if ((process3.platform === "win32" || !https) && await tryPort(80))
|
|
422
|
+
await kill(80);
|
|
423
|
+
}
|
|
424
|
+
return new Promise((resolve, reject) => {
|
|
425
|
+
var _a, _b;
|
|
426
|
+
const setupCleanup = () => {
|
|
427
|
+
process3.on("SIGINT", async () => {
|
|
428
|
+
if (this.stoped)
|
|
429
|
+
return;
|
|
430
|
+
process3.stdin.resume();
|
|
431
|
+
try {
|
|
432
|
+
await this.baseCleanup(restore);
|
|
433
|
+
} finally {
|
|
434
|
+
this.stoped = true;
|
|
435
|
+
process3.nextTick(() => {
|
|
436
|
+
process3.exit();
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
const originalExit = process3.exit.bind(process3);
|
|
441
|
+
process3.exit = async (code) => {
|
|
442
|
+
const port = Number(source.split(":")[1]);
|
|
443
|
+
if (!Number.isNaN(port) && await tryPort(port))
|
|
444
|
+
await kill(port);
|
|
445
|
+
const pwd = process3.cwd();
|
|
446
|
+
const viteCacheDir = `${pwd}/node_modules/.vite`;
|
|
447
|
+
if (existsSync(viteCacheDir))
|
|
448
|
+
await chmodRecursive(viteCacheDir, 511).catch(() => {
|
|
449
|
+
});
|
|
450
|
+
const nuxtCacheDir = `${pwd}/.nuxt`;
|
|
451
|
+
if (existsSync(nuxtCacheDir))
|
|
452
|
+
await chmodRecursive(nuxtCacheDir, 511).catch(() => {
|
|
453
|
+
});
|
|
454
|
+
const webpackCacheDir = `${pwd}/node_modules/.cache`;
|
|
455
|
+
if (existsSync(webpackCacheDir))
|
|
456
|
+
await chmodRecursive(webpackCacheDir, 511).catch(() => {
|
|
457
|
+
});
|
|
458
|
+
if (this.stoped)
|
|
459
|
+
return originalExit(code);
|
|
460
|
+
process3.stdin.resume();
|
|
461
|
+
try {
|
|
462
|
+
await this.baseCleanup();
|
|
463
|
+
} finally {
|
|
464
|
+
this.stoped = true;
|
|
465
|
+
process3.nextTick(() => {
|
|
466
|
+
originalExit(code);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
};
|
|
471
|
+
if (!this.locked) {
|
|
472
|
+
this._caddyChild = process3.platform !== "win32" ? spawn("sudo", ["-E", caddyPath, "run", "--config", caddyFilePath, "--watch"]) : spawn(caddyPath, ["run", "--config", caddyFilePath, "--watch"]);
|
|
473
|
+
lockfile.lockSync(caddyLockFilePath);
|
|
474
|
+
this._caddyChild.on("error", (err) => {
|
|
475
|
+
return reject(err);
|
|
476
|
+
});
|
|
477
|
+
(_a = this._caddyChild.stderr) == null ? void 0 : _a.on("data", async (data) => {
|
|
478
|
+
const lines = data.toString().split("\n").map((line) => line.trim());
|
|
479
|
+
for (const line of lines) {
|
|
480
|
+
showCaddyLog && line && console.info(line);
|
|
481
|
+
if (line.includes("Error:") || line && JSON.parse(line).level === "error") {
|
|
482
|
+
if (line.includes("parsing caddyfile tokens")) {
|
|
483
|
+
consola.error("Caddyfile syntax error, please contact the author:\n", `${this.caddyfile}
|
|
484
|
+
|
|
485
|
+
${line}
|
|
486
|
+
`);
|
|
487
|
+
await this.baseCleanup();
|
|
488
|
+
process3.exit();
|
|
489
|
+
} else {
|
|
490
|
+
consola.error(`${line}
|
|
491
|
+
`);
|
|
492
|
+
return reject(line);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (line.includes("config file changed"))
|
|
496
|
+
consola.success("Detect added a new reverse proxy config, restarted.\n");
|
|
497
|
+
if (line.includes("caddy proxying") || line.includes("serving initial"))
|
|
498
|
+
return resolve();
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
(_b = this._caddyChild.stdout) == null ? void 0 : _b.on("data", (_data) => {
|
|
502
|
+
consola.info(_data.toString());
|
|
503
|
+
resolve();
|
|
504
|
+
});
|
|
505
|
+
setupCleanup();
|
|
506
|
+
} else {
|
|
507
|
+
consola.info("Caddy is running in another process.");
|
|
508
|
+
setupCleanup();
|
|
509
|
+
return resolve();
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
async baseCleanup(restore = true) {
|
|
514
|
+
var _a;
|
|
515
|
+
try {
|
|
516
|
+
if (restore && this._source && this._target) {
|
|
517
|
+
if (await removeHost(this._source.split(":")[0], this._target.split(":")[0]))
|
|
518
|
+
consola.success("Restore host success.\n");
|
|
519
|
+
else
|
|
520
|
+
consola.fail("Restore host failed.\n");
|
|
521
|
+
}
|
|
522
|
+
} catch (e) {
|
|
523
|
+
consola.error(e);
|
|
524
|
+
consola.fail("Restore host failed.\n");
|
|
525
|
+
}
|
|
526
|
+
if (!this.locked && !this.stoped) {
|
|
527
|
+
(_a = this._caddyChild) == null ? void 0 : _a.kill();
|
|
528
|
+
try {
|
|
529
|
+
await lockfile.unlock(caddyLockFilePath).catch(() => {
|
|
530
|
+
});
|
|
531
|
+
await unlink(caddyFilePath).catch(() => {
|
|
532
|
+
});
|
|
533
|
+
await unlink(caddyLockFilePath).catch(() => {
|
|
534
|
+
});
|
|
535
|
+
} catch (e) {
|
|
536
|
+
consola.error(e);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
// src/index.ts
|
|
543
|
+
var config;
|
|
544
|
+
var caddy;
|
|
545
|
+
var cwd = process4.cwd();
|
|
546
|
+
var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
|
|
547
|
+
_printUrls == null ? void 0 : _printUrls();
|
|
548
|
+
if (caddy)
|
|
549
|
+
return;
|
|
550
|
+
caddy = new CaddyInstant();
|
|
551
|
+
caddy.run(source, target, {
|
|
552
|
+
base,
|
|
553
|
+
...options
|
|
554
|
+
}).then(() => {
|
|
555
|
+
const colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
|
|
556
|
+
consola.success(` ${c.green("\u279C")} ${c.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
|
|
557
|
+
}).catch((e) => {
|
|
558
|
+
throw e;
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
var unpluginFactory = (options) => ({
|
|
562
|
+
name: "unplugin-https-reverse-proxy",
|
|
563
|
+
enforce: "pre",
|
|
564
|
+
vite: {
|
|
565
|
+
configResolved(_config) {
|
|
566
|
+
config = _config;
|
|
567
|
+
},
|
|
568
|
+
configureServer(server) {
|
|
569
|
+
var _a;
|
|
570
|
+
if (config.command !== "serve")
|
|
571
|
+
return;
|
|
572
|
+
const {
|
|
573
|
+
enable = true,
|
|
574
|
+
target = ""
|
|
575
|
+
} = options;
|
|
576
|
+
if (!enable)
|
|
577
|
+
return;
|
|
578
|
+
if (!isAdmin()) {
|
|
579
|
+
consola.warn("please run as administrator");
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
if (!target) {
|
|
583
|
+
consola.fail("please provide target");
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const _printUrls = server.printUrls.bind(server);
|
|
587
|
+
let source = `localhost:${config.server.port || 5173}`;
|
|
588
|
+
const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
|
|
589
|
+
if (url) {
|
|
590
|
+
const u = new URL(url);
|
|
591
|
+
source = u.host;
|
|
592
|
+
}
|
|
593
|
+
const base = server.config.base || "/";
|
|
594
|
+
server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls);
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
webpack(compiler) {
|
|
598
|
+
var _a, _b;
|
|
599
|
+
if (process4.env.NODE_ENV !== "development")
|
|
600
|
+
return;
|
|
601
|
+
const {
|
|
602
|
+
enable = true,
|
|
603
|
+
target = ""
|
|
604
|
+
} = options;
|
|
605
|
+
if (!enable)
|
|
606
|
+
return;
|
|
607
|
+
if (!isAdmin()) {
|
|
608
|
+
consola.warn("please run as administrator");
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (!target) {
|
|
612
|
+
consola.fail("please provide target");
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
const devServer = {
|
|
616
|
+
host: "localhost",
|
|
617
|
+
port: 8080,
|
|
618
|
+
...compiler.options.devServer,
|
|
619
|
+
// @ts-expect-error vuecli
|
|
620
|
+
...(_a = process4.VUE_CLI_SERVICE) == null ? void 0 : _a.projectOptions.devServer
|
|
621
|
+
};
|
|
622
|
+
const source = `${devServer.host}:${devServer.port}`;
|
|
623
|
+
if (caddy)
|
|
624
|
+
return;
|
|
625
|
+
const _close = (_b = compiler.close) == null ? void 0 : _b.bind(compiler);
|
|
626
|
+
if (_close) {
|
|
627
|
+
compiler.close = async (callback) => {
|
|
628
|
+
process4.on("exit", () => {
|
|
629
|
+
_close && _close(callback);
|
|
630
|
+
});
|
|
631
|
+
};
|
|
632
|
+
} else {
|
|
633
|
+
options.https = false;
|
|
634
|
+
}
|
|
635
|
+
caddy = new CaddyInstant();
|
|
636
|
+
caddy.run(source, target, {
|
|
637
|
+
...options
|
|
638
|
+
}).then(() => {
|
|
639
|
+
const colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
|
|
640
|
+
compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
|
|
641
|
+
consola.success(` ${c.green("\u279C")} ${c.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
|
|
642
|
+
});
|
|
643
|
+
}).catch((e) => {
|
|
644
|
+
throw e;
|
|
645
|
+
});
|
|
646
|
+
},
|
|
647
|
+
async rspack(compiler) {
|
|
648
|
+
var _a, _b, _c;
|
|
649
|
+
if (process4.env.NODE_ENV !== "development")
|
|
650
|
+
return;
|
|
651
|
+
const {
|
|
652
|
+
enable = true,
|
|
653
|
+
target = ""
|
|
654
|
+
} = options;
|
|
655
|
+
if (!enable)
|
|
656
|
+
return;
|
|
657
|
+
if (!isAdmin()) {
|
|
658
|
+
consola.warn("please run as administrator");
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
if (!target) {
|
|
662
|
+
consola.fail("please provide target");
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const devServer = {
|
|
666
|
+
host: "localhost",
|
|
667
|
+
port: 8080,
|
|
668
|
+
...compiler.options.devServer,
|
|
669
|
+
...(_c = (_b = await ((_a = await import("@rsbuild/core")) == null ? void 0 : _a.loadConfig({
|
|
670
|
+
cwd
|
|
671
|
+
}))) == null ? void 0 : _b.content) == null ? void 0 : _c.server
|
|
672
|
+
};
|
|
673
|
+
const source = `${devServer.host}:${devServer.port}`;
|
|
674
|
+
if (caddy)
|
|
675
|
+
return;
|
|
676
|
+
caddy = new CaddyInstant();
|
|
677
|
+
caddy.run(source, target, {
|
|
678
|
+
...options
|
|
679
|
+
}).then(() => {
|
|
680
|
+
const colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
|
|
681
|
+
consola.success(` ${c.green("\u279C")} ${c.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
|
|
682
|
+
}).catch((e) => {
|
|
683
|
+
throw e;
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
var unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
688
|
+
var src_default = unplugin;
|
|
689
|
+
|
|
690
|
+
export {
|
|
691
|
+
vitePrintUrls,
|
|
692
|
+
unpluginFactory,
|
|
693
|
+
unplugin,
|
|
694
|
+
src_default
|
|
695
|
+
};
|