Skip to content

Commit

Permalink
fix(p2p): disconnect on protocol bad handshake on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Mar 14, 2018
1 parent c02d3d9 commit c6b73ee
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/background/p2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class p2p {
this.tcpServer = net.createServer();
this.tcpServer.maxConnections = config.p2pConnections * 2;
this.tcpServer.on('connection', (socket) => {
this.tcpServer.getConnections((err,con) => {
console.log('server connected', con, 'max', this.tcpServer.maxConnections)
})
socket = new JsonSocket(socket);
socket.on('error', (err) => {})
socket.on('message', (message) => {
Expand All @@ -32,12 +35,15 @@ class p2p {
}, socket)
}
});
socket.protocolTimeout = setTimeout(() => socket._socket.destroy(), 7000)
})
// check protocol
this.on('protocol', (data, callback, socketObject) => {
if(!data || data.protocol != 'rats')
return

// protocol ok
clearTimeout(socketObject.protocolTimeout)
const { _socket: socket } = socketObject
socketObject.rats = true

Expand Down Expand Up @@ -188,6 +194,8 @@ class p2p {
{
this.size--;
this.send('peer', this.size)
// trying reconnect once
setTimeout(() => this.add(this.addr(address)), 5000)
}
this.peers.splice(index, 1);

Expand Down Expand Up @@ -221,6 +229,11 @@ class p2p {
return peers.map(peer => ({address: peer.address, port: peer.port}))
}

addr(peer)
{
return {address: peer.address, port: peer.port}
}

find(peer)
{
return this.peersList().find((localPeer) => {
Expand Down

0 comments on commit c6b73ee

Please sign in to comment.