Skip to content

Commit

Permalink
Rewritten peers sync way
Browse files Browse the repository at this point in the history
  • Loading branch information
typhoonsimon committed Mar 14, 2020
1 parent 917b9c6 commit 8f9945b
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions scripts/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

This comment has been minimized.

Copy link
@uaktags

uaktags Mar 14, 2020

Collaborator

Dont hardcode the port, please revert this change.

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();
});
});
});
});
}
Expand Down

0 comments on commit 8f9945b

Please sign in to comment.