diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..0bb7ae248 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,23 @@ +name: ci +'on': + - push + - pull_request +jobs: + test: + name: Node ${{ matrix.node }} / ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + node: + - '18' + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node }} + - run: npm install + - run: npm run build --if-present + - run: npm test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..61c6902cf --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,36 @@ +name: Release + +on: + push: + branches: + - master + +jobs: + release: + name: Release + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + persist-credentials: false + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + - name: Cache + uses: actions/cache@v4 + with: + path: ~/.npm + key: ${{ runner.os }}-npm-${{ hashFiles('**/package.json') }} + restore-keys: | + ${{ runner.os }}-npm- + - name: Install dependencies + run: npm i + env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + - name: Release + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: npx semantic-release diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 000000000..2b2bab3bc --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,23 @@ +name: Mark stale issues and pull requests + +on: + schedule: + - cron: '0 12 * * *' + +jobs: + stale: + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + + steps: + - uses: actions/stale@v8 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + stale-issue-message: 'Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?' + stale-pr-message: 'Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?' + exempt-issue-labels: accepted,blocked,bug,dependency,enhancement,'help wanted',question,security,meta + exempt-pr-labels: accepted,blocked,bug,dependency,enhancement,'help wanted',question,security,meta + stale-issue-label: 'stale' + stale-pr-label: 'stale' diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..504afef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +package-lock.json diff --git a/.npmignore b/.npmignore index 2e1712c4d..ae28c2087 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,6 @@ -img.png +CONTRIBUTING.md +examples/ +img/ +test/ +tools/ +.github/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 78917f089..000000000 --- a/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ -language: node_js -sudo: false -node_js: - - "node" -before_script: - - export DISPLAY=:99.0; sh -e /etc/init.d/xvfb start diff --git a/AUTHORS.md b/AUTHORS.md index 7dc1be43e..3b930ae6f 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -24,12 +24,51 @@ - Alex (alxmorais8@msn.com) - Harsh Vakharia (harshjv@users.noreply.github.com) - Yoann Ciabaud (yoann@sonora.io) -- Diego Rodríguez Baquero (DiegoRBaquero@users.noreply.github.com) - Autarc (autarc@gmail.com) +- Diego Rodríguez Baquero (diegorbaquero@gmail.com) - Kirill Fomichev (fanatid@ya.ru) - Matt Bell (mappum@gmail.com) +- Diego Rodríguez (diegorbaquero@gmail.com) - Philipp Henkel (henkel@users.noreply.github.com) - jakefb (jacobafb@gmail.com) - Nick Frost (nickfrostatx@gmail.com) +- ZunSThy (zunsthy@gmail.com) +- vijayanand nandam (vijay@cybrilla.com) +- Luigi Pinca (luigipinca@gmail.com) +- Diego R. B (diegorbaquero@gmail.com) +- greenkeeper[bot] (greenkeeper[bot]@users.noreply.github.com) +- hrafnkell orri sigurdsson (hrafnkellos@gmail.com) +- Brian Clifton (brian@clifton.me) +- James M Snell (jasnell@gmail.com) +- crapthings (crapthings@gmail.com) +- daiyu (qqdaiyu55@gmail.com) +- LI (kslrwang@gmail.com) +- Jimmy Wärting (jimmy@warting.se) +- Justin Kalland (justin@kalland.ch) +- greenkeeper[bot] (23040076+greenkeeper[bot]@users.noreply.github.com) +- Eric Guan (guanzo91@gmail.com) +- Koushik Dutta (koush@koushikdutta.com) +- KayleePop (34007889+KayleePop@users.noreply.github.com) +- Diego Rodriguez Baquero (diego.baquero@pager.com) +- Diego Rodríguez Baquero (github@diegorbaquero.com) +- Renovate Bot (bot@renovateapp.com) +- Diego Rodríguez Baquero (diego@mothership.com) +- Diego Rodriguez Baquero (diego@arc.io) +- semantic-release-bot (semantic-release-bot@martynus.net) +- renovate[bot] (29139614+renovate[bot]@users.noreply.github.com) +- Jocelyn Liu (yrliou@gmail.com) +- Bruce Hopkins (behopkinsjr@gmail.com) +- Ryan Finnie (ryan@finnie.org) +- Lookis (lookisliu@gmail.com) +- Paul Sharypov (pavloniym@gmail.com) +- Cas (6506529+ThaUnknown@users.noreply.github.com) +- Tom Snelling (tomsnelling8@gmail.com) +- Cas_ (6506529+ThaUnknown@users.noreply.github.com) +- Arsène Fougerouse (arsene582@gmail.com) +- Brad Marsden (silentbot1@gmail.com) +- krazak (krazak@vt.edu) +- Chocobozzz (chocobozzz@cpy.re) +- uriva (uriva@users.noreply.github.com) +- Subin Siby (mail@subinsb.com) -#### Generated by bin/update-authors.sh. +#### Generated by tools/update-authors.sh. diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..6b00b4dce --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,275 @@ +## [11.2.2](https://github.com/webtorrent/bittorrent-tracker/compare/v11.2.1...v11.2.2) (2025-09-06) + + +### Bug Fixes + +* export WebSocketTracker ([#558](https://github.com/webtorrent/bittorrent-tracker/issues/558)) ([1571551](https://github.com/webtorrent/bittorrent-tracker/commit/15715518decfed77d7888ba21d6ab592fa91cc85)) + +## [11.2.1](https://github.com/webtorrent/bittorrent-tracker/compare/v11.2.0...v11.2.1) (2025-01-19) + + +### Bug Fixes + +* http announce no left ([#548](https://github.com/webtorrent/bittorrent-tracker/issues/548)) ([3cd77f3](https://github.com/webtorrent/bittorrent-tracker/commit/3cd77f3e6f5b52f5d58adaf004b333cd2061a4da)) + +# [11.2.0](https://github.com/webtorrent/bittorrent-tracker/compare/v11.1.2...v11.2.0) (2024-12-28) + + +### Features + +* release [#539](https://github.com/webtorrent/bittorrent-tracker/issues/539) and [#535](https://github.com/webtorrent/bittorrent-tracker/issues/535) ([#544](https://github.com/webtorrent/bittorrent-tracker/issues/544)) ([e7de90c](https://github.com/webtorrent/bittorrent-tracker/commit/e7de90c0cbcfb41c9c53c5caf69cc37c6d3ef1e8)) + +## [11.1.2](https://github.com/webtorrent/bittorrent-tracker/compare/v11.1.1...v11.1.2) (2024-08-13) + + +### Bug Fixes + +* statuscode ([#526](https://github.com/webtorrent/bittorrent-tracker/issues/526)) ([e9d8f8c](https://github.com/webtorrent/bittorrent-tracker/commit/e9d8f8cd754ba26d86f32f9b8da0c0c4a3dcd646)) + +## [11.1.1](https://github.com/webtorrent/bittorrent-tracker/compare/v11.1.0...v11.1.1) (2024-07-01) + + +### Performance Improvements + +* drop clone ([#523](https://github.com/webtorrent/bittorrent-tracker/issues/523)) ([83a24ce](https://github.com/webtorrent/bittorrent-tracker/commit/83a24ce77fb1a96b7fe4c383ce92d7c28fc165a7)) + +# [11.1.0](https://github.com/webtorrent/bittorrent-tracker/compare/v11.0.2...v11.1.0) (2024-05-22) + + +### Bug Fixes + +* semantic release ([#520](https://github.com/webtorrent/bittorrent-tracker/issues/520)) ([428fb22](https://github.com/webtorrent/bittorrent-tracker/commit/428fb224f5666731332738032649f4448b2e1e4f)) + + +### Features + +* updated webrtc implementation ([#519](https://github.com/webtorrent/bittorrent-tracker/issues/519)) ([633d68a](https://github.com/webtorrent/bittorrent-tracker/commit/633d68a32c2c143fec0182317a9801dd1b64faef)) + +## [11.0.2](https://github.com/webtorrent/bittorrent-tracker/compare/v11.0.1...v11.0.2) (2024-03-12) + + +### Bug Fixes + +* **parse-http:** ignore announcements from peers with invalid announcement ports. ([#513](https://github.com/webtorrent/bittorrent-tracker/issues/513)) ([fe75272](https://github.com/webtorrent/bittorrent-tracker/commit/fe75272d51653e626583689081afb0b7aeadb84f)) + +## [11.0.1](https://github.com/webtorrent/bittorrent-tracker/compare/v11.0.0...v11.0.1) (2024-01-16) + + +### Bug Fixes + +* update build badge url ([#506](https://github.com/webtorrent/bittorrent-tracker/issues/506)) ([3f59b58](https://github.com/webtorrent/bittorrent-tracker/commit/3f59b58a020ea8c0926be135471a6666fe8e8b21)) + +# [11.0.0](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.12...v11.0.0) (2023-10-31) + + +### Features + +* **major:** drop simple-get ([#443](https://github.com/webtorrent/bittorrent-tracker/issues/443)) ([bce64e1](https://github.com/webtorrent/bittorrent-tracker/commit/bce64e155df6ff9fa605898cbf7498bf76188d8b)) + + +### BREAKING CHANGES + +* **major:** drop simple-get + +* perf: drop simple-get + +* feat: undici agent and socks + +* fix: undici as dev dependency + +* feat: require user passed proxy objects for http and ws + +* chore: include undici for tests + +## [10.0.12](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.11...v10.0.12) (2023-08-09) + + +### Bug Fixes + +* **deps:** update dependency bencode to v4 ([#487](https://github.com/webtorrent/bittorrent-tracker/issues/487)) ([aeccf9c](https://github.com/webtorrent/bittorrent-tracker/commit/aeccf9c1c4b9115fd23b4fe1a0ab990b5add0f17)) + +## [10.0.11](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.10...v10.0.11) (2023-08-01) + + +### Bug Fixes + +* mangled scrape infohashes ([#486](https://github.com/webtorrent/bittorrent-tracker/issues/486)) ([11cce83](https://github.com/webtorrent/bittorrent-tracker/commit/11cce83ddd858813f5684da8a116de4bee6e518b)) + +## [10.0.10](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.9...v10.0.10) (2023-06-16) + + +### Performance Improvements + +* use simple-peer/lite ([#475](https://github.com/webtorrent/bittorrent-tracker/issues/475)) ([5b8db06](https://github.com/webtorrent/bittorrent-tracker/commit/5b8db067e48cc81796728ff538d7ff6efafc59b8)) + +## [10.0.9](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.8...v10.0.9) (2023-06-16) + + +### Performance Improvements + +* use peer/lite ([#474](https://github.com/webtorrent/bittorrent-tracker/issues/474)) ([7c845f0](https://github.com/webtorrent/bittorrent-tracker/commit/7c845f030d07b1bf7060ab880b790ee85a8c7ac0)) + +## [10.0.8](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.7...v10.0.8) (2023-06-07) + + +### Bug Fixes + +* bigInt ([#472](https://github.com/webtorrent/bittorrent-tracker/issues/472)) ([d7061f7](https://github.com/webtorrent/bittorrent-tracker/commit/d7061f73b2ebff072e064971a5960749a7335bae)) + +## [10.0.7](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.6...v10.0.7) (2023-06-05) + + +### Bug Fixes + +* imports ([#471](https://github.com/webtorrent/bittorrent-tracker/issues/471)) ([a12022a](https://github.com/webtorrent/bittorrent-tracker/commit/a12022ac2c81d7fa3ecb81163852161e64199cf4)) + +## [10.0.6](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.5...v10.0.6) (2023-05-27) + + +### Bug Fixes + +* replace simple-peer with maintained one ([#466](https://github.com/webtorrent/bittorrent-tracker/issues/466)) ([3b2dedb](https://github.com/webtorrent/bittorrent-tracker/commit/3b2dedb4151615831ca12d3d0a830354b1c04e68)) + +## [10.0.5](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.4...v10.0.5) (2023-05-27) + + +### Bug Fixes + +* only stringify views ([#467](https://github.com/webtorrent/bittorrent-tracker/issues/467)) ([52f5502](https://github.com/webtorrent/bittorrent-tracker/commit/52f55020f38894e4d45e12c87184540d8b0acad3)) + +## [10.0.4](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.3...v10.0.4) (2023-05-26) + + +### Bug Fixes + +* drop buffer ([#465](https://github.com/webtorrent/bittorrent-tracker/issues/465)) ([c99eb89](https://github.com/webtorrent/bittorrent-tracker/commit/c99eb892088ef3c67ea5bf014dfdd86799251a7e)) + +## [10.0.3](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.2...v10.0.3) (2023-05-25) + + +### Performance Improvements + +* replace simple websocket with maintained one ([#464](https://github.com/webtorrent/bittorrent-tracker/issues/464)) ([3f01c29](https://github.com/webtorrent/bittorrent-tracker/commit/3f01c29122efd726d805673da82f43ce5592b793)) + +## [10.0.2](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.1...v10.0.2) (2023-02-01) + + +### Bug Fixes + +* **deps:** update dependency ws to v8 ([#448](https://github.com/webtorrent/bittorrent-tracker/issues/448)) ([2209d4f](https://github.com/webtorrent/bittorrent-tracker/commit/2209d4f21bdee10e575c1728c3accf7bd34380c9)), closes [#449](https://github.com/webtorrent/bittorrent-tracker/issues/449) + +## [10.0.1](https://github.com/webtorrent/bittorrent-tracker/compare/v10.0.0...v10.0.1) (2022-12-07) + + +### Bug Fixes + +* **deps:** update dependency bencode to v3 ([#434](https://github.com/webtorrent/bittorrent-tracker/issues/434)) [skip ci] ([926ceee](https://github.com/webtorrent/bittorrent-tracker/commit/926ceee0bac6dfe49877566aaa3cf645689492d1)) +* **deps:** update dependency string2compact to v2 ([#437](https://github.com/webtorrent/bittorrent-tracker/issues/437)) ([9be843c](https://github.com/webtorrent/bittorrent-tracker/commit/9be843c5e46ac2ab518187bf0d348e1e69e8633d)) + +# [10.0.0](https://github.com/webtorrent/bittorrent-tracker/compare/v9.19.0...v10.0.0) (2022-12-05) + + +### Features + +* esm ([#431](https://github.com/webtorrent/bittorrent-tracker/issues/431)) ([e6d3189](https://github.com/webtorrent/bittorrent-tracker/commit/e6d3189edf1a170197a799b97d84c632692b394f)) + + +### BREAKING CHANGES + +* ESM only + +* feat: esm + +* fix: linter oops + +# [9.19.0](https://github.com/webtorrent/bittorrent-tracker/compare/v9.18.6...v9.19.0) (2022-06-01) + + +### Features + +* **events:** Support of `paused` client event ([#411](https://github.com/webtorrent/bittorrent-tracker/issues/411)) ([ef76b3f](https://github.com/webtorrent/bittorrent-tracker/commit/ef76b3f3b6beee87f57d74addd0ca2ef2c517b6d)) + +## [9.18.6](https://github.com/webtorrent/bittorrent-tracker/compare/v9.18.5...v9.18.6) (2022-05-11) + + +### Bug Fixes + +* revert [#420](https://github.com/webtorrent/bittorrent-tracker/issues/420) ([8d54938](https://github.com/webtorrent/bittorrent-tracker/commit/8d54938f164347d57a7991268d191e44b752de7f)) + +## [9.18.5](https://github.com/webtorrent/bittorrent-tracker/compare/v9.18.4...v9.18.5) (2022-03-25) + + +### Bug Fixes + +* connection leaks ([#420](https://github.com/webtorrent/bittorrent-tracker/issues/420)) ([f7928cf](https://github.com/webtorrent/bittorrent-tracker/commit/f7928cfcc646cd95556549b64e61228892314682)) + +## [9.18.4](https://github.com/webtorrent/bittorrent-tracker/compare/v9.18.3...v9.18.4) (2022-03-06) + + +### Bug Fixes + +* typo in ws example ([#417](https://github.com/webtorrent/bittorrent-tracker/issues/417)) ([023afb9](https://github.com/webtorrent/bittorrent-tracker/commit/023afb9a3228d60392a18e70f85cdb6af5fa79fb)) + +## [9.18.3](https://github.com/webtorrent/bittorrent-tracker/compare/v9.18.2...v9.18.3) (2021-10-29) + + +### Bug Fixes + +* **deps:** update dependency clone to v2 ([#393](https://github.com/webtorrent/bittorrent-tracker/issues/393)) ([dc6f796](https://github.com/webtorrent/bittorrent-tracker/commit/dc6f7966844216c39491d6623dd412d5ca65d4c4)) + +## [9.18.2](https://github.com/webtorrent/bittorrent-tracker/compare/v9.18.1...v9.18.2) (2021-09-02) + + +### Bug Fixes + +* **deps:** update dependency socks to v2 ([#394](https://github.com/webtorrent/bittorrent-tracker/issues/394)) ([353e1f4](https://github.com/webtorrent/bittorrent-tracker/commit/353e1f40093a5e74cb54219abbae8ef0cc3d9e0b)) + +## [9.18.1](https://github.com/webtorrent/bittorrent-tracker/compare/v9.18.0...v9.18.1) (2021-09-01) + + +### Bug Fixes + +* disable socks in chromeapp ([#398](https://github.com/webtorrent/bittorrent-tracker/issues/398)) ([7fd5877](https://github.com/webtorrent/bittorrent-tracker/commit/7fd587789548453a852ea01e54900a5e9155db67)) + +# [9.18.0](https://github.com/webtorrent/bittorrent-tracker/compare/v9.17.4...v9.18.0) (2021-08-20) + + +### Features + +* add proxy support for tracker clients ([#356](https://github.com/webtorrent/bittorrent-tracker/issues/356)) ([ad64dc3](https://github.com/webtorrent/bittorrent-tracker/commit/ad64dc3a68cddccc2c1f05d0d8bb833f2c4860b2)) + +## [9.17.4](https://github.com/webtorrent/bittorrent-tracker/compare/v9.17.3...v9.17.4) (2021-07-22) + + +### Bug Fixes + +* if websocket closed, don't produce a response ([ca88435](https://github.com/webtorrent/bittorrent-tracker/commit/ca88435617e59714a456031c75b3a329897d97bd)) + +## [9.17.3](https://github.com/webtorrent/bittorrent-tracker/compare/v9.17.2...v9.17.3) (2021-07-02) + + +### Bug Fixes + +* auto update authors on version ([b5ffc70](https://github.com/webtorrent/bittorrent-tracker/commit/b5ffc708ada0bef66e7fa0cd1872527ea6dd8d53)) + +## [9.17.2](https://github.com/webtorrent/bittorrent-tracker/compare/v9.17.1...v9.17.2) (2021-06-15) + + +### Bug Fixes + +* modernize ([e5994d2](https://github.com/webtorrent/bittorrent-tracker/commit/e5994d2ebdec10fe2165e31f5b498382eeeaaf5f)) + +## [9.17.1](https://github.com/webtorrent/bittorrent-tracker/compare/v9.17.0...v9.17.1) (2021-06-15) + + +### Bug Fixes + +* add package-lock ([0e486b0](https://github.com/webtorrent/bittorrent-tracker/commit/0e486b09d80d30e1c13d4624e29c4251000d4092)) +* **deps:** update dependency bn.js to ^5.2.0 ([2d36e4a](https://github.com/webtorrent/bittorrent-tracker/commit/2d36e4ae60b1bac51773f2dca81c1a158b51cb28)) +* **deps:** update dependency chrome-dgram to ^3.0.6 ([a82aaaa](https://github.com/webtorrent/bittorrent-tracker/commit/a82aaaa31963a0d9adb640166f417142c5d7b970)) +* **deps:** update dependency run-parallel to ^1.2.0 ([fcf25ed](https://github.com/webtorrent/bittorrent-tracker/commit/fcf25ed40e1fd64e630b10a0281bc09604b901d3)) +* **deps:** update dependency run-series to ^1.1.9 ([fa2c33f](https://github.com/webtorrent/bittorrent-tracker/commit/fa2c33fc91f8ef0a47d0f40b7a046ae179ee328a)) +* **deps:** update dependency simple-websocket to ^9.1.0 ([96fedbd](https://github.com/webtorrent/bittorrent-tracker/commit/96fedbdf56ddcf6627eb373a33589db885cb4fb7)) +* **deps:** update dependency ws to ^7.4.5 ([6ad7ead](https://github.com/webtorrent/bittorrent-tracker/commit/6ad7ead994e5cb99980a406aea908e4b9ff6151c)) +* **deps:** update webtorrent ([1e8d47d](https://github.com/webtorrent/bittorrent-tracker/commit/1e8d47dcd8f5f53b42aa75265a129f950d16feef)) +* UDP url parsing ([8e24a8c](https://github.com/webtorrent/bittorrent-tracker/commit/8e24a8c97b55bbaaf2c92a496d1cd30b0c008934)) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index abc173f2c..e5fc5a241 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -24,7 +24,7 @@ and to avoid style arguments. `npm test` runs `standard` automatically, so you d to! [standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg -[standard-url]: https://github.com/feross/standard +[standard-url]: https://standardjs.com ## Project Governance diff --git a/LICENSE b/LICENSE index c7e685275..70a7089c6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) Feross Aboukhadijeh +Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/README.md b/README.md index a0c3aac96..db0eba161 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,17 @@ -# bittorrent-tracker [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] +# bittorrent-tracker [![ci][ci-image]][ci-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] -[travis-image]: https://img.shields.io/travis/feross/bittorrent-tracker/master.svg -[travis-url]: https://travis-ci.org/feross/bittorrent-tracker +[ci-image]: https://img.shields.io/github/actions/workflow/status/webtorrent/bittorrent-tracker/ci.yml +[ci-url]: https://github.com/webtorrent/bittorrent-tracker/actions [npm-image]: https://img.shields.io/npm/v/bittorrent-tracker.svg [npm-url]: https://npmjs.org/package/bittorrent-tracker [downloads-image]: https://img.shields.io/npm/dm/bittorrent-tracker.svg [downloads-url]: https://npmjs.org/package/bittorrent-tracker +[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg +[standard-url]: https://standardjs.com #### Simple, robust, BitTorrent tracker (client & server) implementation -![tracker](https://raw.githubusercontent.com/feross/bittorrent-tracker/master/img.png) +![tracker visualization](img/img.png) Node.js implementation of a [BitTorrent tracker](https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol), client and server. @@ -31,10 +33,14 @@ This module is used by [WebTorrent](http://webtorrent.io). - Supports tracker "scrape" extension - Robust and well-tested - Comprehensive test suite (runs entirely offline, so it's reliable) - - Used by popular clients: [WebTorrent](http://webtorrent.io), [peerflix](https://github.com/mafintosh/peerflix), and [playback](https://mafintosh.github.io/playback/) + - Used by popular clients: [WebTorrent](http://webtorrent.io), [peerflix](https://www.npmjs.com/package/peerflix), and [playback](https://mafintosh.github.io/playback/) - Tracker statistics available via web interface at `/stats` or JSON data at `/stats.json` -Also see [bittorrent-dht](https://github.com/feross/bittorrent-dht). +Also see [bittorrent-dht](https://www.npmjs.com/package/bittorrent-dht). + +### Tracker stats + +![Screenshot](img/trackerStats.png) ## install @@ -49,22 +55,24 @@ npm install bittorrent-tracker To connect to a tracker, just do this: ```js -var Client = require('bittorrent-tracker') +import Client from 'bittorrent-tracker' -var requiredOpts = { +const requiredOpts = { infoHash: new Buffer('012345678901234567890'), // hex string or Buffer peerId: new Buffer('01234567890123456789'), // hex string or Buffer announce: [], // list of tracker server urls port: 6881 // torrent client port, (in browser, optional) } -var optionalOpts = { +const optionalOpts = { // RTCPeerConnection config object (only used in browser) rtcConfig: {}, - // custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc) + // User-Agent header for http requests + userAgent: '', + // Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc) wrtc: {}, getAnnounceOpts: function () { - // provide a callback that will be called whenever announce() is called + // Provide a callback that will be called whenever announce() is called // internally (on timer), or by the user return { uploaded: 0, @@ -72,10 +80,25 @@ var optionalOpts = { left: 0, customParam: 'blah' // custom parameters supported } - } + }, + // Proxy options (used to proxy requests in node) + proxyOpts: { + // For WSS trackers this is always a http.Agent + // For UDP trackers this is an object of options for the Socks Connection + // 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: + // import Socks from 'socks' + // proxyOpts.socksProxy = new Socks.Agent(optionsObject, isHttps) + // or if using Node 16 or later + // import { socksDispatcher } from 'fetch-socks' + // proxyOpts.socksProxy = socksDispatcher(optionsObject) + socksProxy: new SocksProxy(socksOptionsObject), + // Populated with socksProxy if it's provided + httpAgent: new http.Agent(agentOptionsObject), + httpsAgent: new https.Agent(agentOptionsObject) + }, } -var client = new Client(requiredOpts) +const client = new Client(requiredOpts) client.on('error', function (err) { // fatal client error! @@ -136,13 +159,15 @@ client.on('scrape', function (data) { To start a BitTorrent tracker server to track swarms of peers: ```js -var Server = require('bittorrent-tracker').Server +import { Server } from 'bittorrent-tracker' +// Or import Server from 'bittorrent-tracker/server' -var server = new Server({ +const server = new Server({ udp: true, // enable udp server? [default=true] http: true, // enable http server? [default=true] ws: true, // enable websocket server? [default=true] stats: true, // enable web-based statistics? [default=true] + trustProxy: false, // enable trusting x-forwarded-for header for remote IP [default=false] filter: function (infoHash, params, cb) { // Blacklist/whitelist function for allowing/disallowing torrents. If this option is // omitted, all torrents are allowed. It is possible to interface with a database or @@ -154,11 +179,15 @@ var server = new Server({ // This example only allows one torrent. - var allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa') - cb(allowed) - - // In addition to returning a boolean (`true` for allowed, `false` for disallowed), - // you can return an `Error` object to disallow and provide a custom reason. + const allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa') + if (allowed) { + // If the callback is passed `null`, the torrent will be allowed. + cb(null) + } else { + // If the callback is passed an `Error` object, the torrent will be disallowed + // and the error's `message` property will be given as the reason. + cb(new Error('disallowed torrent')) + } } }) @@ -179,12 +208,34 @@ server.on('warning', function (err) { server.on('listening', function () { // fired when all requested servers are listening - console.log('listening on http port:' + server.http.address().port) - console.log('listening on udp port:' + server.udp.address().port) + + // HTTP + const httpAddr = server.http.address() + const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost' + const httpPort = httpAddr.port + console.log(`HTTP tracker: http://${httpHost}:${httpPort}/announce`) + + // UDP + const udpAddr = server.udp.address() + const udpHost = udpAddr.address + const udpPort = udpAddr.port + console.log(`UDP tracker: udp://${udpHost}:${udpPort}`) + + // WS + const wsAddr = server.ws.address() + const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost' + const wsPort = wsAddr.port + console.log(`WebSocket tracker: ws://${wsHost}:${wsPort}`) + }) + // start tracker server listening! Use 0 to listen on a random free port. -server.listen(port, hostname, onlistening) +const port = 0 +const hostname = "localhost" +server.listen(port, hostname, () => { + // Do something on listening... +}) // listen for individual tracker messages from peers: @@ -216,8 +267,10 @@ The http server will handle requests for the following paths: `/announce`, `/scr Scraping multiple torrent info is possible with a static `Client.scrape` method: ```js -var Client = require('bittorrent-tracker') -Client.scrape(announceUrl, [ infoHash1, infoHash2 ], function (err, results) { +import Client from 'bittorrent-tracker' +// Or import Client from 'bittorrent-tracker/client' + +Client.scrape({ announce: announceUrl, infoHash: [ infoHash1, infoHash2 ]}, function (err, results) { results[infoHash1].announce results[infoHash1].infoHash results[infoHash1].complete @@ -230,6 +283,12 @@ Client.scrape(announceUrl, [ infoHash1, infoHash2 ], function (err, results) { ## command line +Install `bittorrent-tracker` globally: + +```sh +$ npm install -g bittorrent-tracker +``` + Easily start a tracker server: ```sh @@ -260,10 +319,8 @@ $ bittorrent-tracker --help -q, --quiet only show error output -s, --silent show no output -v, --version print the current version - - Please report bugs! https://github.com/feross/bittorrent-tracker/issues ``` ## license -MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). +MIT. Copyright (c) [Feross Aboukhadijeh](https://feross.org) and [WebTorrent, LLC](https://webtorrent.io). diff --git a/bin/cmd.js b/bin/cmd.js index 49c8fdd04..c57eef55b 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -1,9 +1,9 @@ #!/usr/bin/env node -var minimist = require('minimist') -var Server = require('../').Server +import minimist from 'minimist' +import { Server } from '../index.js' -var argv = minimist(process.argv.slice(2), { +const argv = minimist(process.argv.slice(2), { alias: { h: 'help', p: 'port', @@ -39,7 +39,7 @@ if (argv.version) { } if (argv.help) { - console.log(function () { + console.log((() => { /* bittorrent-tracker - Start a bittorrent tracker server @@ -63,22 +63,20 @@ if (argv.help) { -s, --silent show no output -v, --version print the current version - Please report bugs! https://github.com/feross/bittorrent-tracker/issues - */ - }.toString().split(/\n/).slice(2, -2).join('\n')) + }).toString().split(/\n/).slice(2, -2).join('\n')) process.exit(0) } if (argv.silent) argv.quiet = true -var allFalsy = !argv.http && !argv.udp && !argv.ws +const allFalsy = !argv.http && !argv.udp && !argv.ws argv.http = allFalsy || argv.http argv.udp = allFalsy || argv.udp argv.ws = allFalsy || argv.ws -var server = new Server({ +const server = new Server({ http: argv.http, interval: argv.interval, stats: argv.stats, @@ -87,60 +85,60 @@ var server = new Server({ ws: argv.ws }) -server.on('error', function (err) { - if (!argv.silent) console.error('ERROR: ' + err.message) +server.on('error', err => { + if (!argv.silent) console.error(`${new Date().toISOString()} ERROR: ${err.message}`) }) -server.on('warning', function (err) { - if (!argv.quiet) console.log('WARNING: ' + err.message) +server.on('warning', err => { + if (!argv.quiet) console.log(`${new Date().toISOString()} WARNING: ${err.message}`) }) -server.on('update', function (addr) { - if (!argv.quiet) console.log('update: ' + addr) +server.on('update', addr => { + if (!argv.quiet) console.log(`${new Date().toISOString()} update: ${addr}`) }) -server.on('complete', function (addr) { - if (!argv.quiet) console.log('complete: ' + addr) +server.on('complete', addr => { + if (!argv.quiet) console.log(`${new Date().toISOString()} complete: ${addr}`) }) -server.on('start', function (addr) { - if (!argv.quiet) console.log('start: ' + addr) +server.on('start', addr => { + if (!argv.quiet) console.log(`${new Date().toISOString()} start: ${addr}`) }) -server.on('stop', function (addr) { - if (!argv.quiet) console.log('stop: ' + addr) +server.on('stop', addr => { + if (!argv.quiet) console.log(`${new Date().toISOString()} stop: ${addr}`) }) -var hostname = { +const hostname = { http: argv['http-hostname'], udp4: argv['udp-hostname'], - udp6: argv['upd6-hostname'] + udp6: argv['udp6-hostname'] } -server.listen(argv.port, hostname, function () { +server.listen(argv.port, hostname, () => { if (server.http && argv.http && !argv.quiet) { - var httpAddr = server.http.address() - var httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost' - var httpPort = httpAddr.port - console.log('HTTP tracker: http://' + httpHost + ':' + httpPort + '/announce') + const httpAddr = server.http.address() + const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost' + const httpPort = httpAddr.port + console.log(`${new Date().toISOString()} HTTP tracker: http://${httpHost}:${httpPort}/announce`) } if (server.udp && !argv.quiet) { - var udpAddr = server.udp.address() - var udpHost = udpAddr.address - var udpPort = udpAddr.port - console.log('UDP tracker: udp://' + udpHost + ':' + udpPort) + const udpAddr = server.udp.address() + const udpHost = udpAddr.address + const udpPort = udpAddr.port + console.log(`${new Date().toISOString()} UDP tracker: udp://${udpHost}:${udpPort}`) } if (server.udp6 && !argv.quiet) { - var udp6Addr = server.udp6.address() - var udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost' - var udp6Port = udp6Addr.port - console.log('UDP6 tracker: udp://' + udp6Host + ':' + udp6Port) + const udp6Addr = server.udp6.address() + const udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost' + const udp6Port = udp6Addr.port + console.log(`${new Date().toISOString()} UDP6 tracker: udp://${udp6Host}:${udp6Port}`) } if (server.ws && !argv.quiet) { - var wsAddr = server.http.address() - var wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost' - var wsPort = wsAddr.port - console.log('WebSocket tracker: ws://' + wsHost + ':' + wsPort) + const wsAddr = server.http.address() + const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost' + const wsPort = wsAddr.port + console.log(`${new Date().toISOString()} WebSocket tracker: ws://${wsHost}:${wsPort}`) } if (server.http && argv.stats && !argv.quiet) { - var statsAddr = server.http.address() - var statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost' - var statsPort = statsAddr.port - console.log('Tracker stats: http://' + statsHost + ':' + statsPort + '/stats') + const statsAddr = server.http.address() + const statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost' + const statsPort = statsAddr.port + console.log(`${new Date().toISOString()} Tracker stats: http://${statsHost}:${statsPort}/stats`) } }) diff --git a/client.js b/client.js index 5f35ce299..c37345f17 100644 --- a/client.js +++ b/client.js @@ -1,22 +1,17 @@ -module.exports = Client - -var Buffer = require('safe-buffer').Buffer -var debug = require('debug')('bittorrent-tracker') -var EventEmitter = require('events').EventEmitter -var extend = require('xtend') -var inherits = require('inherits') -var once = require('once') -var parallel = require('run-parallel') -var Peer = require('simple-peer') -var uniq = require('uniq') -var url = require('url') - -var common = require('./lib/common') -var HTTPTracker = require('./lib/client/http-tracker') // empty object in browser -var UDPTracker = require('./lib/client/udp-tracker') // empty object in browser -var WebSocketTracker = require('./lib/client/websocket-tracker') - -inherits(Client, EventEmitter) +import Debug from 'debug' +import EventEmitter from 'events' +import once from 'once' +import parallel from 'run-parallel' +import Peer from '@thaunknown/simple-peer/lite.js' +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 WebSocketTracker from './lib/client/websocket-tracker.js' + +const debug = Debug('bittorrent-tracker:client') /** * BitTorrent tracker client. @@ -24,94 +19,228 @@ inherits(Client, EventEmitter) * Find torrent peers, to help a torrent client participate in a torrent swarm. * * @param {Object} opts options object - * @param {string|Buffer} opts.infoHash torrent info hash - * @param {string|Buffer} opts.peerId peer id + * @param {string|Uint8Array} opts.infoHash torrent info hash + * @param {string|Uint8Array} opts.peerId peer id * @param {string|Array.} opts.announce announce * @param {number} opts.port torrent client listening port * @param {function} opts.getAnnounceOpts callback to provide data to tracker * @param {number} opts.rtcConfig RTCPeerConnection configuration object + * @param {number} opts.userAgent User-Agent header for http requests * @param {number} opts.wrtc custom webrtc impl (useful in node.js) + * @param {object} opts.proxyOpts proxy options (useful in node.js) */ -function Client (opts) { - var self = this - if (!(self instanceof Client)) return new Client(opts) - EventEmitter.call(self) - if (!opts) opts = {} +class Client extends EventEmitter { + constructor (opts = {}) { + super() + + if (!opts.peerId) throw new Error('Option `peerId` is required') + if (!opts.infoHash) throw new Error('Option `infoHash` is required') + if (!opts.announce) throw new Error('Option `announce` is required') + if (!process.browser && !opts.port) throw new Error('Option `port` is required') + + this.peerId = typeof opts.peerId === 'string' + ? opts.peerId + : arr2hex(opts.peerId) + this._peerIdBuffer = hex2arr(this.peerId) + this._peerIdBinary = hex2bin(this.peerId) + + this.infoHash = typeof opts.infoHash === 'string' + ? opts.infoHash.toLowerCase() + : arr2hex(opts.infoHash) + this._infoHashBuffer = hex2arr(this.infoHash) + this._infoHashBinary = hex2bin(this.infoHash) + + debug('new client %s', this.infoHash) + + this.destroyed = false + + this._port = opts.port + this._getAnnounceOpts = opts.getAnnounceOpts + this._rtcConfig = opts.rtcConfig + this._userAgent = opts.userAgent + this._proxyOpts = opts.proxyOpts + + // Support lazy 'wrtc' module initialization + // See: https://github.com/webtorrent/webtorrent-hybrid/issues/46 + this._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc + + let announce = typeof opts.announce === 'string' + ? [opts.announce] + : opts.announce == null ? [] : opts.announce + + // Remove trailing slash from trackers to catch duplicates + announce = announce.map(announceUrl => { + if (ArrayBuffer.isView(announceUrl)) announceUrl = arr2text(announceUrl) + if (announceUrl[announceUrl.length - 1] === '/') { + announceUrl = announceUrl.substring(0, announceUrl.length - 1) + } + return announceUrl + }) + // remove duplicates by converting to Set and back + announce = Array.from(new Set(announce)) - if (!opts.peerId) throw new Error('Option `peerId` is required') - if (!opts.infoHash) throw new Error('Option `infoHash` is required') - if (!opts.announce) throw new Error('Option `announce` is required') - if (!process.browser && !opts.port) throw new Error('Option `port` is required') + const webrtcSupport = this._wrtc !== false && (!!this._wrtc || Peer.WEBRTC_SUPPORT) - // required - self.peerId = typeof opts.peerId === 'string' - ? opts.peerId - : opts.peerId.toString('hex') - self._peerIdBuffer = Buffer.from(self.peerId, 'hex') - self._peerIdBinary = self._peerIdBuffer.toString('binary') + const nextTickWarn = err => { + queueMicrotask(() => { + this.emit('warning', err) + }) + } - self.infoHash = typeof opts.infoHash === 'string' - ? opts.infoHash - : opts.infoHash.toString('hex') - self._infoHashBuffer = Buffer.from(self.infoHash, 'hex') - self._infoHashBinary = self._infoHashBuffer.toString('binary') + this._trackers = announce + .map(announceUrl => { + let parsedUrl + try { + parsedUrl = common.parseUrl(announceUrl) + } catch (err) { + nextTickWarn(new Error(`Invalid tracker URL: ${announceUrl}`)) + return null + } - self._port = opts.port + const port = parsedUrl.port + if (port < 0 || port > 65535) { + nextTickWarn(new Error(`Invalid tracker port: ${announceUrl}`)) + return null + } - self.destroyed = false + const protocol = parsedUrl.protocol + if ((protocol === 'http:' || protocol === 'https:') && + typeof HTTPTracker === 'function') { + return new HTTPTracker(this, announceUrl) + } else if (protocol === 'udp:' && typeof UDPTracker === 'function') { + return new UDPTracker(this, announceUrl) + } else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) { + // Skip ws:// trackers on https:// sites because they throw SecurityError + if (protocol === 'ws:' && typeof window !== 'undefined' && + window.location.protocol === 'https:') { + nextTickWarn(new Error(`Unsupported tracker protocol: ${announceUrl}`)) + return null + } + return new WebSocketTracker(this, announceUrl) + } else { + nextTickWarn(new Error(`Unsupported tracker protocol: ${announceUrl}`)) + return null + } + }) + .filter(Boolean) + } - self._rtcConfig = opts.rtcConfig - self._wrtc = opts.wrtc - self._getAnnounceOpts = opts.getAnnounceOpts + /** + * Send a `start` announce to the trackers. + * @param {Object} opts + * @param {number=} opts.uploaded + * @param {number=} opts.downloaded + * @param {number=} opts.left (if not set, calculated automatically) + */ + start (opts) { + opts = this._defaultAnnounceOpts(opts) + opts.event = 'started' + debug('send `start` %o', opts) + this._announce(opts) + + // start announcing on intervals + this._trackers.forEach(tracker => { + tracker.setInterval() + }) + } - debug('new client %s', self.infoHash) + /** + * Send a `stop` announce to the trackers. + * @param {Object} opts + * @param {number=} opts.uploaded + * @param {number=} opts.downloaded + * @param {number=} opts.numwant + * @param {number=} opts.left (if not set, calculated automatically) + */ + stop (opts) { + opts = this._defaultAnnounceOpts(opts) + opts.event = 'stopped' + debug('send `stop` %o', opts) + this._announce(opts) + } - var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT) + /** + * Send a `complete` announce to the trackers. + * @param {Object} opts + * @param {number=} opts.uploaded + * @param {number=} opts.downloaded + * @param {number=} opts.numwant + * @param {number=} opts.left (if not set, calculated automatically) + */ + complete (opts) { + if (!opts) opts = {} + opts = this._defaultAnnounceOpts(opts) + opts.event = 'completed' + debug('send `complete` %o', opts) + this._announce(opts) + } - var announce = (typeof opts.announce === 'string') - ? [ opts.announce ] - : opts.announce == null - ? [] - : opts.announce + /** + * Send a `update` announce to the trackers. + * @param {Object} opts + * @param {number=} opts.uploaded + * @param {number=} opts.downloaded + * @param {number=} opts.numwant + * @param {number=} opts.left (if not set, calculated automatically) + */ + update (opts) { + opts = this._defaultAnnounceOpts(opts) + if (opts.event) delete opts.event + debug('send `update` %o', opts) + this._announce(opts) + } - announce = announce.map(function (announceUrl) { - announceUrl = announceUrl.toString() - if (announceUrl[announceUrl.length - 1] === '/') { - // remove trailing slash from trackers to catch duplicates - announceUrl = announceUrl.substring(0, announceUrl.length - 1) - } - return announceUrl - }) + _announce (opts) { + this._trackers.forEach(tracker => { + // tracker should not modify `opts` object, it's passed to all trackers + tracker.announce(opts) + }) + } - announce = uniq(announce) - - self._trackers = announce - .map(function (announceUrl) { - var protocol = url.parse(announceUrl).protocol - if ((protocol === 'http:' || protocol === 'https:') && - typeof HTTPTracker === 'function') { - return new HTTPTracker(self, announceUrl) - } else if (protocol === 'udp:' && typeof UDPTracker === 'function') { - return new UDPTracker(self, announceUrl) - } else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) { - // Skip ws:// trackers on https:// sites because they throw SecurityError - if (protocol === 'ws:' && typeof window !== 'undefined' && - window.location.protocol === 'https:') { - nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl)) - return null - } - return new WebSocketTracker(self, announceUrl) - } else { - nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl)) - return null - } + /** + * Send a scrape request to the trackers. + * @param {Object} opts + */ + scrape (opts) { + debug('send `scrape`') + if (!opts) opts = {} + this._trackers.forEach(tracker => { + // tracker should not modify `opts` object, it's passed to all trackers + tracker.scrape(opts) }) - .filter(Boolean) + } + + setInterval (intervalMs) { + debug('setInterval %d', intervalMs) + this._trackers.forEach(tracker => { + tracker.setInterval(intervalMs) + }) + } - function nextTickWarn (err) { - process.nextTick(function () { - self.emit('warning', err) + destroy (cb) { + if (this.destroyed) return + this.destroyed = true + debug('destroy') + + const tasks = this._trackers.map(tracker => cb => { + tracker.destroy(cb) }) + + parallel(tasks, cb) + + this._trackers = [] + this._getAnnounceOpts = null + } + + _defaultAnnounceOpts (opts = {}) { + if (opts.numwant == null) opts.numwant = common.DEFAULT_ANNOUNCE_PEERS + + if (opts.uploaded == null) opts.uploaded = 0 + if (opts.downloaded == null) opts.downloaded = 0 + + if (this._getAnnounceOpts) opts = Object.assign({}, opts, this._getAnnounceOpts()) + + return opts } } @@ -124,30 +253,30 @@ function Client (opts) { * @param {string} opts.announce * @param {function} cb */ -Client.scrape = function (opts, cb) { +Client.scrape = (opts, cb) => { cb = once(cb) if (!opts.infoHash) throw new Error('Option `infoHash` is required') if (!opts.announce) throw new Error('Option `announce` is required') - var clientOpts = extend(opts, { + const clientOpts = Object.assign({}, opts, { infoHash: Array.isArray(opts.infoHash) ? opts.infoHash[0] : opts.infoHash, - peerId: Buffer.from('01234567890123456789'), // dummy value + peerId: text2arr('01234567890123456789'), // dummy value port: 6881 // dummy value }) - var client = new Client(clientOpts) + const client = new Client(clientOpts) client.once('error', cb) client.once('warning', cb) - var len = Array.isArray(opts.infoHash) ? opts.infoHash.length : 1 - var results = {} - client.on('scrape', function (data) { + let len = Array.isArray(opts.infoHash) ? opts.infoHash.length : 1 + const results = {} + client.on('scrape', data => { len -= 1 results[data.infoHash] = data if (len === 0) { client.destroy() - var keys = Object.keys(results) + const keys = Object.keys(results) if (keys.length === 1) { cb(null, results[keys[0]]) } else { @@ -156,141 +285,8 @@ Client.scrape = function (opts, cb) { } }) - opts.infoHash = Array.isArray(opts.infoHash) - ? opts.infoHash.map(function (infoHash) { - return Buffer.from(infoHash, 'hex') - }) - : Buffer.from(opts.infoHash, 'hex') client.scrape({ infoHash: opts.infoHash }) return client } -/** - * Send a `start` announce to the trackers. - * @param {Object} opts - * @param {number=} opts.uploaded - * @param {number=} opts.downloaded - * @param {number=} opts.left (if not set, calculated automatically) - */ -Client.prototype.start = function (opts) { - var self = this - debug('send `start`') - opts = self._defaultAnnounceOpts(opts) - opts.event = 'started' - self._announce(opts) - - // start announcing on intervals - self._trackers.forEach(function (tracker) { - tracker.setInterval() - }) -} - -/** - * Send a `stop` announce to the trackers. - * @param {Object} opts - * @param {number=} opts.uploaded - * @param {number=} opts.downloaded - * @param {number=} opts.numwant - * @param {number=} opts.left (if not set, calculated automatically) - */ -Client.prototype.stop = function (opts) { - var self = this - debug('send `stop`') - opts = self._defaultAnnounceOpts(opts) - opts.event = 'stopped' - self._announce(opts) -} - -/** - * Send a `complete` announce to the trackers. - * @param {Object} opts - * @param {number=} opts.uploaded - * @param {number=} opts.downloaded - * @param {number=} opts.numwant - * @param {number=} opts.left (if not set, calculated automatically) - */ -Client.prototype.complete = function (opts) { - var self = this - debug('send `complete`') - if (!opts) opts = {} - opts = self._defaultAnnounceOpts(opts) - opts.event = 'completed' - self._announce(opts) -} - -/** - * Send a `update` announce to the trackers. - * @param {Object} opts - * @param {number=} opts.uploaded - * @param {number=} opts.downloaded - * @param {number=} opts.numwant - * @param {number=} opts.left (if not set, calculated automatically) - */ -Client.prototype.update = function (opts) { - var self = this - debug('send `update`') - opts = self._defaultAnnounceOpts(opts) - if (opts.event) delete opts.event - self._announce(opts) -} - -Client.prototype._announce = function (opts) { - var self = this - self._trackers.forEach(function (tracker) { - // tracker should not modify `opts` object, it's passed to all trackers - tracker.announce(opts) - }) -} - -/** - * Send a scrape request to the trackers. - * @param {Object} opts - */ -Client.prototype.scrape = function (opts) { - var self = this - debug('send `scrape`') - if (!opts) opts = {} - self._trackers.forEach(function (tracker) { - // tracker should not modify `opts` object, it's passed to all trackers - tracker.scrape(opts) - }) -} - -Client.prototype.setInterval = function (intervalMs) { - var self = this - debug('setInterval %d', intervalMs) - self._trackers.forEach(function (tracker) { - tracker.setInterval(intervalMs) - }) -} - -Client.prototype.destroy = function (cb) { - var self = this - if (self.destroyed) return - self.destroyed = true - debug('destroy') - - var tasks = self._trackers.map(function (tracker) { - return function (cb) { - tracker.destroy(cb) - } - }) - - parallel(tasks, cb) - - self._trackers = [] - self._getAnnounceOpts = null -} - -Client.prototype._defaultAnnounceOpts = function (opts) { - var self = this - if (!opts) opts = {} - - if (opts.numwant == null) opts.numwant = common.DEFAULT_ANNOUNCE_PEERS - - if (opts.uploaded == null) opts.uploaded = 0 - if (opts.downloaded == null) opts.downloaded = 0 - - if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts()) - return opts -} +export default Client diff --git a/examples/express-embed/server.js b/examples/express-embed/server.js index 0a4a929cd..947dcbfbe 100755 --- a/examples/express-embed/server.js +++ b/examples/express-embed/server.js @@ -1,27 +1,27 @@ #!/usr/bin/env node -var Server = require('../..').Server -var express = require('express') -var app = express() +import { Server } from '../../index.js' +import express from 'express' +const app = express() // https://wiki.theory.org/BitTorrentSpecification#peer_id -var whitelist = { +const whitelist = { UT: true // uTorrent } -var server = new Server({ +const server = new Server({ http: false, // we do our own - udp: false, // not interested - ws: false, // not interested - filter: function (params) { + udp: false, // not interested + ws: false, // not interested + filter (params) { // black/whitelist for disallowing/allowing specific clients [default=allow all] // this example only allows the uTorrent client - var client = params.peer_id[1] + params.peer_id[2] + const client = params.peer_id[1] + params.peer_id[2] return whitelist[client] } }) -var onHttpRequest = server.onHttpRequest.bind(server) +const onHttpRequest = server.onHttpRequest.bind(server) app.get('/announce', onHttpRequest) app.get('/scrape', onHttpRequest) diff --git a/img.png b/img/img.png similarity index 100% rename from img.png rename to img/img.png diff --git a/img/trackerStats.png b/img/trackerStats.png new file mode 100644 index 000000000..54ef54bb7 Binary files /dev/null and b/img/trackerStats.png differ diff --git a/index.js b/index.js index 1f79a6e63..e812c41c8 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ -var Client = require('./client') -var Server = require('./server') +/*! bittorrent-tracker. MIT License. WebTorrent LLC */ +import Client from './client.js' +import Server from './server.js' -module.exports = Client -module.exports.Client = Client -module.exports.Server = Server +export default Client +export { Client, Server } diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 8b767e2ef..43464e8eb 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -1,18 +1,23 @@ -module.exports = HTTPTracker +import arrayRemove from 'unordered-array-remove' +import bencode from 'bencode' +import Debug from 'debug' +import fetch from 'cross-fetch-ponyfill' +import { bin2hex, hex2bin, arr2text, text2arr, arr2hex } from 'uint8-util' -var bencode = require('bencode') -var compact2string = require('compact2string') -var debug = require('debug')('bittorrent-tracker:http-tracker') -var extend = require('xtend') -var get = require('simple-get') -var inherits = require('inherits') +import common from '../common.js' +import Tracker from './tracker.js' +import compact2string from 'compact2string' -var common = require('../common') -var Tracker = require('./tracker') +const debug = Debug('bittorrent-tracker:http-tracker') +const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/ -var HTTP_SCRAPE_SUPPORT = /\/(announce)[^\/]*$/ - -inherits(HTTPTracker, Tracker) +function abortTimeout (ms) { + const controller = new AbortController() + setTimeout(() => { + controller.abort() + }, ms).unref?.() + return controller +} /** * HTTP torrent tracker client (for an individual tracker) @@ -21,189 +26,249 @@ inherits(HTTPTracker, Tracker) * @param {string} announceUrl announce url of tracker * @param {Object} opts options object */ -function HTTPTracker (client, announceUrl, opts) { - var self = this - Tracker.call(self, client, announceUrl) - debug('new http tracker %s', announceUrl) - - // Determine scrape url (if http tracker supports it) - self.scrapeUrl = null - var m - if ((m = self.announceUrl.match(HTTP_SCRAPE_SUPPORT))) { - self.scrapeUrl = self.announceUrl.slice(0, m.index) + '/scrape' + - self.announceUrl.slice(m.index + 9) +class HTTPTracker extends Tracker { + constructor (client, announceUrl) { + super(client, announceUrl) + + debug('new http tracker %s', announceUrl) + + // Determine scrape url (if http tracker supports it) + this.scrapeUrl = null + + const match = this.announceUrl.match(HTTP_SCRAPE_SUPPORT) + if (match) { + const pre = this.announceUrl.slice(0, match.index) + const post = this.announceUrl.slice(match.index + 9) + this.scrapeUrl = `${pre}/scrape${post}` + } + + this.cleanupFns = [] + this.maybeDestroyCleanup = null } -} -HTTPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes + announce (opts) { + if (this.destroyed) return -HTTPTracker.prototype.announce = function (opts) { - var self = this - if (self.destroyed) return - - var params = extend(opts, { - compact: (opts.compact == null) ? 1 : opts.compact, - info_hash: self.client._infoHashBinary, - peer_id: self.client._peerIdBinary, - port: self.client._port - }) - if (self._trackerId) params.trackerid = self._trackerId - - self._request(self.announceUrl, params, function (data) { - self._onAnnounceResponse(data) - }) -} + const params = Object.assign({}, opts, { + compact: (opts.compact == null) ? 1 : opts.compact, + info_hash: this.client._infoHashBinary, + peer_id: this.client._peerIdBinary, + port: this.client._port + }) -HTTPTracker.prototype.scrape = function (opts) { - var self = this - if (self.destroyed) return + if (params.left !== 0 && !params.left) params.left = 16384 + if (this._trackerId) params.trackerid = this._trackerId - if (!self.scrapeUrl) { - self.client.emit('error', new Error('scrape not supported ' + self.announceUrl)) - return + this._request(this.announceUrl, params, (err, data) => { + if (err) return this.client.emit('warning', err) + this._onAnnounceResponse(data) + }) } - var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) - ? opts.infoHash.map(function (infoHash) { - return infoHash.toString('binary') + scrape (opts) { + if (this.destroyed) return + + if (!this.scrapeUrl) { + this.client.emit('error', new Error(`scrape not supported ${this.announceUrl}`)) + return + } + + const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) + ? opts.infoHash.map(infoHash => hex2bin(infoHash)) + : (opts.infoHash && hex2bin(opts.infoHash)) || this.client._infoHashBinary + const params = { + info_hash: infoHashes + } + this._request(this.scrapeUrl, params, (err, data) => { + if (err) return this.client.emit('warning', err) + this._onScrapeResponse(data) }) - : (opts.infoHash && opts.infoHash.toString('binary')) || self.client._infoHashBinary - var params = { - info_hash: infoHashes } - self._request(self.scrapeUrl, params, function (data) { - self._onScrapeResponse(data) - }) -} -HTTPTracker.prototype.destroy = function (cb) { - var self = this - if (self.destroyed) return cb(null) - self.destroyed = true - clearInterval(self.interval) + destroy (cb) { + const self = this + if (this.destroyed) return cb(null) + this.destroyed = true + clearInterval(this.interval) - cb(null) -} + let timeout + + // If there are no pending requests, destroy immediately. + if (this.cleanupFns.length === 0) return destroyCleanup() + + // Otherwise, wait a short time for pending requests to complete, then force + // destroy them. + timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT) + + // But, if all pending requests complete before the timeout fires, do cleanup + // right away. + this.maybeDestroyCleanup = () => { + if (this.cleanupFns.length === 0) destroyCleanup() + } + + function destroyCleanup () { + if (timeout) { + clearTimeout(timeout) + timeout = null + } + self.maybeDestroyCleanup = null + self.cleanupFns.slice(0).forEach(cleanup => { + cleanup() + }) + self.cleanupFns = [] + cb(null) + } + } + + async _request (requestUrl, params, cb) { + const parsedUrl = new URL(requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + common.querystringStringify(params)) + let agent + if (this.client._proxyOpts) { + agent = parsedUrl.protocol === 'https:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent + if (!agent && this.client._proxyOpts.socksProxy) { + agent = this.client._proxyOpts.socksProxy + } + } -HTTPTracker.prototype._request = function (requestUrl, params, cb) { - var self = this - var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + - common.querystringStringify(params) + const cleanup = () => { + if (!controller.signal.aborted) { + arrayRemove(this.cleanupFns, this.cleanupFns.indexOf(cleanup)) + controller.abort() + controller = null + } + if (this.maybeDestroyCleanup) this.maybeDestroyCleanup() + } + + this.cleanupFns.push(cleanup) - get.concat(u, function (err, res, data) { - if (self.destroyed) return - if (err) return self.client.emit('warning', err) - if (res.statusCode !== 200) { - return self.client.emit('warning', new Error('Non-200 response code ' + - res.statusCode + ' from ' + self.announceUrl)) + let res + let controller = abortTimeout(common.REQUEST_TIMEOUT) + try { + res = await fetch(parsedUrl.toString(), { + agent, + signal: controller.signal, + dispatcher: agent, + headers: { + 'user-agent': this.client._userAgent || '' + } + }) + if (res.body.on) res.body.on('error', cb) + } catch (err) { + if (err) return cb(err) + } + let data = new Uint8Array(await res.arrayBuffer()) + cleanup() + if (this.destroyed) return + + if (res.status !== 200) { + return cb(new Error(`Non-200 response code ${res.status} from ${this.announceUrl}`)) } if (!data || data.length === 0) { - return self.client.emit('warning', new Error('Invalid tracker response from' + - self.announceUrl)) + return cb(new Error(`Invalid tracker response from${this.announceUrl}`)) } try { data = bencode.decode(data) } catch (err) { - return self.client.emit('warning', new Error('Error decoding tracker response: ' + err.message)) + return cb(new Error(`Error decoding tracker response: ${err.message}`)) } - var failure = data['failure reason'] + const failure = data['failure reason'] && arr2text(data['failure reason']) if (failure) { - debug('failure from ' + requestUrl + ' (' + failure + ')') - return self.client.emit('warning', new Error(failure)) + debug(`failure from ${requestUrl} (${failure})`) + return cb(new Error(failure)) } - var warning = data['warning message'] + const warning = data['warning message'] && arr2text(data['warning message']) if (warning) { - debug('warning from ' + requestUrl + ' (' + warning + ')') - self.client.emit('warning', new Error(warning)) + debug(`warning from ${requestUrl} (${warning})`) + this.client.emit('warning', new Error(warning)) } - debug('response from ' + requestUrl) + debug(`response from ${requestUrl}`) - cb(data) - }) -} + cb(null, data) + } -HTTPTracker.prototype._onAnnounceResponse = function (data) { - var self = this + _onAnnounceResponse (data) { + const interval = data.interval || data['min interval'] + if (interval) this.setInterval(interval * 1000) - var interval = data.interval || data['min interval'] - if (interval) self.setInterval(interval * 1000) + const trackerId = data['tracker id'] + if (trackerId) { + // If absent, do not discard previous trackerId value + this._trackerId = trackerId + } - var trackerId = data['tracker id'] - if (trackerId) { - // If absent, do not discard previous trackerId value - self._trackerId = trackerId - } + const response = Object.assign({}, data, { + announce: this.announceUrl, + infoHash: bin2hex(data.info_hash || String(data.info_hash)) + }) + this.client.emit('update', response) - self.client.emit('update', { - announce: self.announceUrl, - complete: data.complete, - incomplete: data.incomplete - }) + let addrs + if (ArrayBuffer.isView(data.peers)) { + // tracker returned compact response + try { + addrs = compact2string.multi(Buffer.from(data.peers)) + } catch (err) { + return this.client.emit('warning', err) + } + addrs.forEach(addr => { + this.client.emit('peer', addr) + }) + } else if (Array.isArray(data.peers)) { + // tracker returned normal response + data.peers.forEach(peer => { + this.client.emit('peer', `${peer.ip}:${peer.port}`) + }) + } - var addrs - if (Buffer.isBuffer(data.peers)) { - // tracker returned compact response - try { - addrs = compact2string.multi(data.peers) - } catch (err) { - return self.client.emit('warning', err) + if (ArrayBuffer.isView(data.peers6)) { + // tracker returned compact response + try { + addrs = compact2string.multi6(Buffer.from(data.peers6)) + } catch (err) { + return this.client.emit('warning', err) + } + addrs.forEach(addr => { + this.client.emit('peer', addr) + }) + } else if (Array.isArray(data.peers6)) { + // tracker returned normal response + data.peers6.forEach(peer => { + const ip = /^\[/.test(peer.ip) || !/:/.test(peer.ip) + ? peer.ip /* ipv6 w/ brackets or domain name */ + : `[${peer.ip}]` /* ipv6 without brackets */ + this.client.emit('peer', `${ip}:${peer.port}`) + }) } - addrs.forEach(function (addr) { - self.client.emit('peer', addr) - }) - } else if (Array.isArray(data.peers)) { - // tracker returned normal response - data.peers.forEach(function (peer) { - self.client.emit('peer', peer.ip + ':' + peer.port) - }) } - if (Buffer.isBuffer(data.peers6)) { - // tracker returned compact response - try { - addrs = compact2string.multi6(data.peers6) - } catch (err) { - return self.client.emit('warning', err) + _onScrapeResponse (data) { + // NOTE: the unofficial spec says to use the 'files' key, 'host' has been + // seen in practice + data = data.files || data.host || {} + + const keys = Object.keys(data) + if (keys.length === 0) { + this.client.emit('warning', new Error('invalid scrape response')) + return } - addrs.forEach(function (addr) { - self.client.emit('peer', addr) - }) - } else if (Array.isArray(data.peers6)) { - // tracker returned normal response - data.peers6.forEach(function (peer) { - var ip = /^\[/.test(peer.ip) || !/:/.test(peer.ip) - ? peer.ip /* ipv6 w/ brackets or domain name */ - : '[' + peer.ip + ']' /* ipv6 without brackets */ - self.client.emit('peer', ip + ':' + peer.port) + + keys.forEach(_infoHash => { + // TODO: optionally handle data.flags.min_request_interval + // (separate from announce interval) + const infoHash = _infoHash.length !== 20 ? arr2hex(text2arr(_infoHash)) : bin2hex(_infoHash) + + const response = Object.assign(data[_infoHash], { + announce: this.announceUrl, + infoHash + }) + this.client.emit('scrape', response) }) } } -HTTPTracker.prototype._onScrapeResponse = function (data) { - var self = this - // NOTE: the unofficial spec says to use the 'files' key, 'host' has been - // seen in practice - data = data.files || data.host || {} - - var keys = Object.keys(data) - if (keys.length === 0) { - self.client.emit('warning', new Error('invalid scrape response')) - return - } +HTTPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes - keys.forEach(function (infoHash) { - var response = data[infoHash] - // TODO: optionally handle data.flags.min_request_interval - // (separate from announce interval) - self.client.emit('scrape', { - announce: self.announceUrl, - infoHash: common.binaryToHex(infoHash), - complete: response.complete, - incomplete: response.incomplete, - downloaded: response.downloaded - }) - }) -} +export default HTTPTracker diff --git a/lib/client/tracker.js b/lib/client/tracker.js index 73de2cd26..1129ecd84 100644 --- a/lib/client/tracker.js +++ b/lib/client/tracker.js @@ -1,30 +1,28 @@ -module.exports = Tracker +import EventEmitter from 'events' -var EventEmitter = require('events').EventEmitter -var inherits = require('inherits') +class Tracker extends EventEmitter { + constructor (client, announceUrl) { + super() -inherits(Tracker, EventEmitter) + this.client = client + this.announceUrl = announceUrl -function Tracker (client, announceUrl) { - var self = this - EventEmitter.call(self) - self.client = client - self.announceUrl = announceUrl - - self.interval = null - self.destroyed = false -} + this.interval = null + this.destroyed = false + } -Tracker.prototype.setInterval = function (intervalMs) { - var self = this - if (intervalMs == null) intervalMs = self.DEFAULT_ANNOUNCE_INTERVAL + setInterval (intervalMs) { + if (intervalMs == null) intervalMs = this.DEFAULT_ANNOUNCE_INTERVAL - clearInterval(self.interval) + clearInterval(this.interval) - if (intervalMs) { - self.interval = setInterval(function () { - self.announce(self.client._defaultAnnounceOpts()) - }, intervalMs) - if (self.interval.unref) self.interval.unref() + if (intervalMs) { + this.interval = setInterval(() => { + this.announce(this.client._defaultAnnounceOpts()) + }, intervalMs) + if (this.interval.unref) this.interval.unref() + } } } + +export default Tracker diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index b9ccbb229..b4ac4bdba 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -1,20 +1,17 @@ -module.exports = UDPTracker +import arrayRemove from 'unordered-array-remove' +import Debug from 'debug' +import dgram from 'dgram' +import Socks from 'socks' +import { concat, hex2arr, randomBytes } from 'uint8-util' -var BN = require('bn.js') -var Buffer = require('safe-buffer').Buffer -var compact2string = require('compact2string') -var debug = require('debug')('bittorrent-tracker:udp-tracker') -var dgram = require('dgram') -var inherits = require('inherits') -var randombytes = require('randombytes') -var url = require('url') +import common from '../common.js' +import Tracker from './tracker.js' +import compact2string from 'compact2string' -var common = require('../common') -var Tracker = require('./tracker') +const debug = Debug('bittorrent-tracker:udp-tracker') -var TIMEOUT = 15000 - -inherits(UDPTracker, Tracker) +// this was done some many years ago to fix "prevent Socks instances concurrency", and used some bloated package, no clue if it's needed, but this is simpler, #356 +const clone = obj => JSON.parse(JSON.stringify(obj)) /** * UDP torrent tracker client (for an individual tracker) @@ -23,227 +20,317 @@ inherits(UDPTracker, Tracker) * @param {string} announceUrl announce url of tracker * @param {Object} opts options object */ -function UDPTracker (client, announceUrl, opts) { - var self = this - Tracker.call(self, client, announceUrl) - debug('new udp tracker %s', announceUrl) +class UDPTracker extends Tracker { + constructor (client, announceUrl) { + super(client, announceUrl) + debug('new udp tracker %s', announceUrl) - self.cleanupFns = [] -} + this.cleanupFns = [] + this.maybeDestroyCleanup = null + } -UDPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes + announce (opts) { + if (this.destroyed) return + this._request(opts) + } -UDPTracker.prototype.announce = function (opts) { - var self = this - if (self.destroyed) return - self._request(opts) -} + scrape (opts) { + if (this.destroyed) return + opts._scrape = true + this._request(opts) // udp scrape uses same announce url + } -UDPTracker.prototype.scrape = function (opts) { - var self = this - if (self.destroyed) return - opts._scrape = true - self._request(opts) // udp scrape uses same announce url -} + destroy (cb) { + const self = this + if (this.destroyed) return cb(null) + this.destroyed = true + clearInterval(this.interval) -UDPTracker.prototype.destroy = function (cb) { - var self = this - if (self.destroyed) return cb(null) - self.destroyed = true - clearInterval(self.interval) - - self.cleanupFns.slice(0).forEach(function (cleanup) { - cleanup() - }) - self.cleanupFns = [] - cb(null) -} + let timeout + + // If there are no pending requests, destroy immediately. + if (this.cleanupFns.length === 0) return destroyCleanup() -UDPTracker.prototype._request = function (opts) { - var self = this - if (!opts) opts = {} - var parsedUrl = url.parse(self.announceUrl) - var transactionId = genTransactionId() - var socket = dgram.createSocket('udp4') - - var cleanup = function () { - if (!socket) return - self.cleanupFns.splice(self.cleanupFns.indexOf(cleanup), 1) - if (timeout) { - clearTimeout(timeout) - timeout = null + // Otherwise, wait a short time for pending requests to complete, then force + // destroy them. + timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT) + + // But, if all pending requests complete before the timeout fires, do cleanup + // right away. + this.maybeDestroyCleanup = () => { + if (this.cleanupFns.length === 0) destroyCleanup() + } + + function destroyCleanup () { + if (timeout) { + clearTimeout(timeout) + timeout = null + } + self.maybeDestroyCleanup = null + self.cleanupFns.slice(0).forEach(cleanup => { + cleanup() + }) + self.cleanupFns = [] + cb(null) } - socket.removeListener('error', onError) - socket.removeListener('message', onSocketMessage) - socket.on('error', noop) // ignore all future errors - try { socket.close() } catch (err) {} - socket = null } - self.cleanupFns.push(cleanup) - - // does not matter if `stopped` event arrives, so supress errors & cleanup after timeout - var ms = opts.event === 'stopped' ? TIMEOUT / 10 : TIMEOUT - var timeout = setTimeout(function () { - timeout = null - if (opts.event === 'stopped') cleanup() - else onError(new Error('tracker request timed out (' + opts.event + ')')) - }, ms) - if (timeout.unref) timeout.unref() - - send(Buffer.concat([ - common.CONNECTION_ID, - common.toUInt32(common.ACTIONS.CONNECT), - transactionId - ])) - - socket.on('error', onError) - socket.on('message', onSocketMessage) - - function onSocketMessage (msg) { - if (self.destroyed) return - if (msg.length < 8 || msg.readUInt32BE(4) !== transactionId.readUInt32BE(0)) { - return onError(new Error('tracker sent invalid transaction id')) + + _request (opts) { + const self = this + if (!opts) opts = {} + + let { hostname, port } = common.parseUrl(this.announceUrl) + if (port === '') port = 80 + + let timeout + // Socket used to connect to the socks server to create a relay, null if socks is disabled + let proxySocket + // Socket used to connect to the tracker or to the socks relay if socks is enabled + let socket + // Contains the host/port of the socks relay + let relay + + let transactionId = genTransactionId() + + const proxyOpts = this.client._proxyOpts && clone(this.client._proxyOpts.socksProxy) + if (proxyOpts) { + if (!proxyOpts.proxy) proxyOpts.proxy = {} + // UDP requests uses the associate command + proxyOpts.proxy.command = 'associate' + if (!proxyOpts.target) { + // This should contain client IP and port but can be set to 0 if we don't have this information + proxyOpts.target = { + host: '0.0.0.0', + port: 0 + } + } + + if (proxyOpts.proxy.type === 5) { + Socks.createConnection(proxyOpts, onGotConnection) + } else { + debug('Ignoring Socks proxy for UDP request because type 5 is required') + onGotConnection(null) + } + } else { + onGotConnection(null) } - var action = msg.readUInt32BE(0) - debug('UDP response %s, action %s', self.announceUrl, action) - switch (action) { - case 0: // handshake - if (msg.length < 16) return onError(new Error('invalid udp handshake')) + this.cleanupFns.push(cleanup) - if (opts._scrape) scrape(msg.slice(8, 16)) - else announce(msg.slice(8, 16), opts) + function onGotConnection (err, s, info) { + if (err) return onError(err) - return + proxySocket = s + socket = dgram.createSocket('udp4') + relay = info - case 1: // announce - cleanup() - if (msg.length < 20) return onError(new Error('invalid announce message')) - - var interval = msg.readUInt32BE(8) - if (interval) self.setInterval(interval * 1000) - - self.client.emit('update', { - announce: self.announceUrl, - complete: msg.readUInt32BE(16), - incomplete: msg.readUInt32BE(12) - }) - - var addrs - try { - addrs = compact2string.multi(msg.slice(20)) - } catch (err) { - return self.client.emit('warning', err) + timeout = setTimeout(() => { + // does not matter if `stopped` event arrives, so supress errors + if (opts.event === 'stopped') cleanup() + else onError(new Error(`tracker request timed out (${opts.event})`)) + timeout = null + }, common.REQUEST_TIMEOUT) + if (timeout.unref) timeout.unref() + + send(concat([ + common.CONNECTION_ID, + common.toUInt32(common.ACTIONS.CONNECT), + transactionId + ]), relay) + + socket.once('error', onError) + socket.on('message', onSocketMessage) + } + + function cleanup () { + if (timeout) { + clearTimeout(timeout) + timeout = null + } + if (socket) { + arrayRemove(self.cleanupFns, self.cleanupFns.indexOf(cleanup)) + socket.removeListener('error', onError) + socket.removeListener('message', onSocketMessage) + socket.on('error', noop) // ignore all future errors + try { socket.close() } catch (err) {} + socket = null + if (proxySocket) { + try { proxySocket.close() } catch (err) {} + proxySocket = null } - addrs.forEach(function (addr) { - self.client.emit('peer', addr) - }) - break + } + if (self.maybeDestroyCleanup) self.maybeDestroyCleanup() + } - case 2: // scrape - cleanup() - if (msg.length < 20 || (msg.length - 8) % 12 !== 0) { - return onError(new Error('invalid scrape message')) + function onError (err) { + cleanup() + if (self.destroyed) return + + try { + // Error.message is readonly on some platforms. + if (err.message) err.message += ` (${self.announceUrl})` + } catch (ignoredErr) {} + // errors will often happen if a tracker is offline, so don't treat it as fatal + self.client.emit('warning', err) + } + + function onSocketMessage (msg) { + if (proxySocket) msg = msg.slice(10) + const view = new DataView(transactionId.buffer) + if (msg.length < 8 || msg.readUInt32BE(4) !== view.getUint32(0)) { + return onError(new Error('tracker sent invalid transaction id')) + } + + const action = msg.readUInt32BE(0) + debug('UDP response %s, action %s', self.announceUrl, action) + switch (action) { + case 0: { // handshake + // Note: no check for `self.destroyed` so that pending messages to the + // tracker can still be sent/received even after destroy() is called + + if (msg.length < 16) return onError(new Error('invalid udp handshake')) + + if (opts._scrape) scrape(msg.slice(8, 16)) + else announce(msg.slice(8, 16), opts) + + break } - var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) - ? opts.infoHash.map(function (infoHash) { return infoHash.toString('hex') }) - : [ (opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash ] + case 1: { // announce + cleanup() + if (self.destroyed) return + + if (msg.length < 20) return onError(new Error('invalid announce message')) - for (var i = 0, len = (msg.length - 8) / 12; i < len; i += 1) { - self.client.emit('scrape', { + const interval = msg.readUInt32BE(8) + if (interval) self.setInterval(interval * 1000) + + self.client.emit('update', { announce: self.announceUrl, - infoHash: infoHashes[i], - complete: msg.readUInt32BE(8 + (i * 12)), - downloaded: msg.readUInt32BE(12 + (i * 12)), - incomplete: msg.readUInt32BE(16 + (i * 12)) + complete: msg.readUInt32BE(16), + incomplete: msg.readUInt32BE(12) + }) + + let addrs + try { + addrs = compact2string.multi(msg.slice(20)) + } catch (err) { + return self.client.emit('warning', err) + } + addrs.forEach(addr => { + self.client.emit('peer', addr) }) + + break + } + case 2: { // scrape + cleanup() + if (self.destroyed) return + + if (msg.length < 20 || (msg.length - 8) % 12 !== 0) { + return onError(new Error('invalid scrape message')) + } + const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) + ? opts.infoHash.map(infoHash => infoHash.toString('hex')) + : [(opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash] + + for (let i = 0, len = (msg.length - 8) / 12; i < len; i += 1) { + self.client.emit('scrape', { + announce: self.announceUrl, + infoHash: infoHashes[i], + complete: msg.readUInt32BE(8 + (i * 12)), + downloaded: msg.readUInt32BE(12 + (i * 12)), + incomplete: msg.readUInt32BE(16 + (i * 12)) + }) + } + + break } - break + case 3: { // error + cleanup() + if (self.destroyed) return - case 3: // error - cleanup() - if (msg.length < 8) return onError(new Error('invalid error message')) - self.client.emit('warning', new Error(msg.slice(8).toString())) - break + if (msg.length < 8) return onError(new Error('invalid error message')) + self.client.emit('warning', new Error(msg.slice(8).toString())) - default: - onError(new Error('tracker sent invalid action')) - break + break + } + default: + onError(new Error('tracker sent invalid action')) + break + } } - } - - function onError (err) { - if (self.destroyed) return - cleanup() - if (err.message) err.message += ' (' + self.announceUrl + ')' - // errors will often happen if a tracker is offline, so don't treat it as fatal - self.client.emit('warning', err) - } - function send (message) { - if (!parsedUrl.port) { - parsedUrl.port = 80 + function send (message, proxyInfo) { + if (proxyInfo) { + const pack = Socks.createUDPFrame({ host: hostname, port }, message) + socket.send(pack, 0, pack.length, proxyInfo.port, proxyInfo.host) + } else { + socket.send(message, 0, message.length, port, hostname) + } } - socket.send(message, 0, message.length, parsedUrl.port, parsedUrl.hostname) - } - function announce (connectionId, opts) { - transactionId = genTransactionId() - - send(Buffer.concat([ - connectionId, - common.toUInt32(common.ACTIONS.ANNOUNCE), - transactionId, - self.client._infoHashBuffer, - self.client._peerIdBuffer, - toUInt64(opts.downloaded), - opts.left != null ? toUInt64(opts.left) : Buffer.from('FFFFFFFFFFFFFFFF', 'hex'), - toUInt64(opts.uploaded), - common.toUInt32(common.EVENTS[opts.event] || 0), - common.toUInt32(0), // ip address (optional) - common.toUInt32(0), // key (optional) - common.toUInt32(opts.numwant), - toUInt16(self.client._port) - ])) - } + function announce (connectionId, opts) { + transactionId = genTransactionId() + + send(concat([ + connectionId, + common.toUInt32(common.ACTIONS.ANNOUNCE), + transactionId, + self.client._infoHashBuffer, + self.client._peerIdBuffer, + toUInt64(opts.downloaded), + opts.left != null ? toUInt64(opts.left) : hex2arr('ffffffffffffffff'), + toUInt64(opts.uploaded), + common.toUInt32(common.EVENTS[opts.event] || 0), + common.toUInt32(0), // ip address (optional) + common.toUInt32(0), // key (optional) + common.toUInt32(opts.numwant), + toUInt16(self.client._port) + ]), relay) + } - function scrape (connectionId) { - transactionId = genTransactionId() + function scrape (connectionId) { + transactionId = genTransactionId() - var infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) - ? Buffer.concat(opts.infoHash) - : (opts.infoHash || self.client._infoHashBuffer) + const infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) + ? concat(opts.infoHash) + : (opts.infoHash || self.client._infoHashBuffer) - send(Buffer.concat([ - connectionId, - common.toUInt32(common.ACTIONS.SCRAPE), - transactionId, - infoHash - ])) + send(concat([ + connectionId, + common.toUInt32(common.ACTIONS.SCRAPE), + transactionId, + infoHash + ]), relay) + } } } +UDPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes + function genTransactionId () { - return randombytes(4) + return randomBytes(4) } function toUInt16 (n) { - var buf = Buffer.allocUnsafe(2) - buf.writeUInt16BE(n, 0) + const buf = new Uint8Array(2) + const view = new DataView(buf.buffer) + view.setUint16(0, n) return buf } -var MAX_UINT = 4294967295 +const MAX_UINT = 4294967295 function toUInt64 (n) { if (n > MAX_UINT || typeof n === 'string') { - var bytes = new BN(n).toArray() - while (bytes.length < 8) { - bytes.unshift(0) - } - return Buffer.from(bytes) + const buf = new Uint8Array(8) + const view = new DataView(buf.buffer) + view.setBigUint64(0, BigInt(n)) + return buf } - return Buffer.concat([common.toUInt32(0), common.toUInt32(n)]) + return concat([new Uint8Array(4), common.toUInt32(n)]) } function noop () {} + +export default UDPTracker diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index b0aa6b51a..c4e3e23d7 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -1,404 +1,442 @@ -module.exports = WebSocketTracker +import Debug from 'debug' +import Peer from '@thaunknown/simple-peer/lite.js' +import Socket from '@thaunknown/simple-websocket' +import { arr2text, arr2hex, hex2bin, bin2hex, randomBytes } from 'uint8-util' -var debug = require('debug')('bittorrent-tracker:websocket-tracker') -var extend = require('xtend') -var inherits = require('inherits') -var Peer = require('simple-peer') -var randombytes = require('randombytes') -var Socket = require('simple-websocket') +import common from '../common.js' +import Tracker from './tracker.js' -var common = require('../common') -var Tracker = require('./tracker') +const debug = Debug('bittorrent-tracker:websocket-tracker') // Use a socket pool, so tracker clients share WebSocket objects for the same server. // In practice, WebSockets are pretty slow to establish, so this gives a nice performance // boost, and saves browser resources. -var socketPool = {} +const socketPool = {} -var RECONNECT_MINIMUM = 15 * 1000 -var RECONNECT_MAXIMUM = 30 * 60 * 1000 -var RECONNECT_VARIANCE = 30 * 1000 -var OFFER_TIMEOUT = 50 * 1000 +const RECONNECT_MINIMUM = 10 * 1000 +const RECONNECT_MAXIMUM = 60 * 60 * 1000 +const RECONNECT_VARIANCE = 5 * 60 * 1000 +const OFFER_TIMEOUT = 50 * 1000 -inherits(WebSocketTracker, Tracker) +class WebSocketTracker extends Tracker { + constructor (client, announceUrl) { + super(client, announceUrl) + debug('new websocket tracker %s', announceUrl) -function WebSocketTracker (client, announceUrl, opts) { - var self = this - Tracker.call(self, client, announceUrl) - debug('new websocket tracker %s', announceUrl) + this.peers = {} // peers (offer id -> peer) + this.socket = null - self.peers = {} // peers (offer id -> peer) - self.socket = null + this.reconnecting = false + this.retries = 0 + this.reconnectTimer = null - self.reconnecting = false - self.retries = 0 - self.reconnectTimer = null + // Simple boolean flag to track whether the socket has received data from + // the websocket server since the last time socket.send() was called. + this.expectingResponse = false - self._openSocket() -} + this._openSocket() + } -WebSocketTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 1000 // 30 seconds + announce (opts) { + if (this.destroyed || this.reconnecting) return + if (!this.socket.connected) { + this.socket.once('connect', () => { + this.announce(opts) + }) + return + } -WebSocketTracker.prototype.announce = function (opts) { - var self = this - if (self.destroyed || self.reconnecting) return - if (!self.socket.connected) { - self.socket.once('connect', function () { - self.announce(opts) + const params = Object.assign({}, opts, { + action: 'announce', + info_hash: this.client._infoHashBinary, + peer_id: this.client._peerIdBinary }) - return - } + if (this._trackerId) params.trackerid = this._trackerId - var params = extend(opts, { - action: 'announce', - info_hash: self.client._infoHashBinary, - peer_id: self.client._peerIdBinary - }) - if (self._trackerId) params.trackerid = self._trackerId - - if (opts.event === 'stopped') { - // Don't include offers with 'stopped' event - self._send(params) - } else { - // Limit the number of offers that are generated, since it can be slow - var numwant = Math.min(opts.numwant, 10) - - self._generateOffers(numwant, function (offers) { - params.numwant = numwant - params.offers = offers - self._send(params) - }) - } -} + if (opts.event === 'stopped' || opts.event === 'completed') { + // Don't include offers with 'stopped' or 'completed' event + this._send(params) + } else { + // Limit the number of offers that are generated, since it can be slow + const numwant = Math.min(opts.numwant, 5) -WebSocketTracker.prototype.scrape = function (opts) { - var self = this - if (self.destroyed || self.reconnecting) return - if (!self.socket.connected) { - self.socket.once('connect', function () { - self.scrape(opts) - }) - return + this._generateOffers(numwant, offers => { + params.numwant = numwant + params.offers = offers + this._send(params) + }) + } } - var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) - ? opts.infoHash.map(function (infoHash) { - return infoHash.toString('binary') - }) - : (opts.infoHash && opts.infoHash.toString('binary')) || self.client._infoHashBinary - var params = { - action: 'scrape', - info_hash: infoHashes + scrape (opts) { + if (this.destroyed || this.reconnecting) return + if (!this.socket.connected) { + this.socket.once('connect', () => { + this.scrape(opts) + }) + return + } + + const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) + ? opts.infoHash.map(infoHash => hex2bin(infoHash)) + : (opts.infoHash && hex2bin(opts.infoHash)) || this.client._infoHashBinary + const params = { + action: 'scrape', + info_hash: infoHashes + } + + this._send(params) } - self._send(params) -} + destroy (cb = noop) { + if (this.destroyed) return cb(null) -WebSocketTracker.prototype.destroy = function (cb) { - var self = this - if (!cb) cb = noop - if (self.destroyed) return cb(null) + this.destroyed = true - self.destroyed = true + clearInterval(this.interval) + clearTimeout(this.reconnectTimer) - clearInterval(self.interval) - clearTimeout(self.reconnectTimer) + // Destroy peers + for (const peerId in this.peers) { + const peer = this.peers[peerId] + clearTimeout(peer.trackerTimeout) + peer.destroy() + } + this.peers = null + + if (this.socket) { + this.socket.removeListener('connect', this._onSocketConnectBound) + this.socket.removeListener('data', this._onSocketDataBound) + this.socket.removeListener('close', this._onSocketCloseBound) + this.socket.removeListener('error', this._onSocketErrorBound) + this.socket = null + } - if (self.socket) { - self.socket.removeListener('connect', self._onSocketConnectBound) - self.socket.removeListener('data', self._onSocketDataBound) - self.socket.removeListener('close', self._onSocketCloseBound) - self.socket.removeListener('error', self._onSocketErrorBound) - } + this._onSocketConnectBound = null + this._onSocketErrorBound = null + this._onSocketDataBound = null + this._onSocketCloseBound = null - self._onSocketConnectBound = null - self._onSocketErrorBound = null - self._onSocketDataBound = null - self._onSocketCloseBound = null + if (socketPool[this.announceUrl]) { + socketPool[this.announceUrl].consumers -= 1 + } - // Destroy peers - for (var peerId in self.peers) { - var peer = self.peers[peerId] - clearTimeout(peer.trackerTimeout) - peer.destroy() - } - self.peers = null + // Other instances are using the socket, so there's nothing left to do here + if (socketPool[this.announceUrl].consumers > 0) return cb() - if (socketPool[self.announceUrl]) { - socketPool[self.announceUrl].consumers -= 1 - } + let socket = socketPool[this.announceUrl] + delete socketPool[this.announceUrl] + socket.on('error', noop) // ignore all future errors + socket.once('close', cb) - if (socketPool[self.announceUrl].consumers === 0) { - delete socketPool[self.announceUrl] + let timeout - try { - self.socket.on('error', noop) // ignore all future errors - self.socket.destroy(cb) - } catch (err) { - cb(null) + // If there is no data response expected, destroy immediately. + if (!this.expectingResponse) return destroyCleanup() + + // Otherwise, wait a short time for potential responses to come in from the + // server, then force close the socket. + timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT) + + // But, if a response comes from the server before the timeout fires, do cleanup + // right away. + socket.once('data', destroyCleanup) + + function destroyCleanup () { + if (timeout) { + clearTimeout(timeout) + timeout = null + } + socket.removeListener('data', destroyCleanup) + socket.destroy() + socket = null } - } else { - cb(null) } - self.socket = null -} + _openSocket () { + this.destroyed = false -WebSocketTracker.prototype._openSocket = function () { - var self = this - self.destroyed = false + if (!this.peers) this.peers = {} - if (!self.peers) self.peers = {} + this._onSocketConnectBound = () => { + this._onSocketConnect() + } + this._onSocketErrorBound = err => { + this._onSocketError(err) + } + this._onSocketDataBound = data => { + this._onSocketData(data) + } + this._onSocketCloseBound = () => { + this._onSocketClose() + } - self._onSocketConnectBound = function () { - self._onSocketConnect() - } - self._onSocketErrorBound = function (err) { - self._onSocketError(err) - } - self._onSocketDataBound = function (data) { - self._onSocketData(data) - } - self._onSocketCloseBound = function () { - self._onSocketClose() - } + this.socket = socketPool[this.announceUrl] + if (this.socket) { + socketPool[this.announceUrl].consumers += 1 + if (this.socket.connected) { + this._onSocketConnectBound() + } + } else { + const parsedUrl = new URL(this.announceUrl) + let agent + if (this.client._proxyOpts) { + agent = parsedUrl.protocol === 'wss:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent + if (!agent && this.client._proxyOpts.socksProxy) { + agent = this.client._proxyOpts.socksProxy + } + } + this.socket = socketPool[this.announceUrl] = new Socket({ url: this.announceUrl, agent }) + this.socket.consumers = 1 + this.socket.once('connect', this._onSocketConnectBound) + } - self.socket = socketPool[self.announceUrl] - if (self.socket) { - socketPool[self.announceUrl].consumers += 1 - } else { - self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl) - self.socket.consumers = 1 - self.socket.on('connect', self._onSocketConnectBound) + this.socket.on('data', this._onSocketDataBound) + this.socket.once('close', this._onSocketCloseBound) + this.socket.once('error', this._onSocketErrorBound) } - self.socket.on('data', self._onSocketDataBound) - self.socket.on('close', self._onSocketCloseBound) - self.socket.on('error', self._onSocketErrorBound) -} + _onSocketConnect () { + if (this.destroyed) return -WebSocketTracker.prototype._onSocketConnect = function () { - var self = this - if (self.destroyed) return - - if (self.reconnecting) { - self.reconnecting = false - self.retries = 0 - self.announce(self.client._defaultAnnounceOpts()) + if (this.reconnecting) { + this.reconnecting = false + this.retries = 0 + this.announce(this.client._defaultAnnounceOpts()) + } } -} -WebSocketTracker.prototype._onSocketData = function (data) { - var self = this - if (self.destroyed) return + _onSocketData (data) { + if (this.destroyed) return - try { - data = JSON.parse(data) - } catch (err) { - self.client.emit('warning', new Error('Invalid tracker response')) - return - } + this.expectingResponse = false - if (data.action === 'announce') { - self._onAnnounceResponse(data) - } else if (data.action === 'scrape') { - self._onScrapeResponse(data) - } else { - self._onSocketError(new Error('invalid action in WS response: ' + data.action)) + try { + data = JSON.parse(arr2text(data)) + } catch (err) { + this.client.emit('warning', new Error('Invalid tracker response')) + return + } + + if (data.action === 'announce') { + this._onAnnounceResponse(data) + } else if (data.action === 'scrape') { + this._onScrapeResponse(data) + } else { + this._onSocketError(new Error(`invalid action in WS response: ${data.action}`)) + } } -} -WebSocketTracker.prototype._onAnnounceResponse = function (data) { - var self = this + _onAnnounceResponse (data) { + if (data.info_hash !== this.client._infoHashBinary) { + debug( + 'ignoring websocket data from %s for %s (looking for %s: reused socket)', + this.announceUrl, bin2hex(data.info_hash), this.client.infoHash + ) + return + } + + if (data.peer_id && data.peer_id === this.client._peerIdBinary) { + // ignore offers/answers from this client + return + } - if (data.info_hash !== self.client._infoHashBinary) { debug( - 'ignoring websocket data from %s for %s (looking for %s: reused socket)', - self.announceUrl, common.binaryToHex(data.info_hash), self.client.infoHash + 'received %s from %s for %s', + JSON.stringify(data), this.announceUrl, this.client.infoHash ) - return - } - - if (data.peer_id && data.peer_id === self.client._peerIdBinary) { - // ignore offers/answers from this client - return - } - debug( - 'received %s from %s for %s', - JSON.stringify(data), self.announceUrl, self.client.infoHash - ) + const failure = data['failure reason'] + if (failure) return this.client.emit('warning', new Error(failure)) - var failure = data['failure reason'] - if (failure) return self.client.emit('warning', new Error(failure)) + const warning = data['warning message'] + if (warning) this.client.emit('warning', new Error(warning)) - var warning = data['warning message'] - if (warning) self.client.emit('warning', new Error(warning)) + const interval = data.interval || data['min interval'] + if (interval) this.setInterval(interval * 1000) - var interval = data.interval || data['min interval'] - if (interval) self.setInterval(interval * 1000) + const trackerId = data['tracker id'] + if (trackerId) { + // If absent, do not discard previous trackerId value + this._trackerId = trackerId + } - var trackerId = data['tracker id'] - if (trackerId) { - // If absent, do not discard previous trackerId value - self._trackerId = trackerId - } + if (data.complete != null) { + const response = Object.assign({}, data, { + announce: this.announceUrl, + infoHash: bin2hex(data.info_hash) + }) + this.client.emit('update', response) + } - if (data.complete != null) { - self.client.emit('update', { - announce: self.announceUrl, - complete: data.complete, - incomplete: data.incomplete - }) - } + let peer + if (data.offer && data.peer_id) { + debug('creating peer (from remote offer)') + peer = this._createPeer() + peer.id = bin2hex(data.peer_id) + peer.once('signal', answer => { + const params = { + action: 'announce', + info_hash: this.client._infoHashBinary, + peer_id: this.client._peerIdBinary, + to_peer_id: data.peer_id, + answer, + offer_id: data.offer_id + } + if (this._trackerId) params.trackerid = this._trackerId + this._send(params) + }) + this.client.emit('peer', peer) + peer.signal(data.offer) + } - var peer - if (data.offer && data.peer_id) { - debug('creating peer (from remote offer)') - peer = new Peer({ - trickle: false, - config: self.client._rtcConfig, - wrtc: self.client._wrtc - }) - peer.id = common.binaryToHex(data.peer_id) - peer.once('signal', function (answer) { - var params = { - action: 'announce', - info_hash: self.client._infoHashBinary, - peer_id: self.client._peerIdBinary, - to_peer_id: data.peer_id, - answer: answer, - offer_id: data.offer_id + if (data.answer && data.peer_id) { + const offerId = bin2hex(data.offer_id) + peer = this.peers[offerId] + if (peer) { + peer.id = bin2hex(data.peer_id) + this.client.emit('peer', peer) + peer.signal(data.answer) + + clearTimeout(peer.trackerTimeout) + peer.trackerTimeout = null + delete this.peers[offerId] + } else { + debug(`got unexpected answer: ${JSON.stringify(data.answer)}`) } - if (self._trackerId) params.trackerid = self._trackerId - self._send(params) - }) - peer.signal(data.offer) - self.client.emit('peer', peer) - } - - if (data.answer && data.peer_id) { - var offerId = common.binaryToHex(data.offer_id) - peer = self.peers[offerId] - if (peer) { - peer.id = common.binaryToHex(data.peer_id) - peer.signal(data.answer) - self.client.emit('peer', peer) - - clearTimeout(peer.trackerTimeout) - peer.trackerTimeout = null - delete self.peers[offerId] - } else { - debug('got unexpected answer: ' + JSON.stringify(data.answer)) } } -} -WebSocketTracker.prototype._onScrapeResponse = function (data) { - var self = this - data = data.files || {} + _onScrapeResponse (data) { + data = data.files || {} - var keys = Object.keys(data) - if (keys.length === 0) { - self.client.emit('warning', new Error('invalid scrape response')) - return - } + const keys = Object.keys(data) + if (keys.length === 0) { + this.client.emit('warning', new Error('invalid scrape response')) + return + } - keys.forEach(function (infoHash) { - var response = data[infoHash] - // TODO: optionally handle data.flags.min_request_interval - // (separate from announce interval) - self.client.emit('scrape', { - announce: self.announceUrl, - infoHash: common.binaryToHex(infoHash), - complete: response.complete, - incomplete: response.incomplete, - downloaded: response.downloaded + keys.forEach(infoHash => { + // TODO: optionally handle data.flags.min_request_interval + // (separate from announce interval) + const response = Object.assign(data[infoHash], { + announce: this.announceUrl, + infoHash: bin2hex(infoHash) + }) + this.client.emit('scrape', response) }) - }) -} + } -WebSocketTracker.prototype._onSocketClose = function () { - var self = this - if (self.destroyed) return - self.destroy() - self._startReconnectTimer() -} + _onSocketClose () { + if (this.destroyed) return + this.destroy() + this._startReconnectTimer() + } -WebSocketTracker.prototype._onSocketError = function (err) { - var self = this - if (self.destroyed) return - self.destroy() - // errors will often happen if a tracker is offline, so don't treat it as fatal - self.client.emit('warning', err) - self._startReconnectTimer() -} + _onSocketError (err) { + if (this.destroyed) return + this.destroy() + // errors will often happen if a tracker is offline, so don't treat it as fatal + this.client.emit('warning', err) + this._startReconnectTimer() + } -WebSocketTracker.prototype._startReconnectTimer = function () { - var self = this - var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + Math.min(Math.pow(2, self.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM) + _startReconnectTimer () { + const ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + Math.min(Math.pow(2, this.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM) - self.reconnecting = true - clearTimeout(self.reconnectTimer) - self.reconnectTimer = setTimeout(function () { - self.retries++ - self._openSocket() - }, ms) - if (self.reconnectTimer.unref) self.reconnectTimer.unref() + this.reconnecting = true + clearTimeout(this.reconnectTimer) + this.reconnectTimer = setTimeout(() => { + this.retries++ + this._openSocket() + }, ms) + if (this.reconnectTimer.unref) this.reconnectTimer.unref() - debug('reconnecting socket in %s ms', ms) -} + debug('reconnecting socket in %s ms', ms) + } -WebSocketTracker.prototype._send = function (params) { - var self = this - if (self.destroyed) return + _send (params) { + if (this.destroyed) return + this.expectingResponse = true + const message = JSON.stringify(params) + debug('send %s', message) + this.socket.send(message) + } - var message = JSON.stringify(params) - debug('send %s', message) - self.socket.send(message) -} + _generateOffers (numwant, cb) { + const self = this + const offers = [] + debug('generating %s offers', numwant) -WebSocketTracker.prototype._generateOffers = function (numwant, cb) { - var self = this - var offers = [] - debug('generating %s offers', numwant) + for (let i = 0; i < numwant; ++i) { + generateOffer() + } + checkDone() + + function generateOffer () { + const offerId = arr2hex(randomBytes(20)) + debug('creating peer (from _generateOffers)') + const peer = self.peers[offerId] = self._createPeer({ initiator: true }) + peer.once('signal', offer => { + offers.push({ + offer, + offer_id: hex2bin(offerId) + }) + checkDone() + }) + peer.trackerTimeout = setTimeout(() => { + debug('tracker timeout: destroying peer') + peer.trackerTimeout = null + delete self.peers[offerId] + peer.destroy() + }, OFFER_TIMEOUT) + if (peer.trackerTimeout.unref) peer.trackerTimeout.unref() + } - for (var i = 0; i < numwant; ++i) { - generateOffer() + function checkDone () { + if (offers.length === numwant) { + debug('generated %s offers', numwant) + cb(offers) + } + } } - checkDone() - function generateOffer () { - var offerId = randombytes(20).toString('hex') - debug('creating peer (from _generateOffers)') - var peer = self.peers[offerId] = new Peer({ - initiator: true, + _createPeer (opts) { + const self = this + + opts = Object.assign({ trickle: false, config: self.client._rtcConfig, wrtc: self.client._wrtc - }) - peer.once('signal', function (offer) { - offers.push({ - offer: offer, - offer_id: common.hexToBinary(offerId) - }) - checkDone() - }) - peer.trackerTimeout = setTimeout(function () { - debug('tracker timeout: destroying peer') - peer.trackerTimeout = null - delete self.peers[offerId] + }, opts) + + const peer = new Peer(opts) + + peer.once('error', onError) + peer.once('connect', onConnect) + + return peer + + // Handle peer 'error' events that are fired *before* the peer is emitted in + // a 'peer' event. + function onError (err) { + self.client.emit('warning', new Error(`Connection error: ${err.message}`)) peer.destroy() - }, OFFER_TIMEOUT) - if (peer.trackerTimeout.unref) peer.trackerTimeout.unref() - } + } - function checkDone () { - if (offers.length === numwant) { - debug('generated %s offers', numwant) - cb(offers) + // Once the peer is emitted in a 'peer' event, then it's the consumer's + // responsibility to listen for errors, so the listeners are removed here. + function onConnect () { + peer.removeListener('error', onError) + peer.removeListener('connect', onConnect) } } } +WebSocketTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 1000 // 30 seconds +// Normally this shouldn't be accessed but is occasionally useful +WebSocketTracker._socketPool = socketPool + function noop () {} + +export default WebSocketTracker diff --git a/lib/common-node.js b/lib/common-node.js index dbc3565fe..9fefa5c4d 100644 --- a/lib/common-node.js +++ b/lib/common-node.js @@ -3,35 +3,49 @@ * These are separate from common.js so they can be skipped when bundling for the browser. */ -var Buffer = require('safe-buffer').Buffer -var querystring = require('querystring') +import querystring from 'querystring' +import { concat } from 'uint8-util' -exports.IPV4_RE = /^[\d\.]+$/ -exports.IPV6_RE = /^[\da-fA-F:]+$/ -exports.REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/ +export const IPV4_RE = /^[\d.]+$/ +export const IPV6_RE = /^[\da-fA-F:]+$/ +export const REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/ -exports.CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ]) -exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 } -exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 } -exports.EVENT_IDS = { +export const CONNECTION_ID = concat([toUInt32(0x417), toUInt32(0x27101980)]) +export const ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 } +export const EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 } +export const EVENT_IDS = { 0: 'update', 1: 'completed', 2: 'started', - 3: 'stopped' + 3: 'stopped', + 4: 'paused' } -exports.EVENT_NAMES = { +export const EVENT_NAMES = { update: 'update', completed: 'complete', started: 'start', - stopped: 'stop' + stopped: 'stop', + paused: 'pause' } -function toUInt32 (n) { - var buf = Buffer.allocUnsafe(4) - buf.writeUInt32BE(n, 0) +/** + * Client request timeout. How long to wait before considering a request to a + * tracker server to have timed out. + */ +export const REQUEST_TIMEOUT = 15000 + +/** + * Client destroy timeout. How long to wait before forcibly cleaning up all + * pending requests, open sockets, etc. + */ +export const DESTROY_TIMEOUT = 1000 + +export function toUInt32 (n) { + const buf = new Uint8Array(4) + const view = new DataView(buf.buffer) + view.setUint32(0, n) return buf } -exports.toUInt32 = toUInt32 /** * `querystring.parse` using `unescape` instead of decodeURIComponent, since bittorrent @@ -39,13 +53,7 @@ exports.toUInt32 = toUInt32 * @param {string} q * @return {Object} */ -exports.querystringParse = function (q) { - var saved = querystring.unescape - querystring.unescape = unescape // global - var ret = querystring.parse(q) - querystring.unescape = saved - return ret -} +export const querystringParse = q => querystring.parse(q, null, null, { decodeURIComponent: unescape }) /** * `querystring.stringify` using `escape` instead of encodeURIComponent, since bittorrent @@ -53,14 +61,9 @@ exports.querystringParse = function (q) { * @param {Object} obj * @return {string} */ -exports.querystringStringify = function (obj) { - var saved = querystring.escape - querystring.escape = escape // global - var ret = querystring.stringify(obj) - ret = ret.replace(/[@\*\/\+]/g, function (char) { - // `escape` doesn't encode the characters @*/+ so we do it manually - return '%' + char.charCodeAt(0).toString(16).toUpperCase() - }) - querystring.escape = saved +export const querystringStringify = obj => { + let ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape }) + ret = ret.replace(/[@*/+]/g, char => // `escape` doesn't encode the characters @*/+ so we do it manually + `%${char.charCodeAt(0).toString(16).toUpperCase()}`) return ret } diff --git a/lib/common.js b/lib/common.js index 493b14a99..ef888579c 100644 --- a/lib/common.js +++ b/lib/common.js @@ -1,26 +1,41 @@ /** * Functions/constants needed by both the client and server. */ +import * as common from './common-node.js' +export * from './common-node.js' -var Buffer = require('safe-buffer').Buffer -var extend = require('xtend/mutable') +export const DEFAULT_ANNOUNCE_PEERS = 50 +export const MAX_ANNOUNCE_PEERS = 82 -exports.DEFAULT_ANNOUNCE_PEERS = 50 -exports.MAX_ANNOUNCE_PEERS = 82 +// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like +// 'udp:'. Just replace it with 'http:' since we only need a few properties. +// +// Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and +// Edge. +// +// Note: UDP trackers aren't used in the normal browser build, but they are +// used in a Chrome App build (i.e. by Brave Browser). +// +// Bug reports: +// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880 +// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505 +export const parseUrl = str => { + const url = new URL(str.replace(/^udp:/, 'http:')) -exports.binaryToHex = function (str) { - if (typeof str !== 'string') { - str = String(str) + if (str.match(/^udp:/)) { + Object.defineProperties(url, { + href: { value: url.href.replace(/^http/, 'udp') }, + protocol: { value: url.protocol.replace(/^http/, 'udp') }, + origin: { value: url.origin.replace(/^http/, 'udp') } + }) } - return Buffer.from(str, 'binary').toString('hex') -} -exports.hexToBinary = function (str) { - if (typeof str !== 'string') { - str = String(str) - } - return Buffer.from(str, 'hex').toString('binary') + return url } -var config = require('./common-node') -extend(exports, config) +export default { + DEFAULT_ANNOUNCE_PEERS, + MAX_ANNOUNCE_PEERS, + parseUrl, + ...common +} diff --git a/lib/server/parse-http.js b/lib/server/parse-http.js index 9e83a59e1..97b46e307 100644 --- a/lib/server/parse-http.js +++ b/lib/server/parse-http.js @@ -1,11 +1,11 @@ -module.exports = parseHttpRequest +import { bin2hex } from 'uint8-util' -var common = require('../common') +import common from '../common.js' -function parseHttpRequest (req, opts) { +export default function (req, opts) { if (!opts) opts = {} - var s = req.url.split('?') - var params = common.querystringParse(s[1]) + const s = req.url.split('?') + const params = common.querystringParse(s[1]) params.type = 'http' if (opts.action === 'announce' || s[0] === '/announce') { @@ -14,43 +14,53 @@ function parseHttpRequest (req, opts) { if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { throw new Error('invalid info_hash') } - params.info_hash = common.binaryToHex(params.info_hash) + params.info_hash = bin2hex(params.info_hash) if (typeof params.peer_id !== 'string' || params.peer_id.length !== 20) { throw new Error('invalid peer_id') } - params.peer_id = common.binaryToHex(params.peer_id) + params.peer_id = bin2hex(params.peer_id) params.port = Number(params.port) - if (!params.port) throw new Error('invalid port') + if (!params.port || params.port <= 0 || params.port > 65535) throw new Error('invalid port') + + params.left = Number(params.left) + if (Number.isNaN(params.left)) params.left = Infinity - params.left = Number(params.left) || Infinity params.compact = Number(params.compact) || 0 params.numwant = Math.min( Number(params.numwant) || common.DEFAULT_ANNOUNCE_PEERS, common.MAX_ANNOUNCE_PEERS ) - params.ip = opts.trustProxy - ? req.headers['x-forwarded-for'] || req.connection.remoteAddress - : req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 - params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port + if (opts.trustProxy) { + if (req.headers['x-forwarded-for']) { + const [realIp] = req.headers['x-forwarded-for'].split(',') + params.ip = realIp.trim() + } else { + params.ip = req.connection.remoteAddress + } + } else { + params.ip = req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 + } + + params.addr = `${common.IPV6_RE.test(params.ip) ? `[${params.ip}]` : params.ip}:${params.port}` params.headers = req.headers } else if (opts.action === 'scrape' || s[0] === '/scrape') { params.action = common.ACTIONS.SCRAPE - if (typeof params.info_hash === 'string') params.info_hash = [ params.info_hash ] + if (typeof params.info_hash === 'string') params.info_hash = [params.info_hash] if (Array.isArray(params.info_hash)) { - params.info_hash = params.info_hash.map(function (binaryInfoHash) { + params.info_hash = params.info_hash.map(binaryInfoHash => { if (typeof binaryInfoHash !== 'string' || binaryInfoHash.length !== 20) { throw new Error('invalid info_hash') } - return common.binaryToHex(binaryInfoHash) + return bin2hex(binaryInfoHash) }) } } else { - throw new Error('invalid action in HTTP request: ' + req.url) + throw new Error(`invalid action in HTTP request: ${req.url}`) } return params diff --git a/lib/server/parse-udp.js b/lib/server/parse-udp.js index a189ae53f..f73b64917 100644 --- a/lib/server/parse-udp.js +++ b/lib/server/parse-udp.js @@ -1,19 +1,18 @@ -module.exports = parseUdpRequest +import ipLib from 'ip' +import common from '../common.js' +import { equal } from 'uint8-util' -var ipLib = require('ip') -var common = require('../common') - -function parseUdpRequest (msg, rinfo) { +export default function (msg, rinfo) { if (msg.length < 16) throw new Error('received packet is too short') - var params = { + const params = { connectionId: msg.slice(0, 8), // 64-bit action: msg.readUInt32BE(8), transactionId: msg.readUInt32BE(12), type: 'udp' } - if (!common.CONNECTION_ID.equals(params.connectionId)) { + if (!equal(common.CONNECTION_ID, params.connectionId)) { throw new Error('received packet with invalid connection id') } @@ -29,7 +28,7 @@ function parseUdpRequest (msg, rinfo) { params.event = common.EVENT_IDS[msg.readUInt32BE(80)] if (!params.event) throw new Error('invalid event') // early return - var ip = msg.readUInt32BE(84) // optional + const ip = msg.readUInt32BE(84) // optional params.ip = ip ? ipLib.toString(ip) : rinfo.address @@ -44,32 +43,32 @@ function parseUdpRequest (msg, rinfo) { ) params.port = msg.readUInt16BE(96) || rinfo.port // optional - params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets + params.addr = `${params.ip}:${params.port}` // TODO: ipv6 brackets params.compact = 1 // udp is always compact } else if (params.action === common.ACTIONS.SCRAPE) { // scrape message if ((msg.length - 16) % 20 !== 0) throw new Error('invalid scrape message') params.info_hash = [] - for (var i = 0, len = (msg.length - 16) / 20; i < len; i += 1) { - var infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes + for (let i = 0, len = (msg.length - 16) / 20; i < len; i += 1) { + const infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes params.info_hash.push(infoHash) } } else { - throw new Error('Invalid action in UDP packet: ' + params.action) + throw new Error(`Invalid action in UDP packet: ${params.action}`) } return params } -var TWO_PWR_32 = (1 << 16) * 2 +const TWO_PWR_32 = (1 << 16) * 2 /** * Return the closest floating-point representation to the buffer value. Precision will be * lost for big numbers. */ function fromUInt64 (buf) { - var high = buf.readUInt32BE(0) | 0 // force - var low = buf.readUInt32BE(4) | 0 - var lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low + const high = buf.readUInt32BE(0) | 0 // force + const low = buf.readUInt32BE(4) | 0 + const lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low - return high * TWO_PWR_32 + lowUnsigned + return (high * TWO_PWR_32) + lowUnsigned } diff --git a/lib/server/parse-websocket.js b/lib/server/parse-websocket.js index 0db50b55e..744a3c30b 100644 --- a/lib/server/parse-websocket.js +++ b/lib/server/parse-websocket.js @@ -1,8 +1,8 @@ -module.exports = parseWebSocketRequest +import { bin2hex } from 'uint8-util' -var common = require('../common') +import common from '../common.js' -function parseWebSocketRequest (socket, opts, params) { +export default function (socket, opts, params) { if (!opts) opts = {} params = JSON.parse(params) // may throw @@ -14,21 +14,23 @@ function parseWebSocketRequest (socket, opts, params) { if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { throw new Error('invalid info_hash') } - params.info_hash = common.binaryToHex(params.info_hash) + params.info_hash = bin2hex(params.info_hash) if (typeof params.peer_id !== 'string' || params.peer_id.length !== 20) { throw new Error('invalid peer_id') } - params.peer_id = common.binaryToHex(params.peer_id) + params.peer_id = bin2hex(params.peer_id) if (params.answer) { if (typeof params.to_peer_id !== 'string' || params.to_peer_id.length !== 20) { throw new Error('invalid `to_peer_id` (required with `answer`)') } - params.to_peer_id = common.binaryToHex(params.to_peer_id) + params.to_peer_id = bin2hex(params.to_peer_id) } - params.left = Number(params.left) || Infinity + params.left = Number(params.left) + if (Number.isNaN(params.left)) params.left = Infinity + params.numwant = Math.min( Number(params.offers && params.offers.length) || 0, // no default - explicit only common.MAX_ANNOUNCE_PEERS @@ -37,28 +39,48 @@ function parseWebSocketRequest (socket, opts, params) { } else if (params.action === 'scrape') { params.action = common.ACTIONS.SCRAPE - if (typeof params.info_hash === 'string') params.info_hash = [ params.info_hash ] + if (typeof params.info_hash === 'string') params.info_hash = [params.info_hash] if (Array.isArray(params.info_hash)) { - params.info_hash = params.info_hash.map(function (binaryInfoHash) { + params.info_hash = params.info_hash.map(binaryInfoHash => { if (typeof binaryInfoHash !== 'string' || binaryInfoHash.length !== 20) { throw new Error('invalid info_hash') } - return common.binaryToHex(binaryInfoHash) + return bin2hex(binaryInfoHash) }) } } else { - throw new Error('invalid action in WS request: ' + params.action) + throw new Error(`invalid action in WS request: ${params.action}`) } - params.ip = opts.trustProxy - ? socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress - : socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 - params.port = socket.upgradeReq.connection.remotePort - if (params.port) { - params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port + // On first parse, save important data from `socket.upgradeReq` and delete it + // to reduce memory usage. + if (socket.upgradeReq) { + if (opts.trustProxy) { + if (socket.upgradeReq.headers['x-forwarded-for']) { + const [realIp] = socket.upgradeReq.headers['x-forwarded-for'].split(',') + socket.ip = realIp.trim() + } else { + socket.ip = socket.upgradeReq.connection.remoteAddress + } + } else { + socket.ip = socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 + } + + socket.port = socket.upgradeReq.connection.remotePort + if (socket.port) { + socket.addr = `${common.IPV6_RE.test(socket.ip) ? `[${socket.ip}]` : socket.ip}:${socket.port}` + } + + socket.headers = socket.upgradeReq.headers + + // Delete `socket.upgradeReq` when it is no longer needed to reduce memory usage + socket.upgradeReq = null } - params.headers = socket.upgradeReq.headers + params.ip = socket.ip + params.port = socket.port + params.addr = socket.addr + params.headers = socket.headers return params } diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 3d987bf80..9e3a0a204 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -1,122 +1,164 @@ -module.exports = Swarm +import arrayRemove from 'unordered-array-remove' +import Debug from 'debug' +import LRU from 'lru' +import randomIterate from 'random-iterate' -var debug = require('debug')('bittorrent-tracker') -var LRU = require('lru') -var randomIterate = require('random-iterate') +const debug = Debug('bittorrent-tracker:swarm') // Regard this as the default implementation of an interface that you // need to support when overriding Server.createSwarm() and Server.getSwarm() -function Swarm (infoHash, server) { - this.peers = new LRU({ - max: server.peersCacheLength || 1000, - maxAge: server.peersCacheTtl || 900000 // 900 000ms = 15 minutes - }) - this.complete = 0 - this.incomplete = 0 -} +class Swarm { + constructor (infoHash, server) { + const self = this + self.infoHash = infoHash + self.complete = 0 + self.incomplete = 0 + + self.peers = new LRU({ + max: server.peersCacheLength || 1000, + maxAge: server.peersCacheTtl || 20 * 60 * 1000 // 20 minutes + }) -Swarm.prototype.announce = function (params, cb) { - var self = this - var id = params.type === 'ws' ? params.peer_id : params.addr - // Mark the source peer as recently used in cache - var peer = self.peers.get(id) - - if (params.event === 'started') { - self._onAnnounceStarted(params, peer, id) - } else if (params.event === 'stopped') { - self._onAnnounceStopped(params, peer, id) - } else if (params.event === 'completed') { - self._onAnnounceCompleted(params, peer, id) - } else if (params.event === 'update') { - self._onAnnounceUpdate(params, peer, id) - } else { - cb(new Error('invalid event')) - return + // When a peer is evicted from the LRU store, send a synthetic 'stopped' event + // so the stats get updated correctly. + self.peers.on('evict', data => { + const peer = data.value + const params = { + type: peer.type, + event: 'stopped', + numwant: 0, + peer_id: peer.peerId + } + self._onAnnounceStopped(params, peer, peer.peerId) + peer.socket = null + }) } - cb(null, { - complete: self.complete, - incomplete: self.incomplete, - peers: self._getPeers(params.numwant, params.peer_id, !!params.socket) - }) -} -Swarm.prototype.scrape = function (params, cb) { - cb(null, { - complete: this.complete, - incomplete: this.incomplete - }) -} + announce (params, cb) { + const self = this + const id = params.type === 'ws' ? params.peer_id : params.addr + // Mark the source peer as recently used in cache + const peer = self.peers.get(id) -Swarm.prototype._onAnnounceStarted = function (params, peer, id) { - if (peer) { - debug('unexpected `started` event from peer that is already in swarm') - return this._onAnnounceUpdate(params, peer, id) // treat as an update + if (params.event === 'started') { + self._onAnnounceStarted(params, peer, id) + } else if (params.event === 'stopped') { + self._onAnnounceStopped(params, peer, id) + if (!cb) return // when websocket is closed + } else if (params.event === 'completed') { + self._onAnnounceCompleted(params, peer, id) + } else if (params.event === 'update') { + self._onAnnounceUpdate(params, peer, id) + } else if (params.event === 'paused') { + self._onAnnouncePaused(params, peer, id) + } else { + cb(new Error('invalid event')) + return + } + cb(null, { + complete: self.complete, + incomplete: self.incomplete, + peers: self._getPeers(params.numwant, params.peer_id, !!params.socket) + }) } - if (params.left === 0) this.complete += 1 - else this.incomplete += 1 - peer = this.peers.set(id, { - type: params.type, - complete: params.left === 0, - peerId: params.peer_id, // as hex - ip: params.ip, - port: params.port, - socket: params.socket // only websocket - }) -} - -Swarm.prototype._onAnnounceStopped = function (params, peer, id) { - if (!peer) { - debug('unexpected `stopped` event from peer that is not in swarm') - return // do nothing + scrape (params, cb) { + cb(null, { + complete: this.complete, + incomplete: this.incomplete + }) } - if (peer.complete) this.complete -= 1 - else this.incomplete -= 1 - this.peers.remove(id) -} + _onAnnounceStarted (params, peer, id) { + if (peer) { + debug('unexpected `started` event from peer that is already in swarm') + return this._onAnnounceUpdate(params, peer, id) // treat as an update + } -Swarm.prototype._onAnnounceCompleted = function (params, peer, id) { - if (!peer) { - debug('unexpected `completed` event from peer that is not in swarm') - return this._onAnnounceStarted(params, peer, id) // treat as a start - } - if (peer.complete) { - debug('unexpected `completed` event from peer that is already marked as completed') - return // do nothing + if (params.left === 0) this.complete += 1 + else this.incomplete += 1 + this.peers.set(id, { + type: params.type, + complete: params.left === 0, + peerId: params.peer_id, // as hex + ip: params.ip, + port: params.port, + socket: params.socket // only websocket + }) } - this.complete += 1 - this.incomplete -= 1 - peer.complete = true - this.peers.set(id, peer) -} + _onAnnounceStopped (params, peer, id) { + if (!peer) { + debug('unexpected `stopped` event from peer that is not in swarm') + return // do nothing + } + + if (peer.complete) this.complete -= 1 + else this.incomplete -= 1 + + // If it's a websocket, remove this swarm's infohash from the list of active + // swarms that this peer is participating in. + if (peer.socket && !peer.socket.destroyed) { + const index = peer.socket.infoHashes.indexOf(this.infoHash) + arrayRemove(peer.socket.infoHashes, index) + } -Swarm.prototype._onAnnounceUpdate = function (params, peer, id) { - if (!peer) { - debug('unexpected `update` event from peer that is not in swarm') - return this._onAnnounceStarted(params, peer, id) // treat as a start + this.peers.remove(id) } - if (!peer.complete && params.left === 0) { + _onAnnounceCompleted (params, peer, id) { + if (!peer) { + debug('unexpected `completed` event from peer that is not in swarm') + return this._onAnnounceStarted(params, peer, id) // treat as a start + } + if (peer.complete) { + debug('unexpected `completed` event from peer that is already completed') + return this._onAnnounceUpdate(params, peer, id) // treat as an update + } + this.complete += 1 this.incomplete -= 1 peer.complete = true this.peers.set(id, peer) } -} -Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) { - var peers = [] - var ite = randomIterate(this.peers.keys) - var peerId - while ((peerId = ite()) && peers.length < numwant) { - // Don't mark the peer as most recently used on announce - var peer = this.peers.peek(peerId) - if (!peer) continue - if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself - if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type - peers.push(peer) + _onAnnounceUpdate (params, peer, id) { + if (!peer) { + debug('unexpected `update` event from peer that is not in swarm') + return this._onAnnounceStarted(params, peer, id) // treat as a start + } + + if (!peer.complete && params.left === 0) { + this.complete += 1 + this.incomplete -= 1 + peer.complete = true + } + this.peers.set(id, peer) + } + + _onAnnouncePaused (params, peer, id) { + if (!peer) { + debug('unexpected `paused` event from peer that is not in swarm') + return this._onAnnounceStarted(params, peer, id) // treat as a start + } + + this._onAnnounceUpdate(params, peer, id) + } + + _getPeers (numwant, ownPeerId, isWebRTC) { + const peers = [] + const ite = randomIterate(this.peers.keys) + let peerId + while ((peerId = ite()) && peers.length < numwant) { + // Don't mark the peer as most recently used on announce + const peer = this.peers.peek(peerId) + if (!peer) continue + if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself + if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type + peers.push(peer) + } + return peers } - return peers } + +export default Swarm diff --git a/package.json b/package.json index f2e3676ee..ca9cebd6d 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "bittorrent-tracker", "description": "Simple, robust, BitTorrent tracker (client & server) implementation", - "version": "8.0.11", + "version": "11.2.2", "author": { - "name": "Feross Aboukhadijeh", - "email": "feross@feross.org", - "url": "http://feross.org/" + "name": "WebTorrent LLC", + "email": "feross@webtorrent.io", + "url": "https://webtorrent.io" }, "bin": { "bittorrent-tracker": "./bin/cmd.js" @@ -14,41 +14,68 @@ "./lib/common-node.js": false, "./lib/client/http-tracker.js": false, "./lib/client/udp-tracker.js": false, - "./server.js": false + "./server.js": false, + "socks": false + }, + "chromeapp": { + "./server.js": false, + "dgram": "chrome-dgram", + "socks": false }, "bugs": { - "url": "https://github.com/feross/bittorrent-tracker/issues" + "url": "https://github.com/webtorrent/bittorrent-tracker/issues" }, + "type": "module", "dependencies": { - "bencode": "^0.10.0", - "bittorrent-peerid": "^1.0.2", - "bn.js": "^4.4.0", - "compact2string": "^1.2.0", - "debug": "^2.0.0", - "inherits": "^2.0.1", - "ip": "^1.0.1", - "lru": "^3.0.0", - "minimist": "^1.1.1", - "once": "^1.3.0", + "@thaunknown/simple-peer": "^10.0.8", + "@thaunknown/simple-websocket": "^9.1.3", + "bencode": "^4.0.0", + "bittorrent-peerid": "^1.3.6", + "chrome-dgram": "^3.0.6", + "compact2string": "^1.4.1", + "cross-fetch-ponyfill": "^1.0.3", + "debug": "^4.3.4", + "ip": "^2.0.1", + "lru": "^3.1.0", + "minimist": "^1.2.8", + "once": "^1.4.0", + "queue-microtask": "^1.2.3", "random-iterate": "^1.0.1", - "randombytes": "^2.0.3", - "run-parallel": "^1.1.2", - "run-series": "^1.0.2", - "safe-buffer": "^5.0.0", - "simple-get": "^2.0.0", - "simple-peer": "^6.0.0", - "simple-websocket": "^4.0.0", - "string2compact": "^1.1.1", - "uniq": "^1.0.1", - "ws": "^1.0.0", - "xtend": "^4.0.0" + "run-parallel": "^1.2.0", + "run-series": "^1.1.9", + "socks": "^2.8.3", + "string2compact": "^2.0.1", + "uint8-util": "^2.2.5", + "unordered-array-remove": "^1.0.2", + "ws": "^8.17.0" }, "devDependencies": { - "electron-webrtc": "^0.2.0", - "magnet-uri": "^5.1.3", + "@mapbox/node-pre-gyp": "1.0.11", + "@webtorrent/semantic-release-config": "1.0.10", + "magnet-uri": "7.0.7", + "semantic-release": "21.1.2", "standard": "*", - "tape": "^4.0.0", - "webtorrent-fixtures": "^1.3.0" + "tape": "5.9.0", + "undici": "^6.16.1", + "webrtc-polyfill": "^1.1.5", + "webtorrent-fixtures": "2.0.2" + }, + "engines": { + "node": ">=16.0.0" + }, + "exports": { + ".": { + "import": "./index.js" + }, + "./client": { + "import": "./client.js" + }, + "./server": { + "import": "./server.js" + }, + "./websocket-tracker": { + "import": "./lib/client/websocket-tracker.js" + } }, "keywords": [ "bittorrent", @@ -62,12 +89,39 @@ ], "license": "MIT", "main": "index.js", + "optionalDependencies": { + "bufferutil": "^4.0.8", + "utf-8-validate": "^6.0.4" + }, "repository": { "type": "git", - "url": "git://github.com/feross/bittorrent-tracker.git" + "url": "git://github.com/webtorrent/bittorrent-tracker.git" }, "scripts": { - "update-authors": "./bin/update-authors.sh", - "test": "standard && tape test/*.js" + "preversion": "npm run update-authors", + "test": "standard && tape test/*.js", + "update-authors": "./tools/update-authors.sh" + }, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "renovate": { + "extends": [ + "github>webtorrent/renovate-config" + ] + }, + "release": { + "extends": "@webtorrent/semantic-release-config" } } diff --git a/server.js b/server.js index 87d08a1e2..f7023d99f 100644 --- a/server.js +++ b/server.js @@ -1,24 +1,22 @@ -module.exports = Server - -var Buffer = require('safe-buffer').Buffer -var bencode = require('bencode') -var debug = require('debug')('bittorrent-tracker') -var dgram = require('dgram') -var EventEmitter = require('events').EventEmitter -var http = require('http') -var inherits = require('inherits') -var peerid = require('bittorrent-peerid') -var series = require('run-series') -var string2compact = require('string2compact') -var WebSocketServer = require('ws').Server - -var common = require('./lib/common') -var Swarm = require('./lib/server/swarm') -var parseHttpRequest = require('./lib/server/parse-http') -var parseUdpRequest = require('./lib/server/parse-udp') -var parseWebSocketRequest = require('./lib/server/parse-websocket') - -inherits(Server, EventEmitter) +import bencode from 'bencode' +import Debug from 'debug' +import dgram from 'dgram' +import EventEmitter from 'events' +import http from 'http' +import peerid from 'bittorrent-peerid' +import series from 'run-series' +import string2compact from 'string2compact' +import { WebSocketServer } from 'ws' +import { hex2bin } from 'uint8-util' + +import common from './lib/common.js' +import Swarm from './lib/server/swarm.js' +import parseHttpRequest from './lib/server/parse-http.js' +import parseUdpRequest from './lib/server/parse-udp.js' +import parseWebSocketRequest from './lib/server/parse-websocket.js' + +const debug = Debug('bittorrent-tracker:server') +const hasOwnProperty = Object.prototype.hasOwnProperty /** * BitTorrent tracker server. @@ -27,696 +25,737 @@ inherits(Server, EventEmitter) * metrics from clients that help the tracker keep overall statistics about the torrent. * Responses include a peer list that helps the client participate in the torrent. * - * @param {Object} opts options object - * @param {Number} opts.interval tell clients to announce on this interval (ms) - * @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy - * @param {boolean} opts.http start an http server? (default: true) - * @param {boolean} opts.udp start a udp server? (default: true) - * @param {boolean} opts.ws start a websocket server? (default: true) - * @param {boolean} opts.stats enable web-based statistics? (default: true) - * @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents + * @param {Object} opts options object + * @param {Number} opts.interval tell clients to announce on this interval (ms) + * @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy + * @param {boolean|Object} opts.http start an http server?, or options for http.createServer (default: true) + * @param {boolean|Object} opts.udp start a udp server?, or extra options for dgram.createSocket (default: true) + * @param {boolean|Object} opts.ws start a websocket server?, or extra options for new WebSocketServer (default: true) + * @param {boolean} opts.stats enable web-based statistics? (default: true) + * @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents */ -function Server (opts) { - var self = this - if (!(self instanceof Server)) return new Server(opts) - EventEmitter.call(self) - if (!opts) opts = {} - - debug('new server %s', JSON.stringify(opts)) - - self.intervalMs = opts.interval - ? opts.interval - : 10 * 60 * 1000 // 10 min - - self._trustProxy = !!opts.trustProxy - if (typeof opts.filter === 'function') self._filter = opts.filter - - self.peersCacheLength = opts.peersCacheLength - self.peersCacheTtl = opts.peersCacheTtl - - self._listenCalled = false - self.listening = false - self.destroyed = false - self.torrents = {} - - self.http = null - self.udp4 = null - self.udp6 = null - self.ws = null - - // start an http tracker unless the user explictly says no - if (opts.http !== false) { - self.http = http.createServer() - self.http.on('error', function (err) { self._onError(err) }) - self.http.on('listening', onListening) - - // Add default http request handler on next tick to give user the chance to add - // their own handler first. Handle requests untouched by user's handler. - process.nextTick(function () { - self.http.on('request', function (req, res) { - if (res.headersSent) return - self.onHttpRequest(req, res) - }) - }) - } +class Server extends EventEmitter { + constructor (opts = {}) { + super() + debug('new server %s', JSON.stringify(opts)) - // start a udp tracker unless the user explicitly says no - if (opts.udp !== false) { - var isNode10 = /^v0.10./.test(process.version) - - self.udp4 = self.udp = dgram.createSocket( - isNode10 ? 'udp4' : { type: 'udp4', reuseAddr: true } - ) - self.udp4.on('message', function (msg, rinfo) { self.onUdpRequest(msg, rinfo) }) - self.udp4.on('error', function (err) { self._onError(err) }) - self.udp4.on('listening', onListening) - - self.udp6 = dgram.createSocket( - isNode10 ? 'udp6' : { type: 'udp6', reuseAddr: true } - ) - self.udp6.on('message', function (msg, rinfo) { self.onUdpRequest(msg, rinfo) }) - self.udp6.on('error', function (err) { self._onError(err) }) - self.udp6.on('listening', onListening) - } + this.intervalMs = opts.interval + ? opts.interval + : 10 * 60 * 1000 // 10 min + + this._trustProxy = !!opts.trustProxy + if (typeof opts.filter === 'function') this._filter = opts.filter + + this.peersCacheLength = opts.peersCacheLength + this.peersCacheTtl = opts.peersCacheTtl - // start a websocket tracker (for WebTorrent) unless the user explicitly says no - if (opts.ws !== false) { - if (!self.http) { - self.http = http.createServer() - self.http.on('error', function (err) { self._onError(err) }) - self.http.on('listening', onListening) + this._listenCalled = false + this.listening = false + this.destroyed = false + this.torrents = {} + + this.http = null + this.udp4 = null + this.udp6 = null + this.ws = null + + // start an http tracker unless the user explictly says no + if (opts.http !== false) { + this.http = http.createServer(isObject(opts.http) ? opts.http : undefined) + this.http.on('error', err => { this._onError(err) }) + this.http.on('listening', onListening) // Add default http request handler on next tick to give user the chance to add // their own handler first. Handle requests untouched by user's handler. - process.nextTick(function () { - self.http.on('request', function (req, res) { + process.nextTick(() => { + this.http.on('request', (req, res) => { if (res.headersSent) return - // For websocket trackers, we only need to handle the UPGRADE http method. - // Return 404 for all other request types. - res.statusCode = 404 - res.end('404 Not Found') + this.onHttpRequest(req, res) }) }) } - self.ws = new WebSocketServer({ server: self.http }) - self.ws.address = function () { - return self.http.address() + + // start a udp tracker unless the user explicitly says no + if (opts.udp !== false) { + this.udp4 = this.udp = dgram.createSocket({ + type: 'udp4', + reuseAddr: true, + ...(isObject(opts.udp) ? opts.udp : undefined) + }) + this.udp4.on('message', (msg, rinfo) => { this.onUdpRequest(msg, rinfo) }) + this.udp4.on('error', err => { this._onError(err) }) + this.udp4.on('listening', onListening) + + this.udp6 = dgram.createSocket({ + type: 'udp6', + reuseAddr: true, + ...(isObject(opts.udp) ? opts.udp : undefined) + }) + this.udp6.on('message', (msg, rinfo) => { this.onUdpRequest(msg, rinfo) }) + this.udp6.on('error', err => { this._onError(err) }) + this.udp6.on('listening', onListening) } - self.ws.on('error', function (err) { self._onError(err) }) - self.ws.on('connection', function (socket) { self.onWebSocketConnection(socket) }) - } - if (opts.stats !== false) { - if (!self.http) { - self.http = http.createServer() - self.http.on('error', function (err) { self._onError(err) }) - self.http.on('listening', onListening) + // start a websocket tracker (for WebTorrent) unless the user explicitly says no + if (opts.ws !== false) { + const noServer = isObject(opts.ws) && opts.ws.noServer + if (!this.http && !noServer) { + this.http = http.createServer() + this.http.on('error', err => { this._onError(err) }) + this.http.on('listening', onListening) + + // Add default http request handler on next tick to give user the chance to add + // their own handler first. Handle requests untouched by user's handler. + process.nextTick(() => { + this.http.on('request', (req, res) => { + if (res.headersSent) return + // For websocket trackers, we only need to handle the UPGRADE http method. + // Return 404 for all other request types. + res.statusCode = 404 + res.end('404 Not Found') + }) + }) + } + this.ws = new WebSocketServer({ + server: noServer ? undefined : this.http, + perMessageDeflate: false, + clientTracking: false, + ...(isObject(opts.ws) ? opts.ws : undefined) + }) + + this.ws.address = () => { + if (noServer) { + throw new Error('address() unavailable with { noServer: true }') + } + return this.http.address() + } + + this.ws.on('error', err => { this._onError(err) }) + this.ws.on('connection', (socket, req) => { + // Note: socket.upgradeReq was removed in ws@3.0.0, so re-add it. + // https://github.com/websockets/ws/pull/1099 + socket.upgradeReq = req + this.onWebSocketConnection(socket) + }) } - // Http handler for '/stats' route - self.http.on('request', function (req, res) { - if (res.headersSent) return + if (opts.stats !== false) { + if (!this.http) { + this.http = http.createServer() + this.http.on('error', err => { this._onError(err) }) + this.http.on('listening', onListening) + } + + // Http handler for '/stats' route + this.http.on('request', (req, res) => { + if (res.headersSent) return - var infoHashes = Object.keys(self.torrents) - var activeTorrents = 0 - var allPeers = {} + const infoHashes = Object.keys(this.torrents) + let activeTorrents = 0 + const allPeers = {} - function countPeers (filterFunction) { - var count = 0 - var key + function countPeers (filterFunction) { + let count = 0 + let key - for (key in allPeers) { - if (allPeers.hasOwnProperty(key) && filterFunction(allPeers[key])) { - count++ + for (key in allPeers) { + if (hasOwnProperty.call(allPeers, key) && filterFunction(allPeers[key])) { + count++ + } } - } - return count - } + return count + } - function groupByClient () { - var clients = {} - for (var key in allPeers) { - if (allPeers.hasOwnProperty(key)) { - var peer = allPeers[key] + function groupByClient () { + const clients = {} + for (const key in allPeers) { + if (hasOwnProperty.call(allPeers, key)) { + const peer = allPeers[key] - if (!clients[peer.client.client]) { - clients[peer.client.client] = {} - } - var client = clients[peer.client.client] - // If the client is not known show 8 chars from peerId as version - var version = peer.client.version || new Buffer(peer.peerId, 'hex').toString().substring(0, 8) - if (!client[version]) { - client[version] = 0 + if (!clients[peer.client.client]) { + clients[peer.client.client] = {} + } + const client = clients[peer.client.client] + // If the client is not known show 8 chars from peerId as version + const version = peer.client.version || Buffer.from(peer.peerId, 'hex').toString().substring(0, 8) + if (!client[version]) { + client[version] = 0 + } + client[version]++ } - client[version]++ } + return clients } - return clients - } - function printClients (clients) { - var html = '' - return html - } - if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) { - infoHashes.forEach(function (infoHash) { - var peers = self.torrents[infoHash].peers - var keys = peers.keys - if (keys.length > 0) activeTorrents++ - - keys.forEach(function (peerId) { - // Don't mark the peer as most recently used for stats - var peer = peers.peek(peerId) - if (peer == null) return // peers.peek() can evict the peer - - if (!allPeers.hasOwnProperty(peerId)) { - allPeers[peerId] = { - ipv4: false, - ipv6: false, - seeder: false, - leecher: false + if (req.method === 'GET' && (req.url === '/stats' || req.url === '/stats.json')) { + infoHashes.forEach(infoHash => { + const peers = this.torrents[infoHash].peers + const keys = peers.keys + if (keys.length > 0) activeTorrents++ + + keys.forEach(peerId => { + // Don't mark the peer as most recently used for stats + const peer = peers.peek(peerId) + if (peer == null) return // peers.peek() can evict the peer + + if (!hasOwnProperty.call(allPeers, peerId)) { + allPeers[peerId] = { + ipv4: false, + ipv6: false, + seeder: false, + leecher: false + } } - } - if (peer.ip.indexOf(':') >= 0) { - allPeers[peerId].ipv6 = true - } else { - allPeers[peerId].ipv4 = true - } + if (peer.ip.includes(':')) { + allPeers[peerId].ipv6 = true + } else { + allPeers[peerId].ipv4 = true + } - if (peer.complete) { - allPeers[peerId].seeder = true - } else { - allPeers[peerId].leecher = true - } + if (peer.complete) { + allPeers[peerId].seeder = true + } else { + allPeers[peerId].leecher = true + } - allPeers[peerId].peerId = peer.peerId - allPeers[peerId].client = peerid(peer.peerId) + allPeers[peerId].peerId = peer.peerId + allPeers[peerId].client = peerid(peer.peerId) + }) }) - }) - var isSeederOnly = function (peer) { return peer.seeder && peer.leecher === false } - var isLeecherOnly = function (peer) { return peer.leecher && peer.seeder === false } - var isSeederAndLeecher = function (peer) { return peer.seeder && peer.leecher } - var isIPv4 = function (peer) { return peer.ipv4 } - var isIPv6 = function (peer) { return peer.ipv6 } - - var stats = { - torrents: infoHashes.length, - activeTorrents: activeTorrents, - peersAll: Object.keys(allPeers).length, - peersSeederOnly: countPeers(isSeederOnly), - peersLeecherOnly: countPeers(isLeecherOnly), - peersSeederAndLeecher: countPeers(isSeederAndLeecher), - peersIPv4: countPeers(isIPv4), - peersIPv6: countPeers(isIPv6), - clients: groupByClient() - } + const isSeederOnly = peer => peer.seeder && peer.leecher === false + const isLeecherOnly = peer => peer.leecher && peer.seeder === false + const isSeederAndLeecher = peer => peer.seeder && peer.leecher + const isIPv4 = peer => peer.ipv4 + const isIPv6 = peer => peer.ipv6 + + const stats = { + torrents: infoHashes.length, + activeTorrents, + peersAll: Object.keys(allPeers).length, + peersSeederOnly: countPeers(isSeederOnly), + peersLeecherOnly: countPeers(isLeecherOnly), + peersSeederAndLeecher: countPeers(isSeederAndLeecher), + peersIPv4: countPeers(isIPv4), + peersIPv6: countPeers(isIPv6), + clients: groupByClient() + } - if (req.url === '/stats.json' || req.headers['accept'] === 'application/json') { - res.write(JSON.stringify(stats)) - res.end() - } else if (req.url === '/stats') { - res.end('

