Skip to content

Commit 48cf99c

Browse files
committed
feat: undici agent and socks
1 parent 32505de commit 48cf99c

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var optionalOpts = {
100100

101101
// UserId used when making a SOCKS 4/4a request. (Optional)
102102
userid: "someuserid",
103-
103+
password: "somepassword",
104104
// SOCKS 5 Specific:
105105

106106
// Authentication used for SOCKS 5 (when it's required) (Optional)
@@ -115,6 +115,7 @@ var optionalOpts = {
115115
timeout: 10000
116116
},
117117
// NodeJS HTTP agents (used to proxy HTTP and Websocket requests in node)
118+
// For HTTP trackers this is either an undici Agent if using Node16 or later, or http.Agent if using versions prior to Node 16
118119
// Populated with Socks.Agent if socksProxy is provided
119120
httpAgent: {},
120121
httpsAgent: {}

lib/client/http-tracker.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import clone from 'clone'
44
import Debug from 'debug'
55
import fetch from 'cross-fetch-ponyfill'
66
import Socks from 'socks'
7+
import { socksDispatcher } from 'fetch-socks'
78
import { bin2hex, hex2bin, arr2text, text2arr, arr2hex } from 'uint8-util'
89

910
import common from '../common.js'
@@ -127,7 +128,11 @@ class HTTPTracker extends Tracker {
127128
if (this.client._proxyOpts) {
128129
agent = parsedUrl.protocol === 'https:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent
129130
if (!agent && this.client._proxyOpts.socksProxy) {
130-
agent = new Socks.Agent(clone(this.client._proxyOpts.socksProxy), (parsedUrl.protocol === 'https:'))
131+
if (globalThis.fetch) {
132+
agent = socksDispatcher(clone(this.client._proxyOpts.socksProxy))
133+
} else {
134+
agent = new Socks.Agent(clone(this.client._proxyOpts.socksProxy), (parsedUrl.protocol === 'https:'))
135+
}
131136
}
132137
}
133138

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"compact2string": "^1.4.1",
3737
"cross-fetch-ponyfill": "^1.0.1",
3838
"debug": "^4.1.1",
39+
"fetch-socks": "^1.2.0",
3940
"ip": "^1.1.5",
4041
"lru": "^3.1.0",
4142
"minimist": "^1.2.5",
@@ -47,6 +48,7 @@
4748
"socks": "^2.0.0",
4849
"string2compact": "^2.0.0",
4950
"uint8-util": "^2.1.9",
51+
"undici": "^5.23.0",
5052
"unordered-array-remove": "^1.0.2",
5153
"ws": "^8.0.0"
5254
},

test/client.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import http from 'http'
44
import fixtures from 'webtorrent-fixtures'
55
import net from 'net'
66
import test from 'tape'
7+
import undici from 'undici'
78

89
const peerId1 = Buffer.from('01234567890123456789')
910
const peerId2 = Buffer.from('12345678901234567890')
@@ -572,12 +573,29 @@ function testClientStartHttpAgent (t, serverType) {
572573
t.plan(5)
573574

574575
common.createServer(t, serverType, function (server, announceUrl) {
575-
const agent = new http.Agent()
576-
let agentUsed = false
577-
agent.createConnection = function (opts, fn) {
578-
agentUsed = true
579-
return net.createConnection(opts, fn)
576+
let agent
577+
if (global.fetch && serverType !== 'ws') {
578+
const connector = undici.buildConnector({ rejectUnauthorized: false })
579+
agent = new undici.Agent({
580+
connect (opts, cb) {
581+
agentUsed = true
582+
connector(opts, (err, socket) => {
583+
if (err) {
584+
cb(err, null)
585+
} else {
586+
cb(null, socket)
587+
}
588+
})
589+
}
590+
})
591+
} else {
592+
agent = new http.Agent()
593+
agent.createConnection = function (opts, fn) {
594+
agentUsed = true
595+
return net.createConnection(opts, fn)
596+
}
580597
}
598+
let agentUsed = false
581599
const client = new Client({
582600
infoHash: fixtures.leaves.parsedTorrent.infoHash,
583601
announce: announceUrl,

0 commit comments

Comments
 (0)