Skip to content

Commit b614b3d

Browse files
committed
feat: require user passed proxy objects for http and ws
1 parent b185cf3 commit b614b3d

File tree

4 files changed

+30
-66
lines changed

4 files changed

+30
-66
lines changed

README.md

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ npm install bittorrent-tracker
5555
To connect to a tracker, just do this:
5656

5757
```js
58-
var Client = require('bittorrent-tracker')
58+
import Client from 'bittorrent-tracker'
5959

60-
var requiredOpts = {
60+
const requiredOpts = {
6161
infoHash: new Buffer('012345678901234567890'), // hex string or Buffer
6262
peerId: new Buffer('01234567890123456789'), // hex string or Buffer
6363
announce: [], // list of tracker server urls
6464
port: 6881 // torrent client port, (in browser, optional)
6565
}
6666

67-
var optionalOpts = {
67+
const optionalOpts = {
6868
// RTCPeerConnection config object (only used in browser)
6969
rtcConfig: {},
7070
// User-Agent header for http requests
@@ -81,48 +81,24 @@ var optionalOpts = {
8181
customParam: 'blah' // custom parameters supported
8282
}
8383
},
84-
// Proxy config object
84+
// Proxy options (used to proxy requests in node)
8585
proxyOpts: {
86-
// Socks proxy options (used to proxy requests in node)
87-
socksProxy: {
88-
// Configuration from socks module (https://github.com/JoshGlazebrook/socks)
89-
proxy: {
90-
// IP Address of Proxy (Required)
91-
ipaddress: "1.2.3.4",
92-
// TCP Port of Proxy (Required)
93-
port: 1080,
94-
// Proxy Type [4, 5] (Required)
95-
// Note: 4 works for both 4 and 4a.
96-
// Type 4 does not support UDP association relay
97-
type: 5,
98-
99-
// SOCKS 4 Specific:
100-
101-
// UserId used when making a SOCKS 4/4a request. (Optional)
102-
userid: "someuserid",
103-
password: "somepassword",
104-
// SOCKS 5 Specific:
105-
106-
// Authentication used for SOCKS 5 (when it's required) (Optional)
107-
authentication: {
108-
username: "Josh",
109-
password: "somepassword"
110-
}
111-
},
112-
113-
// Amount of time to wait for a connection to be established. (Optional)
114-
// - defaults to 10000ms (10 seconds)
115-
timeout: 10000
116-
},
117-
// 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
119-
// Populated with Socks.Agent if socksProxy is provided
120-
httpAgent: {},
121-
httpsAgent: {}
86+
// For WSS trackers this is always a http.Agent
87+
// For UDP trackers this is an object of options for the Socks Connection
88+
// For HTTP trackers this is either an undici Agent if using Node16 or later, or http.Agent if using versions prior to Node 16, ex:
89+
// import Socks from 'socks'
90+
// proxyOpts.socksProxy = new Socks.Agent(optionsObject, isHttps)
91+
// or if using Node 16 or later
92+
// import { socksDispatcher } from 'fetch-socks'
93+
// proxyOpts.socksProxy = socksDispatcher(optionsObject)
94+
socksProxy: new SocksProxy(socksOptionsObject),
95+
// Populated with socksProxy if it's provided
96+
httpAgent: new http.Agent(agentOptionsObject),
97+
httpsAgent: new https.Agent(agentOptionsObject)
12298
},
12399
}
124100

125-
var client = new Client(requiredOpts)
101+
const client = new Client(requiredOpts)
126102

127103
client.on('error', function (err) {
128104
// fatal client error!
@@ -183,7 +159,7 @@ client.on('scrape', function (data) {
183159
To start a BitTorrent tracker server to track swarms of peers:
184160

185161
```js
186-
const Server = require('bittorrent-tracker').Server
162+
import { Server } from 'bittorrent-tracker'
187163

