unplugin-https-reverse-proxy 1.0.0-beta.9 → 1.0.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 +11 -1
- package/dist/chunk-2VGRJ3QS.js +45 -0
- package/dist/{chunk-ZSHTX2B6.js → chunk-KVE7AVSI.js} +259 -124
- package/dist/esbuild.cjs +250 -128
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +250 -128
- package/dist/index.js +2 -2
- package/dist/nuxt.cjs +223 -110
- package/dist/nuxt.js +7 -7
- package/dist/rollup.cjs +250 -128
- package/dist/rollup.js +2 -2
- package/dist/rspack.cjs +250 -128
- package/dist/rspack.js +2 -2
- package/dist/types.cjs +1 -1
- package/dist/utils.cjs +2 -2
- package/dist/utils.js +1 -1
- package/dist/vite.cjs +250 -128
- package/dist/vite.js +2 -2
- package/dist/webpack.cjs +250 -128
- package/dist/webpack.js +2 -2
- package/package.json +11 -8
- package/scripts/postinstall.cjs +308 -0
- package/src/caddy/constants.ts +1 -0
- package/src/caddy/index.ts +225 -94
- package/src/caddy/utils.ts +18 -0
- package/src/host/index.ts +9 -15
- package/src/index.ts +20 -15
- package/src/nuxt.ts +7 -5
- package/dist/chunk-RUJE3YFB.js +0 -63
- package/scripts/postinstall.ts +0 -19
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
|
|
25
|
+
// scripts/postinstall.ts
|
|
26
|
+
var import_node_fs3 = require("fs");
|
|
27
|
+
var import_node_process5 = __toESM(require("process"), 1);
|
|
28
|
+
|
|
29
|
+
// src/utils.ts
|
|
30
|
+
var import_node_process = __toESM(require("process"), 1);
|
|
31
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
32
|
+
var import_promises = require("fs/promises");
|
|
33
|
+
var import_node_child_process = require("child_process");
|
|
34
|
+
var import_consola = require("consola");
|
|
35
|
+
var consola = (0, import_consola.createConsola)({
|
|
36
|
+
level: 3
|
|
37
|
+
}).withTag("reverse-proxy");
|
|
38
|
+
function once(fn) {
|
|
39
|
+
let called = false;
|
|
40
|
+
return (...args) => {
|
|
41
|
+
if (called)
|
|
42
|
+
return;
|
|
43
|
+
called = true;
|
|
44
|
+
return fn(...args);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/caddy/index.ts
|
|
49
|
+
var import_node_os2 = require("os");
|
|
50
|
+
var import_node_process4 = __toESM(require("process"), 1);
|
|
51
|
+
var import_node_fs2 = require("fs");
|
|
52
|
+
var import_node_child_process4 = require("child_process");
|
|
53
|
+
var import_promises2 = require("fs/promises");
|
|
54
|
+
var import_got_cjs = require("got-cjs");
|
|
55
|
+
var import_http_proxy_agent = require("http-proxy-agent");
|
|
56
|
+
var import_https_proxy_agent = require("https-proxy-agent");
|
|
57
|
+
var lockfile = __toESM(require("proper-lockfile"), 1);
|
|
58
|
+
|
|
59
|
+
// src/host/index.ts
|
|
60
|
+
var import_node_process2 = __toESM(require("process"), 1);
|
|
61
|
+
var import_node_child_process2 = require("child_process");
|
|
62
|
+
var import_node_fs = require("fs");
|
|
63
|
+
var import_node_util = require("util");
|
|
64
|
+
var import_hostile = __toESM(require("hostile"), 1);
|
|
65
|
+
var _setHost = (0, import_node_util.promisify)(import_hostile.default.set);
|
|
66
|
+
var _removeHost = (0, import_node_util.promisify)(import_hostile.default.remove);
|
|
67
|
+
|
|
68
|
+
// src/caddy/constants.ts
|
|
69
|
+
var import_node_path2 = require("path");
|
|
70
|
+
var import_node_os = __toESM(require("os"), 1);
|
|
71
|
+
var import_node_process3 = __toESM(require("process"), 1);
|
|
72
|
+
var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
|
|
73
|
+
import_dotenv_flow.default.config({
|
|
74
|
+
silent: true
|
|
75
|
+
});
|
|
76
|
+
function resolvePath(path2) {
|
|
77
|
+
if (path2.startsWith("~"))
|
|
78
|
+
return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
|
|
79
|
+
return path2;
|
|
80
|
+
}
|
|
81
|
+
var TEMP_DIR = resolvePath(import_node_process3.default.env.UHRP_CADDY_PATH || import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp");
|
|
82
|
+
var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
|
|
83
|
+
var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
|
|
84
|
+
var caddyLockFilePath = (0, import_node_path2.join)(TEMP_DIR, "caddylock");
|
|
85
|
+
var supportList = [
|
|
86
|
+
{
|
|
87
|
+
platform: "dragonfly",
|
|
88
|
+
arch: "amd64"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
platform: "freebsd",
|
|
92
|
+
arch: "amd64"
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
platform: "freebsd",
|
|
96
|
+
arch: "arm-6"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
platform: "freebsd",
|
|
100
|
+
arch: "arm-7"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
platform: "freebsd",
|
|
104
|
+
arch: "arm64"
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
platform: "linux",
|
|
108
|
+
arch: "amd64"
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
platform: "linux",
|
|
112
|
+
arch: "arm-5"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
platform: "linux",
|
|
116
|
+
arch: "arm-6"
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
platform: "linux",
|
|
120
|
+
arch: "arm-7"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
platform: "linux",
|
|
124
|
+
arch: "arm64"
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
platform: "linux",
|
|
128
|
+
arch: "mips"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
platform: "linux",
|
|
132
|
+
arch: "mips64"
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
platform: "linux",
|
|
136
|
+
arch: "mips64le"
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
platform: "linux",
|
|
140
|
+
arch: "mipsle"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
platform: "linux",
|
|
144
|
+
arch: "ppc64"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
platform: "linux",
|
|
148
|
+
arch: "ppc64le"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
platform: "linux",
|
|
152
|
+
arch: "s390x"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
platform: "darwin",
|
|
156
|
+
arch: "amd64"
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
platform: "darwin",
|
|
160
|
+
arch: "arm64"
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
platform: "openbsd",
|
|
164
|
+
arch: "amd64"
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
platform: "openbsd",
|
|
168
|
+
arch: "arm-6"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
platform: "openbsd",
|
|
172
|
+
arch: "arm-7"
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
platform: "openbsd",
|
|
176
|
+
arch: "arm64"
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
platform: "windows",
|
|
180
|
+
arch: "amd64"
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
platform: "windows",
|
|
184
|
+
arch: "arm-6"
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
platform: "windows",
|
|
188
|
+
arch: "arm-7"
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
platform: "windows",
|
|
192
|
+
arch: "arm64"
|
|
193
|
+
}
|
|
194
|
+
];
|
|
195
|
+
|
|
196
|
+
// src/caddy/utils.ts
|
|
197
|
+
var import_node_net = require("net");
|
|
198
|
+
var import_node_child_process3 = require("child_process");
|
|
199
|
+
var import_ora = __toESM(require("ora"), 1);
|
|
200
|
+
var spinner;
|
|
201
|
+
function getNumber(number) {
|
|
202
|
+
if (number <= 0)
|
|
203
|
+
return "0%";
|
|
204
|
+
else if (number < 100)
|
|
205
|
+
return `${number.toFixed(2)}%`;
|
|
206
|
+
else
|
|
207
|
+
return "100%";
|
|
208
|
+
}
|
|
209
|
+
function logProgress(percent) {
|
|
210
|
+
if (!spinner) {
|
|
211
|
+
consola.log("");
|
|
212
|
+
spinner = (0, import_ora.default)({
|
|
213
|
+
text: "Start Download Caddy: 0%",
|
|
214
|
+
indent: 2
|
|
215
|
+
});
|
|
216
|
+
spinner.start();
|
|
217
|
+
} else {
|
|
218
|
+
spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function logProgressOver() {
|
|
222
|
+
if (!spinner) {
|
|
223
|
+
spinner = (0, import_ora.default)({
|
|
224
|
+
text: "Start Download Caddy: 100%",
|
|
225
|
+
indent: 2
|
|
226
|
+
});
|
|
227
|
+
spinner.start();
|
|
228
|
+
} else {
|
|
229
|
+
spinner.succeed();
|
|
230
|
+
consola.log("");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/caddy/index.ts
|
|
235
|
+
async function download() {
|
|
236
|
+
if (await testCaddy())
|
|
237
|
+
return caddyPath;
|
|
238
|
+
return new Promise((resolve, reject) => {
|
|
239
|
+
if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
|
|
240
|
+
(0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
|
|
241
|
+
(0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
|
|
242
|
+
}
|
|
243
|
+
const file = (0, import_node_fs2.createWriteStream)(caddyPath);
|
|
244
|
+
let _platform = (0, import_node_os2.platform)().toString();
|
|
245
|
+
_platform === "win32" && (_platform = "windows");
|
|
246
|
+
let _arch = import_node_process4.default.arch.toString();
|
|
247
|
+
_arch === "x64" && (_arch = "amd64");
|
|
248
|
+
const support = supportList.find((item) => item.platform === _platform && item.arch === _arch);
|
|
249
|
+
if (!support)
|
|
250
|
+
return reject(new Error("not support"));
|
|
251
|
+
const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}`;
|
|
252
|
+
(0, import_node_fs2.existsSync)(caddyPath) && (0, import_node_fs2.unlinkSync)(caddyPath);
|
|
253
|
+
const httpProxy = import_node_process4.default.env.HTTP_PROXY || import_node_process4.default.env.http_proxy;
|
|
254
|
+
const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
|
|
255
|
+
const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
|
|
256
|
+
const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
|
|
257
|
+
const chmodCaddyOnce = once(() => {
|
|
258
|
+
(0, import_node_fs2.chmodSync)(caddyPath, 511);
|
|
259
|
+
});
|
|
260
|
+
import_got_cjs.got.stream(dowmloadLink, {
|
|
261
|
+
agent: {
|
|
262
|
+
http: httpAgent,
|
|
263
|
+
https: httpsAgent
|
|
264
|
+
}
|
|
265
|
+
}).on("downloadProgress", (progress) => {
|
|
266
|
+
logProgress(progress.percent);
|
|
267
|
+
chmodCaddyOnce();
|
|
268
|
+
}).pipe(file).on("finish", () => {
|
|
269
|
+
logProgressOver();
|
|
270
|
+
if (import_node_process4.default.platform === "win32")
|
|
271
|
+
return resolve(caddyPath);
|
|
272
|
+
(0, import_node_fs2.chmodSync)(caddyPath, 511);
|
|
273
|
+
resolve(caddyPath);
|
|
274
|
+
}).on("error", (err) => {
|
|
275
|
+
reject(err);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function testCaddy() {
|
|
280
|
+
return new Promise((resolve, reject) => {
|
|
281
|
+
if (!(0, import_node_fs2.existsSync)(caddyPath))
|
|
282
|
+
return resolve(false);
|
|
283
|
+
(0, import_node_fs2.chmodSync)(caddyPath, 511);
|
|
284
|
+
const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process4.spawn)(caddyPath, []) : (0, import_node_child_process4.spawn)("sudo", ["-E", caddyPath]);
|
|
285
|
+
child.on("close", () => {
|
|
286
|
+
return resolve(false);
|
|
287
|
+
});
|
|
288
|
+
child.on("error", (err) => {
|
|
289
|
+
return reject(err);
|
|
290
|
+
});
|
|
291
|
+
child.stdout.on("data", (_data) => {
|
|
292
|
+
return resolve(true);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// scripts/postinstall.ts
|
|
298
|
+
async function run() {
|
|
299
|
+
try {
|
|
300
|
+
if (import_node_process5.default.env.UHRP_AUTO_INSTALL_CADDY) {
|
|
301
|
+
(0, import_node_fs3.existsSync)(caddyPath) && (0, import_node_fs3.unlinkSync)(caddyPath);
|
|
302
|
+
await download();
|
|
303
|
+
}
|
|
304
|
+
} catch (e) {
|
|
305
|
+
consola.error(e.code);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
run();
|
package/src/caddy/constants.ts
CHANGED
|
@@ -16,6 +16,7 @@ function resolvePath(path: string) {
|
|
|
16
16
|
export const TEMP_DIR = resolvePath(process.env.UHRP_CADDY_PATH || process.env.TEMP || process.env.TMPDIR || '/tmp')
|
|
17
17
|
export const caddyPath = join(TEMP_DIR, `caddy${process.platform === 'win32' ? '.exe' : ''}`)
|
|
18
18
|
export const caddyFilePath = join(TEMP_DIR, 'CADDYFILE')
|
|
19
|
+
export const caddyLockFilePath = join(TEMP_DIR, 'caddylock')
|
|
19
20
|
|
|
20
21
|
export const supportList = [
|
|
21
22
|
{
|
package/src/caddy/index.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { platform } from 'node:os'
|
|
2
2
|
import process from 'node:process'
|
|
3
|
-
import { chmodSync, createWriteStream, existsSync, mkdirSync, unlinkSync } from 'node:fs'
|
|
3
|
+
import { chmodSync, createWriteStream, existsSync, mkdirSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
4
4
|
import { spawn } from 'node:child_process'
|
|
5
|
+
import { readFile, unlink, writeFile } from 'node:fs/promises'
|
|
5
6
|
import { got } from 'got-cjs'
|
|
6
7
|
import { HttpProxyAgent } from 'http-proxy-agent'
|
|
7
8
|
import { HttpsProxyAgent } from 'https-proxy-agent'
|
|
8
|
-
import
|
|
9
|
+
import * as lockfile from 'proper-lockfile'
|
|
9
10
|
import { chmodRecursive, consola, once } from '../utils'
|
|
10
11
|
import { addHost, removeHost } from '../host'
|
|
11
|
-
import { TEMP_DIR, caddyPath, supportList } from './constants'
|
|
12
|
-
import { logProgress, logProgressOver, tryPort } from './utils'
|
|
12
|
+
import { TEMP_DIR, caddyFilePath, caddyLockFilePath, caddyPath, supportList } from './constants'
|
|
13
|
+
import { kill, logProgress, logProgressOver, tryPort } from './utils'
|
|
13
14
|
|
|
14
15
|
export async function download() {
|
|
15
16
|
if (await testCaddy())
|
|
@@ -103,10 +104,57 @@ export class CaddyInstant {
|
|
|
103
104
|
private inited = false
|
|
104
105
|
private stoped = false
|
|
105
106
|
|
|
107
|
+
/** has locked */
|
|
108
|
+
private locked = false
|
|
109
|
+
private caddyfile: string | undefined
|
|
110
|
+
|
|
111
|
+
private _caddyChild: ReturnType<typeof spawn> | undefined
|
|
112
|
+
|
|
113
|
+
private _source: string | undefined
|
|
114
|
+
private _target: string | undefined
|
|
115
|
+
|
|
106
116
|
constructor() {
|
|
107
|
-
testCaddy().then(() => {
|
|
117
|
+
testCaddy().then(async () => {
|
|
118
|
+
await this.getCaddyfile()
|
|
119
|
+
|
|
108
120
|
this.inited = true
|
|
109
121
|
})
|
|
122
|
+
|
|
123
|
+
this.locked = this.checkLock()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
checkLock() {
|
|
127
|
+
if (!existsSync(caddyLockFilePath))
|
|
128
|
+
writeFileSync(caddyLockFilePath, '')
|
|
129
|
+
return lockfile.checkSync(caddyLockFilePath)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async getCaddyfile() {
|
|
133
|
+
if (!existsSync(caddyFilePath))
|
|
134
|
+
await this.initCaddyfile()
|
|
135
|
+
|
|
136
|
+
this.caddyfile = await readFile(caddyFilePath, 'utf-8')
|
|
137
|
+
return this.caddyfile
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async initCaddyfile() {
|
|
141
|
+
const contet = `
|
|
142
|
+
{
|
|
143
|
+
debug
|
|
144
|
+
auto_https disable_redirects
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
`
|
|
148
|
+
await writeFile(caddyFilePath, contet)
|
|
149
|
+
this.caddyfile = contet
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async writeCaddyfile() {
|
|
153
|
+
return await writeFile(caddyFilePath, this.caddyfile || '')
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async deleteCaddyfile() {
|
|
157
|
+
return await unlink(caddyFilePath)
|
|
110
158
|
}
|
|
111
159
|
|
|
112
160
|
async init() {
|
|
@@ -128,111 +176,194 @@ export class CaddyInstant {
|
|
|
128
176
|
https = false,
|
|
129
177
|
} = options || {}
|
|
130
178
|
|
|
179
|
+
this._source = source
|
|
180
|
+
this._target = target
|
|
181
|
+
|
|
131
182
|
if (!this.inited)
|
|
132
183
|
await this.init()
|
|
133
184
|
|
|
134
|
-
if (https && await tryPort(443))
|
|
135
|
-
await kill(443, 'tcp')
|
|
136
|
-
if ((process.platform === 'win32' || !https) && await tryPort(80))
|
|
137
|
-
await kill(80, 'tcp')
|
|
138
|
-
|
|
139
185
|
if (!await addHost(source.split(':')[0], target.split(':')[0]))
|
|
140
186
|
throw new Error('update host failed')
|
|
141
187
|
consola.success('update host success\n')
|
|
142
188
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
189
|
+
await this.getCaddyfile()
|
|
190
|
+
if (!(this.caddyfile || '').includes(`${target.split(':')[0]}${https ? '' : ':80'}`)) {
|
|
191
|
+
this.caddyfile = `${this.caddyfile}
|
|
192
|
+
${target.split(':')[0]}${https ? '' : ':80'} {
|
|
193
|
+
${https ? 'tls internal' : ''}
|
|
194
|
+
reverse_proxy http://${source} {
|
|
195
|
+
health_uri ${base}
|
|
196
|
+
health_interval 2s
|
|
197
|
+
health_timeout 5s
|
|
198
|
+
fail_duration 2s
|
|
199
|
+
unhealthy_status 502 503 504 404
|
|
200
|
+
lb_try_duration 3s
|
|
201
|
+
lb_try_interval 300ms
|
|
202
|
+
health_headers {
|
|
203
|
+
Accept text/html
|
|
204
|
+
Accept-Encoding gzip, deflate, br
|
|
205
|
+
User-Agent Caddy-Health-Check
|
|
206
|
+
}
|
|
148
207
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
208
|
+
@error status 502 503 504 404
|
|
209
|
+
handle_response @error {
|
|
210
|
+
respond * 503 {
|
|
211
|
+
body "Service Unavailable"
|
|
212
|
+
close
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
152
217
|
|
|
153
|
-
|
|
154
|
-
if (!restore || this.stoped)
|
|
155
|
-
return
|
|
218
|
+
`
|
|
156
219
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
220
|
+
await this.writeCaddyfile()
|
|
221
|
+
}
|
|
222
|
+
if (!existsSync(caddyLockFilePath))
|
|
223
|
+
writeFileSync(caddyLockFilePath, '')
|
|
224
|
+
|
|
225
|
+
if (!this.locked) {
|
|
226
|
+
if (await tryPort(443))
|
|
227
|
+
await kill(443)
|
|
228
|
+
if ((process.platform === 'win32' || !https) && await tryPort(80))
|
|
229
|
+
await kill(80)
|
|
230
|
+
}
|
|
160
231
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
232
|
+
return new Promise<void>((resolve, reject) => {
|
|
233
|
+
const setupCleanup = () => {
|
|
234
|
+
process.on('SIGINT', async () => {
|
|
235
|
+
if (this.stoped)
|
|
236
|
+
return
|
|
237
|
+
|
|
238
|
+
process.stdin.resume()
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
await this.baseCleanup(restore)
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
this.stoped = true
|
|
245
|
+
process.nextTick(() => {
|
|
246
|
+
process.exit()
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
const originalExit = process.exit.bind(process)
|
|
252
|
+
|
|
253
|
+
// @ts-expect-error override
|
|
254
|
+
process.exit = async (code?: number) => {
|
|
255
|
+
const port = Number(source.split(':')[1])
|
|
256
|
+
if (!Number.isNaN(port) && await tryPort(port))
|
|
257
|
+
await kill(port)
|
|
258
|
+
|
|
259
|
+
// fix `Error: EPERM: operation not permitted`
|
|
260
|
+
const pwd = process.cwd()
|
|
261
|
+
const viteCacheDir = `${pwd}/node_modules/.vite`
|
|
262
|
+
if (existsSync(viteCacheDir))
|
|
263
|
+
await chmodRecursive(viteCacheDir, 0o777).catch(() => { })
|
|
264
|
+
const nuxtCacheDir = `${pwd}/.nuxt`
|
|
265
|
+
if (existsSync(nuxtCacheDir))
|
|
266
|
+
await chmodRecursive(nuxtCacheDir, 0o777).catch(() => { })
|
|
267
|
+
const webpackCacheDir = `${pwd}/node_modules/.cache`
|
|
268
|
+
if (existsSync(webpackCacheDir))
|
|
269
|
+
await chmodRecursive(webpackCacheDir, 0o777).catch(() => { })
|
|
270
|
+
|
|
271
|
+
if (this.stoped)
|
|
272
|
+
return originalExit(code)
|
|
273
|
+
|
|
274
|
+
process.stdin.resume()
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
await this.baseCleanup()
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
finally {
|
|
281
|
+
this.stoped = true
|
|
282
|
+
process.nextTick(() => {
|
|
283
|
+
originalExit(code)
|
|
284
|
+
})
|
|
285
|
+
}
|
|
215
286
|
}
|
|
216
287
|
}
|
|
217
288
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
289
|
+
if (!this.locked) {
|
|
290
|
+
this._caddyChild = process.platform !== 'win32'
|
|
291
|
+
? spawn('sudo', ['-E', caddyPath, 'run', '--config', caddyFilePath, '--watch'])
|
|
292
|
+
: spawn(caddyPath, ['run', '--config', caddyFilePath, '--watch'])
|
|
293
|
+
|
|
294
|
+
lockfile.lockSync(caddyLockFilePath)
|
|
295
|
+
|
|
296
|
+
this._caddyChild!.on('error', (err) => {
|
|
297
|
+
return reject(err)
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
this._caddyChild!.stderr?.on('data', async (data) => {
|
|
301
|
+
const lines = (data.toString() as string).split('\n').map(line => line.trim())
|
|
302
|
+
for (const line of lines) {
|
|
303
|
+
// caddy log
|
|
304
|
+
// eslint-disable-next-line no-console
|
|
305
|
+
showCaddyLog && line && console.info(line)
|
|
306
|
+
if (line.includes('Error:') || (line && JSON.parse(line).level === 'error')) {
|
|
307
|
+
if (line.includes('parsing caddyfile tokens')) {
|
|
308
|
+
consola.error('Caddyfile syntax error, please contact the author:\n', `${this.caddyfile}\n\n${line}\n`)
|
|
309
|
+
await this.baseCleanup()
|
|
310
|
+
process.exit()
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
consola.error(`${line}\n`)
|
|
314
|
+
// child.kill()
|
|
315
|
+
return reject(line)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (line.includes('config file changed'))
|
|
319
|
+
consola.success('Detect added a new reverse proxy config, restarted.\n')
|
|
320
|
+
|
|
321
|
+
if (line.includes('caddy proxying') || line.includes('serving initial'))
|
|
322
|
+
return resolve()
|
|
228
323
|
}
|
|
229
|
-
}
|
|
230
|
-
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
this._caddyChild!.stdout?.on('data', (_data) => {
|
|
327
|
+
consola.info(_data.toString())
|
|
328
|
+
resolve()
|
|
329
|
+
})
|
|
231
330
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
331
|
+
setupCleanup()
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
consola.info('Caddy is running in another process.')
|
|
335
|
+
|
|
336
|
+
setupCleanup()
|
|
337
|
+
|
|
338
|
+
return resolve()
|
|
339
|
+
}
|
|
236
340
|
})
|
|
237
341
|
}
|
|
342
|
+
|
|
343
|
+
async baseCleanup(restore: boolean = true) {
|
|
344
|
+
try {
|
|
345
|
+
if (restore && this._source && this._target) {
|
|
346
|
+
if (await removeHost(this._source.split(':')[0], this._target.split(':')[0]))
|
|
347
|
+
consola.success('Restore host success.\n')
|
|
348
|
+
|
|
349
|
+
else
|
|
350
|
+
consola.fail('Restore host failed.\n')
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
catch (e) {
|
|
354
|
+
consola.error(e)
|
|
355
|
+
consola.fail('Restore host failed.\n')
|
|
356
|
+
}
|
|
357
|
+
if (!this.locked && !this.stoped) {
|
|
358
|
+
this._caddyChild?.kill()
|
|
359
|
+
try {
|
|
360
|
+
await lockfile.unlock(caddyLockFilePath).catch(() => { })
|
|
361
|
+
await unlink(caddyFilePath).catch(() => { })
|
|
362
|
+
await unlink(caddyLockFilePath).catch(() => { })
|
|
363
|
+
}
|
|
364
|
+
catch (e) {
|
|
365
|
+
consola.error(e)
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
238
369
|
}
|