-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.js
executable file
·412 lines (347 loc) · 14.6 KB
/
scanner.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
var fs = require('fs'),
http = require('http'),
exec = require('child_process').exec,
_ = require('underscore'),
Geo = require('./geo');
function dpc(t,fn) { if(typeof(t) == 'function') setTimeout(t,0); else setTimeout(fn,t); }
function Scanner(options) {
var self = this;
self.options = options;
var config = eval('('+fs.readFileSync("scanner.cfg",'utf8')+')');
var upload = fs.existsSync('upload.cfg') ? eval('('+fs.readFileSync("upload.cfg",'utf8')+')') : null;
self.addr_pending = { } // list of addresses waiting scan
self.addr_digested = { } // list of scanned addresses
self.addr_working = { } // list of working addresses
self.geo = new Geo({ timeout : config.http_socket_timeout });
// -----------------------------------------
// local http server interface
if(config.http_port)
{
var express = require('express');
var app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/', function(req, res) {
var str = self.render();
res.write(str);
res.end();
});
app.get('/api', function(req, res) {
var str = self.json();
res.write(str);
res.end();
});
app.get('/urls', function(req, res) {
var urls = [];
for(var url in self.addr_working) {
urls.push(url);
}
res.write(JSON.stringify(urls));
res.end();
});
http.createServer(app).listen(config.http_port, function() {
console.log("HTTP server listening on port: ",config.http_port);
});
}
var logo = fs.readFileSync("resources/"+config.currency.toLowerCase()+".png","base64");
if(logo)
logo = "data:image/png;base64,"+logo;
self.json = function() {
return JSON.stringify(self.addr_working);
};
self.render = function() {
var str = "<html><head>"
+"<style>"
+"body { font-family: Consolas; font-size: 14px; background-color: #fff; color: #000; }"
+"a:link { text-decoration: none; color: #0051AD; }"
+"a:visited { text-decoration: none; color: #0051AD; }"
+"a:hover { text-decoration: none; color: #F04800; }"
+".row-grey { background-color: #f3f3f3; }"
+".p2p { width: 838px; margin: auto; border: 1px solid #aaa; box-shadow: 2px 2px 2px #aaa; padding: 2px; }"
+".p2p-row { width: 820px; padding: 10px; height: 16px; }"
+".p2p-caption { width: 820px; text-align: center; background-color: #ddd; padding-top: 4px; padding-bottom: 8px;}"
+".p2p div { float : left; }"
+".p2p-ip { width: 200px; text-align:left; }"
+".p2p-version { margin-left: 10px; width: 100px; text-align: center;}"
+".p2p-fee { margin-left: 10px; width: 90px; text-align: center;}"
+".p2p-uptime { margin-left: 10px; width: 100px; text-align: center;}"
+".p2p-geo { margin-left: 40px; width: 248px; text-align: left;}"
+"img { border: none;}"
+"</style>"
+"</head><body>";
if(logo)
str += "<div style='text-align:center;'><img src=\""+logo+"\" /></div><br style='clear:both;'/>";
str += "<center><a href='https://github.com/axerunners/p2pool-axe' target='_blank'>PEER TO PEER "+(config.currency.toUpperCase())+" MINING NETWORK</a> - PUBLIC NODE LIST<br/><span style='font-size:10px;color:#333;'>GENERATED ON: "+(new Date())+"</span></center><p/>"
if(self.poolstats)
str += "<center>Pool speed: "+(self.poolstats.pool_hash_rate/1000000).toFixed(2)+" "+config.speed_abbrev+"</center>";
str += "<center>Currently observing "+(self.nodes_total || "N/A")+" nodes.<br/>"+_.size(self.addr_working)+" nodes are public with following IPs:</center><p/>";
str += "<div class='p2p'>";
str += "<div class='p2p-row p2p-caption'><div class='p2p-ip'>IPs</div><div class='p2p-version'>Version</div><div class='p2p-fee'>Fee</div><div class='p2p-uptime'>Uptime</div><div class='p2p-geo'>Location</div>";
str += "</div><br style='clear:both;'/>";
var list = _.sortBy(_.toArray(self.addr_working), function(o) { return o.stats ? -o.stats.uptime : 0; })
var row = 0;
_.each(list, function(info) {
var ip = info.ip;
var version = info.stats.version;
var uptime = (info.stats.uptime / 60 / 60 / 24).toFixed(1);
// var fee = parseFloat((info.fee | 0)).toFixed(2);
var fee = info.fee;
str += "<div class='p2p-row "+(row++ & 1 ? "row-grey" : "")+"'><div class='p2p-ip'><a href='http://"+ip+':'+7923+"/static/' target='_blank'>"+ip+":"+7923+"</a></div><div class='p2p-version'>"+version+"</div><div class='p2p-fee'>"+fee+"%</div><div class='p2p-uptime'>"+uptime+" days</div>";
str += "<div class='p2p-geo'>";
if(info.geo) {
str += "<a href='http://www.geoiptool.com/en/?IP="+info.ip+"' target='_blank'>"+info.geo.country+" "+"<img src='"+info.geo.img+"' align='absmiddle' border='0'/></a>";
}
str += "</div>";
str += "</div>";
str += "<br style='clear:both;'/>";
})
str += "</div><p/><br/>";
str += "</body>"
return str;
}
// setup flushing of rendered HTML page to a file (useful for uploading to other sites)
if(config.flush_to_file_every_N_msec && config.flush_filename) {
function flush_rendering() {
var str = self.render();
fs.writeFile(config.flush_filename, str, { encoding : 'utf8'}, (error) => { console.log("Flush to file failed"); });
dpc(config.flush_to_file_every_N_msec, flush_rendering);
}
dpc(5000, flush_rendering);
}
// defer init
dpc(function(){
self.restore_working();
self.update();
})
var p2pool_init = true;
// main function that reloads 'addr' file from p2pool
self.update = function() {
var filename = config.addr_file;
if(!fs.existsSync(filename)) {
console.error("Unable to fetch p2pool address list from:",config.addr_file);
filename = config.init_file; // if we can't read p2pool's addr file, we just cycle on the local default init...
}
fs.readFile(filename, { encoding : 'utf8' }, function(err, data) {
if(err) {
console.error(err);
}
else {
try {
var addr_list = JSON.parse(data);
self.inject(addr_list);
// main init
if(p2pool_init) {
p2pool_init = false;
// if we can read p2pool addr file, also add our pre-collected IPs
// if(filename != config.init_file) {
var init_addr = JSON.parse(fs.readFileSync(config.init_file, 'utf8'));
self.inject(init_addr);
//}
for(var i = 0; i < (config.probe_N_IPs_simultaneously || 1); i++)
self.digest();
dpc(60*1000, function() { self.store_working(); })
}
}
catch(ex) {
console.error("Unable to parse p2pool address list");
console.error(ex);
}
}
dpc(1000 * 60, self.update);
})
}
// store public pools in a file that reloads at startup
self.store_working = function() {
var data = JSON.stringify(self.addr_working);
fs.writeFile(config.store_file, data, { encoding : 'utf8' }, function(err) {
dpc(60*1000, self.store_working);
})
}
// reload public list at startup
self.restore_working = function() {
try {
self.addr_working = JSON.parse(fs.readFileSync(config.store_file, 'utf8'));
} catch(ex) { console.log(ex); }
}
// inject new IPs from p2pool addr file
self.inject = function(addr_list) {
_.each(addr_list, function(info) {
var ip = info[0][0];
var port = info[0][1];
if(!self.addr_digested[ip] && !self.addr_pending[ip]) {
self.addr_pending[ip] = { ip : ip, port : port }
}
self.nodes_total = _.size(self.addr_digested) + _.size(self.addr_pending);
});
}
// as we scan pools, we fetch global info from them to update the page
self.update_global_stats = function(poolstats) {
self.poolstats = poolstats;
}
// execute scan of a single IP
self.digest = function() {
if(!_.size(self.addr_pending))
return self.list_complete();
var info = _.find(self.addr_pending, function() { return true; });
delete self.addr_pending[info.ip];
if(info.ip == "0.0.0.0" || info.ip == "127.0.0.1") {
return;
}
self.addr_digested[info.ip] = info;
console.log("P2POOL DIGESTING:" + info.ip + ":" + 7923);
var allowedVersions = ["fdc4e2d-dirty",
"20e6354-dirty",
"8542674-dirty",
"c174c98-dirty",
"fcfb6ad-dirty",
"3877fc7-dirty",
"fdc4e2d",
"20e6354",
"8542674",
"c174c98",
"fcfb6ad",
"8d29f46",
"8d29f46-dirty",
"9fc7f3a",
"9fc7f3a-dirty",
"e0909bc-dirty",
"e0909bc",
"4cacc36",
"850d3b2-dirty",
"850d3b2",
"4cacc36-dirty",
"e1a9b6a",
"e1a9b6a-dirty",
"649807a",
"649807a-dirty",
"d7ddaa4",
"d7ddaa4-dirty"];
digest_ip(info, function(err, fee){
if(!err) {
digest_local_stats(info, function(err, stats) {
if(!err && allowedVersions.indexOf(stats.version) >= 0) {
info.fee = fee;
info.stats = stats;
console.log("FOUND WORKING POOL: " + info.ip + ":7923 " + info.stats.version);
self.addr_working[info.ip] = info;
digest_global_stats(info, function(err, stats) {
if(!err)
self.update_global_stats(stats);
if(!info.geo)
self.geo.get(info.ip, function(err, geo) {
if(!err)
info.geo = geo;
continue_digest();
});
else
continue_digest();
});
}
});
}
else {
delete self.addr_working[info.ip];
/*if(!err && allowedVersions.indexOf(info.stats.version) < 0) {
console.log("Node was wrong version: " + info.stats.version);
}*/
continue_digest();
}
function continue_digest() {
self.working_size = _.size(self.addr_working);
dpc(self.digest);
}
});
}
// schedule restar of the scan once all IPs are done
self.list_complete = function() {
self.addr_pending = self.addr_digested;
self.addr_digested = { }
dpc(config.rescan_list_delay, self.digest);
}
// functions to fetch data from target node IP
function digest_ip(info, callback) {
var options = {
host: info.ip,
port: 7923,
path: '/fee',
method: 'GET'
};
self.request(options, callback);
}
function digest_local_stats(info, callback) {
var options = {
host: info.ip,
port: 7923,
path: '/local_stats',
method: 'GET'
};
self.request(options, callback);
}
function digest_global_stats(info, callback) {
var options = {
host: info.ip,
port: 7923,
path: '/global_stats',
method: 'GET'
};
self.request(options, callback);
}
// make http request to the target node ip
self.request = function(options, callback, is_plain)
{
http_handler = http;
var req = http_handler.request(options, function(res) {
res.setEncoding('utf8');
var result = '';
res.on('data', function (data) {
result += data;
});
res.on('end', function () {
if(options.plain)
callback(null, result);
else {
try {
var o = JSON.parse(result);
callback(null, o);
} catch(ex) {
console.error(ex);
callback(ex);
}
}
});
});
req.on('socket', function (socket) {
socket.setTimeout(config.http_socket_timeout);
socket.on('timeout', function() {
req.abort();
});
});
req.on('error', function(e) {
callback(e);
});
req.end();
}
if(upload && process.platform != 'win32') {
function do_upload() {
if(upload.ftp) {
var ftp = upload.ftp;
if(!ftp.address || !ftp.username || !ftp.password)
return console.error("upload.cfg ftp configuration must contain target address, username and password");
var cmd = "curl -T "+config.flush_filename+" "+ftp.address+" --user "+ftp.address+":"+ftp.password;
exec(cmd, function(error){ if(error) console.error(error); });
}
if(upload.scp) {
var scp = upload.scp;
if(!scp.address)
return console.error("upload.cfg scp configuration must contain target address");
var cmd = "scp -q ./"+config.flush_filename+" "+scp.address;
exec(cmd, function(error){ if(error) console.error(error); });
}
dpc(config.upload_interval, do_upload);
}
dpc(5000, do_upload);
}
else
console.log("upload.cfg not found, rendering available only on the local interface");
}
global.scanner = new Scanner();