mirror of
https://github.com/logos-storage/bittorrent-tracker.git
synced 2026-01-03 05:23:06 +00:00
This PR merges webtorrent-tracker into this repo. Keeping the code in
sync between the two repos was becoming burdensome. This change should
not effect performance of the server since the webtorrent tracker is
disabled by default.
To enable the webtorrent tracker (disabled by default), do:
```js
var server = new Server({ ws: true })
```
34 lines
991 B
JavaScript
34 lines
991 B
JavaScript
module.exports = parseWebSocketRequest
|
|
|
|
var common = require('./common')
|
|
|
|
function parseWebSocketRequest (socket, params) {
|
|
params = JSON.parse(params) // may throw
|
|
|
|
params.action = common.ACTIONS.ANNOUNCE
|
|
params.socket = socket
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
if (params.answer &&
|
|
(typeof params.to_peer_id !== 'string' || params.to_peer_id.length !== 20)) {
|
|
throw new Error('invalid `to_peer_id` (required with `answer`)')
|
|
}
|
|
|
|
params.left = Number(params.left) || Infinity
|
|
params.numwant = Math.min(
|
|
Number(params.offers && params.offers.length) || 0, // no default - explicit only
|
|
common.MAX_ANNOUNCE_PEERS
|
|
)
|
|
|
|
return params
|
|
}
|