@@ -23,37 +23,40 @@ mongoose.connect(dbString, function(err) {
2323 console . log ( 'Aborting' ) ;
2424 exit ( ) ;
2525 } else {
26- request ( { uri : 'http://127.0.0.1:' + settings . port + '/api/getpeerinfo' , json : true } , function ( error , response , body ) {
26+ var peers = Array ( ) ;
27+ var cnt = 0 ;
28+ request ( { uri : 'http://127.0.0.1:3002/api/getpeerinfo' , json : true } , function ( error , response , body ) {
2729 lib . syncLoop ( body . length , function ( loop ) {
2830 var i = loop . iteration ( ) ;
2931 var address = body [ i ] . addr . split ( ':' ) [ 0 ] ;
3032 var port = body [ i ] . addr . split ( ':' ) [ 1 ] ;
31- db . find_peer ( address , function ( peer ) {
32- if ( peer ) {
33- if ( isNaN ( peer [ 'port' ] ) || peer [ 'port' ] . length < 2 || peer [ 'country' ] . length < 1 ) {
34- db . drop_peers ( function ( ) {
35- console . log ( 'Saved peers missing ports or country, dropping peers. Re-reun this script afterwards.' ) ;
36- exit ( ) ;
37- } ) ;
38- }
39- // peer already exists
40- loop . next ( ) ;
41- } else {
42- request ( { uri : 'https://freegeoip.app/json/' + address , json : true } , function ( error , response , geo ) {
43- db . create_peer ( {
44- address : address ,
45- port : port ,
46- protocol : body [ i ] . version ,
47- version : body [ i ] . subver . replace ( '/' , '' ) . replace ( '/' , '' ) ,
48- country : geo . country_name
49- } , function ( ) {
50- loop . next ( ) ;
51- } ) ;
52- } ) ;
33+ request ( { uri : 'https://freegeoip.app/json/' + address , json : true } , function ( error , response , geo ) {
34+ if ( address . startsWith ( '10.' ) || address . startsWith ( '192.168' ) || address . startsWith ( '172.16' ) ) {
35+ geo . country_name = '[private address]' ;
5336 }
37+ peers [ cnt ++ ] = {
38+ address : address ,
39+ port : port ,
40+ protocol : body [ i ] . version ,
41+ version : body [ i ] . subver . replace ( '/' , '' ) . replace ( '/' , '' ) ,
42+ country : geo . country_name
43+ } ;
44+ loop . next ( ) ;
5445 } ) ;
5546 } , function ( ) {
56- exit ( ) ;
47+
48+ // insert all at once after creation
49+ db . drop_peers ( function ( ) {
50+ console . log ( 'Dropped, rebuilding...' ) ;
51+ lib . syncLoop ( cnt , function ( loop ) {
52+ var i = loop . iteration ( ) ;
53+ db . create_peer ( peers [ i ] , function ( ) {
54+ loop . next ( ) ;
55+ } ) ;
56+ } , function ( ) {
57+ exit ( ) ;
58+ } ) ;
59+ } ) ;
5760 } ) ;
5861 } ) ;
5962 }
0 commit comments