Repository navigation
Expand file tree
/
Copy pathpostinstall.cjs
More file actions
310 lines (302 loc) · 8.67 KB
/
Copy pathpostinstall.cjs
File metadata and controls
310 lines (302 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// scripts/postinstall.ts
var import_node_fs3 = require("fs");
var import_node_process5 = __toESM(require("process"), 1);
// src/utils.ts
var import_node_process = __toESM(require("process"), 1);
var import_node_path = __toESM(require("path"), 1);
var import_promises = require("fs/promises");
var import_node_child_process = require("child_process");
var import_consola = require("consola");
var consola = (0, import_consola.createConsola)({
level: 3
}).withTag("reverse-proxy");
function once(fn) {
let called = false;
return (...args) => {
if (called)
return;
called = true;
return fn(...args);
};
}
// src/caddy/index.ts
var import_node_os2 = require("os");
var import_node_process4 = __toESM(require("process"), 1);
var import_node_fs2 = require("fs");
var import_node_child_process3 = require("child_process");
var import_got_cjs = require("got-cjs");
var import_http_proxy_agent = require("http-proxy-agent");
var import_https_proxy_agent = require("https-proxy-agent");
var import_kill_port = __toESM(require("kill-port"), 1);
// src/host/index.ts
var import_node_process2 = __toESM(require("process"), 1);
var import_node_child_process2 = require("child_process");
var import_node_fs = require("fs");
var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
var hostInstance = new Hosts({
debounceTime: 50,
hostsFile: getPathOfSystemHostsPath()
});
function getPathOfSystemHostsPath() {
return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
}
// src/caddy/constants.ts
var import_node_path2 = require("path");
var import_node_os = __toESM(require("os"), 1);
var import_node_process3 = __toESM(require("process"), 1);
var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
import_dotenv_flow.default.config({
silent: true
});
function resolvePath(path2) {
if (path2.startsWith("~"))
return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
return path2;
}
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");
var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
var supportList = [
{
platform: "dragonfly",
arch: "amd64"
},
{
platform: "freebsd",
arch: "amd64"
},
{
platform: "freebsd",
arch: "arm-6"
},
{
platform: "freebsd",
arch: "arm-7"
},
{
platform: "freebsd",
arch: "arm64"
},
{
platform: "linux",
arch: "amd64"
},
{
platform: "linux",
arch: "arm-5"
},
{
platform: "linux",
arch: "arm-6"
},
{
platform: "linux",
arch: "arm-7"
},
{
platform: "linux",
arch: "arm64"
},
{
platform: "linux",
arch: "mips"
},
{
platform: "linux",
arch: "mips64"
},
{
platform: "linux",
arch: "mips64le"
},
{
platform: "linux",
arch: "mipsle"
},
{
platform: "linux",
arch: "ppc64"
},
{
platform: "linux",
arch: "ppc64le"
},
{
platform: "linux",
arch: "s390x"
},
{
platform: "darwin",
arch: "amd64"
},
{
platform: "darwin",
arch: "arm64"
},
{
platform: "openbsd",
arch: "amd64"
},
{
platform: "openbsd",
arch: "arm-6"
},
{
platform: "openbsd",
arch: "arm-7"
},
{
platform: "openbsd",
arch: "arm64"
},
{
platform: "windows",
arch: "amd64"
},
{
platform: "windows",
arch: "arm-6"
},
{
platform: "windows",
arch: "arm-7"
},
{
platform: "windows",
arch: "arm64"
}
];
// src/caddy/utils.ts
var import_node_net = require("net");
var import_ora = __toESM(require("ora"), 1);
var spinner;
function getNumber(number) {
if (number <= 0)
return "0%";
else if (number < 100)
return `${number.toFixed(2)}%`;
else
return "100%";
}
function logProgress(percent) {
if (!spinner) {
consola.log("");
spinner = (0, import_ora.default)({
text: "Start Download Caddy: 0%",
indent: 2
});
spinner.start();
} else {
spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
}
}
function logProgressOver() {
if (!spinner) {
spinner = (0, import_ora.default)({
text: "Start Download Caddy: 100%",
indent: 2
});
spinner.start();
} else {
spinner.succeed();
consola.log("");
}
}
// src/caddy/index.ts
async function download() {
if (await testCaddy())
return caddyPath;
return new Promise((resolve, reject) => {
if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
(0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
(0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
}
const file = (0, import_node_fs2.createWriteStream)(caddyPath);
let _platform = (0, import_node_os2.platform)().toString();
_platform === "win32" && (_platform = "windows");
let _arch = import_node_process4.default.arch.toString();
_arch === "x64" && (_arch = "amd64");
const support = supportList.find((item) => item.platform === _platform && item.arch === _arch);
if (!support)
return reject(new Error("not support"));
const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}`;
(0, import_node_fs2.existsSync)(caddyPath) && (0, import_node_fs2.unlinkSync)(caddyPath);
const httpProxy = import_node_process4.default.env.HTTP_PROXY || import_node_process4.default.env.http_proxy;
const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
const chmodCaddyOnce = once(() => {
(0, import_node_fs2.chmodSync)(caddyPath, 511);
});
import_got_cjs.got.stream(dowmloadLink, {
agent: {
http: httpAgent,
https: httpsAgent
}
}).on("downloadProgress", (progress) => {
logProgress(progress.percent);
chmodCaddyOnce();
}).pipe(file).on("finish", () => {
logProgressOver();
if (import_node_process4.default.platform === "win32")
return resolve(caddyPath);
(0, import_node_fs2.chmodSync)(caddyPath, 511);
resolve(caddyPath);
}).on("error", (err) => {
reject(err);
});
});
}
function testCaddy() {
return new Promise((resolve, reject) => {
if (!(0, import_node_fs2.existsSync)(caddyPath))
return resolve(false);
(0, import_node_fs2.chmodSync)(caddyPath, 511);
const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
child.on("close", () => {
return resolve(false);
});
child.on("error", (err) => {
return reject(err);
});
child.stdout.on("data", (_data) => {
return resolve(true);
});
});
}
// scripts/postinstall.ts
async function run() {
try {
if (import_node_process5.default.env.UHRP_AUTO_INSTALL_CADDY) {
(0, import_node_fs3.existsSync)(caddyPath) && (0, import_node_fs3.unlinkSync)(caddyPath);
await download();
}
} catch (e) {
consola.error(e.code);
}
}
run();