Skip to content

Commit 2f37e6c

Browse files
committed
Adds a type param to identify the source of the peer
1 parent 60f03b9 commit 2f37e6c

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

lib/common-node.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ exports.EVENT_NAMES = {
2424
started: 'start',
2525
stopped: 'stop'
2626
}
27+
exports.PEER_TYPES = {
28+
http: 'http',
29+
udp: 'udp',
30+
websocket: 'ws'
31+
}
2732

2833
function toUInt32 (n) {
2934
var buf = new Buffer(4)

lib/server/parse-http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function parseHttpRequest (req, opts) {
99

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

1314
if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) {
1415
throw new Error('invalid info_hash')

lib/server/parse-udp.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ function parseUdpRequest (msg, rinfo) {
1010
var params = {
1111
connectionId: msg.slice(0, 8), // 64-bit
1212
action: msg.readUInt32BE(8),
13-
transactionId: msg.readUInt32BE(12)
13+
transactionId: msg.readUInt32BE(12),
14+
type: common.PEER_TYPES.udp
1415
}
1516

1617
if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) {

lib/server/parse-websocket.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function parseWebSocketRequest (socket, opts, params) {
77
params = JSON.parse(params) // may throw
88

99
params.action = common.ACTIONS.ANNOUNCE
10+
params.type = common.PEER_TYPES.websocket
1011
params.socket = socket
1112

1213
if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) {
@@ -35,7 +36,7 @@ function parseWebSocketRequest (socket, opts, params) {
3536

3637
params.ip = opts.trustProxy
3738
? socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress
38-
: (socket.upgradeReq.connection.remoteAddress && socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '')) // force ipv4
39+
: socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
3940
params.port = socket.upgradeReq.connection.remotePort
4041
if (params.port) {
4142
params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port

lib/server/swarm.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = Swarm
22

33
var debug = require('debug')('bittorrent-tracker')
44
var randomIterate = require('random-iterate')
5+
var common = require('../common')
56

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

1415
Swarm.prototype.announce = function (params, cb) {
1516
var self = this
16-
var peer = self.peers[params.addr || params.peer_id]
17+
var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr
18+
var peer = self.peers[id]
1719

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

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

6770
if (peer.complete) this.complete -= 1
6871
else this.incomplete -= 1
69-
this.peers[params.addr || params.peer_id] = null
72+
var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr
73+
this.peers[id] = null
7074
}
7175

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

0 commit comments

Comments
 (0)