-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (154 loc) · 3.32 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
_ "github.com/anacrolix/envpprof"
"github.com/anacrolix/torrent/dht"
_ "github.com/anacrolix/torrent/util/profile"
)
var (
tableFileName = flag.String("tableFile", "", "name of file for storing node info")
serveAddr = flag.String("serveAddr", ":0", "local UDP address")
infoHash = flag.String("infoHash", "", "torrent infohash")
once = flag.Bool("once", false, "only do one scrape iteration")
s *dht.Server
quitting = make(chan struct{})
)
func loadTable() error {
if *tableFileName == "" {
return nil
}
f, err := os.Open(*tableFileName)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return fmt.Errorf("error opening table file: %s", err)
}
defer f.Close()
added := 0
for {
b := make([]byte, dht.CompactNodeInfoLen)
_, err := io.ReadFull(f, b)
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("error reading table file: %s", err)
}
var ni dht.NodeInfo
err = ni.UnmarshalCompact(b)
if err != nil {
return fmt.Errorf("error unmarshaling compact node info: %s", err)
}
s.AddNode(ni)
added++
}
log.Printf("loaded %d nodes from table file", added)
return nil
}
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()
switch len(*infoHash) {
case 20:
case 40:
_, err := fmt.Sscanf(*infoHash, "%x", infoHash)
if err != nil {
log.Fatal(err)
}
default:
log.Fatal("require 20 byte infohash")
}
var err error
s, err = dht.NewServer(&dht.ServerConfig{
Addr: *serveAddr,
})
if err != nil {
log.Fatal(err)
}
err = loadTable()
if err != nil {
log.Fatalf("error loading table: %s", err)
}
log.Printf("dht server on %s, ID is %x", s.Addr(), s.ID())
setupSignals()
}
func saveTable() error {
goodNodes := s.Nodes()
if *tableFileName == "" {
if len(goodNodes) != 0 {
log.Print("good nodes were discarded because you didn't specify a table file")
}
return nil
}
f, err := os.OpenFile(*tableFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
return fmt.Errorf("error opening table file: %s", err)
}
defer f.Close()
for _, nodeInfo := range goodNodes {
var b [dht.CompactNodeInfoLen]byte
err := nodeInfo.PutCompact(b[:])
if err != nil {
return fmt.Errorf("error compacting node info: %s", err)
}
_, err = f.Write(b[:])
if err != nil {
return fmt.Errorf("error writing compact node info: %s", err)
}
}
log.Printf("saved %d nodes to table file", len(goodNodes))
return nil
}
func setupSignals() {
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
close(quitting)
}()
}
func main() {
seen := make(map[string]struct{})
getPeers:
for {
ps, err := s.Announce(*infoHash, 0, false)
if err != nil {
log.Fatal(err)
}
values:
for {
select {
case v, ok := <-ps.Peers:
if !ok {
break values
}
log.Printf("received %d peers from %x", len(v.Peers), v.NodeInfo.ID)
for _, p := range v.Peers {
if _, ok := seen[p.String()]; ok {
continue
}
seen[p.String()] = struct{}{}
fmt.Println((&net.UDPAddr{
IP: p.IP[:],
Port: int(p.Port),
}).String())
}
case <-quitting:
break getPeers
}
}
if *once {
break
}
}
if err := saveTable(); err != nil {
log.Printf("error saving node table: %s", err)
}
}