diff --git a/client.js b/client.js index c37345f1..bf659de6 100644 --- a/client.js +++ b/client.js @@ -7,8 +7,8 @@ import queueMicrotask from 'queue-microtask' import { hex2arr, hex2bin, text2arr, arr2hex, arr2text } from 'uint8-util' import common from './lib/common.js' -import HTTPTracker from './lib/client/http-tracker.js' // empty object in browser -import UDPTracker from './lib/client/udp-tracker.js' // empty object in browser +import HTTPTracker from './lib/client/http-tracker.js' +import UDPTracker from './lib/client/udp-tracker.js' import WebSocketTracker from './lib/client/websocket-tracker.js' const debug = Debug('bittorrent-tracker:client') diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 43464e8e..1d68fde2 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -1,7 +1,7 @@ import arrayRemove from 'unordered-array-remove' import bencode from 'bencode' import Debug from 'debug' -import fetch from 'cross-fetch-ponyfill' +import { serverProxyFetch as fetch } from '@fkn/lib' import { bin2hex, hex2bin, arr2text, text2arr, arr2hex } from 'uint8-util' import common from '../common.js' @@ -121,7 +121,36 @@ class HTTPTracker extends Tracker { } async _request (requestUrl, params, cb) { - const parsedUrl = new URL(requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + common.querystringStringify(params)) + const parsedUrl = new URL( + requestUrl + + (requestUrl.indexOf('?') === -1 ? '?' : '&') + + common.querystringStringify( + Object.fromEntries( + Object + .entries(params) + .filter(([_, value]) => + !( + value === undefined + || value === null + || value === '' + || ( + typeof value === 'number' + && isNaN(value) + ) + ) + ) + ) + ) + ) + + parsedUrl.searchParams.set( + 'info_hash', + new URLSearchParams({ info_hash: escape(params.info_hash) }) + .toString() + .replaceAll('%25', '%') + .replace('info_hash=', '') + ) + let agent if (this.client._proxyOpts) { agent = parsedUrl.protocol === 'https:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent @@ -144,7 +173,7 @@ class HTTPTracker extends Tracker { let res let controller = abortTimeout(common.REQUEST_TIMEOUT) try { - res = await fetch(parsedUrl.toString(), { + res = await fetch(parsedUrl.toString().replaceAll('%25', '%'), { agent, signal: controller.signal, dispatcher: agent, diff --git a/package.json b/package.json index 1593e197..7a604ded 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,8 @@ "bittorrent-tracker": "./bin/cmd.js" }, "browser": { - "./lib/common-node.js": false, - "./lib/client/http-tracker.js": false, - "./lib/client/udp-tracker.js": false, "./server.js": false, + "dgram": false, "socks": false }, "chromeapp": { @@ -27,8 +25,11 @@ }, "type": "module", "dependencies": { + "@fkn/lib": "^0.2.1", "@thaunknown/simple-peer": "^10.0.8", "@thaunknown/simple-websocket": "^9.1.3", + "@webvpn/dgram": "^0.2.0", + "@webvpn/net": "^0.2.0", "bencode": "^4.0.0", "bittorrent-peerid": "^1.3.6", "chrome-dgram": "^3.0.6",