2015-03-27 03:19:06 +00:00
|
|
|
module.exports = parseWebSocketRequest
|
|
|
|
|
2015-07-29 07:26:44 +00:00
|
|
|
var common = require('../common')
|
2015-03-27 03:19:06 +00:00
|
|
|
|
2016-03-03 10:10:54 +00:00
|
|
|
function parseWebSocketRequest (socket, opts, params) {
|
|
|
|
if (!opts) opts = {}
|
2015-03-27 03:19:06 +00:00
|
|
|
params = JSON.parse(params) // may throw
|
|
|
|
|
2016-03-12 01:21:19 +00:00
|
|
|
params.type = 'ws'
|
2015-03-27 03:19:06 +00:00
|
|
|
params.socket = socket
|
2016-03-16 03:06:39 +00:00
|
|
|
if (params.action === 'announce' || params.offers || params.answer) {
|
2016-03-13 23:15:41 +00:00
|
|
|
params.action = common.ACTIONS.ANNOUNCE
|
2015-03-27 03:19:06 +00:00
|
|
|
|
2016-03-13 23:15:41 +00:00
|
|
|
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)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
2016-03-13 23:15:41 +00:00
|
|
|
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)
|
2015-03-27 03:19:06 +00:00
|
|
|
|
2016-03-13 23:15:41 +00:00
|
|
|
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)
|
2015-03-29 08:08:26 +00:00
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
2016-03-13 23:15:41 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
params.compact = -1 // return full peer objects (used for websocket responses)
|
|
|
|
} else if (params.action === 'scrape') {
|
|
|
|
params.action = common.ACTIONS.SCRAPE
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (typeof binaryInfoHash !== 'string' || binaryInfoHash.length !== 20) {
|
|
|
|
throw new Error('invalid info_hash')
|
|
|
|
}
|
|
|
|
return common.binaryToHex(binaryInfoHash)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('invalid action in WS request: ' + params.action)
|
|
|
|
}
|
2015-03-27 03:19:06 +00:00
|
|
|
|
2016-03-03 10:10:54 +00:00
|
|
|
params.ip = opts.trustProxy
|
|
|
|
? socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress
|
2016-03-11 09:09:09 +00:00
|
|
|
: socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
|
2016-03-03 10:10:54 +00:00
|
|
|
params.port = socket.upgradeReq.connection.remotePort
|
|
|
|
if (params.port) {
|
|
|
|
params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port
|
|
|
|
}
|
|
|
|
|
|
|
|
params.headers = socket.upgradeReq.headers
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
return params
|
|
|
|
}
|