fix(network): fix stun error on startup if no network available

Fixed #7
This commit is contained in:
Alexey Kasyanchuk 2018-03-04 00:21:29 +03:00
parent 5832626fd2
commit 3bed2b13cb
3 changed files with 27 additions and 11 deletions

View File

@ -0,0 +1,10 @@
// https://stackoverflow.com/questions/15270902/check-for-internet-connectivity-in-nodejs
module.exports = function checkInternet(cb) {
require('dns').lookup('google.com',function(err) {
if (err && err.code == "ENOTFOUND") {
cb(false);
} else {
cb(true);
}
})
}

View File

@ -15,7 +15,7 @@ let config = {
sitemapMaxSize: 25000,
sphinx: {
host : 'localhost',
host : '127.0.0.1',
port : 9306,
connectionLimit: 30
},

View File

@ -26,6 +26,8 @@ const balanceDebug = _debug('main:balance');
const fakeTorrentsDebug = _debug('main:fakeTorrents');
const quotaDebug = _debug('main:quota');
const checkInternet = require('./checkInternet')
const {torrentTypeDetect} = require('../app/content');
const torrentClient = require('./torrentClient')
@ -1062,18 +1064,22 @@ client.on('complete', function (metadata, infohash, rinfo) {
}
});
checkInternet((connected) => {
if(!connected)
return
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 stun ignore my address', address)
p2p.ignoreAddresses.push(address)
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 stun ignore my address', address)
p2p.ignoreAddresses.push(address)
})
stunServer.send(stunRequest, 19302, 'stun.l.google.com')
})
stunServer.send(stunRequest, 19302, 'stun.l.google.com')
let upnp
if(config.upnp)