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, sitemapMaxSize: 25000,
sphinx: { sphinx: {
host : 'localhost', host : '127.0.0.1',
port : 9306, port : 9306,
connectionLimit: 30 connectionLimit: 30
}, },

View File

@ -26,6 +26,8 @@ const balanceDebug = _debug('main:balance');
const fakeTorrentsDebug = _debug('main:fakeTorrents'); const fakeTorrentsDebug = _debug('main:fakeTorrents');
const quotaDebug = _debug('main:quota'); const quotaDebug = _debug('main:quota');
const checkInternet = require('./checkInternet')
const {torrentTypeDetect} = require('../app/content'); const {torrentTypeDetect} = require('../app/content');
const torrentClient = require('./torrentClient') const torrentClient = require('./torrentClient')
@ -1062,6 +1064,9 @@ client.on('complete', function (metadata, infohash, rinfo) {
} }
}); });
checkInternet((connected) => {
if(!connected)
return
const { STUN_BINDING_REQUEST, STUN_ATTR_XOR_MAPPED_ADDRESS } = stun.constants const { STUN_BINDING_REQUEST, STUN_ATTR_XOR_MAPPED_ADDRESS } = stun.constants
const stunServer = stun.createServer() const stunServer = stun.createServer()
@ -1074,6 +1079,7 @@ stunServer.once('bindingResponse', stunMsg => {
p2p.ignoreAddresses.push(address) p2p.ignoreAddresses.push(address)
}) })
stunServer.send(stunRequest, 19302, 'stun.l.google.com') stunServer.send(stunRequest, 19302, 'stun.l.google.com')
})
let upnp let upnp
if(config.upnp) if(config.upnp)