-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (136 loc) · 3.95 KB
/
index.js
File metadata and controls
159 lines (136 loc) · 3.95 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
const WebSocket = require("ws");
const { WebSocketServer } = require("ws");
const UPSTREAM_URL = "wss://24data.ptfs.app/wss";
const LOCAL_PORT = 80;
const MAX_REQUESTS = 5; // per 10s
const WINDOW_MS = 10000;
const KEEPALIVE_INTERVAL = 20000;
const MAX_RECONNECT_DELAY = 30000;
class RateLimiter {
constructor(limit = 5, interval = 10000) {
this.limit = limit;
this.interval = interval;
this.queue = [];
this.active = 0;
setInterval(() => {
this.active = 0;
this.processQueue();
}, this.interval);
}
processQueue() {
while (this.queue.length > 0 && this.active < this.limit) {
const next = this.queue.shift();
this.active++;
next();
}
}
enqueue(task) {
this.queue.push(task);
this.processQueue();
}
async exec(fn) {
return new Promise((resolve, reject) => {
this.enqueue(async () => {
try {
const result = await fn();
resolve(result);
} catch (err) {
reject(err);
}
});
});
}
}
const limiter = new RateLimiter(MAX_REQUESTS, WINDOW_MS);
const wss = new WebSocketServer({ port: LOCAL_PORT });
console.log(`✅ Relay server running on ws://localhost:${LOCAL_PORT}`);
let upstream = null;
let reconnectDelay = 1000;
let reconnecting = false;
function connectUpstream() {
if (upstream && upstream.readyState === WebSocket.OPEN) return;
if (reconnecting) return;
reconnecting = true;
console.log(`🔌 Connecting to upstream: ${UPSTREAM_URL}`);
const ws = new WebSocket(UPSTREAM_URL);
upstream = ws;
let alive = true;
ws.on("open", () => {
console.log("🟢 Connected to upstream WebSocket");
reconnecting = false;
reconnectDelay = 1000;
const pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
if (!alive) {
console.warn("⚠️ Upstream not responding, reconnecting...");
ws.terminate();
clearInterval(pingInterval);
return;
}
alive = false;
ws.ping();
} else clearInterval(pingInterval);
}, KEEPALIVE_INTERVAL);
});
ws.on("pong", () => (alive = true));
ws.on("message", (data) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(data.toString());
}
});
});
ws.on("close", () => {
console.warn("⚠️ Upstream connection closed.");
scheduleReconnect();
});
ws.on("error", (err) => {
console.error("❌ Upstream error:", err.message);
try {
ws.close();
} catch {}
});
}
function scheduleReconnect() {
if (reconnecting) return;
reconnecting = true;
console.log(`⏳ Reconnecting in ${reconnectDelay / 1000}s...`);
setTimeout(() => {
reconnectDelay = Math.min(reconnectDelay * 2, MAX_RECONNECT_DELAY);
reconnecting = false;
connectUpstream();
}, reconnectDelay);
}
wss.on("connection", (client, req) => {
const ip = req.socket.remoteAddress;
console.log(`🔗 New client connected from ${ip}`);
let alive = true;
const interval = setInterval(() => {
if (client.readyState === WebSocket.OPEN) {
if (!alive) {
console.warn(`⚠️ Client ${ip} did not respond to ping, closing`);
client.terminate();
clearInterval(interval);
return;
}
alive = false;
client.ping();
} else clearInterval(interval);
}, KEEPALIVE_INTERVAL);
client.on("pong", () => (alive = true));
client.on("message", async (msg) => {
if (upstream && upstream.readyState === WebSocket.OPEN) {
await limiter.exec(() => {
upstream.send(msg.toString());
console.log(`📤 Sent upstream (rate-limited)`);
});
} else {
console.warn("⚠️ Upstream not connected — message dropped.");
}
});
client.on("close", () => console.log(`❌ Client ${ip} disconnected`));
});
setInterval(() => {
if (!upstream || upstream.readyState === WebSocket.CLOSED) connectUpstream();
}, 5000);
connectUpstream();