' + stats.torrents + ' torrents (' + stats.activeTorrents + ' active)

\n' + - '

Connected Peers: ' + stats.peersAll + '

\n' + - '

Peers Seeding Only: ' + stats.peersSeederOnly + '

\n' + - '

Peers Leeching Only: ' + stats.peersLeecherOnly + '

\n' + - '

Peers Seeding & Leeching: ' + stats.peersSeederAndLeecher + '

\n' + - '

IPv4 Peers: ' + stats.peersIPv4 + '

\n' + - '

IPv6 Peers: ' + stats.peersIPv6 + '

\n' + - '

Clients:

\n' + - printClients(stats.clients) - ) + if (req.url === '/stats.json' || req.headers.accept === 'application/json') { + res.setHeader('Content-Type', 'application/json') + res.end(JSON.stringify(stats)) + } else if (req.url === '/stats') { + res.setHeader('Content-Type', 'text/html') + res.end(` +

${stats.torrents} torrents (${stats.activeTorrents} active)

+

Connected Peers: ${stats.peersAll}

+

Peers Seeding Only: ${stats.peersSeederOnly}

+

Peers Leeching Only: ${stats.peersLeecherOnly}

+

Peers Seeding & Leeching: ${stats.peersSeederAndLeecher}

+

IPv4 Peers: ${stats.peersIPv4}

