forked from gcash/dnsseeder
-
Notifications
You must be signed in to change notification settings - Fork 4
/
crawler.go
112 lines (95 loc) · 2.97 KB
/
crawler.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
package main
import (
"log"
"net"
"strconv"
"time"
"errors"
"github.com/akshaynexus/quand/chaincfg"
"github.com/akshaynexus/quand/peer"
"github.com/akshaynexus/quand/wire"
)
type crawlError struct {
errLoc string
Err error
}
// Error returns a formatted error about a crawl
func (e *crawlError) Error() string {
return "err: " + e.errLoc + ": " + e.Err.Error()
}
// crawlNode runs in a goroutine, crawls the remote ip and updates the master
// list of currently active addresses
func crawlNode(rc chan *result, s *dnsseeder, nd *node) {
res := &result{
node: net.JoinHostPort(nd.NA.IP.String(), strconv.Itoa(int(nd.NA.Port))),
}
// connect to the remote ip and ask them for their addr list
res.nas, res.msg = crawlIP(s, res)
// all done so push the result back to the seeder.
//This will block until the seeder reads the result
rc <- res
// goroutine will end and be cleaned up
}
// crawlIP retrievs a slice of ip addresses from a client
func crawlIP(s *dnsseeder, r *result) ([]*wire.NetAddress, *crawlError) {
verack := make(chan struct{})
onAddr := make(chan *wire.MsgAddr)
peerCfg := &peer.Config{
UserAgentName: "quantisnet-dnsseeder-go", // User agent name to advertise.
UserAgentVersion: "2.3.1.1", // User agent version to advertise.
ChainParams: &chaincfg.MainNetParams,
Services: 0,
ProtocolVersion: 70214,
Listeners: peer.MessageListeners{
OnAddr: func(p *peer.Peer, msg *wire.MsgAddr) {
onAddr <- msg
},
OnVersion: func(p *peer.Peer, msg *wire.MsgVersion) *wire.MsgReject {
if config.debug {
log.Printf("%s - debug - %s - Remote version: %v\n", s.name, r.node, msg.ProtocolVersion)
}
// fill the node struct with the remote details
r.version = msg.ProtocolVersion
r.services = msg.Services
r.lastBlock = msg.LastBlock
r.strVersion = msg.UserAgent
return nil
},
OnVerAck: func(p *peer.Peer, msg *wire.MsgVerAck) {
verack <- struct{}{}
},
},
}
p, err := peer.NewOutboundPeer(peerCfg, r.node)
if err != nil {
return nil, &crawlError{"NewOutboundPeer: error", err}
}
// Establish the connection to the peer address and mark it connected.
conn, err := net.Dial("tcp", p.Addr())
if err != nil {
return nil, &crawlError{"net.Dial: error", err}
}
p.AssociateConnection(conn)
// Wait for the verack message or timeout in case of failure.
select {
case <-verack:
case <-time.After(time.Second * 5):
return nil, &crawlError{"Verack timeout", errors.New("")}
}
// if we get this far and if the seeder is full then don't ask for addresses. This will reduce bandwidth usage while still
// confirming that we can connect to the remote node
if len(s.theList) > s.maxSize {
return nil, nil
}
// send getaddr command
p.QueueMessage(wire.NewMsgGetAddr(), nil)
addrMsg := new(wire.MsgAddr)
select {
case addrMsg = <-onAddr:
case <-time.After(time.Second * 6):
}
// Disconnect the peer.
p.Disconnect()
p.WaitForDisconnect()
return addrMsg.AddrList, nil
}