2015-04-01 06:29:55 +00:00
|
|
|
// Package DHT implements a DHT for use with the BitTorrent protocol,
|
|
|
|
// described in BEP 5: http://www.bittorrent.org/beps/bep_0005.html.
|
|
|
|
//
|
|
|
|
// Standard use involves creating a NewServer, and calling Announce on it with
|
|
|
|
// the details of your local torrent client and infohash of interest.
|
2014-05-24 06:51:56 +00:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2014-06-24 13:18:30 +00:00
|
|
|
"crypto"
|
|
|
|
_ "crypto/sha1"
|
2014-05-24 06:51:56 +00:00
|
|
|
"encoding/binary"
|
2014-05-25 11:34:29 +00:00
|
|
|
"errors"
|
2014-05-24 06:51:56 +00:00
|
|
|
"fmt"
|
2015-05-20 12:23:50 +00:00
|
|
|
"hash/crc32"
|
2014-05-24 06:51:56 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2014-11-17 07:47:24 +00:00
|
|
|
"math/big"
|
2014-12-07 03:21:20 +00:00
|
|
|
"math/rand"
|
2014-05-24 06:51:56 +00:00
|
|
|
"net"
|
2014-06-24 13:18:30 +00:00
|
|
|
"os"
|
2015-08-17 09:52:47 +00:00
|
|
|
"strconv"
|
2014-05-24 06:51:56 +00:00
|
|
|
"time"
|
2014-12-28 01:51:09 +00:00
|
|
|
|
2015-08-03 14:29:01 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
2015-03-26 06:18:08 +00:00
|
|
|
"github.com/anacrolix/sync"
|
2015-08-03 14:31:53 +00:00
|
|
|
"github.com/tylertreat/BoomFilters"
|
2015-03-26 06:18:08 +00:00
|
|
|
|
2015-04-29 14:31:34 +00:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-03-20 05:37:44 +00:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
|
|
|
"github.com/anacrolix/torrent/logonce"
|
|
|
|
"github.com/anacrolix/torrent/util"
|
2014-05-24 06:51:56 +00:00
|
|
|
)
|
|
|
|
|
2015-02-21 04:00:48 +00:00
|
|
|
const (
|
|
|
|
maxNodes = 320
|
|
|
|
queryResendEvery = 5 * time.Second
|
|
|
|
)
|
2014-12-09 01:09:11 +00:00
|
|
|
|
2014-12-08 22:59:25 +00:00
|
|
|
// Uniquely identifies a transaction to us.
|
|
|
|
type transactionKey struct {
|
|
|
|
RemoteAddr string // host:port
|
|
|
|
T string // The KRPC transaction ID.
|
|
|
|
}
|
|
|
|
|
2014-05-24 06:51:56 +00:00
|
|
|
type Server struct {
|
2014-08-21 08:07:06 +00:00
|
|
|
id string
|
2014-11-17 03:22:29 +00:00
|
|
|
socket net.PacketConn
|
2015-04-01 06:29:55 +00:00
|
|
|
transactions map[transactionKey]*Transaction
|
2014-05-24 06:51:56 +00:00
|
|
|
transactionIDInt uint64
|
2015-04-01 06:29:55 +00:00
|
|
|
nodes map[string]*node // Keyed by dHTAddr.String().
|
2014-05-27 06:28:56 +00:00
|
|
|
mu sync.Mutex
|
2014-07-09 14:13:54 +00:00
|
|
|
closed chan struct{}
|
2015-09-23 08:25:22 +00:00
|
|
|
ipBlockList iplist.Ranger
|
2015-08-03 14:31:53 +00:00
|
|
|
badNodes *boom.BloomFilter
|
2014-11-16 19:08:33 +00:00
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
numConfirmedAnnounces int
|
2015-04-02 22:35:30 +00:00
|
|
|
bootstrapNodes []string
|
2015-05-20 12:23:50 +00:00
|
|
|
config ServerConfig
|
2014-11-17 03:22:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 08:07:06 +00:00
|
|
|
type ServerConfig struct {
|
2015-04-01 06:29:55 +00:00
|
|
|
Addr string // Listen address. Used if Conn is nil.
|
|
|
|
Conn net.PacketConn
|
|
|
|
// Don't respond to queries from other nodes.
|
|
|
|
Passive bool
|
2015-04-02 22:35:30 +00:00
|
|
|
// DHT Bootstrap nodes
|
|
|
|
BootstrapNodes []string
|
2015-05-20 12:23:50 +00:00
|
|
|
// Disable the DHT security extension:
|
|
|
|
// http://www.libtorrent.org/dht_sec.html.
|
|
|
|
NoSecurity bool
|
2015-06-02 13:58:49 +00:00
|
|
|
// Initial IP blocklist to use. Applied before serving and bootstrapping
|
|
|
|
// begins.
|
2015-09-23 08:25:22 +00:00
|
|
|
IPBlocklist iplist.Ranger
|
2015-08-03 14:37:16 +00:00
|
|
|
// Used to secure the server's ID. Defaults to the Conn's LocalAddr().
|
|
|
|
PublicIP net.IP
|
2015-04-01 06:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServerStats struct {
|
|
|
|
// Count of nodes in the node table that responded to our last query or
|
|
|
|
// haven't yet been queried.
|
|
|
|
GoodNodes int
|
|
|
|
// Count of nodes in the node table.
|
|
|
|
Nodes int
|
|
|
|
// Transactions awaiting a response.
|
|
|
|
OutstandingTransactions int
|
|
|
|
// Individual announce_peer requests that got a success response.
|
|
|
|
ConfirmedAnnounces int
|
2015-08-03 14:31:53 +00:00
|
|
|
// Nodes that have been blocked.
|
|
|
|
BadNodes uint
|
2015-04-01 06:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns statistics for the server.
|
|
|
|
func (s *Server) Stats() (ss ServerStats) {
|
2014-12-07 03:19:02 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, n := range s.nodes {
|
2014-12-09 01:09:11 +00:00
|
|
|
if n.DefinitelyGood() {
|
2015-04-01 06:29:55 +00:00
|
|
|
ss.GoodNodes++
|
2014-12-07 03:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
ss.Nodes = len(s.nodes)
|
|
|
|
ss.OutstandingTransactions = len(s.transactions)
|
|
|
|
ss.ConfirmedAnnounces = s.numConfirmedAnnounces
|
2015-08-03 14:31:53 +00:00
|
|
|
ss.BadNodes = s.badNodes.Count()
|
2014-12-07 03:19:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Returns the listen address for the server. Packets arriving to this address
|
|
|
|
// are processed by the server (unless aliens are involved).
|
|
|
|
func (s *Server) Addr() net.Addr {
|
2014-08-21 08:07:06 +00:00
|
|
|
return s.socket.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeSocket(addr string) (socket *net.UDPConn, err error) {
|
|
|
|
addr_, err := net.ResolveUDPAddr("", addr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
socket, err = net.ListenUDP("udp", addr_)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Create a new DHT server.
|
2014-08-21 08:07:06 +00:00
|
|
|
func NewServer(c *ServerConfig) (s *Server, err error) {
|
2014-08-21 11:12:50 +00:00
|
|
|
if c == nil {
|
|
|
|
c = &ServerConfig{}
|
|
|
|
}
|
2015-05-20 12:23:50 +00:00
|
|
|
s = &Server{
|
2015-06-02 13:58:49 +00:00
|
|
|
config: *c,
|
|
|
|
ipBlockList: c.IPBlocklist,
|
2015-08-03 14:31:53 +00:00
|
|
|
badNodes: boom.NewBloomFilter(1000, 0.1),
|
2015-05-20 12:23:50 +00:00
|
|
|
}
|
2014-11-17 05:27:01 +00:00
|
|
|
if c.Conn != nil {
|
|
|
|
s.socket = c.Conn
|
|
|
|
} else {
|
|
|
|
s.socket, err = makeSocket(c.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-21 08:07:06 +00:00
|
|
|
}
|
2015-04-02 22:35:30 +00:00
|
|
|
s.bootstrapNodes = c.BootstrapNodes
|
2014-08-21 08:07:06 +00:00
|
|
|
err = s.init()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
2014-08-21 11:12:50 +00:00
|
|
|
err := s.serve()
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-08-21 08:07:06 +00:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
err := s.bootstrap()
|
|
|
|
if err != nil {
|
2015-06-29 09:34:42 +00:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
default:
|
|
|
|
log.Printf("error bootstrapping DHT: %s", err)
|
|
|
|
}
|
2014-08-21 08:07:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Returns a description of the Server. Python repr-style.
|
2014-07-09 14:13:54 +00:00
|
|
|
func (s *Server) String() string {
|
2014-08-21 08:07:06 +00:00
|
|
|
return fmt.Sprintf("dht server on %s", s.socket.LocalAddr())
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-26 06:21:48 +00:00
|
|
|
type nodeID struct {
|
|
|
|
i big.Int
|
|
|
|
set bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nid *nodeID) IsUnset() bool {
|
|
|
|
return !nid.set
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodeIDFromString(s string) (ret nodeID) {
|
|
|
|
if s == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret.i.SetBytes([]byte(s))
|
|
|
|
ret.set = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nid0 *nodeID) Distance(nid1 *nodeID) (ret big.Int) {
|
|
|
|
if nid0.IsUnset() != nid1.IsUnset() {
|
|
|
|
ret = maxDistance
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret.Xor(&nid0.i, &nid1.i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-20 12:23:50 +00:00
|
|
|
func (nid *nodeID) ByteString() string {
|
|
|
|
var buf [20]byte
|
|
|
|
b := nid.i.Bytes()
|
|
|
|
copy(buf[20-len(b):], b)
|
|
|
|
return string(buf[:])
|
2014-12-26 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
type node struct {
|
2014-11-17 03:22:29 +00:00
|
|
|
addr dHTAddr
|
2014-12-26 06:21:48 +00:00
|
|
|
id nodeID
|
2014-11-16 19:08:33 +00:00
|
|
|
announceToken string
|
2014-12-07 03:21:20 +00:00
|
|
|
|
|
|
|
lastGotQuery time.Time
|
|
|
|
lastGotResponse time.Time
|
|
|
|
lastSentQuery time.Time
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 12:23:50 +00:00
|
|
|
func (n *node) IsSecure() bool {
|
|
|
|
if n.id.IsUnset() {
|
|
|
|
return false
|
|
|
|
}
|
2015-08-04 16:37:43 +00:00
|
|
|
return NodeIdSecure(n.id.ByteString(), n.addr.IP())
|
2015-05-20 12:23:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (n *node) idString() string {
|
2015-05-20 12:23:50 +00:00
|
|
|
return n.id.ByteString()
|
2014-12-26 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (n *node) SetIDFromBytes(b []byte) {
|
2015-05-20 12:23:50 +00:00
|
|
|
if len(b) != 20 {
|
|
|
|
panic(b)
|
|
|
|
}
|
2014-12-26 06:21:48 +00:00
|
|
|
n.id.i.SetBytes(b)
|
|
|
|
n.id.set = true
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (n *node) SetIDFromString(s string) {
|
2015-05-20 12:23:50 +00:00
|
|
|
n.SetIDFromBytes([]byte(s))
|
2014-12-26 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (n *node) IDNotSet() bool {
|
2014-12-26 06:21:48 +00:00
|
|
|
return n.id.i.Int64() == 0
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (n *node) NodeInfo() (ret NodeInfo) {
|
2014-07-11 15:24:01 +00:00
|
|
|
ret.Addr = n.addr
|
2014-12-26 06:21:48 +00:00
|
|
|
if n := copy(ret.ID[:], n.idString()); n != 20 {
|
2014-07-11 15:24:01 +00:00
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (n *node) DefinitelyGood() bool {
|
2014-12-26 06:21:48 +00:00
|
|
|
if len(n.idString()) != 20 {
|
2014-05-27 06:28:56 +00:00
|
|
|
return false
|
|
|
|
}
|
2014-12-09 03:57:53 +00:00
|
|
|
// No reason to think ill of them if they've never been queried.
|
2014-12-07 03:21:20 +00:00
|
|
|
if n.lastSentQuery.IsZero() {
|
2014-11-18 00:02:16 +00:00
|
|
|
return true
|
|
|
|
}
|
2014-12-07 03:21:20 +00:00
|
|
|
// They answered our last query.
|
|
|
|
if n.lastSentQuery.Before(n.lastGotResponse) {
|
2014-11-18 00:02:16 +00:00
|
|
|
return true
|
|
|
|
}
|
2014-05-27 06:28:56 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// A wrapper around the unmarshalled KRPC dict that constitutes messages in
|
|
|
|
// the DHT. There are various helpers for extracting common data from the
|
|
|
|
// message. In normal use, Msg is abstracted away for you, but it can be of
|
|
|
|
// interest.
|
2014-05-24 06:51:56 +00:00
|
|
|
type Msg map[string]interface{}
|
|
|
|
|
|
|
|
var _ fmt.Stringer = Msg{}
|
|
|
|
|
|
|
|
func (m Msg) String() string {
|
|
|
|
return fmt.Sprintf("%#v", m)
|
|
|
|
}
|
|
|
|
|
2014-07-16 07:10:17 +00:00
|
|
|
func (m Msg) T() (t string) {
|
|
|
|
tif, ok := m["t"]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t, _ = tif.(string)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-17 16:11:09 +00:00
|
|
|
func (m Msg) Args() map[string]interface{} {
|
2014-12-26 06:21:48 +00:00
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
2015-08-17 16:11:09 +00:00
|
|
|
return m["a"].(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Msg) SenderID() string {
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
|
|
|
switch m["y"].(string) {
|
|
|
|
case "q":
|
|
|
|
return m.Args()["id"].(string)
|
|
|
|
case "r":
|
|
|
|
return m["r"].(map[string]interface{})["id"].(string)
|
|
|
|
}
|
|
|
|
return ""
|
2014-12-26 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 03:20:21 +00:00
|
|
|
// Suggested nodes in a response.
|
|
|
|
func (m Msg) Nodes() (nodes []NodeInfo) {
|
|
|
|
b := func() string {
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
|
|
|
return m["r"].(map[string]interface{})["nodes"].(string)
|
|
|
|
}()
|
2015-02-06 03:54:59 +00:00
|
|
|
if len(b)%26 != 0 {
|
|
|
|
return
|
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
for i := 0; i < len(b); i += 26 {
|
|
|
|
var n NodeInfo
|
|
|
|
err := n.UnmarshalCompact([]byte(b[i : i+26]))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodes = append(nodes, n)
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
return
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 19:08:33 +00:00
|
|
|
type KRPCError struct {
|
|
|
|
Code int
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me KRPCError) Error() string {
|
|
|
|
return fmt.Sprintf("KRPC error %d: %s", me.Code, me.Msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ error = KRPCError{}
|
|
|
|
|
2014-11-18 18:38:13 +00:00
|
|
|
func (m Msg) Error() (ret *KRPCError) {
|
2014-11-16 19:08:33 +00:00
|
|
|
if m["y"] != "e" {
|
2014-11-18 18:38:13 +00:00
|
|
|
return
|
2014-11-16 19:08:33 +00:00
|
|
|
}
|
2014-11-18 18:38:13 +00:00
|
|
|
ret = &KRPCError{}
|
|
|
|
switch e := m["e"].(type) {
|
|
|
|
case []interface{}:
|
|
|
|
ret.Code = int(e[0].(int64))
|
|
|
|
ret.Msg = e[1].(string)
|
|
|
|
case string:
|
|
|
|
ret.Msg = e
|
|
|
|
default:
|
|
|
|
logonce.Stderr.Printf(`KRPC error "e" value has unexpected type: %T`, e)
|
|
|
|
}
|
|
|
|
return
|
2014-11-16 19:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the token given in response to a get_peers request for future
|
|
|
|
// announce_peer requests to that node.
|
2015-01-29 03:20:21 +00:00
|
|
|
func (m Msg) AnnounceToken() (token string, ok bool) {
|
2014-11-16 19:08:33 +00:00
|
|
|
defer func() { recover() }()
|
2015-01-29 03:20:21 +00:00
|
|
|
token, ok = m["r"].(map[string]interface{})["token"].(string)
|
|
|
|
return
|
2014-11-16 19:08:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
type Transaction struct {
|
2015-02-21 04:00:48 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
remoteAddr dHTAddr
|
|
|
|
t string
|
|
|
|
response chan Msg
|
|
|
|
onResponse func(Msg) // Called with the server locked.
|
|
|
|
done chan struct{}
|
|
|
|
queryPacket []byte
|
|
|
|
timer *time.Timer
|
|
|
|
s *Server
|
|
|
|
retries int
|
|
|
|
lastSend time.Time
|
|
|
|
userOnResponse func(Msg)
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Set a function to be called with the response.
|
|
|
|
func (t *Transaction) SetResponseHandler(f func(Msg)) {
|
2015-02-21 04:00:48 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
t.userOnResponse = f
|
|
|
|
t.tryHandleResponse()
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) tryHandleResponse() {
|
2015-02-21 04:00:48 +00:00
|
|
|
if t.userOnResponse == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case r := <-t.response:
|
|
|
|
t.userOnResponse(r)
|
|
|
|
// Shouldn't be called more than once.
|
|
|
|
t.userOnResponse = nil
|
|
|
|
default:
|
|
|
|
}
|
2014-12-07 03:21:20 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) key() transactionKey {
|
2014-12-08 22:59:25 +00:00
|
|
|
return transactionKey{
|
|
|
|
t.remoteAddr.String(),
|
|
|
|
t.t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 03:21:20 +00:00
|
|
|
func jitterDuration(average time.Duration, plusMinus time.Duration) time.Duration {
|
|
|
|
return average - plusMinus/2 + time.Duration(rand.Int63n(int64(plusMinus)))
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) startTimer() {
|
2015-02-21 04:00:48 +00:00
|
|
|
t.timer = time.AfterFunc(jitterDuration(queryResendEvery, time.Second), t.timerCallback)
|
2014-12-07 03:21:20 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) timerCallback() {
|
2014-12-07 03:21:20 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
select {
|
|
|
|
case <-t.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if t.retries == 2 {
|
|
|
|
t.timeout()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.retries++
|
|
|
|
t.sendQuery()
|
2015-02-21 04:00:48 +00:00
|
|
|
if t.timer.Reset(jitterDuration(queryResendEvery, time.Second)) {
|
2014-12-07 03:21:20 +00:00
|
|
|
panic("timer should have fired to get here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) sendQuery() error {
|
2015-02-21 04:00:48 +00:00
|
|
|
err := t.s.writeToNode(t.queryPacket, t.remoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.lastSend = time.Now()
|
|
|
|
return nil
|
2014-07-17 06:03:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) timeout() {
|
2014-12-09 01:09:11 +00:00
|
|
|
go func() {
|
|
|
|
t.s.mu.Lock()
|
|
|
|
defer t.s.mu.Unlock()
|
|
|
|
t.s.nodeTimedOut(t.remoteAddr)
|
|
|
|
}()
|
2014-12-07 03:21:20 +00:00
|
|
|
t.close()
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) close() {
|
2014-12-07 03:21:20 +00:00
|
|
|
if t.closing() {
|
|
|
|
return
|
|
|
|
}
|
2014-12-09 01:09:11 +00:00
|
|
|
t.queryPacket = nil
|
2015-02-21 04:00:48 +00:00
|
|
|
close(t.response)
|
|
|
|
t.tryHandleResponse()
|
2014-12-07 03:21:20 +00:00
|
|
|
close(t.done)
|
|
|
|
t.timer.Stop()
|
2014-12-08 22:59:25 +00:00
|
|
|
go func() {
|
|
|
|
t.s.mu.Lock()
|
|
|
|
defer t.s.mu.Unlock()
|
|
|
|
t.s.deleteTransaction(t)
|
|
|
|
}()
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) closing() bool {
|
2014-11-18 18:38:13 +00:00
|
|
|
select {
|
|
|
|
case <-t.done:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Abandon the transaction.
|
|
|
|
func (t *Transaction) Close() {
|
2014-11-18 18:38:13 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2014-12-07 03:21:20 +00:00
|
|
|
t.close()
|
2014-07-09 14:13:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (t *Transaction) handleResponse(m Msg) {
|
2014-11-18 18:38:13 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
if t.closing() {
|
|
|
|
t.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2014-07-17 06:03:59 +00:00
|
|
|
close(t.done)
|
2014-11-18 18:38:13 +00:00
|
|
|
t.mu.Unlock()
|
2014-07-09 14:13:54 +00:00
|
|
|
if t.onResponse != nil {
|
2014-12-09 02:00:42 +00:00
|
|
|
t.s.mu.Lock()
|
2014-07-09 14:13:54 +00:00
|
|
|
t.onResponse(m)
|
2014-12-09 02:00:42 +00:00
|
|
|
t.s.mu.Unlock()
|
2014-07-09 14:13:54 +00:00
|
|
|
}
|
2014-12-09 01:09:11 +00:00
|
|
|
t.queryPacket = nil
|
2014-08-25 12:14:10 +00:00
|
|
|
select {
|
2015-02-21 04:00:48 +00:00
|
|
|
case t.response <- m:
|
2014-08-25 12:14:10 +00:00
|
|
|
default:
|
|
|
|
panic("blocked handling response")
|
|
|
|
}
|
2015-02-21 04:00:48 +00:00
|
|
|
close(t.response)
|
|
|
|
t.tryHandleResponse()
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 12:23:50 +00:00
|
|
|
func maskForIP(ip net.IP) []byte {
|
|
|
|
switch {
|
|
|
|
case ip.To4() != nil:
|
|
|
|
return []byte{0x03, 0x0f, 0x3f, 0xff}
|
|
|
|
default:
|
|
|
|
return []byte{0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the CRC used to make or validate secure node ID.
|
|
|
|
func crcIP(ip net.IP, rand uint8) uint32 {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
ip = ip4
|
|
|
|
}
|
|
|
|
// Copy IP so we can make changes. Go sux at this.
|
|
|
|
ip = append(make(net.IP, 0, len(ip)), ip...)
|
|
|
|
mask := maskForIP(ip)
|
|
|
|
for i := range mask {
|
|
|
|
ip[i] &= mask[i]
|
|
|
|
}
|
|
|
|
r := rand & 7
|
|
|
|
ip[0] |= r << 5
|
|
|
|
return crc32.Checksum(ip[:len(mask)], crc32.MakeTable(crc32.Castagnoli))
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:37:43 +00:00
|
|
|
// Makes a node ID secure, in-place. The ID is 20 raw bytes.
|
|
|
|
// http://www.libtorrent.org/dht_sec.html
|
|
|
|
func SecureNodeId(id []byte, ip net.IP) {
|
2015-05-20 12:23:50 +00:00
|
|
|
crc := crcIP(ip, id[19])
|
|
|
|
id[0] = byte(crc >> 24 & 0xff)
|
|
|
|
id[1] = byte(crc >> 16 & 0xff)
|
|
|
|
id[2] = byte(crc>>8&0xf8) | id[2]&7
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:37:43 +00:00
|
|
|
// Returns whether the node ID is considered secure. The id is the 20 raw
|
|
|
|
// bytes. http://www.libtorrent.org/dht_sec.html
|
|
|
|
func NodeIdSecure(id string, ip net.IP) bool {
|
2015-05-20 12:23:50 +00:00
|
|
|
if len(id) != 20 {
|
|
|
|
panic(fmt.Sprintf("%q", id))
|
|
|
|
}
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
ip = ip4
|
|
|
|
}
|
|
|
|
crc := crcIP(ip, id[19])
|
|
|
|
if id[0] != byte(crc>>24&0xff) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if id[1] != byte(crc>>16&0xff) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if id[2]&0xf8 != byte(crc>>8&0xf8) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-26 14:57:07 +00:00
|
|
|
func (s *Server) setDefaults() (err error) {
|
2014-08-21 08:07:06 +00:00
|
|
|
if s.id == "" {
|
2014-05-24 06:51:56 +00:00
|
|
|
var id [20]byte
|
2014-06-24 13:18:30 +00:00
|
|
|
h := crypto.SHA1.New()
|
|
|
|
ss, err := os.Hostname()
|
2014-05-24 06:51:56 +00:00
|
|
|
if err != nil {
|
2014-06-24 13:18:30 +00:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2014-08-21 08:07:06 +00:00
|
|
|
ss += s.socket.LocalAddr().String()
|
2014-06-24 13:18:30 +00:00
|
|
|
h.Write([]byte(ss))
|
|
|
|
if b := h.Sum(id[:0:20]); len(b) != 20 {
|
|
|
|
panic(len(b))
|
|
|
|
}
|
|
|
|
if len(id) != 20 {
|
|
|
|
panic(len(id))
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
2015-08-03 14:37:16 +00:00
|
|
|
publicIP := func() net.IP {
|
|
|
|
if s.config.PublicIP != nil {
|
|
|
|
return s.config.PublicIP
|
|
|
|
} else {
|
|
|
|
return missinggo.AddrIP(s.socket.LocalAddr())
|
|
|
|
}
|
|
|
|
}()
|
2015-08-04 16:37:43 +00:00
|
|
|
SecureNodeId(id[:], publicIP)
|
2014-08-21 08:07:06 +00:00
|
|
|
s.id = string(id[:])
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
2015-05-20 12:24:39 +00:00
|
|
|
s.nodes = make(map[string]*node, maxNodes)
|
2014-06-26 14:57:07 +00:00
|
|
|
return
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Packets to and from any address matching a range in the list are dropped.
|
2015-09-23 08:25:22 +00:00
|
|
|
func (s *Server) SetIPBlockList(list iplist.Ranger) {
|
2014-11-29 01:41:53 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.ipBlockList = list
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:25:22 +00:00
|
|
|
func (s *Server) IPBlocklist() iplist.Ranger {
|
2015-08-03 15:12:09 +00:00
|
|
|
return s.ipBlockList
|
|
|
|
}
|
|
|
|
|
2014-08-21 08:07:06 +00:00
|
|
|
func (s *Server) init() (err error) {
|
2014-07-09 14:13:54 +00:00
|
|
|
err = s.setDefaults()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.closed = make(chan struct{})
|
2015-04-01 06:29:55 +00:00
|
|
|
s.transactions = make(map[transactionKey]*Transaction)
|
2014-07-09 14:13:54 +00:00
|
|
|
return
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 00:03:03 +00:00
|
|
|
func (s *Server) processPacket(b []byte, addr dHTAddr) {
|
2015-08-05 22:59:30 +00:00
|
|
|
if len(b) < 2 || b[0] != 'd' || b[len(b)-1] != 'e' {
|
|
|
|
// KRPC messages are bencoded dicts.
|
|
|
|
readNotKRPCDict.Add(1)
|
|
|
|
return
|
|
|
|
}
|
2014-11-18 00:03:03 +00:00
|
|
|
var d Msg
|
|
|
|
err := bencode.Unmarshal(b, &d)
|
|
|
|
if err != nil {
|
2015-06-29 09:36:08 +00:00
|
|
|
readUnmarshalError.Add(1)
|
2014-12-01 23:01:55 +00:00
|
|
|
func() {
|
|
|
|
if se, ok := err.(*bencode.SyntaxError); ok {
|
2014-12-03 00:43:28 +00:00
|
|
|
// The message was truncated.
|
|
|
|
if int(se.Offset) == len(b) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Some messages seem to drop to nul chars abrubtly.
|
2014-12-02 01:12:26 +00:00
|
|
|
if int(se.Offset) < len(b) && b[se.Offset] == 0 {
|
2014-12-01 23:01:55 +00:00
|
|
|
return
|
2014-11-19 03:54:53 +00:00
|
|
|
}
|
2014-12-03 00:43:28 +00:00
|
|
|
// The message isn't bencode from the first.
|
2014-12-01 23:01:55 +00:00
|
|
|
if se.Offset == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 23:02:50 +00:00
|
|
|
if missinggo.CryHeard() {
|
|
|
|
log.Printf("%s: received bad krpc message from %s: %s: %+q", s, addr, err, b)
|
|
|
|
}
|
2014-12-01 23:01:55 +00:00
|
|
|
}()
|
2014-11-18 00:03:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if d["y"] == "q" {
|
2015-06-29 09:36:08 +00:00
|
|
|
readQuery.Add(1)
|
2014-11-18 00:03:03 +00:00
|
|
|
s.handleQuery(addr, d)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t := s.findResponseTransaction(d.T(), addr)
|
|
|
|
if t == nil {
|
|
|
|
//log.Printf("unexpected message: %#v", d)
|
|
|
|
return
|
|
|
|
}
|
2015-08-17 16:11:09 +00:00
|
|
|
node := s.getNode(addr, d.SenderID())
|
2014-12-08 22:59:25 +00:00
|
|
|
node.lastGotResponse = time.Now()
|
|
|
|
// TODO: Update node ID as this is an authoritative packet.
|
|
|
|
go t.handleResponse(d)
|
|
|
|
s.deleteTransaction(t)
|
2014-11-18 00:03:03 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 08:07:06 +00:00
|
|
|
func (s *Server) serve() error {
|
2014-12-03 07:08:18 +00:00
|
|
|
var b [0x10000]byte
|
2014-05-24 06:51:56 +00:00
|
|
|
for {
|
2014-11-18 00:03:03 +00:00
|
|
|
n, addr, err := s.socket.ReadFrom(b[:])
|
2014-05-24 06:51:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-29 09:36:08 +00:00
|
|
|
read.Add(1)
|
2014-11-19 03:54:53 +00:00
|
|
|
if n == len(b) {
|
|
|
|
logonce.Stderr.Printf("received dht packet exceeds buffer size")
|
|
|
|
continue
|
|
|
|
}
|
2015-06-02 14:00:25 +00:00
|
|
|
s.mu.Lock()
|
2015-08-03 14:29:01 +00:00
|
|
|
blocked := s.ipBlocked(missinggo.AddrIP(addr))
|
2015-06-02 14:00:25 +00:00
|
|
|
s.mu.Unlock()
|
|
|
|
if blocked {
|
2015-06-29 09:36:08 +00:00
|
|
|
readBlocked.Add(1)
|
2015-04-01 06:37:32 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-01-11 10:42:57 +00:00
|
|
|
s.processPacket(b[:n], newDHTAddr(addr))
|
2014-05-27 06:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 13:00:26 +00:00
|
|
|
func (s *Server) ipBlocked(ip net.IP) (blocked bool) {
|
2014-11-29 01:41:53 +00:00
|
|
|
if s.ipBlockList == nil {
|
2015-10-18 13:00:26 +00:00
|
|
|
return
|
2014-11-29 01:41:53 +00:00
|
|
|
}
|
2015-10-18 13:00:26 +00:00
|
|
|
_, blocked = s.ipBlockList.Lookup(ip)
|
|
|
|
return
|
2014-11-29 01:41:53 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Adds directly to the node table.
|
2014-05-27 06:28:56 +00:00
|
|
|
func (s *Server) AddNode(ni NodeInfo) {
|
2014-11-18 18:38:13 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2014-05-27 06:28:56 +00:00
|
|
|
if s.nodes == nil {
|
2015-04-01 06:29:55 +00:00
|
|
|
s.nodes = make(map[string]*node)
|
2014-05-27 06:28:56 +00:00
|
|
|
}
|
2015-05-20 12:23:50 +00:00
|
|
|
s.getNode(ni.Addr, string(ni.ID[:]))
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) nodeByID(id string) *node {
|
2014-07-11 15:24:01 +00:00
|
|
|
for _, node := range s.nodes {
|
2014-12-26 06:21:48 +00:00
|
|
|
if node.idString() == id {
|
2014-07-11 15:24:01 +00:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:22:29 +00:00
|
|
|
func (s *Server) handleQuery(source dHTAddr, m Msg) {
|
2015-08-17 16:12:27 +00:00
|
|
|
node := s.getNode(source, m.SenderID())
|
2014-12-07 03:21:20 +00:00
|
|
|
node.lastGotQuery = time.Now()
|
|
|
|
// Don't respond.
|
2015-08-17 16:11:48 +00:00
|
|
|
if s.config.Passive {
|
2014-11-28 18:04:38 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-17 16:12:52 +00:00
|
|
|
args := m.Args()
|
|
|
|
if args == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-07-11 15:24:01 +00:00
|
|
|
switch m["q"] {
|
|
|
|
case "ping":
|
|
|
|
s.reply(source, m["t"].(string), nil)
|
2014-12-26 06:21:48 +00:00
|
|
|
case "get_peers": // TODO: Extract common behaviour with find_node.
|
2014-07-11 15:24:01 +00:00
|
|
|
targetID := args["info_hash"].(string)
|
2014-12-19 23:10:53 +00:00
|
|
|
if len(targetID) != 20 {
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 15:24:01 +00:00
|
|
|
var rNodes []NodeInfo
|
|
|
|
// TODO: Reply with "values" list if we have peers instead.
|
|
|
|
for _, node := range s.closestGoodNodes(8, targetID) {
|
|
|
|
rNodes = append(rNodes, node.NodeInfo())
|
|
|
|
}
|
|
|
|
nodesBytes := make([]byte, CompactNodeInfoLen*len(rNodes))
|
|
|
|
for i, ni := range rNodes {
|
|
|
|
err := ni.PutCompact(nodesBytes[i*CompactNodeInfoLen : (i+1)*CompactNodeInfoLen])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.reply(source, m["t"].(string), map[string]interface{}{
|
|
|
|
"nodes": string(nodesBytes),
|
|
|
|
"token": "hi",
|
|
|
|
})
|
2014-12-26 06:21:48 +00:00
|
|
|
case "find_node": // TODO: Extract common behaviour with get_peers.
|
2014-07-11 15:24:01 +00:00
|
|
|
targetID := args["target"].(string)
|
2014-07-17 16:38:11 +00:00
|
|
|
if len(targetID) != 20 {
|
|
|
|
log.Printf("bad DHT query: %v", m)
|
|
|
|
return
|
|
|
|
}
|
2014-07-11 15:24:01 +00:00
|
|
|
var rNodes []NodeInfo
|
|
|
|
if node := s.nodeByID(targetID); node != nil {
|
|
|
|
rNodes = append(rNodes, node.NodeInfo())
|
|
|
|
} else {
|
|
|
|
for _, node := range s.closestGoodNodes(8, targetID) {
|
|
|
|
rNodes = append(rNodes, node.NodeInfo())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodesBytes := make([]byte, CompactNodeInfoLen*len(rNodes))
|
|
|
|
for i, ni := range rNodes {
|
2014-12-26 06:21:48 +00:00
|
|
|
// TODO: Put IPv6 nodes into the correct dict element.
|
|
|
|
if ni.Addr.UDPAddr().IP.To4() == nil {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-11 15:24:01 +00:00
|
|
|
err := ni.PutCompact(nodesBytes[i*CompactNodeInfoLen : (i+1)*CompactNodeInfoLen])
|
|
|
|
if err != nil {
|
2014-12-26 06:21:48 +00:00
|
|
|
log.Printf("error compacting %#v: %s", ni, err)
|
|
|
|
continue
|
2014-07-11 15:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s.reply(source, m["t"].(string), map[string]interface{}{
|
|
|
|
"nodes": string(nodesBytes),
|
|
|
|
})
|
|
|
|
case "announce_peer":
|
2014-07-16 07:10:49 +00:00
|
|
|
// TODO(anacrolix): Implement this lolz.
|
|
|
|
// log.Print(m)
|
|
|
|
case "vote":
|
|
|
|
// TODO(anacrolix): Or reject, I don't think I want this.
|
2014-07-11 15:24:01 +00:00
|
|
|
default:
|
2014-07-09 14:13:54 +00:00
|
|
|
log.Printf("%s: not handling received query: q=%s", s, m["q"])
|
2014-05-25 13:04:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:22:29 +00:00
|
|
|
func (s *Server) reply(addr dHTAddr, t string, r map[string]interface{}) {
|
2014-07-11 15:24:01 +00:00
|
|
|
if r == nil {
|
|
|
|
r = make(map[string]interface{}, 1)
|
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
r["id"] = s.ID()
|
2014-05-25 13:04:55 +00:00
|
|
|
m := map[string]interface{}{
|
|
|
|
"t": t,
|
|
|
|
"y": "r",
|
2014-07-11 15:24:01 +00:00
|
|
|
"r": r,
|
2014-05-25 13:04:55 +00:00
|
|
|
}
|
|
|
|
b, err := bencode.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-24 13:19:19 +00:00
|
|
|
err = s.writeToNode(b, addr)
|
2014-05-25 13:04:55 +00:00
|
|
|
if err != nil {
|
2014-11-29 01:41:53 +00:00
|
|
|
log.Printf("error replying to %s: %s", addr, err)
|
2014-05-25 13:04:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 12:23:50 +00:00
|
|
|
// Returns a node struct for the addr. It is taken from the table or created
|
|
|
|
// and possibly added if required and meets validity constraints.
|
|
|
|
func (s *Server) getNode(addr dHTAddr, id string) (n *node) {
|
2014-12-26 06:21:48 +00:00
|
|
|
addrStr := addr.String()
|
|
|
|
n = s.nodes[addrStr]
|
2015-05-20 12:23:50 +00:00
|
|
|
if n != nil {
|
|
|
|
if id != "" {
|
|
|
|
n.SetIDFromString(id)
|
2014-12-09 01:09:11 +00:00
|
|
|
}
|
2015-05-20 12:23:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
n = &node{
|
|
|
|
addr: addr,
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
2015-06-08 08:16:29 +00:00
|
|
|
if len(id) == 20 {
|
2015-05-20 12:23:50 +00:00
|
|
|
n.SetIDFromString(id)
|
|
|
|
}
|
|
|
|
if len(s.nodes) >= maxNodes {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !s.config.NoSecurity && !n.IsSecure() {
|
|
|
|
return
|
|
|
|
}
|
2015-08-03 14:31:53 +00:00
|
|
|
if s.badNodes.Test([]byte(addrStr)) {
|
|
|
|
return
|
|
|
|
}
|
2015-05-20 12:23:50 +00:00
|
|
|
s.nodes[addrStr] = n
|
2014-05-24 06:51:56 +00:00
|
|
|
return
|
|
|
|
}
|
2015-05-20 12:23:50 +00:00
|
|
|
|
2014-12-09 01:09:11 +00:00
|
|
|
func (s *Server) nodeTimedOut(addr dHTAddr) {
|
|
|
|
node, ok := s.nodes[addr.String()]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if node.DefinitelyGood() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(s.nodes) < maxNodes {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(s.nodes, addr.String())
|
|
|
|
}
|
2014-05-24 06:51:56 +00:00
|
|
|
|
2014-11-17 03:22:29 +00:00
|
|
|
func (s *Server) writeToNode(b []byte, node dHTAddr) (err error) {
|
2014-11-29 01:41:53 +00:00
|
|
|
if list := s.ipBlockList; list != nil {
|
2015-10-18 13:02:42 +00:00
|
|
|
if r, ok := list.Lookup(missinggo.AddrIP(node.UDPAddr())); ok {
|
2014-11-29 01:41:53 +00:00
|
|
|
err = fmt.Errorf("write to %s blocked: %s", node, r.Description)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-12-26 06:21:48 +00:00
|
|
|
n, err := s.socket.WriteTo(b, node.UDPAddr())
|
2014-06-24 13:19:19 +00:00
|
|
|
if err != nil {
|
2015-02-21 04:00:48 +00:00
|
|
|
err = fmt.Errorf("error writing %d bytes to %s: %#v", len(b), node, err)
|
2014-06-24 13:19:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) findResponseTransaction(transactionID string, sourceNode dHTAddr) *Transaction {
|
2014-12-08 22:59:25 +00:00
|
|
|
return s.transactions[transactionKey{
|
|
|
|
sourceNode.String(),
|
|
|
|
transactionID}]
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) nextTransactionID() string {
|
|
|
|
var b [binary.MaxVarintLen64]byte
|
|
|
|
n := binary.PutUvarint(b[:], s.transactionIDInt)
|
|
|
|
s.transactionIDInt++
|
|
|
|
return string(b[:n])
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) deleteTransaction(t *Transaction) {
|
|
|
|
delete(s.transactions, t.key())
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) addTransaction(t *Transaction) {
|
|
|
|
if _, ok := s.transactions[t.key()]; ok {
|
2014-12-08 22:59:25 +00:00
|
|
|
panic("transaction not unique")
|
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
s.transactions[t.key()] = t
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Returns the 20-byte server ID. This is the ID used to communicate with the
|
|
|
|
// DHT network.
|
|
|
|
func (s *Server) ID() string {
|
2014-08-21 08:07:06 +00:00
|
|
|
if len(s.id) != 20 {
|
2014-05-24 06:51:56 +00:00
|
|
|
panic("bad node id")
|
|
|
|
}
|
2014-08-21 08:07:06 +00:00
|
|
|
return s.id
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) query(node dHTAddr, q string, a map[string]interface{}, onResponse func(Msg)) (t *Transaction, err error) {
|
2014-05-24 06:51:56 +00:00
|
|
|
tid := s.nextTransactionID()
|
|
|
|
if a == nil {
|
2014-11-16 19:08:33 +00:00
|
|
|
a = make(map[string]interface{}, 1)
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
a["id"] = s.ID()
|
2014-05-24 06:51:56 +00:00
|
|
|
d := map[string]interface{}{
|
|
|
|
"t": tid,
|
|
|
|
"y": "q",
|
|
|
|
"q": q,
|
|
|
|
"a": a,
|
|
|
|
}
|
2015-08-17 16:09:10 +00:00
|
|
|
// BEP 43. Outgoing queries from uncontactiable nodes should contain
|
|
|
|
// "ro":1 in the top level dictionary.
|
|
|
|
if s.config.Passive {
|
|
|
|
d["ro"] = 1
|
|
|
|
}
|
2014-05-24 06:51:56 +00:00
|
|
|
b, err := bencode.Marshal(d)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
t = &Transaction{
|
2014-12-07 03:21:20 +00:00
|
|
|
remoteAddr: node,
|
|
|
|
t: tid,
|
2015-02-21 04:00:48 +00:00
|
|
|
response: make(chan Msg, 1),
|
2014-12-07 03:21:20 +00:00
|
|
|
done: make(chan struct{}),
|
|
|
|
queryPacket: b,
|
|
|
|
s: s,
|
2015-02-21 04:00:48 +00:00
|
|
|
onResponse: onResponse,
|
2014-12-07 03:21:20 +00:00
|
|
|
}
|
|
|
|
err = t.sendQuery()
|
2014-05-24 06:51:56 +00:00
|
|
|
if err != nil {
|
2014-07-17 06:03:59 +00:00
|
|
|
return
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
2015-05-20 12:23:50 +00:00
|
|
|
s.getNode(node, "").lastSentQuery = time.Now()
|
2014-12-07 03:21:20 +00:00
|
|
|
t.startTimer()
|
|
|
|
s.addTransaction(t)
|
2014-05-24 06:51:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// The size in bytes of a NodeInfo in its compact binary representation.
|
2014-05-27 06:28:56 +00:00
|
|
|
const CompactNodeInfoLen = 26
|
2014-05-25 11:34:29 +00:00
|
|
|
|
2014-05-27 06:28:56 +00:00
|
|
|
type NodeInfo struct {
|
2014-05-25 11:34:29 +00:00
|
|
|
ID [20]byte
|
2014-11-17 03:22:29 +00:00
|
|
|
Addr dHTAddr
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Writes the node info to its compact binary representation in b. See
|
|
|
|
// CompactNodeInfoLen.
|
2014-05-27 06:28:56 +00:00
|
|
|
func (ni *NodeInfo) PutCompact(b []byte) error {
|
|
|
|
if n := copy(b[:], ni.ID[:]); n != 20 {
|
2014-05-25 11:34:29 +00:00
|
|
|
panic(n)
|
|
|
|
}
|
2015-08-03 14:29:01 +00:00
|
|
|
ip := missinggo.AddrIP(ni.Addr).To4()
|
2014-05-25 11:34:29 +00:00
|
|
|
if len(ip) != 4 {
|
2014-12-26 06:21:48 +00:00
|
|
|
return errors.New("expected ipv4 address")
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
|
|
|
if n := copy(b[20:], ip); n != 4 {
|
|
|
|
panic(n)
|
|
|
|
}
|
2015-08-03 14:29:01 +00:00
|
|
|
binary.BigEndian.PutUint16(b[24:], uint16(missinggo.AddrPort(ni.Addr)))
|
2014-05-27 06:28:56 +00:00
|
|
|
return nil
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
2014-05-24 06:51:56 +00:00
|
|
|
|
2014-05-27 06:28:56 +00:00
|
|
|
func (cni *NodeInfo) UnmarshalCompact(b []byte) error {
|
2014-05-25 11:34:29 +00:00
|
|
|
if len(b) != 26 {
|
|
|
|
return errors.New("expected 26 bytes")
|
|
|
|
}
|
2015-08-03 14:29:01 +00:00
|
|
|
missinggo.CopyExact(cni.ID[:], b[:20])
|
2014-11-17 03:22:29 +00:00
|
|
|
cni.Addr = newDHTAddr(&net.UDPAddr{
|
|
|
|
IP: net.IPv4(b[20], b[21], b[22], b[23]),
|
|
|
|
Port: int(binary.BigEndian.Uint16(b[24:26])),
|
|
|
|
})
|
2014-05-25 11:34:29 +00:00
|
|
|
return nil
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Sends a ping query to the address given.
|
|
|
|
func (s *Server) Ping(node *net.UDPAddr) (*Transaction, error) {
|
2014-08-25 12:14:31 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2015-02-21 04:00:48 +00:00
|
|
|
return s.query(newDHTAddr(node), "ping", nil, nil)
|
2014-05-24 06:51:56 +00:00
|
|
|
}
|
2014-05-25 11:34:29 +00:00
|
|
|
|
2015-02-21 04:00:48 +00:00
|
|
|
func (s *Server) announcePeer(node dHTAddr, infoHash string, port int, token string, impliedPort bool) (err error) {
|
2015-01-29 03:20:21 +00:00
|
|
|
if port == 0 && !impliedPort {
|
|
|
|
return errors.New("nothing to announce")
|
|
|
|
}
|
2015-02-21 04:00:48 +00:00
|
|
|
_, err = s.query(node, "announce_peer", map[string]interface{}{
|
2014-11-16 19:08:33 +00:00
|
|
|
"implied_port": func() int {
|
|
|
|
if impliedPort {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
"info_hash": infoHash,
|
|
|
|
"port": port,
|
|
|
|
"token": token,
|
2015-02-21 04:00:48 +00:00
|
|
|
}, func(m Msg) {
|
2014-11-16 19:08:33 +00:00
|
|
|
if err := m.Error(); err != nil {
|
2015-08-03 15:20:44 +00:00
|
|
|
announceErrors.Add(1)
|
|
|
|
// log.Print(token)
|
|
|
|
// logonce.Stderr.Printf("announce_peer response: %s", err)
|
2014-11-16 19:08:33 +00:00
|
|
|
return
|
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
s.numConfirmedAnnounces++
|
2014-11-16 19:08:33 +00:00
|
|
|
})
|
2015-02-21 04:00:48 +00:00
|
|
|
return
|
2014-07-09 14:13:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 03:17:27 +00:00
|
|
|
// Add response nodes to node table.
|
2014-07-03 15:43:04 +00:00
|
|
|
func (s *Server) liftNodes(d Msg) {
|
|
|
|
if d["y"] != "r" {
|
|
|
|
return
|
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
for _, cni := range d.Nodes() {
|
2015-08-03 14:29:01 +00:00
|
|
|
if missinggo.AddrPort(cni.Addr) == 0 {
|
2015-01-29 03:20:21 +00:00
|
|
|
// TODO: Why would people even do this?
|
|
|
|
continue
|
2014-07-03 15:43:04 +00:00
|
|
|
}
|
2015-08-03 14:29:01 +00:00
|
|
|
if s.ipBlocked(missinggo.AddrIP(cni.Addr)) {
|
2015-01-29 03:20:21 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-05-20 12:23:50 +00:00
|
|
|
n := s.getNode(cni.Addr, string(cni.ID[:]))
|
2015-01-29 03:20:21 +00:00
|
|
|
n.SetIDFromBytes(cni.ID[:])
|
2014-07-03 15:43:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:20:07 +00:00
|
|
|
// Sends a find_node query to addr. targetID is the node we're looking for.
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) findNode(addr dHTAddr, targetID string) (t *Transaction, err error) {
|
2015-02-21 04:00:48 +00:00
|
|
|
t, err = s.query(addr, "find_node", map[string]interface{}{"target": targetID}, func(d Msg) {
|
|
|
|
// Scrape peers from the response to put in the server's table before
|
|
|
|
// handing the response back to the caller.
|
|
|
|
s.liftNodes(d)
|
|
|
|
})
|
2014-05-25 11:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-07-03 15:43:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-17 09:52:47 +00:00
|
|
|
type Peer struct {
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Peer) String() string {
|
|
|
|
return net.JoinHostPort(me.IP.String(), strconv.FormatInt(int64(me.Port), 10))
|
|
|
|
}
|
|
|
|
|
2015-01-29 03:20:21 +00:00
|
|
|
// In a get_peers response, the addresses of torrent clients involved with the
|
|
|
|
// queried info-hash.
|
2015-08-17 09:52:47 +00:00
|
|
|
func (m Msg) Values() (vs []Peer) {
|
|
|
|
v := func() interface{} {
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
|
|
|
return m["r"].(map[string]interface{})["values"]
|
|
|
|
}()
|
|
|
|
if v == nil {
|
2014-07-03 15:43:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
vl, ok := v.([]interface{})
|
|
|
|
if !ok {
|
2015-08-05 23:02:50 +00:00
|
|
|
if missinggo.CryHeard() {
|
|
|
|
log.Printf(`unexpected krpc "values" field: %#v`, v)
|
|
|
|
}
|
2014-12-01 09:29:06 +00:00
|
|
|
return
|
2014-07-03 15:43:04 +00:00
|
|
|
}
|
2015-08-17 09:52:47 +00:00
|
|
|
vs = make([]Peer, 0, len(vl))
|
2014-07-03 15:43:04 +00:00
|
|
|
for _, i := range vl {
|
|
|
|
s, ok := i.(string)
|
2014-05-25 11:34:29 +00:00
|
|
|
if !ok {
|
2014-07-03 15:43:04 +00:00
|
|
|
panic(i)
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
2015-08-17 09:52:47 +00:00
|
|
|
// Because it's a list of strings, we can let the length of the string
|
|
|
|
// determine the IP version of the compact peer.
|
2014-08-21 08:07:06 +00:00
|
|
|
var cp util.CompactPeer
|
2014-07-03 15:43:04 +00:00
|
|
|
err := cp.UnmarshalBinary([]byte(s))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error decoding values list element: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-08-17 09:52:47 +00:00
|
|
|
vs = append(vs, Peer{cp.IP[:], int(cp.Port)})
|
2014-07-03 15:43:04 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) getPeers(addr dHTAddr, infoHash string) (t *Transaction, err error) {
|
2014-07-03 15:43:04 +00:00
|
|
|
if len(infoHash) != 20 {
|
|
|
|
err = fmt.Errorf("infohash has bad length")
|
|
|
|
return
|
|
|
|
}
|
2015-02-21 04:00:48 +00:00
|
|
|
t, err = s.query(addr, "get_peers", map[string]interface{}{"info_hash": infoHash}, func(m Msg) {
|
2014-07-03 15:43:04 +00:00
|
|
|
s.liftNodes(m)
|
2015-01-29 03:20:21 +00:00
|
|
|
at, ok := m.AnnounceToken()
|
|
|
|
if ok {
|
2015-08-17 16:11:09 +00:00
|
|
|
s.getNode(addr, m.SenderID()).announceToken = at
|
2015-01-29 03:20:21 +00:00
|
|
|
}
|
2014-07-03 15:43:04 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-02 22:35:30 +00:00
|
|
|
func bootstrapAddrs(nodeAddrs []string) (addrs []*net.UDPAddr, err error) {
|
|
|
|
bootstrapNodes := nodeAddrs
|
|
|
|
if len(bootstrapNodes) == 0 {
|
|
|
|
bootstrapNodes = []string{
|
|
|
|
"router.utorrent.com:6881",
|
|
|
|
"router.bittorrent.com:6881",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, addrStr := range bootstrapNodes {
|
2014-12-19 23:09:11 +00:00
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp4", addrStr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs = append(addrs, udpAddr)
|
|
|
|
}
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
err = errors.New("nothing resolved")
|
|
|
|
}
|
|
|
|
return
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 12:26:33 +00:00
|
|
|
// Adds bootstrap nodes directly to table, if there's room. Node ID security
|
|
|
|
// is bypassed, but the IP blocklist is not.
|
2014-12-19 23:09:11 +00:00
|
|
|
func (s *Server) addRootNodes() error {
|
2015-04-02 22:35:30 +00:00
|
|
|
addrs, err := bootstrapAddrs(s.bootstrapNodes)
|
2014-05-27 06:28:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-19 23:09:11 +00:00
|
|
|
for _, addr := range addrs {
|
2015-05-20 12:26:33 +00:00
|
|
|
if len(s.nodes) >= maxNodes {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if s.nodes[addr.String()] != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s.ipBlocked(addr.IP) {
|
|
|
|
log.Printf("dht root node is in the blocklist: %s", addr.IP)
|
|
|
|
continue
|
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
s.nodes[addr.String()] = &node{
|
2014-12-19 23:09:11 +00:00
|
|
|
addr: newDHTAddr(addr),
|
|
|
|
}
|
2014-05-27 06:28:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populates the node table.
|
2014-08-21 08:07:06 +00:00
|
|
|
func (s *Server) bootstrap() (err error) {
|
2014-05-27 06:28:56 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2014-05-25 11:34:29 +00:00
|
|
|
if len(s.nodes) == 0 {
|
2014-12-19 23:09:11 +00:00
|
|
|
err = s.addRootNodes()
|
2014-07-09 14:13:54 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
2014-05-27 06:28:56 +00:00
|
|
|
}
|
2014-07-03 15:43:04 +00:00
|
|
|
for {
|
2014-07-09 14:13:54 +00:00
|
|
|
var outstanding sync.WaitGroup
|
2014-07-03 15:43:04 +00:00
|
|
|
for _, node := range s.nodes {
|
2015-04-01 06:29:55 +00:00
|
|
|
var t *Transaction
|
2014-08-21 08:07:06 +00:00
|
|
|
t, err = s.findNode(node.addr, s.id)
|
2014-07-03 15:43:04 +00:00
|
|
|
if err != nil {
|
2015-01-11 10:42:57 +00:00
|
|
|
err = fmt.Errorf("error sending find_node: %s", err)
|
|
|
|
return
|
2014-07-03 15:43:04 +00:00
|
|
|
}
|
2014-07-09 14:13:54 +00:00
|
|
|
outstanding.Add(1)
|
2015-02-21 04:00:48 +00:00
|
|
|
t.SetResponseHandler(func(Msg) {
|
2014-07-09 14:13:54 +00:00
|
|
|
outstanding.Done()
|
2015-02-21 04:00:48 +00:00
|
|
|
})
|
2014-07-03 15:43:04 +00:00
|
|
|
}
|
2014-07-09 14:13:54 +00:00
|
|
|
noOutstanding := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
outstanding.Wait()
|
|
|
|
close(noOutstanding)
|
|
|
|
}()
|
|
|
|
s.mu.Unlock()
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
s.mu.Lock()
|
|
|
|
return
|
|
|
|
case <-time.After(15 * time.Second):
|
|
|
|
case <-noOutstanding:
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
2014-07-16 07:08:25 +00:00
|
|
|
// log.Printf("now have %d nodes", len(s.nodes))
|
2014-07-24 03:45:18 +00:00
|
|
|
if s.numGoodNodes() >= 160 {
|
2014-07-03 15:43:04 +00:00
|
|
|
break
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-27 06:28:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-24 03:45:18 +00:00
|
|
|
func (s *Server) numGoodNodes() (num int) {
|
|
|
|
for _, n := range s.nodes {
|
2014-12-09 01:09:11 +00:00
|
|
|
if n.DefinitelyGood() {
|
2014-07-24 03:45:18 +00:00
|
|
|
num++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Returns how many nodes are in the node table.
|
2014-07-16 07:07:28 +00:00
|
|
|
func (s *Server) NumNodes() int {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return len(s.nodes)
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Exports the current node table.
|
2014-06-26 14:57:07 +00:00
|
|
|
func (s *Server) Nodes() (nis []NodeInfo) {
|
2014-05-27 06:28:56 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
for _, node := range s.nodes {
|
2014-06-26 14:57:07 +00:00
|
|
|
// if !node.Good() {
|
|
|
|
// continue
|
|
|
|
// }
|
2014-05-27 06:28:56 +00:00
|
|
|
ni := NodeInfo{
|
|
|
|
Addr: node.addr,
|
|
|
|
}
|
2014-12-26 06:21:48 +00:00
|
|
|
if n := copy(ni.ID[:], node.idString()); n != 20 && n != 0 {
|
2014-05-27 06:28:56 +00:00
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
nis = append(nis, ni)
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
2014-05-27 06:28:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Stops the server network activity. This is all that's required to clean-up a Server.
|
2014-08-21 11:12:50 +00:00
|
|
|
func (s *Server) Close() {
|
2014-07-09 14:13:54 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
2014-08-21 11:12:50 +00:00
|
|
|
s.socket.Close()
|
2014-07-09 14:13:54 +00:00
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
2014-05-25 11:34:29 +00:00
|
|
|
}
|
2014-06-24 13:20:49 +00:00
|
|
|
|
2014-12-09 06:25:49 +00:00
|
|
|
var maxDistance big.Int
|
2014-11-20 02:02:03 +00:00
|
|
|
|
2014-12-09 06:25:49 +00:00
|
|
|
func init() {
|
|
|
|
var zero big.Int
|
|
|
|
maxDistance.SetBit(&zero, 160, 1)
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) closestGoodNodes(k int, targetID string) []*node {
|
|
|
|
return s.closestNodes(k, nodeIDFromString(targetID), func(n *node) bool { return n.DefinitelyGood() })
|
2014-11-16 19:08:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (s *Server) closestNodes(k int, target nodeID, filter func(*node) bool) []*node {
|
2014-12-26 06:21:48 +00:00
|
|
|
sel := newKClosestNodesSelector(k, target)
|
2015-04-01 06:29:55 +00:00
|
|
|
idNodes := make(map[string]*node, len(s.nodes))
|
2014-07-11 15:24:01 +00:00
|
|
|
for _, node := range s.nodes {
|
2014-11-16 19:08:33 +00:00
|
|
|
if !filter(node) {
|
2014-07-11 15:24:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sel.Push(node.id)
|
2014-12-26 06:21:48 +00:00
|
|
|
idNodes[node.idString()] = node
|
2014-07-11 15:24:01 +00:00
|
|
|
}
|
|
|
|
ids := sel.IDs()
|
2015-04-01 06:29:55 +00:00
|
|
|
ret := make([]*node, 0, len(ids))
|
2014-07-11 15:24:01 +00:00
|
|
|
for _, id := range ids {
|
2015-05-20 12:23:50 +00:00
|
|
|
ret = append(ret, idNodes[id.ByteString()])
|
2014-07-11 15:24:01 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2015-08-03 14:31:53 +00:00
|
|
|
|
|
|
|
func (me *Server) badNode(addr dHTAddr) {
|
|
|
|
me.badNodes.Add([]byte(addr.String()))
|
|
|
|
delete(me.nodes, addr.String())
|
|
|
|
}
|