Skip to content

Commit 540c5a4

Browse files
committed
Use SocksProxyAgent
1 parent a6922f7 commit 540c5a4

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

lib/client/http-tracker.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import arrayRemove from 'unordered-array-remove'
22
import bencode from 'bencode'
3-
import clone from 'clone'
43
import Debug from 'debug'
54
import get from 'simple-get'
6-
import Socks from 'socks'
5+
import { SocksProxyAgent } from 'socks-proxy-agent'
76
import { bin2hex, hex2bin, arr2text, text2arr, arr2hex } from 'uint8-util'
87

98
import common from '../common.js'
@@ -118,8 +117,20 @@ class HTTPTracker extends Tracker {
118117
let agent
119118
if (this.client._proxyOpts) {
120119
agent = parsedUrl.protocol === 'https:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent
120+
121121
if (!agent && this.client._proxyOpts.socksProxy) {
122-
agent = new Socks.Agent(clone(this.client._proxyOpts.socksProxy), (parsedUrl.protocol === 'https:'))
122+
const socksOpts = this.client._proxyOpts.socksProxy.proxy
123+
124+
const socksURI = new URL(`socks${socksOpts.type}://`)
125+
socksURI.hostname = socksOpts.ipaddress
126+
socksURI.port = socksOpts.port
127+
128+
if (socksOpts.authentication) {
129+
socksURI.username = socksOpts.authentication.username
130+
socksURI.password = socksOpts.authentication.password
131+
}
132+
133+
agent = new SocksProxyAgent(socksURI.href)
123134
}
124135
}
125136

lib/client/websocket-tracker.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import clone from 'clone'
21
import Debug from 'debug'
32
import Peer from '@thaunknown/simple-peer/lite.js'
43
import Socket from '@thaunknown/simple-websocket'
5-
import Socks from 'socks'
4+
import { SocksProxyAgent } from 'socks-proxy-agent'
65
import { arr2text, arr2hex, hex2bin, bin2hex, randomBytes } from 'uint8-util'
76

87
import common from '../common.js'
@@ -184,8 +183,20 @@ class WebSocketTracker extends Tracker {
184183
let agent
185184
if (this.client._proxyOpts) {
186185
agent = parsedUrl.protocol === 'wss:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent
186+
187187
if (!agent && this.client._proxyOpts.socksProxy) {
188-
agent = new Socks.Agent(clone(this.client._proxyOpts.socksProxy), (parsedUrl.protocol === 'wss:'))
188+
const socksOpts = this.client._proxyOpts.socksProxy.proxy
189+
190+
const socksURI = new URL(`socks${socksOpts.type}://`)
191+
socksURI.hostname = socksOpts.ipaddress
192+
socksURI.port = socksOpts.port
193+
194+
if (socksOpts.authentication) {
195+
socksURI.username = socksOpts.authentication.username
196+
socksURI.password = socksOpts.authentication.password
197+
}
198+
199+
agent = new SocksProxyAgent(socksURI.href)
189200
}
190201
}
191202
this.socket = socketPool[this.announceUrl] = new Socket({ url: this.announceUrl, agent })

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"run-series": "^1.1.9",
4646
"simple-get": "^4.0.0",
4747
"socks": "^2.0.0",
48+
"socks-proxy-agent": "^8.0.2",
4849
"string2compact": "^2.0.0",
4950
"uint8-util": "^2.1.9",
5051
"unordered-array-remove": "^1.0.2",

0 commit comments

Comments
 (0)