peerpigeon 0.0.3 โ 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/websocket-server/server.js +148 -22
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WebSocketServer } from 'ws';
|
|
1
|
+
import { WebSocketServer, WebSocket } from 'ws';
|
|
2
2
|
import { createServer } from 'http';
|
|
3
3
|
import { URL } from 'url';
|
|
4
4
|
|
|
@@ -50,14 +50,57 @@ function findClosestPeers(targetPeerId, allPeerIds, maxPeers = 3) {
|
|
|
50
50
|
|
|
51
51
|
function getActivePeers(excludePeerId = null) {
|
|
52
52
|
const peers = [];
|
|
53
|
+
const stalePeers = [];
|
|
54
|
+
|
|
53
55
|
for (const [peerId, connection] of connections) {
|
|
54
|
-
if (peerId !== excludePeerId
|
|
55
|
-
|
|
56
|
+
if (peerId !== excludePeerId) {
|
|
57
|
+
if (connection.readyState === WebSocket.OPEN && isConnectionAlive(connection)) {
|
|
58
|
+
peers.push(peerId);
|
|
59
|
+
} else {
|
|
60
|
+
// Mark stale connections for cleanup
|
|
61
|
+
stalePeers.push(peerId);
|
|
62
|
+
}
|
|
56
63
|
}
|
|
57
64
|
}
|
|
65
|
+
|
|
66
|
+
// Clean up stale connections
|
|
67
|
+
stalePeers.forEach(peerId => {
|
|
68
|
+
console.log(`๐งน Cleaning up stale connection: ${peerId.substring(0, 8)}...`);
|
|
69
|
+
connections.delete(peerId);
|
|
70
|
+
peerData.delete(peerId);
|
|
71
|
+
});
|
|
72
|
+
|
|
58
73
|
return peers;
|
|
59
74
|
}
|
|
60
75
|
|
|
76
|
+
function isConnectionAlive(connection) {
|
|
77
|
+
if (!connection || connection.readyState !== WebSocket.OPEN) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Check if we've received a recent pong or if this is a fresh connection
|
|
82
|
+
const now = Date.now();
|
|
83
|
+
const lastPong = connection.lastPong || connection.connectedAt || now;
|
|
84
|
+
const timeSinceLastPong = now - lastPong;
|
|
85
|
+
|
|
86
|
+
// Consider connection dead if no pong for more than 60 seconds
|
|
87
|
+
return timeSinceLastPong < 60000;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function pingConnection(peerId, connection) {
|
|
91
|
+
if (connection.readyState === WebSocket.OPEN) {
|
|
92
|
+
try {
|
|
93
|
+
connection.ping();
|
|
94
|
+
return true;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.log(`๐ Failed to ping ${peerId.substring(0, 8)}...:`, error.message);
|
|
97
|
+
cleanupPeer(peerId);
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
61
104
|
function sendToConnection(peerId, data) {
|
|
62
105
|
const connection = connections.get(peerId);
|
|
63
106
|
if (connection && connection.readyState === WebSocket.OPEN) {
|
|
@@ -66,12 +109,39 @@ function sendToConnection(peerId, data) {
|
|
|
66
109
|
return true;
|
|
67
110
|
} catch (error) {
|
|
68
111
|
console.error(`Error sending to ${peerId}:`, error);
|
|
112
|
+
// Clean up failed connection
|
|
113
|
+
cleanupPeer(peerId);
|
|
69
114
|
return false;
|
|
70
115
|
}
|
|
116
|
+
} else if (connection && (connection.readyState === WebSocket.CLOSED || connection.readyState === WebSocket.CLOSING)) {
|
|
117
|
+
// Clean up closed connection
|
|
118
|
+
cleanupPeer(peerId);
|
|
71
119
|
}
|
|
72
120
|
return false;
|
|
73
121
|
}
|
|
74
122
|
|
|
123
|
+
function cleanupPeer(peerId) {
|
|
124
|
+
const wasConnected = connections.has(peerId);
|
|
125
|
+
connections.delete(peerId);
|
|
126
|
+
peerData.delete(peerId);
|
|
127
|
+
|
|
128
|
+
if (wasConnected) {
|
|
129
|
+
console.log(`๐งน Cleaned up peer: ${peerId.substring(0, 8)}...`);
|
|
130
|
+
|
|
131
|
+
// Notify other peers about disconnection
|
|
132
|
+
const activePeers = getActivePeers();
|
|
133
|
+
activePeers.forEach(otherPeerId => {
|
|
134
|
+
sendToConnection(otherPeerId, {
|
|
135
|
+
type: 'peer-disconnected',
|
|
136
|
+
data: { peerId },
|
|
137
|
+
fromPeerId: 'system',
|
|
138
|
+
targetPeerId: otherPeerId,
|
|
139
|
+
timestamp: Date.now()
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
75
145
|
function broadcastToClosestPeers(fromPeerId, message, maxPeers = 5) {
|
|
76
146
|
const activePeers = getActivePeers(fromPeerId);
|
|
77
147
|
const closestPeers = findClosestPeers(fromPeerId, activePeers, maxPeers);
|
|
@@ -95,6 +165,33 @@ const wss = new WebSocketServer({ server });
|
|
|
95
165
|
|
|
96
166
|
console.log(`๐ Starting PeerPigeon WebSocket server...`);
|
|
97
167
|
|
|
168
|
+
// Periodic cleanup of stale connections
|
|
169
|
+
setInterval(() => {
|
|
170
|
+
const totalConnections = connections.size;
|
|
171
|
+
const activePeers = getActivePeers(); // This will clean up stale connections
|
|
172
|
+
const cleanedUp = totalConnections - connections.size;
|
|
173
|
+
|
|
174
|
+
if (cleanedUp > 0) {
|
|
175
|
+
console.log(`๐งน Periodic cleanup: removed ${cleanedUp} stale connections, ${connections.size} active`);
|
|
176
|
+
}
|
|
177
|
+
}, 30000); // Clean up every 30 seconds
|
|
178
|
+
|
|
179
|
+
// Heartbeat ping every 30 seconds
|
|
180
|
+
setInterval(() => {
|
|
181
|
+
console.log(`๐ Pinging ${connections.size} connections...`);
|
|
182
|
+
const stalePeers = [];
|
|
183
|
+
|
|
184
|
+
for (const [peerId, connection] of connections) {
|
|
185
|
+
if (!pingConnection(peerId, connection)) {
|
|
186
|
+
stalePeers.push(peerId);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (stalePeers.length > 0) {
|
|
191
|
+
console.log(`๐งน Heartbeat cleanup: removed ${stalePeers.length} unresponsive connections`);
|
|
192
|
+
}
|
|
193
|
+
}, 30000); // Ping every 30 seconds
|
|
194
|
+
|
|
98
195
|
wss.on('connection', (ws, req) => {
|
|
99
196
|
let peerId = null;
|
|
100
197
|
|
|
@@ -110,6 +207,20 @@ wss.on('connection', (ws, req) => {
|
|
|
110
207
|
|
|
111
208
|
peerId = queryPeerId;
|
|
112
209
|
|
|
210
|
+
// Check if peerId is already connected
|
|
211
|
+
if (connections.has(peerId)) {
|
|
212
|
+
const existingConnection = connections.get(peerId);
|
|
213
|
+
if (existingConnection.readyState === WebSocket.OPEN) {
|
|
214
|
+
console.log(`โ ๏ธ Peer ${peerId.substring(0, 8)}... already connected, closing duplicate`);
|
|
215
|
+
ws.close(1008, 'Peer already connected');
|
|
216
|
+
return;
|
|
217
|
+
} else {
|
|
218
|
+
// Clean up stale connection
|
|
219
|
+
console.log(`๐ Replacing stale connection for ${peerId.substring(0, 8)}...`);
|
|
220
|
+
cleanupPeer(peerId);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
113
224
|
// Store connection
|
|
114
225
|
connections.set(peerId, ws);
|
|
115
226
|
peerData.set(peerId, {
|
|
@@ -118,6 +229,15 @@ wss.on('connection', (ws, req) => {
|
|
|
118
229
|
connected: true
|
|
119
230
|
});
|
|
120
231
|
|
|
232
|
+
// Set up connection metadata
|
|
233
|
+
ws.connectedAt = Date.now();
|
|
234
|
+
ws.lastPong = Date.now();
|
|
235
|
+
|
|
236
|
+
// Set up pong handler for heartbeat
|
|
237
|
+
ws.on('pong', () => {
|
|
238
|
+
ws.lastPong = Date.now();
|
|
239
|
+
});
|
|
240
|
+
|
|
121
241
|
console.log(`โ
Peer ${peerId.substring(0, 8)}... connected (${connections.size} total)`);
|
|
122
242
|
|
|
123
243
|
// Send connection confirmation
|
|
@@ -163,11 +283,25 @@ wss.on('connection', (ws, req) => {
|
|
|
163
283
|
connected: true
|
|
164
284
|
});
|
|
165
285
|
|
|
286
|
+
// Get active peers with immediate validation
|
|
166
287
|
const activePeers = getActivePeers(peerId);
|
|
167
|
-
console.log(`๐ข Announcing ${peerId.substring(0, 8)}... to ${activePeers.length} peers`);
|
|
168
288
|
|
|
169
|
-
//
|
|
170
|
-
|
|
289
|
+
// Double-check each active peer with a quick ping test
|
|
290
|
+
const validatedPeers = [];
|
|
291
|
+
for (const otherPeerId of activePeers) {
|
|
292
|
+
const connection = connections.get(otherPeerId);
|
|
293
|
+
if (connection && isConnectionAlive(connection)) {
|
|
294
|
+
validatedPeers.push(otherPeerId);
|
|
295
|
+
} else {
|
|
296
|
+
console.log(`๏ฟฝ Found dead connection during announce: ${otherPeerId.substring(0, 8)}...`);
|
|
297
|
+
cleanupPeer(otherPeerId);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
console.log(`๏ฟฝ๐ข Announcing ${peerId.substring(0, 8)}... to ${validatedPeers.length} validated peers`);
|
|
302
|
+
|
|
303
|
+
// Send peer-discovered messages to validated peers only
|
|
304
|
+
validatedPeers.forEach(otherPeerId => {
|
|
171
305
|
sendToConnection(otherPeerId, {
|
|
172
306
|
type: 'peer-discovered',
|
|
173
307
|
data: { peerId, ...messageData },
|
|
@@ -177,8 +311,8 @@ wss.on('connection', (ws, req) => {
|
|
|
177
311
|
});
|
|
178
312
|
});
|
|
179
313
|
|
|
180
|
-
// Send existing peers to the new peer
|
|
181
|
-
|
|
314
|
+
// Send existing validated peers to the new peer
|
|
315
|
+
validatedPeers.forEach(existingPeerId => {
|
|
182
316
|
const existingPeerData = peerData.get(existingPeerId);
|
|
183
317
|
ws.send(JSON.stringify({
|
|
184
318
|
type: 'peer-discovered',
|
|
@@ -235,20 +369,7 @@ wss.on('connection', (ws, req) => {
|
|
|
235
369
|
console.log(`๐ Peer ${peerId?.substring(0, 8)}... disconnected (${code}: ${reason})`);
|
|
236
370
|
|
|
237
371
|
if (peerId) {
|
|
238
|
-
|
|
239
|
-
peerData.delete(peerId);
|
|
240
|
-
|
|
241
|
-
// Notify other peers
|
|
242
|
-
const activePeers = getActivePeers();
|
|
243
|
-
activePeers.forEach(otherPeerId => {
|
|
244
|
-
sendToConnection(otherPeerId, {
|
|
245
|
-
type: 'peer-disconnected',
|
|
246
|
-
data: { peerId },
|
|
247
|
-
fromPeerId: 'system',
|
|
248
|
-
targetPeerId: otherPeerId,
|
|
249
|
-
timestamp: Date.now()
|
|
250
|
-
});
|
|
251
|
-
});
|
|
372
|
+
cleanupPeer(peerId);
|
|
252
373
|
}
|
|
253
374
|
|
|
254
375
|
console.log(`๐ Active connections: ${connections.size}`);
|
|
@@ -257,6 +378,11 @@ wss.on('connection', (ws, req) => {
|
|
|
257
378
|
// Handle connection errors
|
|
258
379
|
ws.on('error', (error) => {
|
|
259
380
|
console.error(`โ WebSocket error for ${peerId?.substring(0, 8)}...:`, error);
|
|
381
|
+
|
|
382
|
+
// Clean up errored connection
|
|
383
|
+
if (peerId && (ws.readyState === WebSocket.CLOSED || ws.readyState === WebSocket.CLOSING)) {
|
|
384
|
+
cleanupPeer(peerId);
|
|
385
|
+
}
|
|
260
386
|
});
|
|
261
387
|
});
|
|
262
388
|
|