diff --git a/scripts/peers.js b/scripts/peers.js index 17909f00e..9902bbfd7 100644 --- a/scripts/peers.js +++ b/scripts/peers.js @@ -23,37 +23,40 @@ mongoose.connect(dbString, function(err) { console.log('Aborting'); exit(); } else { - request({uri: 'http://127.0.0.1:' + settings.port + '/api/getpeerinfo', json: true}, function (error, response, body) { + var peers = Array(); + var cnt = 0; + request({uri: 'http://127.0.0.1:3002/api/getpeerinfo', json: true}, function (error, response, body) { lib.syncLoop(body.length, function (loop) { var i = loop.iteration(); var address = body[i].addr.split(':')[0]; var port = body[i].addr.split(':')[1]; - db.find_peer(address, function(peer) { - if (peer) { - if (isNaN(peer['port']) || peer['port'].length < 2 || peer['country'].length < 1) { - db.drop_peers(function() { - console.log('Saved peers missing ports or country, dropping peers. Re-reun this script afterwards.'); - exit(); - }); - } - // peer already exists - loop.next(); - } else { - request({uri: 'https://freegeoip.app/json/' + address, json: true}, function (error, response, geo) { - db.create_peer({ - address: address, - port: port, - protocol: body[i].version, - version: body[i].subver.replace('/', '').replace('/', ''), - country: geo.country_name - }, function(){ - loop.next(); - }); - }); + request({uri: 'https://freegeoip.app/json/' + address, json: true}, function (error, response, geo) { + if (address.startsWith('10.') || address.startsWith('192.168') || address.startsWith('172.16')) { + geo.country_name = '[private address]'; } + peers[cnt++] = { + address: address, + port: port, + protocol: body[i].version, + version: body[i].subver.replace('/', '').replace('/', ''), + country: geo.country_name + }; + loop.next(); }); }, function() { - exit(); + + // insert all at once after creation + db.drop_peers(function() { + console.log('Dropped, rebuilding...'); + lib.syncLoop(cnt, function (loop) { + var i = loop.iteration(); + db.create_peer(peers[i], function() { + loop.next(); + }); + }, function() { + exit(); + }); + }); }); }); }