/
grpc_diagnose.go
359 lines (328 loc) · 9.13 KB
/
grpc_diagnose.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package yakgrpc
import (
"fmt"
"net/url"
"strings"
"sync"
"time"
"github.com/yaklang/yaklang/common/filter"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/netx"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/netutil"
"github.com/yaklang/yaklang/common/utils/pingutil"
"github.com/yaklang/yaklang/common/yakgrpc/ypb"
)
func (s *Server) DiagnoseNetwork(req *ypb.DiagnoseNetworkRequest, server ypb.Yak_DiagnoseNetworkServer) error {
var wg sync.WaitGroup
timeout := utils.FloatSecondDuration(req.GetNetworkTimeout())
if timeout <= 0 {
timeout = 10 * time.Second
}
addNetworkDiagnose := func(title string, diagnoseType string, desc string, level ...string) {
// diagnose type:
// 1. route
// 2. network
// 3. transport
// 4. log
var loglevel string
if len(level) > 0 {
loglevel = strings.ToLower(level[0])
}
server.Send(&ypb.DiagnoseNetworkResponse{
Title: title,
DiagnoseType: diagnoseType,
DiagnoseResult: desc,
LogLevel: loglevel,
})
}
_addDiagnoseLog := func(level, message string) {
addNetworkDiagnose("diagnose-log", "log", message, level)
}
info := func(message string) {
_addDiagnoseLog("info", message)
}
warning := func(message string) {
_addDiagnoseLog("warning", message)
}
ipChannel := make(chan string, 1000)
ipPortChannel := make(chan string, 1000)
domainChannel := make(chan string, 1000)
routeFilter := filter.NewFilter()
defer routeFilter.Close()
route := func(ip string, verbose ...string) {
if routeFilter.Exist(ip) {
return
}
routeFilter.Insert(ip)
var suffix string
if len(verbose) > 0 {
suffix = fmt.Sprintf("(%v)", strings.Join(verbose, ", "))
}
iface, gw, _, err := netutil.Route(timeout, ip)
if err != nil {
warning(fmt.Sprintf("Route[%v] failed: %v", ip, err.Error()))
return
}
var lines []string
if iface != nil {
lines = append(lines, fmt.Sprintf("Interface[%d]: %v", iface.Index, iface.Name))
lines = append(lines, "Mac(hardware): "+iface.HardwareAddr.String())
addrs, _ := iface.Addrs()
for _, a := range addrs {
lines = append(lines, "Address: "+a.String())
}
lines = append(lines, "")
}
if gw.IsLoopback() {
lines = append(lines, "Gateway: Loopback")
} else {
lines = append(lines, fmt.Sprintf("Gateway: %v", gw.String()))
}
if len(lines) > 0 {
addNetworkDiagnose(
fmt.Sprintf("route-diagnose[%#v%v]", ip, suffix),
"route",
strings.Join(lines, "\n"),
)
}
}
wg.Add(4)
defer wg.Wait()
go func() {
f := filter.NewFilter()
defer func() {
wg.Done()
f.Close()
}()
for domain := range domainChannel {
if f.Exist(domain) {
continue
}
f.Insert(domain)
var lines []string
for _, dnsServer := range req.GetDNSServers() {
ips := netx.LookupAll(domain, netx.WithTimeout(timeout), netx.WithDNSServers(dnsServer))
for _, i := range ips {
if utils.IsIPv4(i) {
lines = append(lines, fmt.Sprintf("%v => [A]: %v", dnsServer, i))
} else {
lines = append(lines, fmt.Sprintf("%v => [AAAA]: %v", dnsServer, i))
}
route(i, fmt.Sprintf("%v @%v", domain, dnsServer))
}
}
systemDNS, err := utils.GetSystemDnsServers()
if err != nil {
warning("Get system dns servers failed: " + err.Error())
continue
}
for _, dnsServer := range systemDNS {
ips := netx.LookupAll(domain, netx.WithTimeout(timeout), netx.WithDNSServers(dnsServer))
for _, i := range ips {
if utils.IsIPv4(i) {
lines = append(lines, fmt.Sprintf("SYSTEM: %v => [A]: %v", dnsServer, i))
} else {
lines = append(lines, fmt.Sprintf("SYSTEM: %v => [AAAA]: %v", dnsServer, i))
}
route(i, fmt.Sprintf("%v @%v", domain, dnsServer))
}
}
if len(lines) > 0 {
addNetworkDiagnose(
fmt.Sprintf("dns-diagnose[%#v]", domain),
"dns",
strings.Join(lines, "\n"),
)
}
}
}()
go func() {
defer wg.Done()
for ip := range ipChannel {
route(ip)
}
}()
proxy := req.GetProxy()
if proxy != "" {
info("Start to diagnose proxy ...")
proxyUrl, err := url.Parse(proxy)
if err != nil {
warning("Parse proxy url failed: " + err.Error())
} else {
proxyUrl.User = url.UserPassword(req.GetProxyAuthUsername(), req.GetProxyAuthUsername())
proxy = proxyUrl.String()
}
}
go func() {
defer wg.Done()
if proxy == "" {
return
}
if req.GetProxyToAddr() == "" {
warning("ProxyToAddr is empty. (specific a host to test proxy)")
return
}
conn, err := netx.DialTCPTimeoutForceProxy(timeout, req.GetProxyToAddr(), proxy)
if err != nil {
warning("Get proxy connection failed: " + err.Error())
return
}
conn.Close()
addNetworkDiagnose(
"proxy-diagnose",
"proxy",
fmt.Sprintf("Proxy [%v] connection to [%v] success.", proxy, req.GetProxyToAddr()),
)
}()
go func() {
f := filter.NewFilter()
defer func() {
wg.Done()
f.Close()
}()
for ipPort := range ipPortChannel {
if f.Exist(ipPort) {
continue
}
f.Insert(ipPort)
host, port, _ := utils.ParseStringToHostPort(ipPort)
if port <= 0 {
continue
}
var lines []string
conn, err := netx.DialTCPTimeoutForceProxy(timeout, ipPort, proxy)
if err != nil {
warning(fmt.Sprintf("Get tcp connection to [%v:%v] failed: %v", host, port, err.Error()))
continue
}
lines = append(lines, fmt.Sprintf("TCP [%v:%v] connection success.", host, port))
tlsConn := utils.NewDefaultTLSClient(conn)
err = tlsConn.HandshakeContext(utils.TimeoutContext(timeout))
if err == nil {
lines = append(lines, fmt.Sprintf("TLS [%v:%v] connection success.", host, port))
}
conn.Close()
}
}()
info("Initialized Diagnose Network ...")
for _, i := range utils.PrettifyListFromStringSplitEx(req.GetConnectTarget(), "\n", ",", "|") {
hosts := utils.ParseStringToHosts(i)
var h string
if len(hosts) > 0 {
h = hosts[0]
} else {
h = i
}
params := make(map[string]any)
params["origin"] = h
host, port, _ := utils.ParseStringToHostPort(h)
if port <= 0 {
// only host
if utils.IsIPv4(h) || utils.IsIPv6(h) {
ipChannel <- h
} else {
domainChannel <- h
}
} else {
ipPortChannel <- h
if utils.IsIPv4(host) || utils.IsIPv6(h) {
ipChannel <- host
} else {
domainChannel <- host
}
}
}
if req.GetDomain() != "" {
domainChannel <- req.GetDomain()
}
defer func() {
close(ipPortChannel)
close(domainChannel)
close(ipChannel)
}()
return nil
}
func (s *Server) DiagnoseNetworkDNS(req *ypb.DiagnoseNetworkDNSRequest, server ypb.Yak_DiagnoseNetworkDNSServer) error {
domain := req.GetDomain()
if domain == "" {
return utils.Error("domain is empty")
}
var count int
callback := func(dnsType, domain, ip, fromServer string, method string) {
count++
server.Send(&ypb.DiagnoseNetworkResponse{
Title: domain + ` dns-resolved to ` + ip,
DiagnoseType: "dns",
DiagnoseResult: fmt.Sprintf("[%v] -%v %v %v=> %v", method, fromServer, domain, dnsType, ip),
})
}
info := func(msg string) {
server.Send(&ypb.DiagnoseNetworkResponse{
Title: "diagnose-log",
DiagnoseType: "log",
LogLevel: "info",
DiagnoseResult: msg,
})
}
info(`start to check system dns resolver, fallbackTCP: false, fallbackDoH: false`)
var err error
err = netx.LookupCallback(domain, callback,
netx.WithDNSDisableSystemResolver(false),
netx.WithDNSFallbackTCP(false), netx.WithDNSPreferTCP(false),
netx.WithDNSFallbackDoH(false), netx.WithDNSPreferDoH(false),
netx.WithDNSNoCache(true),
)
if err != nil {
log.Errorf("yakdns.Lookup Failed: %s", err)
}
config := netx.NewDefaultReliableDNSConfig()
if len(config.SpecificDNSServers) > 0 {
info("start to check default dns servers: " + strings.Join(config.SpecificDNSServers, ", "))
err = netx.LookupCallback(domain, callback,
netx.WithDNSDisableSystemResolver(true),
netx.WithDNSFallbackTCP(false), netx.WithDNSPreferTCP(false),
netx.WithDNSFallbackDoH(false), netx.WithDNSPreferDoH(false),
netx.WithDNSNoCache(true),
)
if err != nil {
log.Errorf("yakdns.Lookup Failed: %s", err)
}
} else {
info("no backup custom servers found, skip to check default dns servers")
}
if len(config.SpecificDoH) > 0 {
info("start to check prefer doh dns servers: " + strings.Join(config.SpecificDoH, ", "))
err = netx.LookupCallback(domain, callback,
netx.WithDNSDisableSystemResolver(true),
netx.WithDNSFallbackTCP(false), netx.WithDNSPreferTCP(false),
netx.WithDNSServers(), netx.WithDNSNoCache(true),
netx.WithDNSFallbackDoH(false), netx.WithDNSPreferDoH(true),
)
if err != nil {
log.Errorf("yakdns.Lookup Failed: %s", err)
}
} else {
info("no backup custom doh servers found, skip to check default doh dns servers")
}
if count <= 0 {
info("no dns resolved for " + req.GetDomain())
return nil
}
return nil
}
func (s *Server) TraceRoute(req *ypb.TraceRouteRequest, server ypb.Yak_TraceRouteServer) error {
res, err := pingutil.Traceroute(req.GetHost(), pingutil.WithCtx(server.Context()))
if err != nil {
return err
}
for rsp := range res {
server.Send(&ypb.TraceRouteResponse{
Hop: int64(rsp.Hop),
Ip: rsp.IP,
Rtt: rsp.RTT,
Reason: rsp.Reason,
})
}
return nil
}