@hive-p2p/server 1.0.23 → 1.0.24
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 +17 -21
- package/core/node.mjs +2 -1
- package/core/topologist.mjs +17 -21
- package/package.json +1 -1
package/core/node-services.mjs
CHANGED
|
@@ -59,27 +59,23 @@ 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
|
-
|
|
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
|
-
}
|
|
62
|
+
const d = new Uint8Array(data); if (d[0] > 127) return; // not unicast, ignore
|
|
63
|
+
const message = this.cryptoCodex.readUnicastMessage(d);
|
|
64
|
+
if (!message) return; // invalid unicast message, ignore
|
|
65
|
+
|
|
66
|
+
const { route, type, neighborsList } = message;
|
|
67
|
+
if (type !== 'handshake' || route.length !== 2) return;
|
|
68
|
+
|
|
69
|
+
const { signatureStart, pubkey, signature } = message;
|
|
70
|
+
const signedData = d.subarray(0, signatureStart);
|
|
71
|
+
if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
|
|
72
|
+
|
|
73
|
+
remoteId = route[0];
|
|
74
|
+
this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
|
|
75
|
+
this.peerStore.connecting[remoteId]?.out?.close(); // close outgoing connection if any
|
|
76
|
+
if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
|
|
77
|
+
this.peerStore.connecting[remoteId].in = new PeerConnection(remoteId, ws, 'in', true);
|
|
78
|
+
for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'in');
|
|
83
79
|
}
|
|
84
80
|
});
|
|
85
81
|
ws.send(this.cryptoCodex.createUnicastMessage('handshake', null, [this.id, this.id], this.peerStore.neighborsList));
|
package/core/node.mjs
CHANGED
|
@@ -16,7 +16,8 @@ import { NodeServices } from './node-services.mjs';
|
|
|
16
16
|
* @param {string} [options.domain] If provided, the node will operate as a public node and start necessary services (e.g., WebSocket server)
|
|
17
17
|
* @param {number} [options.port] If provided, the node will listen on this port (default: SERVICE.PORT)
|
|
18
18
|
* @param {number} [options.verbose] Verbosity level for logging (default: NODE.DEFAULT_VERBOSE) */
|
|
19
|
-
export async function createPublicNode(options) {
|
|
19
|
+
export async function createPublicNode(options) {
|
|
20
|
+
await CLOCK.sync(this.verbose);
|
|
20
21
|
const verbose = options.verbose !== undefined ? options.verbose : NODE.DEFAULT_VERBOSE;
|
|
21
22
|
const domain = options.domain || undefined;
|
|
22
23
|
const codex = options.cryptoCodex || new CryptoCodex(undefined, verbose);
|
package/core/topologist.mjs
CHANGED
|
@@ -157,27 +157,23 @@ 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
|
-
|
|
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
|
-
}
|
|
160
|
+
const d = new Uint8Array(data.data); if (d[0] > 127) return; // not unicast, ignore
|
|
161
|
+
const message = this.cryptoCodex.readUnicastMessage(d);
|
|
162
|
+
if (!message) return; // invalid unicast message, ignore
|
|
163
|
+
|
|
164
|
+
const { route, type, neighborsList } = message;
|
|
165
|
+
if (type !== 'handshake' || route.length !== 2) return;
|
|
166
|
+
|
|
167
|
+
const { signatureStart, pubkey, signature } = message;
|
|
168
|
+
const signedData = d.subarray(0, signatureStart);
|
|
169
|
+
if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
|
|
170
|
+
|
|
171
|
+
remoteId = route[0];
|
|
172
|
+
this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
|
|
173
|
+
this.peerStore.connecting[remoteId]?.in?.close(); // close incoming connection if any
|
|
174
|
+
if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
|
|
175
|
+
this.peerStore.connecting[remoteId].out = new PeerConnection(remoteId, ws, 'out', true);
|
|
176
|
+
for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'out');
|
|
181
177
|
}
|
|
182
178
|
};
|
|
183
179
|
ws.send(this.cryptoCodex.createUnicastMessage('handshake', null, [this.id, this.id], this.peerStore.neighborsList));
|