bittorrent-relay
Advanced tools
Comparing version 1.0.4 to 1.0.5
84
index.js
@@ -82,6 +82,5 @@ import Debug from 'debug' | ||
this.domain = opts.domain || null | ||
this.host = null | ||
this.port = null | ||
this.id = null | ||
this.address = null | ||
this.id = crypto.createHash('sha1').update(`${this.DHTHOST}:${this.DHTPORT}`).digest('hex') | ||
// this.web = {host: '', port: ''} | ||
this.address = `ws://${this.domain || this.TRACKERHOST}:${this.TRACKERPORT}` | ||
this.status = {cpu: 0, memory: 0, state: 1} | ||
@@ -105,10 +104,8 @@ // this.guards = new Set() | ||
debug('listening') | ||
if(!self.address){ | ||
const test = self.http.address() | ||
self.host = test.address | ||
self.port = test.port | ||
self.id = crypto.createHash('sha1').update(`${test.address}:${test.port}`).digest('hex') | ||
self.address = `ws://${self.domain || test.address}:${test.port}` | ||
// self.status.id = self.id | ||
// self.status.address = self.address | ||
const test = self.http.address() | ||
self.address = `ws://${self.domain || test.address}:${test.port}` | ||
for(const socket in this.trackers){ | ||
if(this.trackers[socket].readyState === 1){ | ||
this.trackers[socket].send(JSON.stringify({action: 'address', host: self.domain || test.address, port: test.port, address: self.address})) | ||
} | ||
} | ||
@@ -280,8 +277,16 @@ self.talkToRelay() | ||
// else handle websockets as usual | ||
let action = req.url.slice(0, req.url.lastIndexOf('/')).slice(1) | ||
let hash = req.url.slice(req.url.lastIndexOf('/')).slice(1) | ||
if(!action || !hash || action === hash || action === '/' || hash === '/'){ | ||
socket.terminate() | ||
} else { | ||
if(action === '/relay'){ | ||
try { | ||
const action = req.url.slice(0, req.url.lastIndexOf('/')).slice(1) | ||
const hash = req.url.slice(req.url.lastIndexOf('/')).slice(1) | ||
if(action === 'announce'){ | ||
if(this.status.state !== 1 && this.sendTo[hash] && this.sendTo[hash].length){ | ||
// send them tracker url that is usable then close the socket | ||
socket.send(JSON.stringify({action: 'relay', tracker: this.sendTo[hash][Math.floor(Math.random() * this.sendTo[hash].length)]})) | ||
socket.terminate() | ||
} else { | ||
// use regular socket function from bittorrent-tracker | ||
socket.upgradeReq = req | ||
this.onWebSocketConnection(socket) | ||
} | ||
} else if(action === 'relay'){ | ||
if(this.trackers[hash]){ | ||
@@ -296,15 +301,8 @@ socket.terminate() | ||
} | ||
} else if(action === '/announce'){ | ||
if(this.status.state !== 1 && this.sendTo[hash] && this.sendTo[hash].length){ | ||
// send them tracker url that is usable then close the socket | ||
socket.send(JSON.stringify({action: 'relay', tracker: this.sendTo[hash][Math.floor(Math.random() * this.sendTo[hash].length)]})) | ||
socket.terminate() | ||
} else { | ||
// use regular socket function from bittorrent-tracker | ||
socket.upgradeReq = req | ||
this.onWebSocketConnection(socket) | ||
} | ||
} else { | ||
socket.terminate() | ||
throw new Error('invalid path') | ||
} | ||
} catch (error) { | ||
socket.send(JSON.stringify({action: 'failure reason', error: error.message})) | ||
socket.terminate() | ||
} | ||
@@ -322,9 +320,2 @@ } | ||
this.ws.on('connection', this.ws.onConnection) | ||
this.ws.address = () => { | ||
if(this.http.listening){ | ||
return this.http.address() | ||
} else { | ||
return null | ||
} | ||
} | ||
@@ -509,3 +500,3 @@ // this.intervalUsage(60000) | ||
self.trackers[socket.id] = socket | ||
socket.send(JSON.stringify({id: self.id, address: self.address, host: self.host, port: self.port, relays: self.relays, hashes: self.hashes, action: 'session'})) | ||
socket.send(JSON.stringify({id: self.id, address: self.address, host: self.DHTHOST, port: self.DHTPORT, relays: self.relays, hashes: self.hashes, action: 'session'})) | ||
} | ||
@@ -519,3 +510,3 @@ socket.onError = function(err){ | ||
if(message.action === 'session'){ | ||
if(socket.id !== message.id){ | ||
if(socket.id !== message.id || socket.id !== crypto.createHash('sha1').update(`${message.host}:${message.port}`).digest('hex')){ | ||
socket.terminate() | ||
@@ -526,4 +517,4 @@ } | ||
socket.announce = message.address + '/announce' | ||
socket.host = message.host | ||
socket.port = message.port | ||
socket.dhtHost = message.host | ||
socket.dhtPort = message.port | ||
for(const messageRelay of message.relays){ | ||
@@ -544,2 +535,15 @@ if(self.relays.includes(messageRelay)){ | ||
} | ||
if(message.action === 'address'){ | ||
if(socket.address !== message.address){ | ||
socket.address = message.address | ||
socket.relay = message.address + '/relay' | ||
socket.announce = message.address + '/announce' | ||
} | ||
if(socket.trackerHost !== message.host){ | ||
socket.trackerHost = message.host | ||
} | ||
if(socket.trackerPort !== message.port){ | ||
socket.trackerPort = message.port | ||
} | ||
} | ||
if(message.action === 'status'){ | ||
@@ -546,0 +550,0 @@ if(message.state === 1){ |
{ | ||
"name": "bittorrent-relay", | ||
"description": "Uses the mainline dht to relay requests to other trackers in a swarm", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"bin": { | ||
@@ -20,3 +20,2 @@ "bittorrent-tracker": "./bin/cmd.js" | ||
"express": "^4.18.2", | ||
"http-proxy-middleware": "^2.0.6", | ||
"ip": "^1.1.5", | ||
@@ -23,0 +22,0 @@ "lru": "^3.1.0", |
47476
28
1247
- Removedhttp-proxy-middleware@^2.0.6
- Removed@types/http-proxy@1.17.15(transitive)
- Removed@types/node@22.10.7(transitive)
- Removedbraces@3.0.3(transitive)
- Removedeventemitter3@4.0.7(transitive)
- Removedfill-range@7.1.1(transitive)
- Removedfollow-redirects@1.15.9(transitive)
- Removedhttp-proxy@1.18.1(transitive)
- Removedhttp-proxy-middleware@2.0.7(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-number@7.0.0(transitive)
- Removedis-plain-obj@3.0.0(transitive)
- Removedmicromatch@4.0.8(transitive)
- Removedpicomatch@2.3.1(transitive)
- Removedrequires-port@1.0.0(transitive)
- Removedto-regex-range@5.0.1(transitive)
- Removedundici-types@6.20.0(transitive)