magnetk 2.3.0 → 2.4.0
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/client.js +50 -1
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -24,6 +24,8 @@ const MSG_TYPE = {
|
|
|
24
24
|
MSG_CHUNK_RES: 0x21,
|
|
25
25
|
MSG_GET_IDENTITY: 0x32,
|
|
26
26
|
MSG_IDENTITY_RES: 0x33,
|
|
27
|
+
MSG_PROXY_CONNECT: 0x40,
|
|
28
|
+
MSG_PROXY_READY: 0x41,
|
|
27
29
|
MSG_ERROR: 0xFF
|
|
28
30
|
};
|
|
29
31
|
|
|
@@ -512,7 +514,54 @@ export class MagnetkClient extends EventEmitter {
|
|
|
512
514
|
} catch (e) { }
|
|
513
515
|
}
|
|
514
516
|
|
|
515
|
-
if (!socket)
|
|
517
|
+
if (!socket) {
|
|
518
|
+
// ===== RELAY PROXY FALLBACK =====
|
|
519
|
+
// All direct connections failed — try connecting through the relay
|
|
520
|
+
if (this.config.enableRelayFallback !== false && lookupResult && lookupResult.found && lookupResult.seeds.length > 0) {
|
|
521
|
+
const seed = lookupResult.seeds[0];
|
|
522
|
+
console.log(`[Magnetk-JS] Direct connection failed, trying relay proxy for seed ${seed.peer_id.substring(0, 12)}...`);
|
|
523
|
+
|
|
524
|
+
try {
|
|
525
|
+
socket = await this.connect(relayHost, discoveryPort);
|
|
526
|
+
const proxyWriter = new FrameWriter(socket);
|
|
527
|
+
const proxyReader = new FrameReader(socket);
|
|
528
|
+
|
|
529
|
+
// Request relay to proxy connection to seed
|
|
530
|
+
await proxyWriter.writeJSON(MSG_TYPE.MSG_PROXY_CONNECT, {
|
|
531
|
+
peer_id: seed.peer_id,
|
|
532
|
+
file_hash: ml.fileHash
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
// Wait for proxy ready or error
|
|
536
|
+
const proxyFrame = await proxyReader.readFrame();
|
|
537
|
+
if (proxyFrame.type === MSG_TYPE.MSG_PROXY_READY) {
|
|
538
|
+
const proxyResult = JSON.parse(proxyFrame.payload.toString());
|
|
539
|
+
if (proxyResult.success) {
|
|
540
|
+
console.log(`[Magnetk-JS] Relay proxy connected to seed`);
|
|
541
|
+
connectionType = CONNECTION_TYPE.RELAYED;
|
|
542
|
+
finalAddr = `relay-proxy:${relayHost}:${discoveryPort}`;
|
|
543
|
+
this.emit('connection-type', { type: connectionType });
|
|
544
|
+
} else {
|
|
545
|
+
socket.destroy();
|
|
546
|
+
socket = null;
|
|
547
|
+
}
|
|
548
|
+
} else if (proxyFrame.type === MSG_TYPE.MSG_ERROR) {
|
|
549
|
+
const errResult = JSON.parse(proxyFrame.payload.toString());
|
|
550
|
+
console.log(`[Magnetk-JS] Relay proxy error: ${errResult.message}`);
|
|
551
|
+
socket.destroy();
|
|
552
|
+
socket = null;
|
|
553
|
+
} else {
|
|
554
|
+
socket.destroy();
|
|
555
|
+
socket = null;
|
|
556
|
+
}
|
|
557
|
+
} catch (e) {
|
|
558
|
+
console.log(`[Magnetk-JS] Relay proxy failed: ${e.message}`);
|
|
559
|
+
if (socket) { socket.destroy(); socket = null; }
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (!socket) throw new Error('All seed connection attempts failed (direct + relay proxy).');
|
|
516
565
|
|
|
517
566
|
this.lastDownloadStats.connectionType = connectionType;
|
|
518
567
|
this.lastDownloadStats.remoteAddr = finalAddr;
|