+

IPv6 Peers: ${stats.peersIPv6}

+

Clients:

+ ${printClients(stats.clients)} + `.replace(/^\s+/gm, '')) // trim left + } } + }) + } + + let num = !!this.http + !!this.udp4 + !!this.udp6 + const self = this + function onListening () { + num -= 1 + if (num === 0) { + self.listening = true + debug('listening') + self.emit('listening') } - }) + } } - var num = !!self.http + !!self.udp4 + !!self.udp6 - function onListening () { - num -= 1 - if (num === 0) { - self.listening = true - debug('listening') - self.emit('listening') - } + _onError (err) { + this.emit('error', err) } -} -Server.prototype._onError = function (err) { - var self = this - self.emit('error', err) -} + listen (...args) /* port, hostname, onlistening */{ + if (this._listenCalled || this.listening) throw new Error('server already listening') + this._listenCalled = true -Server.prototype.listen = function (/* port, hostname, onlistening */) { - var self = this + const lastArg = args[args.length - 1] + if (typeof lastArg === 'function') this.once('listening', lastArg) - if (self._listenCalled || self.listening) throw new Error('server already listening') - self._listenCalled = true + const port = toNumber(args[0]) || args[0] || 0 + const hostname = typeof args[1] !== 'function' ? args[1] : undefined - var lastArg = arguments[arguments.length - 1] - if (typeof lastArg === 'function') self.once('listening', lastArg) + debug('listen (port: %o hostname: %o)', port, hostname) - var port = toNumber(arguments[0]) || arguments[0] || 0 - var hostname = typeof arguments[1] !== 'function' ? arguments[1] : undefined + const httpPort = isObject(port) ? (port.http || 0) : port + const udpPort = isObject(port) ? (port.udp || 0) : port - debug('listen (port: %o hostname: %o)', port, hostname) + // binding to :: only receives IPv4 connections if the bindv6only sysctl is set 0, + // which is the default on many operating systems + const httpHostname = isObject(hostname) ? hostname.http : hostname + const udp4Hostname = isObject(hostname) ? hostname.udp : hostname + const udp6Hostname = isObject(hostname) ? hostname.udp6 : hostname - function isObject (obj) { - return typeof obj === 'object' && obj !== null + if (this.http) this.http.listen(httpPort, httpHostname) + if (this.udp4) this.udp4.bind(udpPort, udp4Hostname) + if (this.udp6) this.udp6.bind(udpPort, udp6Hostname) } - var httpPort = isObject(port) ? (port.http || 0) : port - var udpPort = isObject(port) ? (port.udp || 0) : port + close (cb = noop) { + debug('close') - // binding to :: only receives IPv4 connections if the bindv6only sysctl is set 0, - // which is the default on many operating systems - var httpHostname = isObject(hostname) ? hostname.http : hostname - var udp4Hostname = isObject(hostname) ? hostname.udp : hostname - var udp6Hostname = isObject(hostname) ? hostname.udp6 : hostname + this.listening = false + this.destroyed = true - if (self.http) self.http.listen(httpPort, httpHostname) - if (self.udp4) self.udp4.bind(udpPort, udp4Hostname) - if (self.udp6) self.udp6.bind(udpPort, udp6Hostname) -} + if (this.udp4) { + try { + this.udp4.close() + } catch (err) {} + } -Server.prototype.close = function (cb) { - var self = this - if (!cb) cb = noop - debug('close') + if (this.udp6) { + try { + this.udp6.close() + } catch (err) {} + } - self.listening = false - self.destroyed = true + if (this.ws) { + try { + this.ws.close() + } catch (err) {} + } - if (self.udp4) { - try { - self.udp4.close() - } catch (err) {} + if (this.http) this.http.close(cb) + else cb(null) } - if (self.udp6) { - try { - self.udp6.close() - } catch (err) {} + createSwarm (infoHash, cb) { + if (ArrayBuffer.isView(infoHash)) infoHash = infoHash.toString('hex') + + process.nextTick(() => { + const swarm = this.torrents[infoHash] = new Server.Swarm(infoHash, this) + cb(null, swarm) + }) } - if (self.ws) { - try { - self.ws.close() - } catch (err) {} + getSwarm (infoHash, cb) { + if (ArrayBuffer.isView(infoHash)) infoHash = infoHash.toString('hex') + + process.nextTick(() => { + cb(null, this.torrents[infoHash]) + }) } - if (self.http) self.http.close(cb) - else cb(null) -} + onHttpRequest (req, res, opts = {}) { + opts.trustProxy = opts.trustProxy || this._trustProxy -Server.prototype.createSwarm = function (infoHash, cb) { - var self = this - if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') + let params + try { + params = parseHttpRequest(req, opts) + params.httpReq = req + params.httpRes = res + } catch (err) { + res.end(bencode.encode({ + 'failure reason': err.message + })) - process.nextTick(function () { - var swarm = self.torrents[infoHash] = new Swarm(infoHash, self) - cb(null, swarm) - }) -} + // even though it's an error for the client, it's just a warning for the server. + // don't crash the server because a client sent bad data :) + this.emit('warning', err) + return + } -Server.prototype.getSwarm = function (infoHash, cb) { - var self = this - if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') + this._onRequest(params, (err, response) => { + if (err) { + this.emit('warning', err) + response = { + 'failure reason': err.message + } + } + if (this.destroyed) return res.end() - process.nextTick(function () { - cb(null, self.torrents[infoHash]) - }) -} + delete response.action // only needed for UDP encoding + res.end(bencode.encode(response)) -Server.prototype.onHttpRequest = function (req, res, opts) { - var self = this - if (!opts) opts = {} - opts.trustProxy = opts.trustProxy || self._trustProxy - - var params - try { - params = parseHttpRequest(req, opts) - params.httpReq = req - params.httpRes = res - } catch (err) { - res.end(bencode.encode({ - 'failure reason': err.message - })) - - // even though it's an error for the client, it's just a warning for the server. - // don't crash the server because a client sent bad data :) - self.emit('warning', err) - return + if (params.action === common.ACTIONS.ANNOUNCE) { + this.emit(common.EVENT_NAMES[params.event], params.addr, params) + } + }) } - self._onRequest(params, function (err, response) { - if (err) { - self.emit('warning', err) - response = { - 'failure reason': err.message - } + onUdpRequest (msg, rinfo) { + let params + try { + params = parseUdpRequest(msg, rinfo) + } catch (err) { + this.emit('warning', err) + // Do not reply for parsing errors + return } - if (self.destroyed) return res.end() - delete response.action // only needed for UDP encoding - res.end(bencode.encode(response)) + this._onRequest(params, (err, response) => { + if (err) { + this.emit('warning', err) + response = { + action: common.ACTIONS.ERROR, + 'failure reason': err.message + } + } + if (this.destroyed) return - if (params.action === common.ACTIONS.ANNOUNCE) { - self.emit(common.EVENT_NAMES[params.event], params.addr, params) - } - }) -} + response.transactionId = params.transactionId + response.connectionId = params.connectionId -Server.prototype.onUdpRequest = function (msg, rinfo) { - var self = this + const buf = makeUdpPacket(response) - var params - try { - params = parseUdpRequest(msg, rinfo) - } catch (err) { - self.emit('warning', err) - // Do not reply for parsing errors - return - } + try { + const udp = (rinfo.family === 'IPv4') ? this.udp4 : this.udp6 + udp.send(buf, 0, buf.length, rinfo.port, rinfo.address) + } catch (err) { + this.emit('warning', err) + } - self._onRequest(params, function (err, response) { - if (err) { - self.emit('warning', err) - response = { - action: common.ACTIONS.ERROR, - 'failure reason': err.message + if (params.action === common.ACTIONS.ANNOUNCE) { + this.emit(common.EVENT_NAMES[params.event], params.addr, params) } + }) + } + + onWebSocketConnection (socket, opts = {}) { + opts.trustProxy = opts.trustProxy || this._trustProxy + + socket.peerId = null // as hex + socket.infoHashes = [] // swarms that this socket is participating in + socket.onSend = err => { + this._onWebSocketSend(socket, err) } - if (self.destroyed) return - response.transactionId = params.transactionId - response.connectionId = params.connectionId + socket.onMessageBound = params => { + this._onWebSocketRequest(socket, opts, params) + } + socket.on('message', socket.onMessageBound) - var buf = makeUdpPacket(response) + socket.onErrorBound = err => { + this._onWebSocketError(socket, err) + } + socket.on('error', socket.onErrorBound) + + socket.onCloseBound = () => { + this._onWebSocketClose(socket) + } + socket.on('close', socket.onCloseBound) + } + _onWebSocketRequest (socket, opts, params) { try { - var udp = (rinfo.family === 'IPv4') ? self.udp4 : self.udp6 - udp.send(buf, 0, buf.length, rinfo.port, rinfo.address) + params = parseWebSocketRequest(socket, opts, params) } catch (err) { - self.emit('warning', err) - } + socket.send(JSON.stringify({ + 'failure reason': err.message + }), socket.onSend) - if (params.action === common.ACTIONS.ANNOUNCE) { - self.emit(common.EVENT_NAMES[params.event], params.addr, params) + // even though it's an error for the client, it's just a warning for the server. + // don't crash the server because a client sent bad data :) + this.emit('warning', err) + return } - }) -} -Server.prototype.onWebSocketConnection = function (socket, opts) { - var self = this - if (!opts) opts = {} - opts.trustProxy = opts.trustProxy || self._trustProxy + if (!socket.peerId) socket.peerId = params.peer_id // as hex - socket.peerId = null // as hex - socket.infoHashes = [] // swarms that this socket is participating in - socket.onSend = function (err) { - self._onWebSocketSend(socket, err) - } + this._onRequest(params, (err, response) => { + if (this.destroyed || socket.destroyed) return + if (err) { + socket.send(JSON.stringify({ + action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape', + 'failure reason': err.message, + info_hash: hex2bin(params.info_hash) + }), socket.onSend) - socket.onMessageBound = function (params) { - self._onWebSocketRequest(socket, opts, params) - } - socket.on('message', socket.onMessageBound) + this.emit('warning', err) + return + } - socket.onErrorBound = function (err) { - self._onWebSocketError(socket, err) - } - socket.on('error', socket.onErrorBound) + response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape' - socket.onCloseBound = function () { - self._onWebSocketClose(socket) - } - socket.on('close', socket.onCloseBound) -} + let peers + if (response.action === 'announce') { + peers = response.peers + delete response.peers -Server.prototype._onWebSocketRequest = function (socket, opts, params) { - var self = this + if (!socket.infoHashes.includes(params.info_hash)) { + socket.infoHashes.push(params.info_hash) + } - try { - params = parseWebSocketRequest(socket, opts, params) - } catch (err) { - socket.send(JSON.stringify({ - 'failure reason': err.message - }), socket.onSend) + response.info_hash = hex2bin(params.info_hash) - // even though it's an error for the client, it's just a warning for the server. - // don't crash the server because a client sent bad data :) - self.emit('warning', err) - return - } + // WebSocket tracker should have a shorter interval – default: 2 minutes + response.interval = Math.ceil(this.intervalMs / 1000 / 5) + } - if (!socket.peerId) socket.peerId = params.peer_id // as hex + // Skip sending update back for 'answer' announce messages – not needed + if (!params.answer) { + socket.send(JSON.stringify(response), socket.onSend) + debug('sent response %s to %s', JSON.stringify(response), params.peer_id) + } - self._onRequest(params, function (err, response) { - if (self.destroyed) return - if (err) { - socket.send(JSON.stringify({ - action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape', - 'failure reason': err.message, - info_hash: common.hexToBinary(params.info_hash) - }), socket.onSend) + if (Array.isArray(params.offers)) { + debug('got %s offers from %s', params.offers.length, params.peer_id) + debug('got %s peers from swarm %s', peers.length, params.info_hash) + peers.forEach((peer, i) => { + peer.socket.send(JSON.stringify({ + action: 'announce', + offer: params.offers[i].offer, + offer_id: params.offers[i].offer_id, + peer_id: hex2bin(params.peer_id), + info_hash: hex2bin(params.info_hash) + }), peer.socket.onSend) + debug('sent offer to %s from %s', peer.peerId, params.peer_id) + }) + } - self.emit('warning', err) - return - } + const done = () => { + // emit event once the announce is fully "processed" + if (params.action === common.ACTIONS.ANNOUNCE) { + this.emit(common.EVENT_NAMES[params.event], params.peer_id, params) + } + } - response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape' + if (params.answer) { + debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id) - var peers - if (response.action === 'announce') { - peers = response.peers - delete response.peers + this.getSwarm(params.info_hash, (err, swarm) => { + if (this.destroyed) return + if (err) return this.emit('warning', err) + if (!swarm) { + return this.emit('warning', new Error('no swarm with that `info_hash`')) + } + // Mark the destination peer as recently used in cache + const toPeer = swarm.peers.get(params.to_peer_id) + if (!toPeer) { + return this.emit('warning', new Error('no peer with that `to_peer_id`')) + } - if (socket.infoHashes.indexOf(params.info_hash) === -1) { - socket.infoHashes.push(params.info_hash) + toPeer.socket.send(JSON.stringify({ + action: 'announce', + answer: params.answer, + offer_id: params.offer_id, + peer_id: hex2bin(params.peer_id), + info_hash: hex2bin(params.info_hash) + }), toPeer.socket.onSend) + debug('sent answer to %s from %s', toPeer.peerId, params.peer_id) + + done() + }) + } else { + done() } + }) + } - response.info_hash = common.hexToBinary(params.info_hash) + _onWebSocketSend (socket, err) { + if (err) this._onWebSocketError(socket, err) + } - // WebSocket tracker should have a shorter interval – default: 2 minutes - response.interval = Math.ceil(self.intervalMs / 1000 / 5) - } + _onWebSocketClose (socket) { + debug('websocket close %s', socket.peerId) + socket.destroyed = true - socket.send(JSON.stringify(response), socket.onSend) - debug('sent response %s to %s', JSON.stringify(response), params.peer_id) - - if (Array.isArray(params.offers)) { - debug('got %s offers from %s', params.offers.length, params.peer_id) - debug('got %s peers from swarm %s', peers.length, params.info_hash) - peers.forEach(function (peer, i) { - peer.socket.send(JSON.stringify({ - action: 'announce', - offer: params.offers[i].offer, - offer_id: params.offers[i].offer_id, - peer_id: common.hexToBinary(params.peer_id), - info_hash: common.hexToBinary(params.info_hash) - }), peer.socket.onSend) - debug('sent offer to %s from %s', peer.peerId, params.peer_id) + if (socket.peerId) { + socket.infoHashes.slice(0).forEach(infoHash => { + const swarm = this.torrents[infoHash] + if (swarm) { + swarm.announce({ + type: 'ws', + event: 'stopped', + numwant: 0, + peer_id: socket.peerId + }) + } }) } - if (params.answer) { - debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id) - - self.getSwarm(params.info_hash, function (err, swarm) { - if (err) return self.emit('warning', err) - if (!swarm) { - return self.emit('warning', new Error('no swarm with that `info_hash`')) - } - // Mark the destination peer as recently used in cache - var toPeer = swarm.peers.get(params.to_peer_id) - if (!toPeer) { - return self.emit('warning', new Error('no peer with that `to_peer_id`')) - } + // ignore all future errors + socket.onSend = noop + socket.on('error', noop) - toPeer.socket.send(JSON.stringify({ - action: 'announce', - answer: params.answer, - offer_id: params.offer_id, - peer_id: common.hexToBinary(params.peer_id), - info_hash: common.hexToBinary(params.info_hash) - }), toPeer.socket.onSend) - debug('sent answer to %s from %s', toPeer.peerId, params.peer_id) + socket.peerId = null + socket.infoHashes = null - done() - }) - } else { - done() + if (typeof socket.onMessageBound === 'function') { + socket.removeListener('message', socket.onMessageBound) } + socket.onMessageBound = null - function done () { - // emit event once the announce is fully "processed" - if (params.action === common.ACTIONS.ANNOUNCE) { - self.emit(common.EVENT_NAMES[params.event], params.peer_id, params) - } + if (typeof socket.onErrorBound === 'function') { + socket.removeListener('error', socket.onErrorBound) } - }) -} + socket.onErrorBound = null -Server.prototype._onRequest = function (params, cb) { - var self = this - if (params && params.action === common.ACTIONS.CONNECT) { - cb(null, { action: common.ACTIONS.CONNECT }) - } else if (params && params.action === common.ACTIONS.ANNOUNCE) { - self._onAnnounce(params, cb) - } else if (params && params.action === common.ACTIONS.SCRAPE) { - self._onScrape(params, cb) - } else { - cb(new Error('Invalid action')) + if (typeof socket.onCloseBound === 'function') { + socket.removeListener('close', socket.onCloseBound) + } + socket.onCloseBound = null } -} -Server.prototype._onAnnounce = function (params, cb) { - var self = this + _onWebSocketError (socket, err) { + debug('websocket error %s', err.message || err) + this.emit('warning', err) + this._onWebSocketClose(socket) + } - self.getSwarm(params.info_hash, function (err, swarm) { - if (err) return cb(err) - if (swarm) { - announce(swarm) + _onRequest (params, cb) { + if (params && params.action === common.ACTIONS.CONNECT) { + cb(null, { action: common.ACTIONS.CONNECT }) + } else if (params && params.action === common.ACTIONS.ANNOUNCE) { + this._onAnnounce(params, cb) + } else if (params && params.action === common.ACTIONS.SCRAPE) { + this._onScrape(params, cb) } else { - createSwarm() + cb(new Error('Invalid action')) } - }) - - function createSwarm () { - if (self._filter) { - self._filter(params.info_hash, params, function (allowed) { - if (allowed instanceof Error) { - cb(allowed) - } else if (!allowed) { - cb(new Error('disallowed info_hash')) - } else { - self.createSwarm(params.info_hash, function (err, swarm) { - if (err) return cb(err) - announce(swarm) - }) - } + } + + _onAnnounce (params, cb) { + const self = this + + if (this._filter) { + this._filter(params.info_hash, params, err => { + // Presence of `err` means that this announce request is disallowed + if (err) return cb(err) + + getOrCreateSwarm((err, swarm) => { + if (err) return cb(err) + announce(swarm) + }) }) } else { - self.createSwarm(params.info_hash, function (err, swarm) { + getOrCreateSwarm((err, swarm) => { if (err) return cb(err) announce(swarm) }) } - } - function announce (swarm) { - if (!params.event || params.event === 'empty') params.event = 'update' - swarm.announce(params, function (err, response) { - if (err) return cb(err) + // Get existing swarm, or create one if one does not exist + function getOrCreateSwarm (cb) { + self.getSwarm(params.info_hash, (err, swarm) => { + if (err) return cb(err) + if (swarm) return cb(null, swarm) + self.createSwarm(params.info_hash, (err, swarm) => { + if (err) return cb(err) + cb(null, swarm) + }) + }) + } - if (!response.action) response.action = common.ACTIONS.ANNOUNCE - if (!response.interval) response.interval = Math.ceil(self.intervalMs / 1000) - - if (params.compact === 1) { - var peers = response.peers - - // Find IPv4 peers - response.peers = string2compact(peers.filter(function (peer) { - return common.IPV4_RE.test(peer.ip) - }).map(function (peer) { - return peer.ip + ':' + peer.port - })) - // Find IPv6 peers - response.peers6 = string2compact(peers.filter(function (peer) { - return common.IPV6_RE.test(peer.ip) - }).map(function (peer) { - return '[' + peer.ip + ']:' + peer.port - })) - } else if (params.compact === 0) { - // IPv6 peers are not separate for non-compact responses - response.peers = response.peers.map(function (peer) { - return { - 'peer id': common.hexToBinary(peer.peerId), + function announce (swarm) { + if (!params.event || params.event === 'empty') params.event = 'update' + swarm.announce(params, (err, response) => { + if (err) return cb(err) + + if (!response.action) response.action = common.ACTIONS.ANNOUNCE + if (!response.interval) response.interval = Math.ceil(self.intervalMs / 1000) + + if (params.compact === 1) { + const peers = response.peers + + // Find IPv4 peers + response.peers = string2compact(peers.filter(peer => common.IPV4_RE.test(peer.ip)).map(peer => `${peer.ip}:${peer.port}`)) + // Find IPv6 peers + response.peers6 = string2compact(peers.filter(peer => common.IPV6_RE.test(peer.ip)).map(peer => `[${peer.ip}]:${peer.port}`)) + } else if (params.compact === 0) { + // IPv6 peers are not separate for non-compact responses + response.peers = response.peers.map(peer => ({ + 'peer id': hex2bin(peer.peerId), ip: peer.ip, port: peer.port - } - }) - } // else, return full peer objects (used for websocket responses) + })) + } // else, return full peer objects (used for websocket responses) - cb(null, response) - }) + cb(null, response) + }) + } } -} -Server.prototype._onScrape = function (params, cb) { - var self = this - - if (params.info_hash == null) { - // if info_hash param is omitted, stats for all torrents are returned - // TODO: make this configurable! - params.info_hash = Object.keys(self.torrents) - } + _onScrape (params, cb) { + if (params.info_hash == null) { + // if info_hash param is omitted, stats for all torrents are returned + // TODO: make this configurable! + params.info_hash = Object.keys(this.torrents) + } - series(params.info_hash.map(function (infoHash) { - return function (cb) { - self.getSwarm(infoHash, function (err, swarm) { + series(params.info_hash.map(infoHash => cb => { + this.getSwarm(infoHash, (err, swarm) => { if (err) return cb(err) if (swarm) { - swarm.scrape(params, function (err, scrapeInfo) { + swarm.scrape(params, (err, scrapeInfo) => { if (err) return cb(err) cb(null, { - infoHash: infoHash, + infoHash, complete: (scrapeInfo && scrapeInfo.complete) || 0, incomplete: (scrapeInfo && scrapeInfo.incomplete) || 0 }) }) } else { - cb(null, { infoHash: infoHash, complete: 0, incomplete: 0 }) + cb(null, { infoHash, complete: 0, incomplete: 0 }) } }) - } - }), function (err, results) { - if (err) return cb(err) - - var response = { - action: common.ACTIONS.SCRAPE, - files: {}, - flags: { min_request_interval: Math.ceil(self.intervalMs / 1000) } - } + }), (err, results) => { + if (err) return cb(err) - results.forEach(function (result) { - response.files[common.hexToBinary(result.infoHash)] = { - complete: result.complete || 0, - incomplete: result.incomplete || 0, - downloaded: result.complete || 0 // TODO: this only provides a lower-bound + const response = { + action: common.ACTIONS.SCRAPE, + files: {}, + flags: { min_request_interval: Math.ceil(this.intervalMs / 1000) } } - }) - cb(null, response) - }) + results.forEach(result => { + response.files[hex2bin(result.infoHash)] = { + complete: result.complete || 0, + incomplete: result.incomplete || 0, + downloaded: result.complete || 0 // TODO: this only provides a lower-bound + } + }) + + cb(null, response) + }) + } } +Server.Swarm = Swarm + function makeUdpPacket (params) { - var packet + let packet switch (params.action) { - case common.ACTIONS.CONNECT: + case common.ACTIONS.CONNECT: { packet = Buffer.concat([ common.toUInt32(common.ACTIONS.CONNECT), common.toUInt32(params.transactionId), params.connectionId ]) break - case common.ACTIONS.ANNOUNCE: + } + case common.ACTIONS.ANNOUNCE: { packet = Buffer.concat([ common.toUInt32(common.ACTIONS.ANNOUNCE), common.toUInt32(params.transactionId), @@ -726,13 +765,14 @@ function makeUdpPacket (params) { params.peers ]) break - case common.ACTIONS.SCRAPE: - var scrapeResponse = [ + } + case common.ACTIONS.SCRAPE: { + const scrapeResponse = [ common.toUInt32(common.ACTIONS.SCRAPE), common.toUInt32(params.transactionId) ] - for (var infoHash in params.files) { - var file = params.files[infoHash] + for (const infoHash in params.files) { + const file = params.files[infoHash] scrapeResponse.push( common.toUInt32(file.complete), common.toUInt32(file.downloaded), // TODO: this only provides a lower-bound @@ -741,70 +781,23 @@ function makeUdpPacket (params) { } packet = Buffer.concat(scrapeResponse) break - case common.ACTIONS.ERROR: + } + case common.ACTIONS.ERROR: { packet = Buffer.concat([ common.toUInt32(common.ACTIONS.ERROR), common.toUInt32(params.transactionId || 0), Buffer.from(String(params['failure reason'])) ]) break + } default: - throw new Error('Action not implemented: ' + params.action) + throw new Error(`Action not implemented: ${params.action}`) } return packet } -Server.prototype._onWebSocketSend = function (socket, err) { - var self = this - if (err) self._onWebSocketError(socket, err) -} - -Server.prototype._onWebSocketClose = function (socket) { - var self = this - debug('websocket close %s', socket.peerId) - - if (socket.peerId) { - socket.infoHashes.forEach(function (infoHash) { - var swarm = self.torrents[infoHash] - if (swarm) { - swarm.announce({ - type: 'ws', - event: 'stopped', - numwant: 0, - peer_id: socket.peerId - }, noop) - } - }) - } - - // ignore all future errors - socket.onSend = noop - socket.on('error', noop) - - socket.peerId = null - socket.infoHashes = null - - if (typeof socket.onMessageBound === 'function') { - socket.removeListener('message', socket.onMessageBound) - } - socket.onMessageBound = null - - if (typeof socket.onErrorBound === 'function') { - socket.removeListener('error', socket.onErrorBound) - } - socket.onErrorBound = null - - if (typeof socket.onCloseBound === 'function') { - socket.removeListener('close', socket.onCloseBound) - } - socket.onCloseBound = null -} - -Server.prototype._onWebSocketError = function (socket, err) { - var self = this - debug('websocket error %s', err.message || err) - self.emit('warning', err) - self._onWebSocketClose(socket) +function isObject (obj) { + return typeof obj === 'object' && obj !== null } function toNumber (x) { @@ -813,3 +806,5 @@ function toNumber (x) { } function noop () {} + +export default Server diff --git a/test/client-large-torrent.js b/test/client-large-torrent.js index 7f9f2497e..db0d0fbf9 100644 --- a/test/client-large-torrent.js +++ b/test/client-large-torrent.js @@ -1,42 +1,41 @@ -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +import Client from '../index.js' +import common from './common.js' +import fixtures from 'webtorrent-fixtures' +import test from 'tape' -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testLargeTorrent (t, serverType) { t.plan(9) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.sintel.parsedTorrent.infoHash, - peerId: peerId, + peerId, port: 6881, announce: announceUrl, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.update() - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.stop() - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') @@ -51,14 +50,14 @@ function testLargeTorrent (t, serverType) { }) } -test('http: large torrent: client.start()', function (t) { +test('http: large torrent: client.start()', t => { testLargeTorrent(t, 'http') }) -test('udp: large torrent: client.start()', function (t) { +test('udp: large torrent: client.start()', t => { testLargeTorrent(t, 'udp') }) -test('ws: large torrent: client.start()', function (t) { +test('ws: large torrent: client.start()', t => { testLargeTorrent(t, 'ws') }) diff --git a/test/client-magnet.js b/test/client-magnet.js index ba1949c94..a6268b3fc 100644 --- a/test/client-magnet.js +++ b/test/client-magnet.js @@ -1,45 +1,44 @@ -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var magnet = require('magnet-uri') -var test = require('tape') +import Client from '../index.js' +import common from './common.js' +import fixtures from 'webtorrent-fixtures' +import magnet from 'magnet-uri' +import test from 'tape' -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testMagnet (t, serverType) { t.plan(9) - var parsedTorrent = magnet(fixtures.leaves.magnetURI) + const parsedTorrent = magnet(fixtures.leaves.magnetURI) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.update() - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.stop() - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') @@ -54,14 +53,14 @@ function testMagnet (t, serverType) { }) } -test('http: magnet: client.start/update/stop()', function (t) { +test('http: magnet: client.start/update/stop()', t => { testMagnet(t, 'http') }) -test('udp: magnet: client.start/update/stop()', function (t) { +test('udp: magnet: client.start/update/stop()', t => { testMagnet(t, 'udp') }) -test('ws: magnet: client.start/update/stop()', function (t) { +test('ws: magnet: client.start/update/stop()', t => { testMagnet(t, 'ws') }) diff --git a/test/client-ws-socket-pool.js b/test/client-ws-socket-pool.js index 94f6b2dd3..6d3fe1a3f 100644 --- a/test/client-ws-socket-pool.js +++ b/test/client-ws-socket-pool.js @@ -1,52 +1,51 @@ -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +import Client from '../index.js' +import common from './common.js' +import fixtures from 'webtorrent-fixtures' +import test from 'tape' -var peerId = Buffer.from('01234567890123456789') -var port = 6681 +const peerId = Buffer.from('01234567890123456789') +const port = 6681 -test('ensure client.destroy() callback is called with re-used websockets in socketPool', function (t) { +test('ensure client.destroy() callback is called with re-used websockets in socketPool', t => { t.plan(4) - common.createServer(t, 'ws', function (server, announceUrl) { - var client1 = new Client({ + common.createServer(t, 'ws', (server, announceUrl) => { + const client1 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, - port: port, + peerId, + port, wrtc: {} }) common.mockWebsocketTracker(client1) - client1.on('error', function (err) { t.error(err) }) - client1.on('warning', function (err) { t.error(err) }) + client1.on('error', err => { t.error(err) }) + client1.on('warning', err => { t.error(err) }) client1.start() - client1.once('update', function () { + client1.once('update', () => { t.pass('got client1 update') // second ws client using same announce url will re-use the same websocket - var client2 = new Client({ + const client2 = new Client({ infoHash: fixtures.alice.parsedTorrent.infoHash, // different info hash announce: announceUrl, - peerId: peerId, - port: port, + peerId, + port, wrtc: {} }) common.mockWebsocketTracker(client2) - client2.on('error', function (err) { t.error(err) }) - client2.on('warning', function (err) { t.error(err) }) + client2.on('error', err => { t.error(err) }) + client2.on('warning', err => { t.error(err) }) client2.start() - client2.once('update', function () { + client2.once('update', () => { t.pass('got client2 update') - client1.destroy(function (err) { + client1.destroy(err => { t.error(err, 'got client1 destroy callback') - client2.destroy(function (err) { + client2.destroy(err => { t.error(err, 'got client2 destroy callback') server.close() }) diff --git a/test/client.js b/test/client.js index 9f1c397c8..d81ee55f0 100644 --- a/test/client.js +++ b/test/client.js @@ -1,38 +1,40 @@ -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') - -var peerId1 = Buffer.from('01234567890123456789') -var peerId2 = Buffer.from('12345678901234567890') -var peerId3 = Buffer.from('23456789012345678901') -var port = 6881 +import Client from '../index.js' +import common from './common.js' +import http from 'http' +import fixtures from 'webtorrent-fixtures' +import net from 'net' +import test from 'tape' +import undici from 'undici' + +const peerId1 = Buffer.from('01234567890123456789') +const peerId2 = Buffer.from('12345678901234567890') +const peerId3 = Buffer.from('23456789012345678901') +const port = 6881 function testClientStart (t, serverType) { t.plan(4) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, - port: port, + port, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.stop() - client.once('update', function () { + client.once('update', () => { t.pass('got response to stop') server.close() client.destroy() @@ -43,40 +45,42 @@ function testClientStart (t, serverType) { }) } -test('http: client.start()', function (t) { +test('http: client.start()', t => { testClientStart(t, 'http') }) -test('udp: client.start()', function (t) { +test('udp: client.start()', t => { testClientStart(t, 'udp') }) -test('ws: client.start()', function (t) { +test('ws: client.start()', t => { testClientStart(t, 'ws') }) function testClientStop (t, serverType) { - t.plan(3) + t.plan(4) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, - port: port, + port, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) client.start() - setTimeout(function () { + client.once('update', () => { + t.pass('client received response to "start" message') + client.stop() - client.once('update', function (data) { + client.once('update', data => { // receive one final update after calling stop t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') @@ -85,54 +89,108 @@ function testClientStop (t, serverType) { server.close() client.destroy() }) - }, 1000) + }) }) } -test('http: client.stop()', function (t) { +test('http: client.stop()', t => { testClientStop(t, 'http') }) -test('udp: client.stop()', function (t) { +test('udp: client.stop()', t => { testClientStop(t, 'udp') }) -test('ws: client.stop()', function (t) { +test('ws: client.stop()', t => { testClientStop(t, 'ws') }) +function testClientStopDestroy (t, serverType) { + t.plan(2) + + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ + infoHash: fixtures.leaves.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId1, + port, + wrtc: {} + }) + + if (serverType === 'ws') common.mockWebsocketTracker(client) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) + + client.start() + + client.once('update', () => { + t.pass('client received response to "start" message') + + client.stop() + + client.on('update', () => { t.fail('client should not receive update after destroy is called') }) + + // Call destroy() in the same tick as stop(), but the message should still + // be received by the server, though obviously the client won't receive the + // response. + client.destroy() + + server.once('stop', (peer, params) => { + t.pass('server received "stop" message') + setTimeout(() => { + // give the websocket server time to finish in progress (stream) messages + // to peers + server.close() + }, 100) + }) + }) + }) +} + +test('http: client.stop(); client.destroy()', t => { + testClientStopDestroy(t, 'http') +}) + +test('udp: client.stop(); client.destroy()', t => { + testClientStopDestroy(t, 'udp') +}) + +test('ws: client.stop(); client.destroy()', t => { + testClientStopDestroy(t, 'ws') +}) + function testClientUpdate (t, serverType) { t.plan(4) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, - port: port, + port, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) - client.setInterval(2000) + client.setInterval(500) client.start() - client.once('update', function () { - client.setInterval(2000) + client.once('update', () => { + client.setInterval(500) - // after interval (2s), we should get another update - client.once('update', function (data) { + // after interval, we should get another update + client.once('update', data => { // received an update! t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.stop() - client.once('update', function () { + client.once('update', () => { t.pass('got response to stop') server.close() client.destroy() @@ -142,35 +200,35 @@ function testClientUpdate (t, serverType) { }) } -test('http: client.update()', function (t) { +test('http: client.update()', t => { testClientUpdate(t, 'http') }) -test('udp: client.update()', function (t) { +test('udp: client.update()', t => { testClientUpdate(t, 'udp') }) -test('ws: client.update()', function (t) { +test('ws: client.update()', t => { testClientUpdate(t, 'ws') }) function testClientScrape (t, serverType) { t.plan(4) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, - port: port, + port, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) - client.once('scrape', function (data) { + client.once('scrape', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') @@ -184,46 +242,46 @@ function testClientScrape (t, serverType) { }) } -test('http: client.scrape()', function (t) { +test('http: client.scrape()', t => { testClientScrape(t, 'http') }) -test('udp: client.scrape()', function (t) { +test('udp: client.scrape()', t => { testClientScrape(t, 'udp') }) -test('ws: client.scrape()', function (t) { +test('ws: client.scrape()', t => { testClientScrape(t, 'ws') }) function testClientAnnounceWithParams (t, serverType) { t.plan(5) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, - port: port, + port, wrtc: {} }) - server.on('start', function (peer, params) { + server.on('start', (peer, params) => { t.equal(params.testParam, 'this is a test') }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.stop() - client.once('update', function () { + client.once('update', () => { t.pass('got response to stop') server.close() client.destroy() @@ -236,24 +294,24 @@ function testClientAnnounceWithParams (t, serverType) { }) } -test('http: client.announce() with params', function (t) { +test('http: client.announce() with params', t => { testClientAnnounceWithParams(t, 'http') }) -test('ws: client.announce() with params', function (t) { +test('ws: client.announce() with params', t => { testClientAnnounceWithParams(t, 'ws') }) function testClientGetAnnounceOpts (t, serverType) { t.plan(5) - common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, - port: port, - getAnnounceOpts: function () { + port, + getAnnounceOpts () { return { testParam: 'this is a test' } @@ -261,22 +319,22 @@ function testClientGetAnnounceOpts (t, serverType) { wrtc: {} }) - server.on('start', function (peer, params) { + server.on('start', (peer, params) => { t.equal(params.testParam, 'this is a test') }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) - client.once('update', function (data) { + client.once('update', data => { t.equal(data.announce, announceUrl) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') client.stop() - client.once('update', function () { + client.once('update', () => { t.pass('got response to stop') server.close() client.destroy() @@ -287,33 +345,33 @@ function testClientGetAnnounceOpts (t, serverType) { }) } -test('http: client `opts.getAnnounceOpts`', function (t) { +test('http: client `opts.getAnnounceOpts`', t => { testClientGetAnnounceOpts(t, 'http') }) -test('ws: client `opts.getAnnounceOpts`', function (t) { +test('ws: client `opts.getAnnounceOpts`', t => { testClientGetAnnounceOpts(t, 'ws') }) function testClientAnnounceWithNumWant (t, serverType) { t.plan(4) - common.createServer(t, serverType, function (server, announceUrl) { - var client1 = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client1 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, - announce: [ announceUrl ], + announce: [announceUrl], peerId: peerId1, - port: port, + port, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client1) - client1.on('error', function (err) { t.error(err) }) - client1.on('warning', function (err) { t.error(err) }) + client1.on('error', err => { t.error(err) }) + client1.on('warning', err => { t.error(err) }) client1.start() - client1.once('update', function () { - var client2 = new Client({ + client1.once('update', () => { + const client2 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId2, @@ -322,12 +380,12 @@ function testClientAnnounceWithNumWant (t, serverType) { }) if (serverType === 'ws') common.mockWebsocketTracker(client2) - client2.on('error', function (err) { t.error(err) }) - client2.on('warning', function (err) { t.error(err) }) + client2.on('error', err => { t.error(err) }) + client2.on('warning', err => { t.error(err) }) client2.start() - client2.once('update', function () { - var client3 = new Client({ + client2.once('update', () => { + const client3 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId3, @@ -336,33 +394,33 @@ function testClientAnnounceWithNumWant (t, serverType) { }) if (serverType === 'ws') common.mockWebsocketTracker(client3) - client3.on('error', function (err) { t.error(err) }) - client3.on('warning', function (err) { t.error(err) }) + client3.on('error', err => { t.error(err) }) + client3.on('warning', err => { t.error(err) }) client3.start({ numwant: 1 }) - client3.on('peer', function () { + client3.on('peer', () => { t.pass('got one peer (this should only fire once)') - var num = 3 + let num = 3 function tryCloseServer () { num -= 1 if (num === 0) server.close() } client1.stop() - client1.once('update', function () { + client1.once('update', () => { t.pass('got response to stop (client1)') client1.destroy() tryCloseServer() }) client2.stop() - client2.once('update', function () { + client2.once('update', () => { t.pass('got response to stop (client2)') client2.destroy() tryCloseServer() }) client3.stop() - client3.once('update', function () { + client3.once('update', () => { t.pass('got response to stop (client3)') client3.destroy() tryCloseServer() @@ -373,10 +431,210 @@ function testClientAnnounceWithNumWant (t, serverType) { }) } -test('http: client announce with numwant', function (t) { +test('http: client announce with numwant', t => { testClientAnnounceWithNumWant(t, 'http') }) -test('udp: client announce with numwant', function (t) { +test('udp: client announce with numwant', t => { testClientAnnounceWithNumWant(t, 'udp') }) + +test('http: userAgent', t => { + t.plan(2) + + common.createServer(t, 'http', (server, announceUrl) => { + // Confirm that user-agent header is set + server.http.on('request', (req, res) => { + t.ok(req.headers['user-agent'].includes('WebTorrent')) + }) + + const client = new Client({ + infoHash: fixtures.leaves.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId1, + port, + userAgent: 'WebTorrent/0.98.0 (https://webtorrent.io)', + wrtc: {} + }) + + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) + + client.once('update', data => { + t.equal(data.announce, announceUrl) + + server.close() + client.destroy() + }) + + client.start() + }) +}) + +function testSupportedTracker (t, serverType) { + t.plan(1) + + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ + infoHash: fixtures.leaves.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId1, + port, + wrtc: {} + }) + + if (serverType === 'ws') common.mockWebsocketTracker(client) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) + + client.start() + + client.once('update', data => { + t.pass('tracker is valid') + + server.close() + client.destroy() + }) + }) +} + +test('http: valid tracker port', t => { + testSupportedTracker(t, 'http') +}) + +test('udp: valid tracker port', t => { + testSupportedTracker(t, 'udp') +}) + +test('ws: valid tracker port', t => { + testSupportedTracker(t, 'ws') +}) + +function testUnsupportedTracker (t, announceUrl) { + t.plan(1) + + const client = new Client({ + infoHash: fixtures.leaves.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId1, + port, + wrtc: {} + }) + + client.on('error', err => { t.error(err) }) + client.on('warning', err => { + t.ok(err.message.includes('tracker'), 'got warning') + + client.destroy() + }) +} + +test('unsupported tracker protocol', t => { + testUnsupportedTracker(t, 'badprotocol://127.0.0.1:8080/announce') +}) + +test('http: invalid tracker port', t => { + testUnsupportedTracker(t, 'http://127.0.0.1:69691337/announce') +}) + +test('http: invalid tracker url', t => { + testUnsupportedTracker(t, 'http:') +}) + +test('http: invalid tracker url with slash', t => { + testUnsupportedTracker(t, 'http://') +}) + +test('udp: invalid tracker port', t => { + testUnsupportedTracker(t, 'udp://127.0.0.1:69691337') +}) + +test('udp: invalid tracker url', t => { + testUnsupportedTracker(t, 'udp:') +}) + +test('udp: invalid tracker url with slash', t => { + testUnsupportedTracker(t, 'udp://') +}) + +test('ws: invalid tracker port', t => { + testUnsupportedTracker(t, 'ws://127.0.0.1:69691337') +}) + +test('ws: invalid tracker url', t => { + testUnsupportedTracker(t, 'ws:') +}) + +test('ws: invalid tracker url with slash', t => { + testUnsupportedTracker(t, 'ws://') +}) + +function testClientStartHttpAgent (t, serverType) { + t.plan(5) + + common.createServer(t, serverType, function (server, announceUrl) { + let agent + if (global.fetch && serverType !== 'ws') { + const connector = undici.buildConnector({ rejectUnauthorized: false }) + agent = new undici.Agent({ + connect (opts, cb) { + agentUsed = true + connector(opts, (err, socket) => { + if (err) { + cb(err, null) + } else { + cb(null, socket) + } + }) + } + }) + } else { + agent = new http.Agent() + agent.createConnection = function (opts, fn) { + agentUsed = true + return net.createConnection(opts, fn) + } + } + let agentUsed = false + const client = new Client({ + infoHash: fixtures.leaves.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId1, + port, + wrtc: {}, + proxyOpts: { + httpAgent: agent + } + }) + + if (serverType === 'ws') common.mockWebsocketTracker(client) + client.on('error', function (err) { t.error(err) }) + client.on('warning', function (err) { t.error(err) }) + + client.once('update', function (data) { + t.equal(data.announce, announceUrl) + t.equal(typeof data.complete, 'number') + t.equal(typeof data.incomplete, 'number') + + t.ok(agentUsed) + + client.stop() + + client.once('update', function () { + t.pass('got response to stop') + server.close() + client.destroy() + }) + }) + + client.start() + }) +} + +test('http: client.start(httpAgent)', function (t) { + testClientStartHttpAgent(t, 'http') +}) + +test('ws: client.start(httpAgent)', function (t) { + testClientStartHttpAgent(t, 'ws') +}) diff --git a/test/common.js b/test/common.js index 0f872e397..5ff6ce39f 100644 --- a/test/common.js +++ b/test/common.js @@ -1,40 +1,42 @@ -var Server = require('../').Server +import { Server } from '../index.js' -exports.createServer = function (t, opts, cb) { +export const createServer = (t, opts, cb) => { if (typeof opts === 'string') opts = { serverType: opts } opts.http = (opts.serverType === 'http') opts.udp = (opts.serverType === 'udp') opts.ws = (opts.serverType === 'ws') - var server = new Server(opts) + const server = new Server(opts) - server.on('error', function (err) { t.error(err) }) - server.on('warning', function (err) { t.error(err) }) + server.on('error', err => { t.error(err) }) + server.on('warning', err => { t.error(err) }) - server.listen(0, function () { - var port = server[opts.serverType].address().port - var announceUrl + server.listen(0, () => { + const port = server[opts.serverType].address().port + let announceUrl if (opts.serverType === 'http') { - announceUrl = 'http://127.0.0.1:' + port + '/announce' + announceUrl = `http://127.0.0.1:${port}/announce` } else if (opts.serverType === 'udp') { - announceUrl = 'udp://127.0.0.1:' + port + announceUrl = `udp://127.0.0.1:${port}` } else if (opts.serverType === 'ws') { - announceUrl = 'ws://127.0.0.1:' + port + announceUrl = `ws://127.0.0.1:${port}` } cb(server, announceUrl) }) } -exports.mockWebsocketTracker = function (client) { - client._trackers[0]._generateOffers = function (numwant, cb) { - var offers = [] - for (var i = 0; i < numwant; i++) { - offers.push({ fake_offer: 'fake_offer_' + i }) +export const mockWebsocketTracker = client => { + client._trackers[0]._generateOffers = (numwant, cb) => { + const offers = [] + for (let i = 0; i < numwant; i++) { + offers.push({ fake_offer: `fake_offer_${i}` }) } - process.nextTick(function () { + process.nextTick(() => { cb(offers) }) } } + +export default { mockWebsocketTracker, createServer } diff --git a/test/destroy.js b/test/destroy.js new file mode 100644 index 000000000..e9d7af3a4 --- /dev/null +++ b/test/destroy.js @@ -0,0 +1,50 @@ +import Client from '../index.js' +import common from './common.js' +import fixtures from 'webtorrent-fixtures' +import test from 'tape' + +const peerId = Buffer.from('01234567890123456789') +const port = 6881 + +function testNoEventsAfterDestroy (t, serverType) { + t.plan(1) + + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ + infoHash: fixtures.leaves.parsedTorrent.infoHash, + announce: announceUrl, + peerId, + port, + wrtc: {} + }) + + if (serverType === 'ws') common.mockWebsocketTracker(client) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) + + client.once('update', () => { + t.fail('no "update" event should fire, since client is destroyed') + }) + + // announce, then immediately destroy + client.update() + client.destroy() + + setTimeout(() => { + t.pass('wait to see if any events are fired') + server.close() + }, 1000) + }) +} + +test('http: no "update" events after destroy()', t => { + testNoEventsAfterDestroy(t, 'http') +}) + +test('udp: no "update" events after destroy()', t => { + testNoEventsAfterDestroy(t, 'udp') +}) + +test('ws: no "update" events after destroy()', t => { + testNoEventsAfterDestroy(t, 'ws') +}) diff --git a/test/evict.js b/test/evict.js new file mode 100644 index 000000000..385fae2a6 --- /dev/null +++ b/test/evict.js @@ -0,0 +1,117 @@ +import Client from '../index.js' +import common from './common.js' +import test from 'tape' +import wrtc from 'webrtc-polyfill' + +const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' +const peerId = Buffer.from('01234567890123456789') +const peerId2 = Buffer.from('12345678901234567890') +const peerId3 = Buffer.from('23456789012345678901') + +function serverTest (t, serverType, serverFamily) { + t.plan(10) + + const hostname = serverFamily === 'inet6' + ? '[::1]' + : '127.0.0.1' + + const opts = { + serverType, + peersCacheLength: 2 // LRU cache can only contain a max of 2 peers + } + + common.createServer(t, opts, server => { + // Not using announceUrl param from `common.createServer()` since we + // want to control IPv4 vs IPv6. + const port = server[serverType].address().port + const announceUrl = `${serverType}://${hostname}:${port}/announce` + + const client1 = new Client({ + infoHash, + announce: [announceUrl], + peerId, + port: 6881, + wrtc + }) + if (serverType === 'ws') common.mockWebsocketTracker(client1) + + client1.start() + + client1.once('update', data => { + const client2 = new Client({ + infoHash, + announce: [announceUrl], + peerId: peerId2, + port: 6882, + wrtc + }) + if (serverType === 'ws') common.mockWebsocketTracker(client2) + + client2.start() + + client2.once('update', data => { + server.getSwarm(infoHash, (err, swarm) => { + t.error(err) + + t.equal(swarm.complete + swarm.incomplete, 2) + + // Ensure that first peer is evicted when a third one is added + let evicted = false + swarm.peers.once('evict', evictedPeer => { + t.equal(evictedPeer.value.peerId, peerId.toString('hex')) + t.equal(swarm.complete + swarm.incomplete, 2) + evicted = true + }) + + const client3 = new Client({ + infoHash, + announce: [announceUrl], + peerId: peerId3, + port: 6880, + wrtc + }) + if (serverType === 'ws') common.mockWebsocketTracker(client3) + + client3.start() + + client3.once('update', data => { + t.ok(evicted, 'client1 was evicted from server before client3 gets response') + t.equal(swarm.complete + swarm.incomplete, 2) + + client1.destroy(() => { + t.pass('client1 destroyed') + }) + + client2.destroy(() => { + t.pass('client3 destroyed') + }) + + client3.destroy(() => { + t.pass('client3 destroyed') + }) + + server.close(() => { + t.pass('server destroyed') + }) + }) + }) + }) + }) + }) +} + +test('evict: ipv4 server', t => { + serverTest(t, 'http', 'inet') +}) + +test('evict: http ipv6 server', t => { + serverTest(t, 'http', 'inet6') +}) + +test('evict: udp server', t => { + serverTest(t, 'udp', 'inet') +}) + +test('evict: ws server', t => { + serverTest(t, 'ws', 'inet') +}) diff --git a/test/filter.js b/test/filter.js index 4e7429479..4dc3da02d 100644 --- a/test/filter.js +++ b/test/filter.js @@ -1,158 +1,165 @@ -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +import Client from '../index.js' +import common from './common.js' +import fixtures from 'webtorrent-fixtures' +import test from 'tape' -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testFilterOption (t, serverType) { t.plan(8) - var opts = { serverType: serverType } // this is test-suite-only option - opts.filter = function (infoHash, params, cb) { - process.nextTick(function () { - cb(infoHash !== fixtures.alice.parsedTorrent.infoHash) + const opts = { serverType } // this is test-suite-only option + opts.filter = (infoHash, params, cb) => { + process.nextTick(() => { + if (infoHash === fixtures.alice.parsedTorrent.infoHash) { + cb(new Error('disallowed info_hash (Alice)')) + } else { + cb(null) + } }) } - common.createServer(t, opts, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, opts, (server, announceUrl) => { + const client1 = new Client({ infoHash: fixtures.alice.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881, wrtc: {} }) - client.on('error', function (err) { t.error(err) }) - if (serverType === 'ws') common.mockWebsocketTracker(client) + client1.on('error', err => { t.error(err) }) + if (serverType === 'ws') common.mockWebsocketTracker(client1) - client.once('warning', function (err) { - t.ok(/disallowed info_hash/.test(err.message), 'got client warning') + client1.once('warning', err => { + t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got client warning') - client.destroy(function () { - t.pass('client destroyed') - client = new Client({ + client1.destroy(() => { + t.pass('client1 destroyed') + + const client2 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881, wrtc: {} }) - if (serverType === 'ws') common.mockWebsocketTracker(client) + if (serverType === 'ws') common.mockWebsocketTracker(client2) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client2.on('error', err => { t.error(err) }) + client2.on('warning', err => { t.error(err) }) - client.on('update', function () { + client2.on('update', () => { t.pass('got announce') - client.destroy(function () { t.pass('client destroyed') }) - server.close(function () { t.pass('server closed') }) + client2.destroy(() => { t.pass('client2 destroyed') }) + server.close(() => { t.pass('server closed') }) }) - server.on('start', function () { + server.on('start', () => { t.equal(Object.keys(server.torrents).length, 1) }) - client.start() + client2.start() }) }) server.removeAllListeners('warning') - server.once('warning', function (err) { - t.ok(/disallowed info_hash/.test(err.message), 'got server warning') + server.once('warning', err => { + t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got server warning') t.equal(Object.keys(server.torrents).length, 0) }) - client.start() + client1.start() }) } -test('http: filter option blocks tracker from tracking torrent', function (t) { +test('http: filter option blocks tracker from tracking torrent', t => { testFilterOption(t, 'http') }) -test('udp: filter option blocks tracker from tracking torrent', function (t) { +test('udp: filter option blocks tracker from tracking torrent', t => { testFilterOption(t, 'udp') }) -test('ws: filter option blocks tracker from tracking torrent', function (t) { +test('ws: filter option blocks tracker from tracking torrent', t => { testFilterOption(t, 'ws') }) function testFilterCustomError (t, serverType) { t.plan(8) - var opts = { serverType: serverType } // this is test-suite-only option - opts.filter = function (infoHash, params, cb) { - process.nextTick(function () { - if (infoHash === fixtures.alice.parsedTorrent.infoHash) cb(new Error('alice blocked')) - else cb(true) + const opts = { serverType } // this is test-suite-only option + opts.filter = (infoHash, params, cb) => { + process.nextTick(() => { + if (infoHash === fixtures.alice.parsedTorrent.infoHash) { + cb(new Error('alice blocked')) + } else { + cb(null) + } }) } - common.createServer(t, opts, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, opts, (server, announceUrl) => { + const client1 = new Client({ infoHash: fixtures.alice.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881, wrtc: {} }) - client.on('error', function (err) { t.error(err) }) - if (serverType === 'ws') common.mockWebsocketTracker(client) + client1.on('error', err => { t.error(err) }) + if (serverType === 'ws') common.mockWebsocketTracker(client1) - client.once('warning', function (err) { + client1.once('warning', err => { t.ok(/alice blocked/.test(err.message), 'got client warning') - client.destroy(function () { - t.pass('client destroyed') - client = new Client({ + client1.destroy(() => { + t.pass('client1 destroyed') + const client2 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881, wrtc: {} }) - if (serverType === 'ws') common.mockWebsocketTracker(client) + if (serverType === 'ws') common.mockWebsocketTracker(client2) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client2.on('error', err => { t.error(err) }) + client2.on('warning', err => { t.error(err) }) - client.on('update', function () { + client2.on('update', () => { t.pass('got announce') - client.destroy(function () { t.pass('client destroyed') }) - server.close(function () { t.pass('server closed') }) + client2.destroy(() => { t.pass('client2 destroyed') }) + server.close(() => { t.pass('server closed') }) }) - server.on('start', function () { + server.on('start', () => { t.equal(Object.keys(server.torrents).length, 1) }) - client.start() + client2.start() }) }) server.removeAllListeners('warning') - server.once('warning', function (err) { + server.once('warning', err => { t.ok(/alice blocked/.test(err.message), 'got server warning') t.equal(Object.keys(server.torrents).length, 0) }) - client.start() + client1.start() }) } -test('http: filter option with custom error', function (t) { +test('http: filter option with custom error', t => { testFilterCustomError(t, 'http') }) -test('udp: filter option filter option with custom error', function (t) { +test('udp: filter option filter option with custom error', t => { testFilterCustomError(t, 'udp') }) -test('ws: filter option filter option with custom error', function (t) { +test('ws: filter option filter option with custom error', t => { testFilterCustomError(t, 'ws') }) diff --git a/test/querystring.js b/test/querystring.js index d7785bec6..85e14e544 100644 --- a/test/querystring.js +++ b/test/querystring.js @@ -1,13 +1,12 @@ -var Buffer = require('safe-buffer').Buffer -var common = require('../lib/common') -var test = require('tape') +import common from '../lib/common.js' +import test from 'tape' -// https://github.com/feross/webtorrent/issues/196 -test('encode special chars +* in http tracker urls', function (t) { - var q = { - info_hash: Buffer.from('a2a15537542b22925ad10486bf7a8b2a9c42f0d1', 'hex').toString('binary') - } - var encoded = 'info_hash=%A2%A1U7T%2B%22%92Z%D1%04%86%BFz%8B%2A%9CB%F0%D1' +// https://github.com/webtorrent/webtorrent/issues/196 +test('encode special chars +* in http tracker urls', t => { + const q = Object.create(null) + q.info_hash = Buffer.from('a2a15537542b22925ad10486bf7a8b2a9c42f0d1', 'hex').toString('binary') + + const encoded = 'info_hash=%A2%A1U7T%2B%22%92Z%D1%04%86%BFz%8B%2A%9CB%F0%D1' t.equal(common.querystringStringify(q), encoded) // sanity check that encode-decode matches up diff --git a/test/request-handler.js b/test/request-handler.js new file mode 100644 index 000000000..6532cbbb8 --- /dev/null +++ b/test/request-handler.js @@ -0,0 +1,73 @@ +import Client from '../index.js' +import common from './common.js' +import fixtures from 'webtorrent-fixtures' +import test from 'tape' +import Server from '../server.js' + +const peerId = Buffer.from('01234567890123456789') + +function testRequestHandler (t, serverType) { + t.plan(5) + + const opts = { serverType } // this is test-suite-only option + + class Swarm extends Server.Swarm { + announce (params, cb) { + super.announce(params, (err, response) => { + if (cb && err) return cb(response) + response.complete = 246 + response.extraData = 'hi' + if (cb) cb(null, response) + }) + } + } + + // Use a custom Swarm implementation for this test only + const OldSwarm = Server.Swarm + Server.Swarm = Swarm + t.on('end', () => { + Server.Swarm = OldSwarm + }) + + common.createServer(t, opts, (server, announceUrl) => { + const client1 = new Client({ + infoHash: fixtures.alice.parsedTorrent.infoHash, + announce: announceUrl, + peerId, + port: 6881, + wrtc: {} + }) + + client1.on('error', err => { t.error(err) }) + if (serverType === 'ws') common.mockWebsocketTracker(client1) + + server.once('start', () => { + t.pass('got start message from client1') + }) + + client1.once('update', data => { + t.equal(data.complete, 246) + t.equal(Buffer.from(data.extraData).toString(), 'hi') + + client1.destroy(() => { + t.pass('client1 destroyed') + }) + + server.close(() => { + t.pass('server destroyed') + }) + }) + + client1.start() + }) +} + +test('http: request handler option intercepts announce requests and responses', t => { + testRequestHandler(t, 'http') +}) + +test('ws: request handler option intercepts announce requests and responses', t => { + testRequestHandler(t, 'ws') +}) + +// NOTE: it's not possible to include extra data in a UDP response, because it's compact and accepts only params that are in the spec! diff --git a/test/scrape.js b/test/scrape.js index 6391449b8..c0c101a5f 100644 --- a/test/scrape.js +++ b/test/scrape.js @@ -1,71 +1,70 @@ -var bencode = require('bencode') -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var common = require('./common') -var commonLib = require('../lib/common') -var commonTest = require('./common') -var fixtures = require('webtorrent-fixtures') -var get = require('simple-get') -var test = require('tape') - -var peerId = Buffer.from('01234567890123456789') +import bencode from 'bencode' +import Client from '../index.js' +import common from './common.js' +import commonLib from '../lib/common.js' +import fixtures from 'webtorrent-fixtures' +import fetch from 'cross-fetch-ponyfill' +import test from 'tape' +import { hex2bin } from 'uint8-util' + +const peerId = Buffer.from('01234567890123456789') function testSingle (t, serverType) { - commonTest.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881, wrtc: {} }) if (serverType === 'ws') common.mockWebsocketTracker(client) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) client.scrape() - client.on('scrape', function (data) { + client.on('scrape', data => { t.equal(data.announce, announceUrl) t.equal(data.infoHash, fixtures.leaves.parsedTorrent.infoHash) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') t.equal(typeof data.downloaded, 'number') client.destroy() - server.close(function () { + server.close(() => { t.end() }) }) }) } -test('http: single info_hash scrape', function (t) { +test('http: single info_hash scrape', t => { testSingle(t, 'http') }) -test('udp: single info_hash scrape', function (t) { +test('udp: single info_hash scrape', t => { testSingle(t, 'udp') }) -test('ws: single info_hash scrape', function (t) { +test('ws: single info_hash scrape', t => { testSingle(t, 'ws') }) function clientScrapeStatic (t, serverType) { - commonTest.createServer(t, serverType, function (server, announceUrl) { - var client = Client.scrape({ + common.createServer(t, serverType, (server, announceUrl) => { + const client = Client.scrape({ announce: announceUrl, infoHash: fixtures.leaves.parsedTorrent.infoHash, wrtc: {} - }, function (err, data) { + }, (err, data) => { t.error(err) t.equal(data.announce, announceUrl) t.equal(data.infoHash, fixtures.leaves.parsedTorrent.infoHash) t.equal(typeof data.complete, 'number') t.equal(typeof data.incomplete, 'number') t.equal(typeof data.downloaded, 'number') - server.close(function () { + server.close(() => { t.end() }) }) @@ -73,55 +72,55 @@ function clientScrapeStatic (t, serverType) { }) } -test('http: scrape using Client.scrape static method', function (t) { +test('http: scrape using Client.scrape static method', t => { clientScrapeStatic(t, 'http') }) -test('udp: scrape using Client.scrape static method', function (t) { +test('udp: scrape using Client.scrape static method', t => { clientScrapeStatic(t, 'udp') }) -test('ws: scrape using Client.scrape static method', function (t) { +test('ws: scrape using Client.scrape static method', t => { clientScrapeStatic(t, 'ws') }) // Ensure the callback function gets called when an invalid url is passed function clientScrapeStaticInvalid (t, serverType) { - var announceUrl = serverType + '://invalid.lol' + let announceUrl = `${serverType}://invalid.lol` if (serverType === 'http') announceUrl += '/announce' - var client = Client.scrape({ + const client = Client.scrape({ announce: announceUrl, infoHash: fixtures.leaves.parsedTorrent.infoHash, wrtc: {} - }, function (err, data) { + }, (err, data) => { t.ok(err instanceof Error) t.end() }) if (serverType === 'ws') common.mockWebsocketTracker(client) } -test('http: scrape using Client.scrape static method (invalid url)', function (t) { +test('http: scrape using Client.scrape static method (invalid url)', t => { clientScrapeStaticInvalid(t, 'http') }) -test('udp: scrape using Client.scrape static method (invalid url)', function (t) { +test('udp: scrape using Client.scrape static method (invalid url)', t => { clientScrapeStaticInvalid(t, 'udp') }) -test('ws: scrape using Client.scrape static method (invalid url)', function (t) { +test('ws: scrape using Client.scrape static method (invalid url)', t => { clientScrapeStaticInvalid(t, 'ws') }) function clientScrapeMulti (t, serverType) { - var infoHash1 = fixtures.leaves.parsedTorrent.infoHash - var infoHash2 = fixtures.alice.parsedTorrent.infoHash + const infoHash1 = fixtures.leaves.parsedTorrent.infoHash + const infoHash2 = fixtures.alice.parsedTorrent.infoHash - commonTest.createServer(t, serverType, function (server, announceUrl) { + common.createServer(t, serverType, (server, announceUrl) => { Client.scrape({ - infoHash: [ infoHash1, infoHash2 ], + infoHash: [infoHash1, infoHash2], announce: announceUrl - }, function (err, results) { + }, (err, results) => { t.error(err) t.equal(results[infoHash1].announce, announceUrl) @@ -136,96 +135,103 @@ function clientScrapeMulti (t, serverType) { t.equal(typeof results[infoHash2].incomplete, 'number') t.equal(typeof results[infoHash2].downloaded, 'number') - server.close(function () { + server.close(() => { t.end() }) }) }) } -test('http: MULTI scrape using Client.scrape static method', function (t) { +test('http: MULTI scrape using Client.scrape static method', t => { clientScrapeMulti(t, 'http') }) -test('udp: MULTI scrape using Client.scrape static method', function (t) { +test('udp: MULTI scrape using Client.scrape static method', t => { clientScrapeMulti(t, 'udp') }) -test('server: multiple info_hash scrape (manual http request)', function (t) { - t.plan(13) - - var binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash) - var binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash) +test('server: multiple info_hash scrape (manual http request)', t => { + t.plan(12) - commonTest.createServer(t, 'http', function (server, announceUrl) { - var scrapeUrl = announceUrl.replace('/announce', '/scrape') + const binaryInfoHash1 = hex2bin(fixtures.leaves.parsedTorrent.infoHash) + const binaryInfoHash2 = hex2bin(fixtures.alice.parsedTorrent.infoHash) - var url = scrapeUrl + '?' + commonLib.querystringStringify({ - info_hash: [ binaryInfoHash1, binaryInfoHash2 ] - }) + common.createServer(t, 'http', async (server, announceUrl) => { + const scrapeUrl = announceUrl.replace('/announce', '/scrape') - get.concat(url, function (err, res, data) { + const url = `${scrapeUrl}?${commonLib.querystringStringify({ + info_hash: [binaryInfoHash1, binaryInfoHash2] +})}` + let res + try { + res = await fetch(url) + } catch (err) { t.error(err) + } + let data = Buffer.from(await res.arrayBuffer()) - t.equal(res.statusCode, 200) + t.equal(res.status, 200) - data = bencode.decode(data) - t.ok(data.files) - t.equal(Object.keys(data.files).length, 2) + data = bencode.decode(data) + t.ok(data.files) + t.equal(Object.keys(data.files).length, 2) - t.ok(data.files[binaryInfoHash1]) - t.equal(typeof data.files[binaryInfoHash1].complete, 'number') - t.equal(typeof data.files[binaryInfoHash1].incomplete, 'number') - t.equal(typeof data.files[binaryInfoHash1].downloaded, 'number') + t.ok(data.files[binaryInfoHash1]) + t.equal(typeof data.files[binaryInfoHash1].complete, 'number') + t.equal(typeof data.files[binaryInfoHash1].incomplete, 'number') + t.equal(typeof data.files[binaryInfoHash1].downloaded, 'number') - t.ok(data.files[binaryInfoHash2]) - t.equal(typeof data.files[binaryInfoHash2].complete, 'number') - t.equal(typeof data.files[binaryInfoHash2].incomplete, 'number') - t.equal(typeof data.files[binaryInfoHash2].downloaded, 'number') + t.ok(data.files[binaryInfoHash2]) + t.equal(typeof data.files[binaryInfoHash2].complete, 'number') + t.equal(typeof data.files[binaryInfoHash2].incomplete, 'number') + t.equal(typeof data.files[binaryInfoHash2].downloaded, 'number') - server.close(function () { t.pass('server closed') }) - }) + server.close(() => { t.pass('server closed') }) }) }) -test('server: all info_hash scrape (manual http request)', function (t) { - t.plan(10) +test('server: all info_hash scrape (manual http request)', t => { + t.plan(9) - var binaryInfoHash = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash) + const binaryInfoHash = hex2bin(fixtures.leaves.parsedTorrent.infoHash) - commonTest.createServer(t, 'http', function (server, announceUrl) { - var scrapeUrl = announceUrl.replace('/announce', '/scrape') + common.createServer(t, 'http', (server, announceUrl) => { + const scrapeUrl = announceUrl.replace('/announce', '/scrape') // announce a torrent to the tracker - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881 }) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) client.start() - server.once('start', function () { + server.once('start', async () => { // now do a scrape of everything by omitting the info_hash param - get.concat(scrapeUrl, function (err, res, data) { + let res + try { + res = await fetch(scrapeUrl) + } catch (err) { t.error(err) + } + let data = Buffer.from(await res.arrayBuffer()) - t.equal(res.statusCode, 200) - data = bencode.decode(data) - t.ok(data.files) - t.equal(Object.keys(data.files).length, 1) + t.equal(res.status, 200) + data = bencode.decode(data) + t.ok(data.files) + t.equal(Object.keys(data.files).length, 1) - t.ok(data.files[binaryInfoHash]) - t.equal(typeof data.files[binaryInfoHash].complete, 'number') - t.equal(typeof data.files[binaryInfoHash].incomplete, 'number') - t.equal(typeof data.files[binaryInfoHash].downloaded, 'number') + t.ok(data.files[binaryInfoHash]) + t.equal(typeof data.files[binaryInfoHash].complete, 'number') + t.equal(typeof data.files[binaryInfoHash].incomplete, 'number') + t.equal(typeof data.files[binaryInfoHash].downloaded, 'number') - client.destroy(function () { t.pass('client destroyed') }) - server.close(function () { t.pass('server closed') }) - }) + client.destroy(() => { t.pass('client destroyed') }) + server.close(() => { t.pass('server closed') }) }) }) }) diff --git a/test/server.js b/test/server.js index b3a7b1634..d4cf493e4 100644 --- a/test/server.js +++ b/test/server.js @@ -1,58 +1,54 @@ -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var common = require('./common') -var test = require('tape') -// var wrtc = require('electron-webrtc')() - -// var wrtcReady = false -// wrtc.electronDaemon.once('ready', function () { -// wrtcReady = true -// }) - -var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' -var peerId = Buffer.from('01234567890123456789') -var peerId2 = Buffer.from('12345678901234567890') -var peerId3 = Buffer.from('23456789012345678901') +import Client from '../index.js' +import common from './common.js' +import test from 'tape' +import wrtc from 'webrtc-polyfill' + +const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' +const peerId = Buffer.from('01234567890123456789') +const peerId2 = Buffer.from('12345678901234567890') +const peerId3 = Buffer.from('23456789012345678901') function serverTest (t, serverType, serverFamily) { - t.plan(32) + t.plan(40) - var hostname = serverFamily === 'inet6' + const hostname = serverFamily === 'inet6' ? '[::1]' : '127.0.0.1' - var clientIp = serverFamily === 'inet6' + const clientIp = serverFamily === 'inet6' ? '::1' : '127.0.0.1' - var opts = { - serverType: serverType, - peersCacheLength: 2 + const opts = { + serverType } - common.createServer(t, opts, function (server) { - var port = server[serverType].address().port - var announceUrl = serverType + '://' + hostname + ':' + port + '/announce' - - var client1 = new Client({ - infoHash: infoHash, - announce: [ announceUrl ], - peerId: peerId, - port: 6881 - // wrtc: wrtc + common.createServer(t, opts, server => { + // Not using announceUrl param from `common.createServer()` since we + // want to control IPv4 vs IPv6. + const port = server[serverType].address().port + const announceUrl = `${serverType}://${hostname}:${port}/announce` + + const client1 = new Client({ + infoHash, + announce: [announceUrl], + peerId, + port: 6881, + wrtc }) + if (serverType === 'ws') common.mockWebsocketTracker(client1) client1.start() - server.once('start', function () { + server.once('start', () => { t.pass('got start message from client1') }) - client1.once('update', function (data) { + client1.once('update', data => { t.equal(data.announce, announceUrl) t.equal(data.complete, 0) t.equal(data.incomplete, 1) - server.getSwarm(infoHash, function (err, swarm) { + server.getSwarm(infoHash, (err, swarm) => { t.error(err) t.equal(Object.keys(server.torrents).length, 1) @@ -60,11 +56,11 @@ function serverTest (t, serverType, serverFamily) { t.equal(swarm.incomplete, 1) t.equal(swarm.peers.length, 1) - var id = serverType === 'ws' + const id = serverType === 'ws' ? peerId.toString('hex') - : hostname + ':6881' + : `${hostname}:6881` - var peer = swarm.peers.peek(id) + const peer = swarm.peers.peek(id) t.equal(peer.type, serverType) t.equal(peer.ip, clientIp) t.equal(peer.peerId, peerId.toString('hex')) @@ -79,71 +75,84 @@ function serverTest (t, serverType, serverFamily) { client1.complete() - client1.once('update', function (data) { + client1.once('update', data => { t.equal(data.announce, announceUrl) t.equal(data.complete, 1) t.equal(data.incomplete, 0) client1.scrape() - client1.once('scrape', function (data) { + client1.once('scrape', data => { t.equal(data.announce, announceUrl) - t.equal(typeof data.complete, 'number') - t.equal(typeof data.incomplete, 'number') + t.equal(data.complete, 1) + t.equal(data.incomplete, 0) t.equal(typeof data.downloaded, 'number') - var client2 = new Client({ - infoHash: infoHash, - announce: [ announceUrl ], + const client2 = new Client({ + infoHash, + announce: [announceUrl], peerId: peerId2, - port: 6882 - // wrtc: wrtc + port: 6882, + wrtc }) + if (serverType === 'ws') common.mockWebsocketTracker(client2) client2.start() - server.once('start', function () { + server.once('start', () => { t.pass('got start message from client2') }) - client2.once('peer', function (addr) { - t.ok(addr === hostname + ':6881' || addr === hostname + ':6882' || addr.id === peerId.toString('hex')) + client2.once('update', data => { + t.equal(data.announce, announceUrl) + t.equal(data.complete, 1) + t.equal(data.incomplete, 1) - swarm.peers.once('evict', function (evicted) { - t.equals(evicted.value.peerId, peerId.toString('hex')) - }) - var client3 = new Client({ - infoHash: infoHash, - announce: [ announceUrl ], + const client3 = new Client({ + infoHash, + announce: [announceUrl], peerId: peerId3, - port: 6880 - // wrtc: wrtc + port: 6880, + wrtc }) + if (serverType === 'ws') common.mockWebsocketTracker(client3) + client3.start() - server.once('start', function () { + server.once('start', () => { t.pass('got start message from client3') }) - client3.once('update', function () { + client3.once('update', data => { + t.equal(data.announce, announceUrl) + t.equal(data.complete, 1) + t.equal(data.incomplete, 2) + client2.stop() - client2.once('update', function (data) { + client2.once('update', data => { t.equal(data.announce, announceUrl) t.equal(data.complete, 1) t.equal(data.incomplete, 1) - client2.destroy() - client3.stop() - client3.once('update', function (data) { - t.equal(data.announce, announceUrl) - t.equal(data.complete, 1) - t.equal(data.incomplete, 0) + client2.destroy(() => { + t.pass('client2 destroyed') + client3.stop() + client3.once('update', data => { + t.equal(data.announce, announceUrl) + t.equal(data.complete, 1) + t.equal(data.incomplete, 0) - client3.destroy(function () { - client1.destroy(function () { - server.close() + client1.destroy(() => { + t.pass('client1 destroyed') + }) + + client3.destroy(() => { + t.pass('client3 destroyed') + }) + + server.close(() => { + t.pass('server destroyed') }) - // if (serverType === 'ws') wrtc.close() }) }) }) @@ -156,25 +165,18 @@ function serverTest (t, serverType, serverFamily) { }) } -// test('websocket server', function (t) { -// if (wrtcReady) { -// runTest() -// } else { -// wrtc.electronDaemon.once('ready', runTest) -// } -// function runTest () { -// serverTest(t, 'ws', 'inet') -// } -// }) - -test('http ipv4 server', function (t) { +test('http ipv4 server', t => { serverTest(t, 'http', 'inet') }) -test('http ipv6 server', function (t) { +test('http ipv6 server', t => { serverTest(t, 'http', 'inet6') }) -test('udp server', function (t) { +test('udp server', t => { serverTest(t, 'udp', 'inet') }) + +test('ws server', t => { + serverTest(t, 'ws', 'inet') +}) diff --git a/test/stats.js b/test/stats.js index bed8ae535..7223da014 100644 --- a/test/stats.js +++ b/test/stats.js @@ -1,24 +1,22 @@ -var Buffer = require('safe-buffer').Buffer -var Client = require('../') -var commonTest = require('./common') -var fixtures = require('webtorrent-fixtures') -var get = require('simple-get') -var test = require('tape') +import Client from '../index.js' +import commonTest from './common.js' +import fixtures from 'webtorrent-fixtures' +import fetch from 'cross-fetch-ponyfill' +import test from 'tape' -var peerId = Buffer.from('-WW0091-4ea5886ce160') -var unknownPeerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('-WW0091-4ea5886ce160') +const unknownPeerId = Buffer.from('01234567890123456789') function parseHtml (html) { - var extractValue = new RegExp('[^v^h](\\d+)') - var array = html.replace('torrents', '\n').split('\n').filter(function (line) { - return line && line.trim().length > 0 - }).map(function (line) { - var a = extractValue.exec(line) + const extractValue = /[^v^h](\d+)/ + const array = html.replace('torrents', '\n').split('\n').filter(line => line && line.trim().length > 0).map(line => { + const a = extractValue.exec(line) if (a) { return parseInt(a[1]) } + return null }) - var i = 0 + let i = 0 return { torrents: array[i++], activeTorrents: array[i++], @@ -31,166 +29,169 @@ function parseHtml (html) { } } -test('server: get empty stats', function (t) { - t.plan(11) +test('server: get empty stats', t => { + t.plan(10) - commonTest.createServer(t, 'http', function (server, announceUrl) { - var url = announceUrl.replace('/announce', '/stats') + commonTest.createServer(t, 'http', async (server, announceUrl) => { + const url = announceUrl.replace('/announce', '/stats') - get.concat(url, function (err, res, data) { + let res + try { + res = await fetch(url) + } catch (err) { t.error(err) - - var stats = parseHtml(data.toString()) - t.equal(res.statusCode, 200) - t.equal(stats.torrents, 0) - t.equal(stats.activeTorrents, 0) - t.equal(stats.peersAll, 0) - t.equal(stats.peersSeederOnly, 0) - t.equal(stats.peersLeecherOnly, 0) - t.equal(stats.peersSeederAndLeecher, 0) - t.equal(stats.peersIPv4, 0) - t.equal(stats.peersIPv6, 0) - - server.close(function () { t.pass('server closed') }) - }) + } + const data = Buffer.from(await res.arrayBuffer()) + + const stats = parseHtml(data.toString()) + t.equal(res.status, 200) + t.equal(stats.torrents, 0) + t.equal(stats.activeTorrents, 0) + t.equal(stats.peersAll, 0) + t.equal(stats.peersSeederOnly, 0) + t.equal(stats.peersLeecherOnly, 0) + t.equal(stats.peersSeederAndLeecher, 0) + t.equal(stats.peersIPv4, 0) + t.equal(stats.peersIPv6, 0) + + server.close(() => { t.pass('server closed') }) }) }) -test('server: get empty stats with json header', function (t) { - t.plan(11) +test('server: get empty stats with json header', t => { + t.plan(10) - commonTest.createServer(t, 'http', function (server, announceUrl) { - var opts = { + commonTest.createServer(t, 'http', async (server, announceUrl) => { + const opts = { url: announceUrl.replace('/announce', '/stats'), headers: { - 'accept': 'application/json' - }, - json: true + accept: 'application/json' + } } - - get.concat(opts, function (err, res, stats) { + let res + try { + res = await fetch(announceUrl.replace('/announce', '/stats'), opts) + } catch (err) { t.error(err) - - t.equal(res.statusCode, 200) - t.equal(stats.torrents, 0) - t.equal(stats.activeTorrents, 0) - t.equal(stats.peersAll, 0) - t.equal(stats.peersSeederOnly, 0) - t.equal(stats.peersLeecherOnly, 0) - t.equal(stats.peersSeederAndLeecher, 0) - t.equal(stats.peersIPv4, 0) - t.equal(stats.peersIPv6, 0) - - server.close(function () { t.pass('server closed') }) - }) + } + const stats = await res.json() + + t.equal(res.status, 200) + t.equal(stats.torrents, 0) + t.equal(stats.activeTorrents, 0) + t.equal(stats.peersAll, 0) + t.equal(stats.peersSeederOnly, 0) + t.equal(stats.peersLeecherOnly, 0) + t.equal(stats.peersSeederAndLeecher, 0) + t.equal(stats.peersIPv4, 0) + t.equal(stats.peersIPv6, 0) + + server.close(() => { t.pass('server closed') }) }) }) -test('server: get empty stats on stats.json', function (t) { - t.plan(11) - - commonTest.createServer(t, 'http', function (server, announceUrl) { - var opts = { - url: announceUrl.replace('/announce', '/stats.json'), - json: true - } +test('server: get empty stats on stats.json', t => { + t.plan(10) - get.concat(opts, function (err, res, stats) { + commonTest.createServer(t, 'http', async (server, announceUrl) => { + let res + try { + res = await fetch(announceUrl.replace('/announce', '/stats.json')) + } catch (err) { t.error(err) - - t.equal(res.statusCode, 200) - t.equal(stats.torrents, 0) - t.equal(stats.activeTorrents, 0) - t.equal(stats.peersAll, 0) - t.equal(stats.peersSeederOnly, 0) - t.equal(stats.peersLeecherOnly, 0) - t.equal(stats.peersSeederAndLeecher, 0) - t.equal(stats.peersIPv4, 0) - t.equal(stats.peersIPv6, 0) - - server.close(function () { t.pass('server closed') }) - }) + } + const stats = await res.json() + + t.equal(res.status, 200) + t.equal(stats.torrents, 0) + t.equal(stats.activeTorrents, 0) + t.equal(stats.peersAll, 0) + t.equal(stats.peersSeederOnly, 0) + t.equal(stats.peersLeecherOnly, 0) + t.equal(stats.peersSeederAndLeecher, 0) + t.equal(stats.peersIPv4, 0) + t.equal(stats.peersIPv6, 0) + + server.close(() => { t.pass('server closed') }) }) }) -test('server: get leecher stats.json', function (t) { - t.plan(11) +test('server: get leecher stats.json', t => { + t.plan(10) - commonTest.createServer(t, 'http', function (server, announceUrl) { + commonTest.createServer(t, 'http', (server, announceUrl) => { // announce a torrent to the tracker - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, - peerId: peerId, + peerId, port: 6881 }) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) client.start() - server.once('start', function () { - var opts = { - url: announceUrl.replace('/announce', '/stats.json'), - json: true + server.once('start', async () => { + let res + try { + res = await fetch(announceUrl.replace('/announce', '/stats.json')) + } catch (err) { + t.error(err) } + const stats = await res.json() - get.concat(opts, function (err, res, stats) { - t.error(err) + t.equal(res.status, 200) + t.equal(stats.torrents, 1) + t.equal(stats.activeTorrents, 1) + t.equal(stats.peersAll, 1) + t.equal(stats.peersSeederOnly, 0) + t.equal(stats.peersLeecherOnly, 1) + t.equal(stats.peersSeederAndLeecher, 0) + t.equal(stats.clients.WebTorrent['0.91'], 1) - t.equal(res.statusCode, 200) - t.equal(stats.torrents, 1) - t.equal(stats.activeTorrents, 1) - t.equal(stats.peersAll, 1) - t.equal(stats.peersSeederOnly, 0) - t.equal(stats.peersLeecherOnly, 1) - t.equal(stats.peersSeederAndLeecher, 0) - t.equal(stats.clients['WebTorrent']['0.91'], 1) - - client.destroy(function () { t.pass('client destroyed') }) - server.close(function () { t.pass('server closed') }) - }) + client.destroy(() => { t.pass('client destroyed') }) + server.close(() => { t.pass('server closed') }) }) }) }) -test('server: get leecher stats.json (unknown peerId)', function (t) { - t.plan(11) +test('server: get leecher stats.json (unknown peerId)', t => { + t.plan(10) - commonTest.createServer(t, 'http', function (server, announceUrl) { + commonTest.createServer(t, 'http', (server, announceUrl) => { // announce a torrent to the tracker - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: unknownPeerId, port: 6881 }) - client.on('error', function (err) { t.error(err) }) - client.on('warning', function (err) { t.error(err) }) + client.on('error', err => { t.error(err) }) + client.on('warning', err => { t.error(err) }) client.start() - server.once('start', function () { - var opts = { - url: announceUrl.replace('/announce', '/stats.json'), - json: true + server.once('start', async () => { + let res + try { + res = await fetch(announceUrl.replace('/announce', '/stats.json')) + } catch (err) { + t.error(err) } + const stats = await res.json() - get.concat(opts, function (err, res, stats) { - t.error(err) + t.equal(res.status, 200) + t.equal(stats.torrents, 1) + t.equal(stats.activeTorrents, 1) + t.equal(stats.peersAll, 1) + t.equal(stats.peersSeederOnly, 0) + t.equal(stats.peersLeecherOnly, 1) + t.equal(stats.peersSeederAndLeecher, 0) + t.equal(stats.clients.unknown['01234567'], 1) - t.equal(res.statusCode, 200) - t.equal(stats.torrents, 1) - t.equal(stats.activeTorrents, 1) - t.equal(stats.peersAll, 1) - t.equal(stats.peersSeederOnly, 0) - t.equal(stats.peersLeecherOnly, 1) - t.equal(stats.peersSeederAndLeecher, 0) - t.equal(stats.clients['unknown']['01234567'], 1) - - client.destroy(function () { t.pass('client destroyed') }) - server.close(function () { t.pass('server closed') }) - }) + client.destroy(() => { t.pass('client destroyed') }) + server.close(() => { t.pass('server closed') }) }) }) }) diff --git a/bin/update-authors.sh b/tools/update-authors.sh similarity index 75% rename from bin/update-authors.sh rename to tools/update-authors.sh index c7b8dbb35..ef80c638a 100755 --- a/bin/update-authors.sh +++ b/tools/update-authors.sh @@ -10,13 +10,14 @@ while (<>) { next if /(support\@greenkeeper.io)/; next if /(yoann\@atacma.agency)/; next if /(yciabaud\@users.noreply.github.com)/; - next if /(diegorbaquero\@gmail.com)/; + next if /(DiegoRBaquero\@users.noreply.github.com)/; + next if /(gustavcaplan\@gmail.com)/; $seen{$_} = push @authors, "- ", $_; } END { print "# Authors\n\n"; print "#### Ordered by first contribution.\n\n"; print @authors, "\n"; - print "#### Generated by bin/update-authors.sh.\n"; + print "#### Generated by tools/update-authors.sh.\n"; } ' > AUTHORS.md