github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/tools/peers/peers.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "sort" 7 "time" 8 9 "github.com/piotrnar/gocoin/client/network/peersdb" 10 "github.com/piotrnar/gocoin/lib/others/qdb" 11 "github.com/piotrnar/gocoin/lib/others/sys" 12 ) 13 14 type manyPeers []*peersdb.PeerAddr 15 16 func (mp manyPeers) Len() int { 17 return len(mp) 18 } 19 20 func (mp manyPeers) Less(i, j int) bool { 21 return mp[i].Time > mp[j].Time 22 } 23 24 func (mp manyPeers) Swap(i, j int) { 25 mp[i], mp[j] = mp[j], mp[i] 26 } 27 28 func main() { 29 var dir string 30 31 if len(os.Args) > 1 { 32 dir = os.Args[1] 33 } else { 34 dir = sys.BitcoinHome() + "gocoin" + string(os.PathSeparator) + "btcnet" + string(os.PathSeparator) + "peers3" 35 } 36 37 db, er := qdb.NewDB(dir, true) 38 39 if er != nil { 40 println(er.Error()) 41 os.Exit(1) 42 } 43 44 println(db.Count(), "peers in databse", dir) 45 if db.Count() == 0 { 46 return 47 } 48 49 tmp := make(manyPeers, db.Count()) 50 cnt := 0 51 db.Browse(func(k qdb.KeyType, v []byte) uint32 { 52 np := peersdb.NewPeer(v) 53 if !sys.ValidIp4(np.Ip4[:]) { 54 return 0 55 } 56 if cnt < len(tmp) { 57 tmp[cnt] = np 58 cnt++ 59 } 60 return 0 61 }) 62 63 sort.Sort(tmp[:cnt]) 64 for cnt = 0; cnt < len(tmp) && cnt < 2500; cnt++ { 65 ad := tmp[cnt] 66 fmt.Printf("%3d) %16s %5d - seen %5d min ago\n", cnt+1, 67 fmt.Sprintf("%d.%d.%d.%d", ad.Ip4[0], ad.Ip4[1], ad.Ip4[2], ad.Ip4[3]), 68 ad.Port, (time.Now().Unix()-int64(ad.Time))/60) 69 } 70 }