Skip to content

Commit

Permalink
feat(p2p): peers limitation to prevent connection spam
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Mar 9, 2018
1 parent cc10630 commit 9177691
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/background/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ let config = {
spiderPort: 4445,
udpTrackersPort: 4446,
udpTrackersTimeout: 3 * 60 * 1000,

p2p: true,
p2pConnections: 10,

upnp: true,

sitemapMaxSize: 25000,
Expand Down Expand Up @@ -54,6 +57,13 @@ if(app && app.getPath("userData") && app.getPath("userData").length > 0)

const configProxy = new Proxy(config, {
set: (target, prop, value, receiver) => {
// some values check (important!)
if(prop == 'p2pConnections' && value < 10)
value = 10
if(prop == 'p2pConnections' && value > 300)
value = 300


target[prop] = value

if(!fs.existsSync(configPath))
Expand Down
6 changes: 5 additions & 1 deletion src/background/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class p2p {
{
this.send = send
this.tcpServer = net.createServer();
this.tcpServer.maxConnections = config.p2pConnections * 2;
this.tcpServer.on('connection', (socket) => {
this.tcpServer.getConnections((err, count) => {
console.log('connection', count)
})
socket = new JsonSocket(socket);
socket.on('error', (err) => {})
socket.on('message', (message) => {
Expand Down Expand Up @@ -93,7 +97,7 @@ class p2p {
add(address) {
const { peers } = this

if(this.size > 10)
if(this.size > config.p2pConnections)
return;

if(address.port <= 1 || address.port > 65535)
Expand Down

0 comments on commit 9177691

Please sign in to comment.