-
-
Notifications
You must be signed in to change notification settings - Fork 335
Provide IP and HTTP headers in both HTTP and Websocket server #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,14 @@ module.exports = parseHttpRequest | |
|
|
||
| var common = require('../common') | ||
|
|
||
| var REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/ | ||
|
|
||
| function parseHttpRequest (req, opts) { | ||
| if (!opts) opts = {} | ||
| var s = req.url.split('?') | ||
| var params = common.querystringParse(s[1]) | ||
|
|
||
| if (opts.action === 'announce' || s[0] === '/announce') { | ||
| params.action = common.ACTIONS.ANNOUNCE | ||
| params.type = common.PEER_TYPES.http | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going to move this out of the if-statement so it's set when the action is |
||
|
|
||
| if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { | ||
| throw new Error('invalid info_hash') | ||
|
|
@@ -34,8 +33,10 @@ function parseHttpRequest (req, opts) { | |
|
|
||
| params.ip = opts.trustProxy | ||
| ? req.headers['x-forwarded-for'] || req.connection.remoteAddress | ||
| : req.connection.remoteAddress.replace(REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 | ||
| : 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 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,8 @@ function parseUdpRequest (msg, rinfo) { | |
| var params = { | ||
| connectionId: msg.slice(0, 8), // 64-bit | ||
| action: msg.readUInt32BE(8), | ||
| transactionId: msg.readUInt32BE(12) | ||
| transactionId: msg.readUInt32BE(12), | ||
| type: common.PEER_TYPES.udp | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that |
||
| } | ||
|
|
||
| if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,12 @@ module.exports = parseWebSocketRequest | |
|
|
||
| var common = require('../common') | ||
|
|
||
| function parseWebSocketRequest (socket, params) { | ||
| function parseWebSocketRequest (socket, opts, params) { | ||
| if (!opts) opts = {} | ||
| params = JSON.parse(params) // may throw | ||
|
|
||
| params.action = common.ACTIONS.ANNOUNCE | ||
| params.type = common.PEER_TYPES.websocket | ||
| params.socket = socket | ||
|
|
||
| if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { | ||
|
|
@@ -32,5 +34,15 @@ function parseWebSocketRequest (socket, params) { | |
| ) | ||
| params.compact = -1 // return full peer objects (used for websocket responses) | ||
|
|
||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather not name this |
||
| if (params.port) { | ||
| params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This The presence of |
||
| } | ||
|
|
||
| params.headers = socket.upgradeReq.headers | ||
|
|
||
| return params | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would rather just use strings directly for these. The strings are the same as the object keys anyway.