188164
const server = new Server({
189165
udp: true, // enable udp server? [default=true]
@@ -290,7 +266,7 @@ The http server will handle requests for the following paths: `/announce`, `/scr
290266
Scraping multiple torrent info is possible with a static `Client.scrape` method:
291267

292268
```js
293-
var Client = require('bittorrent-tracker')
269+
import Client from 'bittorrent-tracker'
294270
Client.scrape({ announce: announceUrl, infoHash: [ infoHash1, infoHash2 ]}, function (err, results) {
295271
results[infoHash1].announce
296272
results[infoHash1].infoHash

lib/client/http-tracker.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import arrayRemove from 'unordered-array-remove'
22
import bencode from 'bencode'
3-
import clone from 'clone'
43
import Debug from 'debug'
54
import fetch from 'cross-fetch-ponyfill'
6-
import Socks from 'socks'
7-
import { socksDispatcher } from 'fetch-socks'
85
import { bin2hex, hex2bin, arr2text, text2arr, arr2hex } from 'uint8-util'
96

107
import common from '../common.js'
@@ -122,18 +119,22 @@ class HTTPTracker extends Tracker {
122119
}
123120

124121
async _request (requestUrl, params, cb) {
125-
const self = this
126122
const parsedUrl = new URL(requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + common.querystringStringify(params))
127123
let agent
128124
if (this.client._proxyOpts) {
129125
agent = parsedUrl.protocol === 'https:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent
130126
if (!agent && this.client._proxyOpts.socksProxy) {
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-
}
127+
agent = this.client._proxyOpts.socksProxy
128+
}
129+
}
130+
131+
const cleanup = () => {
132+
if (!controller.signal.aborted) {
133+
arrayRemove(this.cleanupFns, this.cleanupFns.indexOf(cleanup))
134+
controller.abort()
135+
controller = null
136136
}
137+
if (this.maybeDestroyCleanup) this.maybeDestroyCleanup()
137138
}
138139

139140
this.cleanupFns.push(cleanup)
@@ -183,15 +184,6 @@ class HTTPTracker extends Tracker {
183184
debug(`response from ${requestUrl}`)
184185

185186
cb(null, data)
186-
187-
function cleanup () {
188-
if (!controller.signal.aborted) {
189-
arrayRemove(self.cleanupFns, self.cleanupFns.indexOf(cleanup))
190-
controller.abort()
191-
controller = null
192-
}
193-
if (self.maybeDestroyCleanup) self.maybeDestroyCleanup()
194-
}
195187
}
196188

197189
_onAnnounceResponse (data) {

lib/client/websocket-tracker.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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'
64
import { arr2text, arr2hex, hex2bin, bin2hex, randomBytes } from 'uint8-util'
75

86
import common from '../common.js'
@@ -185,7 +183,7 @@ class WebSocketTracker extends Tracker {
185183
if (this.client._proxyOpts) {
186184
agent = parsedUrl.protocol === 'wss:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent
187185
if (!agent && this.client._proxyOpts.socksProxy) {
188-
agent = new Socks.Agent(clone(this.client._proxyOpts.socksProxy), (parsedUrl.protocol === 'wss:'))
186+
agent = this.client._proxyOpts.socksProxy
189187
}
190188
}
191189
this.socket = socketPool[this.announceUrl] = new Socket({ url: this.announceUrl, agent })

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"compact2string": "^1.4.1",
3737
"cross-fetch-ponyfill": "^1.0.1",
3838
"debug": "^4.1.1",
39-
"fetch-socks": "^1.2.0",
4039
"ip": "^1.1.5",
4140
"lru": "^3.1.0",
4241
"minimist": "^1.2.5",
@@ -58,7 +57,6 @@
5857
"semantic-release": "21.1.1",
5958
"standard": "*",
6059
"tape": "5.6.6",
61-
"undici": "^5.23.0",
6260
"webtorrent-fixtures": "2.0.2",
6361
"wrtc": "0.4.7"
6462
},

0 commit comments

Comments
 (0)