-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeers.go
70 lines (57 loc) · 1.27 KB
/
peers.go
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
package main
import (
"os"
"fmt"
"sort"
"time"
"github.com/lentus/wotscoin/lib/others/qdb"
"github.com/lentus/wotscoin/lib/others/sys"
"github.com/lentus/wotscoin/lib/others/utils"
)
type manyPeers []*utils.OnePeer
func (mp manyPeers) Len() int {
return len(mp)
}
func (mp manyPeers) Less(i, j int) bool {
return mp[i].Time > mp[j].Time
}
func (mp manyPeers) Swap(i, j int) {
mp[i], mp[j] = mp[j], mp[i]
}
func main() {
var dir string
if len(os.Args)>1 {
dir = os.Args[1]
} else {
dir = sys.BitcoinHome() + "gocoin" + string(os.PathSeparator) + "btcnet" + string(os.PathSeparator) + "peers3"
}
db, er := qdb.NewDB(dir, true)
if er!=nil {
println(er.Error())
os.Exit(1)
}
println(db.Count(), "peers in databse", dir)
if db.Count()==0 {
return
}
tmp := make(manyPeers, db.Count())
cnt := 0
db.Browse(func(k qdb.KeyType, v []byte) uint32 {
np := utils.NewPeer(v)
if !sys.ValidIp4(np.Ip4[:]) {
return 0
}
if cnt < len(tmp) {
tmp[cnt] = np
cnt++
}
return 0
})
sort.Sort(tmp[:cnt])
for cnt=0; cnt<len(tmp)&&cnt<2500; cnt++ {
ad := tmp[cnt]
fmt.Printf("%3d) %16s %5d - seen %5d min ago\n", cnt+1,
fmt.Sprintf("%d.%d.%d.%d", ad.Ip4[0], ad.Ip4[1], ad.Ip4[2], ad.Ip4[3]),
ad.Port, (time.Now().Unix() - int64(ad.Time))/60)
}
}