-
Notifications
You must be signed in to change notification settings - Fork 41
/
app.js
473 lines (417 loc) · 12.8 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* eslint-disable no-use-before-define */
const fs = require('fs');
const { LocalStorage } = require('node-localstorage');
const jsonfile = require('jsonfile');
const io = require('socket.io-client');
const os = require('os');
const SNode = require('./SNodeTracker').auto();
const pkg = require('./package.json');
const init = require('./init.json');
const configuration = require('./config/config.json');
const file = './config/config.json';
const local = new LocalStorage('./config/local');
// check if setup was run
if (!configuration) {
console.log('Please run setup: node setup');
process.exit();
}
// get user settings
const nodetype = configuration.active;
const config = configuration[nodetype];
// get general app settings
const savedSettings = local.getItem('gensettings');
let genCfg = {};
const defaultInterval = 30000;
// get saved or set defaults. can be updated from server
if (savedSettings) {
const cfg = JSON.parse(savedSettings);
Object.assign(genCfg, cfg);
} else {
genCfg.initInterval = 20000;
genCfg.conCheckInterval = 30000;
genCfg.failoverInterval = 70000;
genCfg.checkIn = {
enabled: false,
timeout: 5000,
max: 2,
};
local.setItem('gensettings', JSON.stringify(genCfg));
}
SNode.genCfg = genCfg;
if (config.ipv === '6') {
console.log('You setup ipv6 connectivity. We need to apply a workaround for dns resolution.');
require('./ipv6-dns-workaround');
}
const logtime = () => `${(new Date()).toISOString().replace(/T/, ' ').replace(/\..+/, '')} UTC --`;
const isEmpty = (obj) => {
if (Object.entries(obj).length > 0) return false;
return true;
};
const saveConfig = (key, value) => {
config[key] = value;
configuration[nodetype] = config;
if (isEmpty(configuration)) {
console.log(logtime(), `Could not save ${key}=${value}, configuration is empty.`);
return;
}
fs.copyFile(file, `${file}.BACK`, () => {
jsonfile.writeFile(file, configuration, { spaces: 1 }, (err) => {
if (err) {
console.error(err);
console.log(logtime(), `Could not save ${key}=${value}`, err);
}
console.log(logtime(), `Saved ${key}=${value}`);
});
});
};
// socket latency
let pongCount = 0;
let latencies = [];
const latencyReset = 20;
// host names without domain
let { servers } = config;
let home = (config.home).trim();
if (!home) {
console.log('ERROR SETTING THE HOME SERVER. Please try running setup again or report the issue.');
process.exit();
}
console.log(logtime(), 'STARTING NODETRACKER');
let curIdx = servers.indexOf(home);
let curServer = home;
let protocol = `${init.protocol}://`;
let domain = `.${genCfg.domain || init.domain}`;
if (process.env.DEV) {
protocol = process.env.DEV_PROTOCOL;
domain = process.env.DEV_DOMAIN;
}
let failoverTimer = null;
let conCheckTimer = null;
let returningHome = false;
let checkInsMissed = 0;
// get cpu config
const cpus = os.cpus();
console.log(`CPU ${cpus[0].model} count=${cpus.length} speed=${cpus[0].speed}`);
const hw = { CPU: cpus[0].model, cores: cpus.length, speed: cpus[0].speed };
// platform
const { platform } = process;
// check memory
if (platform === 'linux') {
SNode.getProcMeminfo(true, true);
} else {
const memtotal = os.totalmem() / (1000 * 1000 * 1024);
const memfree = os.freemem() / (1000 * 1000 * 1024);
SNode.mem.memtotal = Number(memtotal.toFixed(1));
SNode.mem.memfree = Number(memfree.toFixed(1));
SNode.mem.units = 'GB';
console.log(`Total memory=${memtotal.toFixed(1)}GB free memory=${memfree.toFixed(1)}GB`);
}
// node version
const nodejs = process.version;
console.log(`Node.js version: ${nodejs}`);
// self version
const trkver = pkg.version;
console.log(`Tracker app version: ${trkver}`);
// node type
console.log(`Node type: ${nodetype}`);
// gather identity
const nodeid = config.nodeid || null;
const fqdn = config.fqdn.trim() || null;
const stkaddr = config.stakeaddr.trim();
const ident = { nid: nodeid, stkaddr, fqdn };
ident.con = { home, cur: curServer };
if (nodeid) console.log(`Node Id: ${nodeid}`);
// add zend info to ident to send to server
ident.zend = {
zip4: SNode.zencfg.zip4,
zip6: SNode.zencfg.zip6,
port: SNode.zencfg.port,
};
// optional category
let cat = config.category;
if (cat) {
cat = cat.trim();
ident.cat = cat;
}
// connection options
const socketOptions = {};
const savedOpts = local.getItem('socketoptions');
if (savedOpts) {
const opts = JSON.parse(savedOpts);
Object.assign(socketOptions, opts);
} else {
// defaults
socketOptions.transports = ['websocket'];
socketOptions.reconnectionDelay = 30000;
socketOptions.reconnectionDelayMax = 60000;
socketOptions.randomizationFactor = 0.8;
}
let socket = io(protocol + curServer + domain, socketOptions);
const authenticate = () => {
ident.email = config.email;
SNode.getNetworks(null, (err, nets) => {
if (!err) {
ident.nets = nets;
socket.emit('initnode', ident, () => {
// only pass email and nets on init.
delete ident.email;
delete ident.nets;
});
}
});
};
const initialize = () => {
// check connectivity by getting the t_address.
// pass identity to server on success
console.log(logtime(), 'Checking t-address...');
SNode.getPrimaryAddress((err, taddr) => {
if (err) {
setTimeout(() => {
initialize();
}, genCfg.initInterval || defaultInterval);
} else {
ident.taddr = taddr;
console.log(`Node t_address (not for stake)=${taddr}`);
SNode.ident = ident;
console.log(logtime(), 'Authenticating...');
authenticate();
}
});
};
const switchServer = (server) => {
let nextIdx = 0;
if (server) {
nextIdx = servers.indexOf(server);
} else {
nextIdx = curIdx + 1 >= servers.length ? 0 : curIdx + 1;
}
curServer = servers[nextIdx];
curIdx = nextIdx;
console.log(logtime(), `Trying server: ${curServer}`);
socket.close();
ident.con.cur = curServer;
socket = io(protocol + curServer + domain, socketOptions);
SNode.socket = socket;
setSocketEvents();
local.setItem('curserver', curServer);
};
const changeHome = (server) => {
home = server;
saveConfig('home', server);
const region = server.split('.')[1];
if (config.region !== region) saveConfig('region', region);
curServer = home;
curIdx = servers.indexOf(home);
returningHome = true;
console.log(logtime(), `Change home server to ${curServer}.`);
socket.close();
ident.con.home = home;
ident.con.cur = curServer;
socket = io(protocol + curServer + domain, socketOptions);
SNode.socket = socket;
setSocketEvents();
returningHome = false;
local.setItem('curserver', curServer);
};
const returnHome = () => {
curServer = home;
curIdx = servers.indexOf(home);
returningHome = true;
console.log(logtime(), `Returning to home server ${curServer}.`);
socket.close();
ident.con.cur = curServer;
socket = io(protocol + curServer + domain, socketOptions);
SNode.socket = socket;
setSocketEvents();
returningHome = false;
local.setItem('curserver', curServer);
};
const resetSocket = (msg) => {
console.log(logtime(), `Reset connection ${msg || ''}`);
socket.close();
socket = io(protocol + curServer + domain, socketOptions);
SNode.socket = socket;
setSocketEvents();
};
let dTime = new Date();
let rTime;
const getDiff = (dt) => {
const diff = (new Date() - dt) / 1000;
return `${diff.toFixed(1)} seconds`;
};
const setSocketEvents = () => {
socket.on('connect', () => {
console.log(logtime(), `Connected to server ${curServer}. Initializing...`);
if (rTime) rTime = null;
if (dTime) dTime = null;
initialize();
if (failoverTimer) clearInterval(failoverTimer);
});
socket.on('disconnect', () => {
if (!returningHome) {
console.log(logtime(), `Disconnected from ${curServer}. Retry at random intervals.`);
}
dTime = new Date();
failoverTimer = setInterval(() => {
switchServer();
}, genCfg.failoverInterval || defaultInterval);
});
socket.on('returnhome', () => {
returnHome();
});
socket.on('newconnection', (msg) => {
resetSocket(msg);
});
socket.on('msg', (msg) => {
console.log(logtime(), msg);
// for backward compatibility until servers support 0.4.0
if (msg.indexOf('Stats received') !== -1) {
SNode.ackStats();
}
});
socket.on('action', (data) => {
let oldInterval;
switch (data.action) {
case 'set nid':
saveConfig('nodeid', data.nid);
break;
case 'get stats':
SNode.collectStats((err, stats) => {
if (err) {
if (ident) {
socket.emit('node', { type: 'down', ident });
}
} else {
socket.emit('node', {
type: 'stats',
stats,
ident,
req: true,
});
}
});
console.log(logtime(), 'Stats: send initial stats.');
break;
case 'get tlsPeers':
SNode.getTLSPeers((err, tlsPeers) => {
if (err) {
if (ident) {
socket.emit('node', { type: 'down', ident });
}
} else {
socket.emit('node', { type: 'tlsPeers', tlsPeers, ident });
}
});
console.log(logtime(), 'TLS Peers: sent list');
break;
case 'get config':
SNode.getConfig(data, trkver, hw, nodejs, platform);
break;
case 'challenge':
SNode.execChallenge(data.chal);
break;
case 'networks':
SNode.getNets(data);
break;
case 'changeServer':
switchServer(data.server);
break;
case 'changeHome':
changeHome(data.server);
break;
case 'updateServers':
servers = data.servers;
curIdx = servers.indexOf(curServer);
saveConfig('servers', servers);
console.log(logtime(), 'Updated server list');
break;
case 'setSocketOpts':
local.setItem('socketoptions', JSON.stringify(data.cfg));
SNode.socketOptions = data.cfg;
console.log(logtime(), 'Updated socketOptions');
break;
case 'setStatsCheck':
local.setItem('statscfg', JSON.stringify(data.cfg));
SNode.statsCfg = data.cfg;
SNode.restartStatTimer(data.cfg);
console.log(logtime(), 'Updated statCheck config');
break;
case 'setGenCfg':
local.setItem('gensettings', JSON.stringify(data.cfg));
oldInterval = genCfg.conCheckInterval;
genCfg = data.cfg;
if (data.cfg.conCheckInterval !== oldInterval) conCheck();
SNode.genCfg = data.cfg;
console.log(logtime(), 'Updated general settings');
break;
case 'statsAck':
console.log(logtime(), `Stats received by ${data.server}`);
SNode.ackStats();
break;
default:
// no default
}
});
socket.on('error', (err) => {
if (!err.message === 'websocket error') {
console.log(logtime(), `Socket.io ERROR: ${err.message}`);
}
});
socket.on('reconnect', (num) => {
console.log(logtime(), 'Reconnect attempts', num, `${!rTime ? getDiff(dTime) : getDiff(rTime)}`);
rTime = new Date();
});
socket.on('reconnect_error', (err) => {
console.log(logtime(), `Reconnect ${err.message}`);
});
socket.on('pong', (latency) => {
pongCount += 1;
latencies.push(latency);
if (pongCount >= latencyReset) {
const sum = latencies.reduce((tot, val) => tot + val);
const avg = (sum / pongCount).toFixed(0);
console.log(logtime(), `Server response latency average (last ${pongCount}) ${avg}ms`);
latencies = [];
pongCount = 0;
}
if (!SNode.statsTimerRunning) {
const interval = SNode.statsInterval + SNode.statsAckTimeout;
if ((new Date()).getTime() - interval > SNode.statsLastSentTime) {
console.log(logtime(), 'Restart statCheck timer');
SNode.initialize();
}
}
});
socket.on('checkedIn', (srv) => {
if (srv !== curServer) console.log(logtime(), `Actually connected to ${srv}`);
checkInsMissed = 0;
});
};
setSocketEvents();
const conCheck = () => {
if (conCheckTimer) clearInterval(conCheckTimer);
conCheckTimer = setInterval(() => {
if (!socket.connected) {
console.log(logtime(), `No connection to server ${curServer}.`);
if (!failoverTimer) {
failoverTimer = setInterval(() => {
switchServer();
}, genCfg.failoverInterval || defaultInterval);
}
} else if (genCfg.checkIn.enabled) {
// application ping/pong
checkInsMissed += 1;
socket.emit('checkIn');
setTimeout(() => {
if (checkInsMissed >= genCfg.checkIn.max) {
console.log(logtime(), 'Server does not appear to be responding. Resetting connection');
resetSocket();
}
}, genCfg.checkIn.timeout);
}
}, genCfg.conCheckInterval || defaultInterval);
};
SNode.resetSocket = resetSocket;
SNode.socketOptions = socketOptions;
SNode.socket = socket;
SNode.initialize();
conCheck();