feat: rebuild xiteng.site homelab platform
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import {mkdirSync, rmSync} from "node:fs";
|
||||
import net from "node:net";
|
||||
|
||||
const mode = process.env.BRIDGE_MODE || "network";
|
||||
const socketPath = process.env.BRIDGE_SOCKET || "/run/provider-proxy/upstream.sock";
|
||||
const listenPort = Number.parseInt(process.env.LISTEN_PORT || "17897", 10);
|
||||
const upstreamHost = process.env.UPSTREAM_HOST || "127.0.0.1";
|
||||
const upstreamPort = Number.parseInt(process.env.UPSTREAM_PORT || "7897", 10);
|
||||
|
||||
if (mode === "host") {
|
||||
mkdirSync("/run/provider-proxy", {recursive: true});
|
||||
rmSync(socketPath, {force: true});
|
||||
}
|
||||
|
||||
const server = net.createServer((client) => {
|
||||
client.pause();
|
||||
const upstream = mode === "host"
|
||||
? net.connect({host: upstreamHost, port: upstreamPort})
|
||||
: net.connect(socketPath);
|
||||
upstream.once("connect", () => {
|
||||
client.pipe(upstream);
|
||||
upstream.pipe(client);
|
||||
client.resume();
|
||||
});
|
||||
const close = () => {
|
||||
client.destroy();
|
||||
upstream.destroy();
|
||||
};
|
||||
client.on("error", close);
|
||||
upstream.on("error", close);
|
||||
});
|
||||
|
||||
const listenTarget = mode === "host" ? socketPath : {host: "0.0.0.0", port: listenPort};
|
||||
server.listen(listenTarget, () => {
|
||||
const target = mode === "host" ? socketPath : `0.0.0.0:${listenPort}`;
|
||||
const upstream = mode === "host" ? `${upstreamHost}:${upstreamPort}` : socketPath;
|
||||
console.log(`provider proxy ${mode} bridge ${target} -> ${upstream}`);
|
||||
});
|
||||
|
||||
function shutdown() {
|
||||
server.close(() => {
|
||||
if (mode === "host") rmSync(socketPath, {force: true});
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
process.on("SIGTERM", shutdown);
|
||||
process.on("SIGINT", shutdown);
|
||||
Reference in New Issue
Block a user