forked from cruzbit/cruzbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
146 lines (127 loc) · 3.25 KB
/
dns.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
// Copyright 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package cruzbit
import (
"fmt"
"log"
"math/rand"
"net"
"strconv"
"sync"
"time"
"github.com/miekg/dns"
)
// DNSSeeder returns known peers in response to DNS queries.
type DNSSeeder struct {
peerStore PeerStorage
server *dns.Server
port int
wg sync.WaitGroup
}
// NewDNSSeeder creates a new DNS seeder given a PeerStorage interface.
func NewDNSSeeder(peerStore PeerStorage, port int) *DNSSeeder {
return &DNSSeeder{
peerStore: peerStore,
port: port,
server: &dns.Server{Addr: "0.0.0.0:" + strconv.Itoa(port), Net: "udp"},
}
}
func (d *DNSSeeder) handleQuery(m *dns.Msg, externalIP string) {
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
// get up to 128 peers that we've connected to in the last 48 hours
addresses, err := d.peerStore.GetSince(128, time.Now().Unix()-(60*60*48))
if err != nil {
log.Printf("Error requesting peers from storage: %s\n", err)
return
}
// add ourself
if len(externalIP) != 0 {
addresses = append(addresses, externalIP+":"+strconv.Itoa(d.port))
}
// shuffle them
for n := len(addresses); n > 0; n-- {
randIndex := rand.Intn(n)
addresses[n-1], addresses[randIndex] = addresses[randIndex], addresses[n-1]
}
// return at most 4
limit := 4
for i, addr := range addresses {
if i == limit {
return
}
ip, port, _ := net.SplitHostPort(addr)
if port != strconv.Itoa(DEFAULT_CRUZBIT_PORT) {
continue
}
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip))
if err == nil {
m.Answer = append(m.Answer, rr)
}
}
}
}
}
// Run executes the main loop for the DNSSeeder in its own goroutine.
func (d *DNSSeeder) Run() {
d.wg.Add(1)
go d.run()
}
func (d *DNSSeeder) run() {
defer d.wg.Done()
externalIP, err := determineExternalIP()
if err != nil {
log.Println(err)
}
handleDnsRequest := func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
d.handleQuery(m, externalIP)
}
w.WriteMsg(m)
}
dns.HandleFunc("cruzbit.", handleDnsRequest)
log.Printf("Starting DNS server")
if err := d.server.ListenAndServe(); err != nil {
log.Println(err)
}
}
// Shutdown shuts down the DNS seeder server.
func (d *DNSSeeder) Shutdown() {
log.Println("DNS seeder shutting down...")
d.server.Shutdown()
d.wg.Wait()
log.Println("DNS seeder shutdown")
}
var Seeders = [...]string{
"66.172.27.47:8831",
"66.172.12.98:8831",
"66.117.62.146:8831",
"dns.cruzbit.xyz:8831",
}
// Query DNS seeders
func dnsQueryForPeers() ([]string, error) {
var peers []string
for _, seeder := range Seeders {
c := dns.Client{}
m := dns.Msg{}
m.SetQuestion("client.cruzbit.", dns.TypeA)
log.Printf("Querying DNS seeder %s\n", seeder)
r, _, err := c.Exchange(&m, seeder)
if err != nil {
log.Printf("Error querying seeder: %s, error: %s\n",
seeder, err)
continue
}
for _, answer := range r.Answer {
a := answer.(*dns.A)
log.Printf("Seeder returned: %s\n", a.A.String())
peers = append(peers, a.A.String()+":"+strconv.Itoa(DEFAULT_CRUZBIT_PORT))
}
}
return peers, nil
}