@hive-p2p/server 1.0.22 → 1.0.23
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/core/node-services.mjs +21 -17
- package/core/topologist.mjs +21 -17
- package/package.json +1 -1
package/core/node-services.mjs
CHANGED
|
@@ -59,23 +59,27 @@ export class NodeServices {
|
|
|
59
59
|
ws.on('message', (data) => { // When peer proves his id, we can handle data normally
|
|
60
60
|
if (remoteId) for (const cb of this.peerStore.callbacks.data) cb(remoteId, data);
|
|
61
61
|
else { // FIRST MESSAGE SHOULD BE HANDSHAKE WITH ID
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
62
|
+
try {
|
|
63
|
+
const d = new Uint8Array(data); if (d[0] > 127) return; // not unicast, ignore
|
|
64
|
+
const message = this.cryptoCodex.readUnicastMessage(d);
|
|
65
|
+
if (!message) return; // invalid unicast message, ignore
|
|
66
|
+
|
|
67
|
+
const { route, type, neighborsList } = message;
|
|
68
|
+
if (type !== 'handshake' || route.length !== 2) return;
|
|
69
|
+
|
|
70
|
+
const { signatureStart, pubkey, signature } = message;
|
|
71
|
+
const signedData = d.subarray(0, signatureStart);
|
|
72
|
+
if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
|
|
73
|
+
|
|
74
|
+
remoteId = route[0];
|
|
75
|
+
this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
|
|
76
|
+
this.peerStore.connecting[remoteId]?.out?.close(); // close outgoing connection if any
|
|
77
|
+
if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
|
|
78
|
+
this.peerStore.connecting[remoteId].in = new PeerConnection(remoteId, ws, 'in', true);
|
|
79
|
+
for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'in');
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error(`Error handling WebSocket message on Node #${this.id}:`, error);
|
|
82
|
+
}
|
|
79
83
|
}
|
|
80
84
|
});
|
|
81
85
|
ws.send(this.cryptoCodex.createUnicastMessage('handshake', null, [this.id, this.id], this.peerStore.neighborsList));
|
package/core/topologist.mjs
CHANGED
|
@@ -157,23 +157,27 @@ export class Topologist {
|
|
|
157
157
|
ws.onmessage = (data) => {
|
|
158
158
|
if (remoteId) for (const cb of this.peerStore.callbacks.data) cb(remoteId, data.data);
|
|
159
159
|
else { // FIRST MESSAGE SHOULD BE HANDSHAKE WITH ID
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
160
|
+
try {
|
|
161
|
+
const d = new Uint8Array(data.data); if (d[0] > 127) return; // not unicast, ignore
|
|
162
|
+
const message = this.cryptoCodex.readUnicastMessage(d);
|
|
163
|
+
if (!message) return; // invalid unicast message, ignore
|
|
164
|
+
|
|
165
|
+
const { route, type, neighborsList } = message;
|
|
166
|
+
if (type !== 'handshake' || route.length !== 2) return;
|
|
167
|
+
|
|
168
|
+
const { signatureStart, pubkey, signature } = message;
|
|
169
|
+
const signedData = d.subarray(0, signatureStart);
|
|
170
|
+
if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
|
|
171
|
+
|
|
172
|
+
remoteId = route[0];
|
|
173
|
+
this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
|
|
174
|
+
this.peerStore.connecting[remoteId]?.in?.close(); // close incoming connection if any
|
|
175
|
+
if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
|
|
176
|
+
this.peerStore.connecting[remoteId].out = new PeerConnection(remoteId, ws, 'out', true);
|
|
177
|
+
for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'out');
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error(`Error handling WebSocket message on Node #${this.id}:`, error);
|
|
180
|
+
}
|
|
177
181
|
}
|
|
178
182
|
};
|
|
179
183
|
ws.send(this.cryptoCodex.createUnicastMessage('handshake', null, [this.id, this.id], this.peerStore.neighborsList));
|