-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
171 lines (133 loc) · 3.95 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
const SecNode = require('./SecNodeTracker').auto();
const LocalStorage = require('node-localstorage').LocalStorage;
const local = new LocalStorage('./config');
const socket = require('socket.io-client')(local.getItem('serverurl')); //('http://192.168.1.50:3333');
const os = require('os');
const pkg = require('./package.json');
//check if setup was run
if (local.length == 0) {
console.log("Please run setup: node setup.js");
process.exit();
}
//get cpu config
const cpus = os.cpus();
console.log("CPU " + cpus[0].model + " cores=" + cpus.length + " speed=" + cpus[0].speed);
const hw = { "CPU": cpus[0].model, "cores": cpus.length, "speed": cpus[0].speed }
//self version
const poolver = pkg.version;
console.log("Pool app version: " + poolver);
//check memory
if (process.platform == 'linux') {
console.log(SecNode.getProcMeminfo(true));
} else {
let totmem = os.totalmem() / (1000 * 1000 * 1024);
let freemem = os.freemem() / (1000 * 1000 * 1024);
console.log("total memory=" + totmem.toFixed(1) + "GB free memory=" + freemem.toFixed(1)) + "GB";
}
let taddr;
//check if already registered
let nodeid = local.getItem('nodeid') || null;
let fqdn = local.getItem('fqdn') || null;
if (fqdn) fqdn = fqdn.trim();
let stkaddr = local.getItem('stakeaddr').trim();
let ident = { "nid": nodeid, "stkaddr":stkaddr, "fqdn": fqdn };
let initTimer;
const initialize = () => {
// check connectivity by getting the t_address.
// pass identity to server on success
SecNode.getPrimaryAddress((err, taddr) => {
if (err) {
console.log(err);
if (!initTimer) {
initTimer = setInterval(() => {
initialize();
}, 10000)
}
} else {
if (initTimer) clearInterval(initTimer);
ident.taddr = taddr;
console.log("Secure Node t_address=" + taddr);
SecNode.ident = ident;
SecNode.getAddrWithBal((err, result) => {
if (err) {
console.log(err);
return
}
if (result.bal == 0 && result.valid) {
console.log("Challenge private address balance is 0");
console.log("Please add at least 1 zen to the private address below");
if (!nodeid) {
console.log(result.addr)
console.log("Unable to register node. Exiting.")
process.exit();
}
} else {
console.log("Balance for challenge transactions is " + result.bal);
if (result.bal < 0.01 && result.valid) {
console.log("Challenge private address balance getting low");
console.log("Please add at least 1 zen to the private address below");
}
}
console.log("Using the following address for challenges");
console.log(result.addr)
ident.email = local.getItem('email');
SecNode.getNetworks(null, (err, nets)=>{
ident.nets = nets;
socket.emit('initnode', ident, ()=>{
//only pass email and nets on init.
delete ident.email;
delete ident.nets;
});
})
return
})
}
});
}
socket.on('connect', () => {
console.log(logtime(), "Connected to node pool server. Initializing...");
initialize();
});
socket.on('msg', (msg) => {
console.log(logtime(), msg);
});
socket.on("action", (data) => {
switch (data.action) {
case "set nid":
local.setItem("nodeid", data.nid);
break;
case 'get stats':
SecNode.getStats((err, stats) => {
if (err) {
if (ident) {
socket.emit("node", { type: "down", ident: ident });
}
} else {
socket.emit("node", { type: "stats", stats: stats, ident: ident });
}
});
console.log(logtime(), "send stats")
break;
case 'get config':
SecNode.getConfig(data, poolver, hw);
break;
case 'challenge':
SecNode.execChallenge(data.chal);
break;
case 'networks':
SecNode.getNets(data);
break;
}
})
const logtime = () => {
return (new Date()).toISOString().replace(/T/, ' ').replace(/\..+/, '') + " GMT" + " --";
}
const conCheck = () => {
setInterval(() => {
if (!socket.connected) console.log(logtime(), "No connection to server");
}, 60000
)
}
SecNode.socket = socket;
SecNode.initialize();
conCheck();