Skip to content

Commit

Permalink
Adds a type param to identify the source of the peer
Browse files Browse the repository at this point in the history
  • Loading branch information
yciabaud committed Mar 11, 2016
1 parent 60f03b9 commit 2f37e6c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/common-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ exports.EVENT_NAMES = {
started: 'start',
stopped: 'stop'
}
exports.PEER_TYPES = {
http: 'http',
udp: 'udp',
websocket: 'ws'
}

function toUInt32 (n) {
var buf = new Buffer(4)
Expand Down
1 change: 1 addition & 0 deletions lib/server/parse-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function parseHttpRequest (req, opts) {

if (opts.action === 'announce' || s[0] === '/announce') {
params.action = common.ACTIONS.ANNOUNCE
params.type = common.PEER_TYPES.http

if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) {
throw new Error('invalid info_hash')
Expand Down
3 changes: 2 additions & 1 deletion lib/server/parse-udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/server/parse-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function parseWebSocketRequest (socket, opts, params) {
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) {
Expand Down Expand Up @@ -35,7 +36,7 @@ function parseWebSocketRequest (socket, opts, params) {

params.ip = opts.trustProxy
? socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress
: (socket.upgradeReq.connection.remoteAddress && socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '')) // force ipv4
: 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
Expand Down
10 changes: 7 additions & 3 deletions lib/server/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = Swarm

var debug = require('debug')('bittorrent-tracker')
var randomIterate = require('random-iterate')
var common = require('../common')

// Regard this as the default implementation of an interface that you
// need to support when overriding Server.createSwarm() and Server.getSwarm()
Expand All @@ -13,7 +14,8 @@ function Swarm (infoHash, server) {

Swarm.prototype.announce = function (params, cb) {
var self = this
var peer = self.peers[params.addr || params.peer_id]
var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr
var peer = self.peers[id]

if (params.event === 'started') {
self._onAnnounceStarted(params, peer)
Expand Down Expand Up @@ -49,7 +51,8 @@ Swarm.prototype._onAnnounceStarted = function (params, peer) {

if (params.left === 0) this.complete += 1
else this.incomplete += 1
peer = this.peers[params.addr || params.peer_id] = {
var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr
peer = this.peers[id] = {
complete: params.left === 0,
peerId: params.peer_id, // as hex
ip: params.ip, // only http, udp
Expand All @@ -66,7 +69,8 @@ Swarm.prototype._onAnnounceStopped = function (params, peer) {

if (peer.complete) this.complete -= 1
else this.incomplete -= 1
this.peers[params.addr || params.peer_id] = null
var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr
this.peers[id] = null
}

Swarm.prototype._onAnnounceCompleted = function (params, peer) {
Expand Down

0 comments on commit 2f37e6c

Please sign in to comment.