Skip to content

Commit

Permalink
feat(p2p): ignore your address
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Feb 17, 2018
1 parent 5bd2e75 commit f0f3692
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"react-input-range": "^1.3.0",
"react-markdown": "^3.1.5",
"react-tap-event-plugin": "^3.0.2",
"stun": "^1.1.0",
"webtorrent": "github:DEgITx/webtorrent"
},
"devDependencies": {
Expand Down
19 changes: 18 additions & 1 deletion src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getPeersStatisticUDP = require('./bt/udp-tracker-request')
const net = require('net')
const JsonSocket = require('json-socket')
const crypto = require('crypto')

const stun = require('stun')
//var express = require('express');
//var app = express();
//var server = require('http').Server(app);
Expand Down Expand Up @@ -1047,6 +1047,7 @@ client.on('complete', function (metadata, infohash, rinfo) {

const p2p = {
peers: [],
ignoreAddresses: [],
add(address) {
const { peers } = this

Expand All @@ -1056,6 +1057,9 @@ const p2p = {
if(address.port <= 1 || address.port > 65535)
return;

if(this.ignoreAddresses.includes(address.address))
return;

for(let peer of peers)
{
if(peer.address === address.address) {
Expand Down Expand Up @@ -1117,6 +1121,19 @@ const p2p = {
}
}
}

const { STUN_BINDING_REQUEST, STUN_ATTR_XOR_MAPPED_ADDRESS } = stun.constants
const stunServer = stun.createServer()
const stunRequest = stun.createMessage(STUN_BINDING_REQUEST)
stunServer.once('bindingResponse', stunMsg => {
const {address, port} = stunMsg.getAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS).value
stunServer.close()

console.log('p2p ignore my address', address)
p2p.ignoreAddresses.push(address)
})
stunServer.send(stunRequest, 19302, 'stun.l.google.com')

spider.on('peer', (IPs) => {
const { peers } = p2p;

Expand Down

0 comments on commit f0f3692

Please sign in to comment.