-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (57 loc) · 1.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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
_ "net/http/pprof"
"github.com/MildC/dht-crawler/dht"
"github.com/MildC/dht-crawler/torrent"
)
func main() {
go func() {
http.ListenAndServe(":6060", nil)
}()
logger := NewConsoleLogger()
w := dht.NewWire(65536, 1024, 256)
go func() {
for resp := range w.Response() {
metadata, err := dht.Decode(resp.MetadataInfo)
if err != nil {
continue
}
info := metadata.(map[string]interface{})
if _, ok := info["name"]; !ok {
continue
}
bt := torrent.BitTorrent{
InfoHash: hex.EncodeToString(resp.InfoHash),
Name: info["name"].(string),
}
if v, ok := info["files"]; ok {
files := v.([]interface{})
bt.Files = make([]torrent.File, len(files))
for i, item := range files {
f := item.(map[string]interface{})
bt.Files[i] = torrent.File{
Path: f["path"].([]interface{}),
Length: f["length"].(int),
}
}
} else if _, ok := info["length"]; ok {
bt.Length = info["length"].(int)
}
data, err := json.Marshal(bt)
if err == nil {
fmt.Printf("%s\n\n", data)
}
}
}()
go w.Run()
config := dht.NewCrawlConfig()
config.OnAnnouncePeer = func(infoHash, ip string, port int) {
w.Request([]byte(infoHash), ip, port)
}
d := dht.New(logger, config)
d.Run()
}