-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttps-server.js
More file actions
61 lines (54 loc) · 1.83 KB
/
Copy pathhttps-server.js
File metadata and controls
61 lines (54 loc) · 1.83 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
const https = require("https");
const http = require("http");
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const HTTPS_PORT = parseInt(process.env.PORT, 10) || 9997;
const INTERNAL_PORT = 19997;
const HOSTNAME = process.env.HOSTNAME || "0.0.0.0";
const sslOptions = {
key: fs.readFileSync(path.join(__dirname, "ssl", "key.pem")),
cert: fs.readFileSync(path.join(__dirname, "ssl", "cert.pem")),
};
const child = spawn("node", [".next/standalone/server.js"], {
cwd: __dirname,
env: { ...process.env, PORT: String(INTERNAL_PORT), HOSTNAME: "127.0.0.1" },
stdio: "inherit",
shell: true,
});
child.on("error", (err) => console.error("Spawn error:", err));
child.on("exit", (code) => process.exit(code));
setTimeout(() => {
const server = https.createServer(sslOptions, (req, res) => {
const proxy = http.request(
{
hostname: "127.0.0.1",
port: INTERNAL_PORT,
path: req.url,
method: req.method,
headers: req.headers,
},
(proxyRes) => {
const headers = { ...proxyRes.headers };
res.writeHead(proxyRes.statusCode, headers);
proxyRes.pipe(res);
}
);
req.pipe(proxy);
proxy.on("error", (e) => {
console.error("Proxy error:", e.message);
res.writeHead(502);
res.end("Bad Gateway");
});
});
server.listen(HTTPS_PORT, HOSTNAME, () => {
console.log("=== 9Router HTTPS ====================================");
console.log(" URL: https://0.0.0.0:" + HTTPS_PORT);
console.log(" Cert: " + path.join(__dirname, "ssl", "cert.pem"));
console.log(" Int: http://127.0.0.1:" + INTERNAL_PORT);
console.log("=====================================================");
});
server.on("error", (err) => {
console.error("HTTPS server error:", err.message);
});
}, 5000);