Skip to content

Commit 35d3d2a

Browse files
committed
Use new socket to connect to the proxy relay and slice the proxy header from the message
1 parent 591ffbb commit 35d3d2a

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/client/udp-tracker.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ UDPTracker.prototype._request = function (opts) {
6868
parsedUrl.port = 80
6969
}
7070
var timeout
71+
var proxySocket
7172
var socket
73+
var relay
7274
var transactionId = genTransactionId()
7375
if (self.client._socksProxyOpts) {
7476
if (!self.client._socksProxyOpts.proxy) {
@@ -84,9 +86,9 @@ UDPTracker.prototype._request = function (opts) {
8486
}
8587
}
8688

87-
Socks.createConnection(self.client._socksProxyOpts, onGotSocket)
89+
Socks.createConnection(self.client._socksProxyOpts, onGotConnection)
8890
} else {
89-
onGotSocket(null, dgram.createSocket('udp4'))
91+
onGotConnection(null)
9092
}
9193

9294
var cleanup = function () {
@@ -101,13 +103,19 @@ UDPTracker.prototype._request = function (opts) {
101103
socket.on('error', noop) // ignore all future errors
102104
try { socket.close() } catch (err) {}
103105
socket = null
106+
if (proxySocket) {
107+
try { proxySocket.close() } catch (err) {}
108+
proxySocket = null
109+
}
104110
}
105111
self.cleanupFns.push(cleanup)
106112

107-
function onGotSocket (err, s, info) {
113+
function onGotConnection (err, s, info) {
108114
if (err) return onError(err)
109115

110-
socket = s
116+
proxySocket = s
117+
socket = dgram.createSocket('udp4')
118+
relay = info
111119

112120
// does not matter if `stopped` event arrives, so supress errors & cleanup after timeout
113121
var ms = opts.event === 'stopped' ? TIMEOUT / 10 : TIMEOUT
@@ -122,14 +130,15 @@ UDPTracker.prototype._request = function (opts) {
122130
common.CONNECTION_ID,
123131
common.toUInt32(common.ACTIONS.CONNECT),
124132
transactionId
125-
]), info)
133+
]), relay)
126134

127135
socket.on('error', onError)
128136
socket.on('message', onSocketMessage)
129137
}
130138

131139
function onSocketMessage (msg) {
132140
if (self.destroyed) return
141+
if (self.client._socksProxyOpts) msg = msg.slice(10)
133142
if (msg.length < 8 || msg.readUInt32BE(4) !== transactionId.readUInt32BE(0)) {
134143
return onError(new Error('tracker sent invalid transaction id'))
135144
}
@@ -235,7 +244,7 @@ UDPTracker.prototype._request = function (opts) {
235244
common.toUInt32(0), // key (optional)
236245
common.toUInt32(opts.numwant),
237246
toUInt16(self.client._port)
238-
]))
247+
]), relay)
239248
}
240249

241250
function scrape (connectionId) {
@@ -250,7 +259,7 @@ UDPTracker.prototype._request = function (opts) {
250259
common.toUInt32(common.ACTIONS.SCRAPE),
251260
transactionId,
252261
infoHash
253-
]))
262+
]), relay)
254263
}
255264
}
256265

0 commit comments

Comments
 (0)