From 944fe4fae44d552ad6cbcf3f6d30e2d774909afd Mon Sep 17 00:00:00 2001 From: Benjamin Asbach Date: Sat, 16 Dec 2017 12:36:59 +0100 Subject: [PATCH] Resolve dns according the selected ip version before connecting to host (see #25) Otherwise the ipv4 address is resolved. Removed "{ multiplex: false }" option since it's the same as "{ forceNew : true }" according docs (https://socket.io/docs/client-api/#new-manager-url-options) "[...] unless the multiplex option is passed with false. Passing this option is the equivalent of passing 'force new connection': true or forceNew: true." --- app.js | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/app.js b/app.js index 8a1bc03..321e9bb 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,7 @@ const SecNode = require('./SecNodeTracker').auto(); const LocalStorage = require('node-localstorage').LocalStorage; const local = new LocalStorage('./config'); +const dns = require('dns'); const io = require('socket.io-client') const os = require('os'); const pkg = require('./package.json'); @@ -15,12 +16,14 @@ if (local.length == 0) { // host names without domain const servers = local.getItem('servers').split(','); const home = local.getItem('home'); +const ipv = local.getItem('ipv'); if (!home) return console.log("ERROR SETTING THE HOME SERVER. Please try running setup again or report the issue.") let curIdx = servers.indexOf(home); let curServer = home; const protocol = `${init.protocol}://`; let domain = `.${init.domain}`; -let socket = io(protocol + curServer + domain, { multiplex: false }); + +let socket; let failoverTimer; //get cpu config @@ -55,6 +58,15 @@ ident.con = { home: home, cur: curServer } let initTimer; let returningHome = false; +function openSocket(afterCreation) { + dns.lookup(curServer + domain, {family: ipv}, (err, address, family) => { + console.log("Using " + address + " for tracker communication"); + socket = io(protocol + address, { forceNew : true }) + setSocketEvents(); + afterCreation(); + }); +} + const initialize = () => { // check connectivity by getting the t_address. // pass identity to server on success @@ -106,7 +118,7 @@ const initialize = () => { SecNode.getNetworks(null, (err, nets) => { ident.nets = nets; socket.emit('initnode', ident, () => { - //only pass email and nets on init. + //only pass email and nets on init. delete ident.email; delete ident.nets; }); @@ -140,11 +152,11 @@ const setSocketEvents = () => { returningHome = true; console.log(logtime(), `Returning to home server ${curServer}.`); socket.close(); - socket = io(protocol + curServer + domain, { forceNew: true }); - setSocketEvents(); - SecNode.socket = socket; - ident.con.cur = curServer; - returningHome = false; + openSocket(() => { + SecNode.socket = socket; + ident.con.cur = curServer; + returningHome = false; + }); }) socket.on('msg', (msg) => { @@ -182,14 +194,13 @@ const setSocketEvents = () => { case 'networks': SecNode.getNets(data); break; - + case 'changeServer': switchServer(data.server); break; } }) } -setSocketEvents(); const logtime = () => { return (new Date()).toISOString().replace(/T/, ' ').replace(/\..+/, '') + " GMT" + " --"; @@ -206,10 +217,10 @@ const switchServer = (server) => { curIdx = nextIdx; console.log(logtime(), "Trying server: " + curServer); socket.close(); - socket = io.connect(protocol + curServer + domain); - setSocketEvents(); - SecNode.socket = socket; - ident.con.cur = curServer; + openSocket(() => { + SecNode.socket = socket; + ident.con.cur = curServer; + }); } @@ -227,7 +238,8 @@ const conCheck = () => { ) } -SecNode.socket = socket; -SecNode.initialize(); -conCheck(); - +openSocket(() => { + SecNode.socket = socket; + SecNode.initialize(); + conCheck(); +});