Skip to content

Commit

Permalink
Resolve dns according the selected ip version before connecting to ho…
Browse files Browse the repository at this point in the history
…st (see HorizenOfficial#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."
  • Loading branch information
asbachb committed Dec 17, 2017
1 parent cbb4e3e commit 944fe4f
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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" + " --";
Expand All @@ -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;
});
}


Expand All @@ -227,7 +238,8 @@ const conCheck = () => {
)
}

SecNode.socket = socket;
SecNode.initialize();
conCheck();

openSocket(() => {
SecNode.socket = socket;
SecNode.initialize();
conCheck();
});

0 comments on commit 944fe4f

Please sign in to comment.