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
|
package dht
import (
"fmt"
"net"
"github.com/felix/dhtsearch/krpc"
"github.com/felix/dhtsearch/models"
)
func (n *Node) onPingQuery(rn remoteNode, msg map[string]interface{}) error {
t, err := krpc.GetString(msg, "t")
if err != nil {
return err
}
n.queueMsg(rn, krpc.MakeResponse(t, map[string]interface{}{
"id": string(n.id),
}))
return nil
}
func (n *Node) onGetPeersQuery(rn remoteNode, msg map[string]interface{}) error {
a, err := krpc.GetMap(msg, "a")
if err != nil {
return err
}
// This is the ih of the torrent
torrent, err := krpc.GetString(a, "info_hash")
if err != nil {
return err
}
th, err := models.InfohashFromString(torrent)
if err != nil {
return err
}
//n.log.Debug("get_peers query", "source", rn, "torrent", th)
token := torrent[:2]
neighbour := models.GenerateNeighbour(n.id, *th)
/*
nodes := n.rTable.get(8)
compactNS := []string{}
for _, rn := range nodes {
ns := encodeCompactNodeAddr(rn.addr.String())
if ns == "" {
n.log.Warn("failed to compact node", "address", rn.address.String())
continue
}
compactNS = append(compactNS, ns)
}
*/
t := msg["t"].(string)
n.queueMsg(rn, krpc.MakeResponse(t, map[string]interface{}{
"id": string(neighbour),
"token": token,
"nodes": "",
//"nodes": strings.Join(compactNS, ""),
}))
//nodes := n.rTable.get(50)
/*
fmt.Printf("sending get_peers for %s to %d nodes\n", *th, len(nodes))
q := krpc.MakeQuery(newTransactionID(), "get_peers", map[string]interface{}{
"id": string(id),
"info_hash": string(*th),
})
for _, o := range nodes {
n.queueMsg(*o, q)
}
*/
return nil
}
func (n *Node) onAnnouncePeerQuery(rn remoteNode, msg map[string]interface{}) error {
a, err := krpc.GetMap(msg, "a")
if err != nil {
return err
}
//n.log.Debug("announce_peer", "source", rn)
host, port, err := net.SplitHostPort(rn.addr.String())
if err != nil {
return err
}
if port == "0" {
return fmt.Errorf("ignoring port 0")
}
newPort, err := krpc.GetInt(a, "port")
if err == nil {
if iPort, err := krpc.GetInt(a, "implied_port"); err == nil && iPort == 0 {
// Use the port in the message
addr, err := net.ResolveUDPAddr(n.family, fmt.Sprintf("%s:%d", host, newPort))
if err != nil {
return err
}
rn = remoteNode{addr: addr, id: rn.id}
}
}
// TODO do we reply?
ihStr, err := krpc.GetString(a, "info_hash")
if err != nil {
return err
}
ih, err := models.InfohashFromString(ihStr)
if err != nil {
n.log.Warn("invalid torrent", "infohash", ihStr)
}
p := models.Peer{Addr: rn.addr, Infohash: *ih}
if n.OnAnnouncePeer != nil {
go n.OnAnnouncePeer(p)
}
return nil
}
func (n *Node) onFindNodeResponse(rn remoteNode, msg map[string]interface{}) {
r := msg["r"].(map[string]interface{})
nodes := r["nodes"].(string)
n.processFindNodeResults(rn, nodes)
}
|