-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.mjs
More file actions
64 lines (53 loc) · 1.78 KB
/
main.mjs
File metadata and controls
64 lines (53 loc) · 1.78 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
import { spawn } from "child_process";
import path from "path";
import os from "os";
import { reqProxy } from "./req-proxy.mjs";
function getGoBinary() {
const platform = os.platform();
if (platform === "win32") return path.resolve("./server-windows.exe");
if (platform === "linux") return path.resolve("./server-linux");
if (platform === "android") return path.resolve("./server-android"); // 安卓设备
throw new Error("Unsupported platform: " + platform);
}
const goBinary = getGoBinary();
// 可以通过 Node 传入端口号
const port = process.argv[2] || "57571";
console.log("Starting Go server:", goBinary, "on port", port);
const proc = spawn(goBinary, ["-p", port], {
stdio: ["ignore", "pipe", "pipe"],
});
// 打印 Go 服务输出
proc.stdout.on("data", (data) => {
console.log("[Go-Server]", data.toString().trim());
});
proc.stderr.on("data", (data) => {
console.error("[Go-Server-ERR]", data.toString().trim());
});
// 确保 Node 退出时,杀掉 Go 服务
process.on("exit", () => {
proc.kill();
});
process.on("SIGINT", () => process.exit());
process.on("SIGTERM", () => process.exit());
console.log("Node wrapper started, Go server should be running...");
(async () => {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 2000);
});
try {
const res = await reqProxy({
method: "GET",
url: "https://self-signed.badssl.com/",
headers: { "User-Agent": "okhttp/4.19" },
timeout: 10000,
});
console.log("Status:", res.status);
console.log("Headers:", res.headers);
console.log("Body length:", res.body.length);
console.log("Body:", res.body);
} catch (err) {
console.error(err);
}
})();