-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (41 loc) · 1.54 KB
/
server.js
File metadata and controls
48 lines (41 loc) · 1.54 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
// server.js
import { createHttpServer } from "./server/httpServer.js";
import { createTcpServer } from "./server/tcpServer.js";
import { createWsServer } from "./server/wsServer.js";
import * as wsHandler from "./core/ws-handler.js";
import dotenv from "dotenv";
import { WebSocketServer } from "ws";
import { fileURLToPath } from "url";
import path from "path";
// Decla __filename & __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Chargement .env
dotenv.config();
// Declaration des variables
const tcpClients = new Map();
const wsTunnelRef = { wsTunnel: null };
const ctx = { exposerIp: null, wsTunnel: null, tcpClients };
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 8000;
// SRV HTTP & TCP & WS
const wss = new WebSocketServer({ noServer: true });
const httpServer = createHttpServer(wsTunnelRef, tcpClients, ctx);
const tcpServer = createTcpServer(httpServer, wsTunnelRef, tcpClients, ctx);
const setupWsUpgrade = createWsServer(process.env.TUNNEL_TOKEN, wss, wsTunnelRef, tcpClients, wsHandler, ctx);
// Patch log anti-dotenv
const _log = console.log;
console.log = function (...args) {
if (
typeof args[0] === "string" &&
args[0].startsWith("[dotenv@")
) return;
_log.apply(console, args);
};
console.log = _log;
// Branche tunnel WS sur SRV HTTP
setupWsUpgrade(httpServer);
// Lancement du SRV TCP
tcpServer.listen(PORT, "0.0.0.0", () => {
console.log(`[INFO] Server listens on port ${PORT}`);
console.log("[INFO] Waiting for a WS tunnel client on /tunnel...");
});