magnetk 2.2.6 → 2.3.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 +54 -3
- package/package.json +2 -2
- package/share.js +1 -1
package/client.js
CHANGED
|
@@ -426,21 +426,68 @@ export class MagnetkClient extends EventEmitter {
|
|
|
426
426
|
}
|
|
427
427
|
|
|
428
428
|
const addressesToTry = [];
|
|
429
|
+
|
|
430
|
+
// Helper: filter out non-routable addresses
|
|
431
|
+
const isRoutable = (host) => {
|
|
432
|
+
if (!host) return false;
|
|
433
|
+
// 0.0.0.0 is a bind-all address, not valid for connecting
|
|
434
|
+
if (host === '0.0.0.0') return false;
|
|
435
|
+
// Link-local, loopback handled separately
|
|
436
|
+
if (host.startsWith('169.254.')) return false;
|
|
437
|
+
return true;
|
|
438
|
+
};
|
|
439
|
+
|
|
429
440
|
if (lookupResult && lookupResult.found && lookupResult.seeds.length > 0) {
|
|
430
441
|
const seed = lookupResult.seeds[0];
|
|
431
|
-
|
|
442
|
+
console.log(`[Magnetk-JS] Relay returned seed: peer_id=${seed.peer_id}, transport_addr=${seed.transport_addr}, addresses=${JSON.stringify(seed.addresses)}`);
|
|
443
|
+
|
|
444
|
+
// Add transport_addr if routable
|
|
445
|
+
if (seed.transport_addr) {
|
|
446
|
+
const [h] = seed.transport_addr.split(':');
|
|
447
|
+
if (isRoutable(h)) {
|
|
448
|
+
addressesToTry.push(seed.transport_addr);
|
|
449
|
+
} else {
|
|
450
|
+
console.log(`[Magnetk-JS] Skipping non-routable transport_addr: ${seed.transport_addr}`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// Parse multiaddr addresses
|
|
432
455
|
if (seed.addresses) {
|
|
433
456
|
seed.addresses.forEach(addr => {
|
|
434
457
|
const parts = addr.split('/');
|
|
435
458
|
if (parts.length >= 5 && parts[1] === 'ip4' && parts[3] === 'tcp') {
|
|
436
|
-
|
|
459
|
+
const ip = parts[2];
|
|
460
|
+
if (isRoutable(ip)) {
|
|
461
|
+
addressesToTry.push(`${ip}:${parts[4]}`);
|
|
462
|
+
} else {
|
|
463
|
+
console.log(`[Magnetk-JS] Skipping non-routable address: ${addr}`);
|
|
464
|
+
}
|
|
437
465
|
}
|
|
438
466
|
});
|
|
439
467
|
}
|
|
468
|
+
} else {
|
|
469
|
+
console.log(`[Magnetk-JS] Relay lookup: no seeds found for this file hash`);
|
|
440
470
|
}
|
|
471
|
+
|
|
472
|
+
// Fallback: extract seed IP from magnet link relay addr
|
|
473
|
+
// If the magnet link contains the relay multiaddr, the seed's IP might be known
|
|
474
|
+
// from the relay's host (when seed is on same VPS as relay)
|
|
475
|
+
if (addressesToTry.length === 0 && ml.relayAddr) {
|
|
476
|
+
// Try to parse /ip4/HOST/tcp/PORT from relay multiaddr
|
|
477
|
+
const parts = ml.relayAddr.split('/');
|
|
478
|
+
if (parts.length >= 3 && parts[1] === 'ip4') {
|
|
479
|
+
const seedAddr = `${parts[2]}:${this.config.seedPort}`;
|
|
480
|
+
console.log(`[Magnetk-JS] No routable seeds from relay, trying relay host as seed: ${seedAddr}`);
|
|
481
|
+
addressesToTry.push(seedAddr);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// Local fallback (same machine)
|
|
441
486
|
addressesToTry.push(`127.0.0.1:${this.config.seedPort}`);
|
|
442
487
|
|
|
443
488
|
const uniqueAddrs = [...new Set(addressesToTry)];
|
|
489
|
+
console.log(`[Magnetk-JS] Connection attempts: ${uniqueAddrs.join(', ')}`);
|
|
490
|
+
|
|
444
491
|
let socket;
|
|
445
492
|
let connectionType = CONNECTION_TYPE.UNKNOWN;
|
|
446
493
|
let finalAddr = null;
|
|
@@ -450,7 +497,11 @@ export class MagnetkClient extends EventEmitter {
|
|
|
450
497
|
const port = parseInt(portStr, 10) || 4002;
|
|
451
498
|
|
|
452
499
|
try {
|
|
453
|
-
|
|
500
|
+
// Use configurable timeout for connection attempts
|
|
501
|
+
socket = await Promise.race([
|
|
502
|
+
this.connect(host, port),
|
|
503
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), this.config.relayTimeout || 5000))
|
|
504
|
+
]);
|
|
454
505
|
finalAddr = addr;
|
|
455
506
|
if (host === '127.0.0.1' || host === 'localhost') connectionType = CONNECTION_TYPE.LOCAL;
|
|
456
507
|
else if (host === relayHost) connectionType = CONNECTION_TYPE.RELAYED;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "magnetk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "JavaScript SDK for Magnetk P2P File Transfer System",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -34,4 +34,4 @@
|
|
|
34
34
|
],
|
|
35
35
|
"author": "Magnetk Contributors",
|
|
36
36
|
"license": "MIT"
|
|
37
|
-
}
|
|
37
|
+
}
|
package/share.js
CHANGED
|
@@ -8,7 +8,7 @@ async function share() {
|
|
|
8
8
|
const client = new MagnetkClient({
|
|
9
9
|
relayUrl: '69.169.109.243', // Default to local for dev, change to public IPs for prod
|
|
10
10
|
relayPort: 4003,
|
|
11
|
-
seederPath: process.env.MAGNETK_SEEDER_PATH,
|
|
11
|
+
// seederPath: process.env.MAGNETK_SEEDER_PATH || "~root/Desktop/Magnetk/magnetk/bin/seed",
|
|
12
12
|
seedPort: 4002 // Data transfer port (must not conflict with relay 4003)
|
|
13
13
|
});
|
|
14
14
|
|