-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdebug.go
More file actions
245 lines (207 loc) · 5.42 KB
/
debug.go
File metadata and controls
245 lines (207 loc) · 5.42 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package config
import (
"errors"
"fingertip/internal/resolvers"
"fmt"
"github.com/buffrr/letsdane/resolver"
"github.com/miekg/dns"
"math/rand"
"strings"
"sync"
"time"
)
const (
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
letterBytes = "abcdefghijklmnopqrstuvwxyz"
)
var weakRandSrc = rand.NewSource(time.Now().UnixNano())
var dnsTestClient = dns.Client{Timeout: time.Second * 5, SingleInflight: true}
type Debugger struct {
proxyProbeDomain string
proxyProbeReached bool
dnsProbeInProgress bool
dnsProbeErr error
checkCert func() bool
checkSynced func() bool
blockHeight uint64
progress float64
lastPing time.Time
sync.RWMutex
}
type DebugInfo struct {
BlockHeight uint64 `json:"blockHeight"`
Progress float64 `json:"progress"`
ProbeURL string `json:"proxyProbeUrl"`
ProbeReached bool `json:"proxyProbeReached"`
Syncing bool `json:"syncing"`
CertInstalled bool `json:"certInstalled"`
DNSReachable bool `json:"dnsTestPassed"`
DNSProbeInProgress bool `json:"dnsTestInProgress"`
DNSProbeErr string `json:"dnsTestError"`
}
// Check if udp over port 53 is reachable
// and whether the network interferes with DNS
// responses.
//
// Test inspired by RFC8027 #3.2
// https://datatracker.ietf.org/doc/html/rfc8027#section-3.2
//
// Attempt to reach .org nameservers
// and ask it for the address of "isc.org":
//
// Some possible cases:
// 1. The request fails for some reason like times out ..etc
// which indicates a interference
// 2. Receive a positive answer = probable interference
// 3. Receive a referral to isc.org nameservers
// interference is unlikely
func testDNSInterference() error {
msg := new(dns.Msg)
msg.CheckingDisabled = true
msg.RecursionDesired = false
msg.SetEdns0(4096, true)
msg.SetQuestion("isc.org.", dns.TypeA)
r, _, err := exchangeWithRetry(msg, []string{
"a0.org.afilias-nst.info:53",
"b2.org.afilias-nst.org:53",
// a2.org.afilias-nst.org
"199.249.112.1:53",
})
switch {
case err != nil:
return fmt.Errorf("DNS request failed: %v", err)
case r.Truncated:
return fmt.Errorf("DNS response trunacted")
case len(r.Answer) > 0:
return fmt.Errorf("your network appears to intercept and redirect outgoing DNS requests")
}
referral := false
hasRRSIGs := false
for _, rr := range r.Ns {
if rr.Header().Rrtype == dns.TypeNS && strings.EqualFold("isc.org.", rr.Header().Name) {
referral = true
}
if rr.Header().Rrtype == dns.TypeRRSIG {
hasRRSIGs = true
}
}
if referral {
if hasRRSIGs {
return nil
}
return fmt.Errorf("received a response without DNSSEC signatures")
}
return fmt.Errorf("received unexpected referral")
}
func exchangeWithRetry(m *dns.Msg, addrs []string) (r *dns.Msg, rtt time.Duration, err error) {
serverId := 0
for i := 0; i < 3; i++ {
if r, rtt, err = dnsTestClient.Exchange(m, addrs[serverId]); err == nil {
if !r.Truncated {
return
}
}
serverId = (serverId + 1) % len(addrs)
}
return
}
func (d *Debugger) SetChainInfo(height uint64, progress float64) {
d.Lock()
defer d.Unlock()
d.blockHeight = height
d.progress = progress
}
func (d *Debugger) Ping() {
d.Lock()
defer d.Unlock()
d.lastPing = time.Now()
}
func (d *Debugger) GetLastPing() time.Time {
d.RLock()
defer d.RUnlock()
return d.lastPing
}
func (d *Debugger) SetCheckCert(c func() bool) {
d.Lock()
defer d.Unlock()
d.checkCert = c
}
func (d *Debugger) SetCheckSynced(s func() bool) {
d.Lock()
defer d.Unlock()
d.checkSynced = s
}
func (d *Debugger) NewProbe() {
d.Lock()
d.proxyProbeReached = false
d.proxyProbeDomain = randString(50)
d.dnsProbeInProgress = true
d.Unlock()
go func() {
err := testDNSInterference()
d.Lock()
d.dnsProbeInProgress = false
d.dnsProbeErr = err
d.Unlock()
}()
}
func (d *Debugger) GetInfo() DebugInfo {
d.RLock()
defer d.RUnlock()
var err string
if d.dnsProbeErr != nil {
err = d.dnsProbeErr.Error()
}
return DebugInfo{
BlockHeight: d.blockHeight,
Progress: d.progress,
ProbeURL: "http://" + d.proxyProbeDomain,
ProbeReached: d.proxyProbeReached,
Syncing: d.checkSynced != nil && !d.checkSynced(),
CertInstalled: d.checkCert != nil && d.checkCert(),
DNSReachable: !d.dnsProbeInProgress && d.dnsProbeErr == nil,
DNSProbeErr: err,
DNSProbeInProgress: d.dnsProbeInProgress,
}
}
func (d *Debugger) GetDNSProbeMiddleware() resolvers.QueryMiddlewareFunc {
return func(qname string, qtype uint16) (bool, *resolver.DNSResult) {
d.RLock()
probeName := d.proxyProbeDomain
skipName := d.proxyProbeReached || len(probeName) != len(qname)
d.RUnlock()
if skipName {
return false, nil
}
if strings.EqualFold(probeName, qname) {
d.Lock()
d.proxyProbeReached = true
d.Unlock()
return true, &resolver.DNSResult{
Records: nil,
Secure: false,
Err: errors.New(""),
}
}
return false, nil
}
}
// source: http://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
func randString(n int) string {
sb := strings.Builder{}
sb.Grow(n)
for i, cache, remain := n-1, weakRandSrc.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = weakRandSrc.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
sb.WriteByte(letterBytes[idx])
i--
}
cache >>= letterIdxBits
remain--
}
return sb.String()
}