Skip to content

Commit 7a889d7

Browse files
committed
feat(debug): add netcheck
add netcheck Signed-off-by: ysicing <i@ysicing.me>
1 parent 05d4298 commit 7a889d7

File tree

6 files changed

+251
-15
lines changed

6 files changed

+251
-15
lines changed

cmd/debug/netcheck.go

+60-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
package debug
88

99
import (
10+
"sync"
11+
12+
"github.com/ysicing/tiga/pkg/util/ts"
13+
14+
"github.com/ergoapi/util/color"
15+
1016
"github.com/spf13/cobra"
17+
"github.com/ysicing/tiga/common"
1118
"github.com/ysicing/tiga/pkg/factory"
1219
"github.com/ysicing/tiga/pkg/util/netutil"
1320
)
@@ -19,24 +26,70 @@ func NetCheckCommand(f factory.Factory) *cobra.Command {
1926
Short: "netcheck",
2027
Run: func(cmd *cobra.Command, args []string) {
2128
if gw, err := netutil.CheckDefaultRoute(); err == nil {
22-
logpkg.Donef("default route %s reachable via icmp", gw.String())
29+
logpkg.Donef("default route %s reachable via icmp", color.SGreen(gw.String()))
2330
} else {
24-
logpkg.Warnf("default route %s unreachable via icmp", gw.String())
31+
logpkg.Warnf("default route %s unreachable via icmp", color.SRed(gw.String()))
2532
}
2633
if ns, err := netutil.GetDefaultNameserver(); err == nil {
27-
a := ""
28-
34+
a := "reachable"
2935
if !netutil.CheckReachabilityWithICMP(ns) {
30-
a = "un"
36+
a = "unreachable"
3137
}
3238
if err := netutil.CheckNameserverAvailability(ns + ":53"); err != nil {
33-
logpkg.Warnf("Default nameserver: %s (ICMP %s reachable, DNS unreachable: %s)", ns, a, err)
39+
logpkg.Warnf("nameserver: %s (ICMP %s, DNS unreachable: %s)", color.SRed(ns), a, err)
3440
} else {
35-
logpkg.Donef("Default nameserver: %s (ICMP %s reachable, DNS reachable)", ns, a)
41+
logpkg.Donef("nameserver: %s (ICMP %s, DNS reachable)", color.SGreen(ns), a)
3642
}
3743
} else {
3844
logpkg.Warnf("failed to reading default nameserver from system: %s", err)
3945
}
46+
var wg sync.WaitGroup
47+
wg.Add(6)
48+
go func() {
49+
defer wg.Done()
50+
if err := netutil.CheckCaptivePortal(); err == nil {
51+
logpkg.Donef("captive portal %s detected success", color.SGreen(common.DefaultGenerate204URL))
52+
} else {
53+
logpkg.Warnf("captive portal %s detected failed: %s", color.SRed(common.DefaultGenerate204URL), err)
54+
}
55+
}()
56+
go func() {
57+
defer wg.Done()
58+
if err := netutil.CheckCaptivePortal(common.MiuiGenerate204URL); err == nil {
59+
logpkg.Donef("captive portal %s detected success", color.SGreen(common.MiuiGenerate204URL))
60+
} else {
61+
logpkg.Warnf("captive portal %s detected failed: %s", color.SRed(common.MiuiGenerate204URL), err)
62+
}
63+
}()
64+
go func() {
65+
defer wg.Done()
66+
if err := netutil.CheckNameserverAvailability("8.8.8.8:53"); err != nil {
67+
logpkg.Warnf("remote dns %s is unavailable: %s", color.SRed("8.8.8.8"), err)
68+
} else {
69+
logpkg.Donef("remote dns %s is available", color.SGreen("8.8.8.8"))
70+
}
71+
}()
72+
go func() {
73+
defer wg.Done()
74+
if err := netutil.CheckNameserverAvailability("1.2.4.8:53"); err != nil {
75+
logpkg.Warnf("remote dns %s is unavailable: %s", color.SRed("1.2.4.8"), err)
76+
} else {
77+
logpkg.Donef("remote dns %s is available", color.SGreen("1.2.4.8"))
78+
}
79+
}()
80+
go func() {
81+
defer wg.Done()
82+
if loc, err := netutil.GetCloudflareEdgeTrace(); err == nil {
83+
logpkg.Donef("match Cloudflare CDN: %s", color.SGreen(loc))
84+
} else {
85+
logpkg.Warnf("miss Cloudflare CDN failed")
86+
}
87+
}()
88+
go func() {
89+
defer wg.Done()
90+
ts.GetTailscaleStatus()
91+
}()
92+
wg.Wait()
4093
},
4194
}
4295
return cmd

common/const.go

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const (
1818
DefaultCacheDir = "tiga/cache"
1919

2020
DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.0.0"
21+
22+
DefaultGenerate204URL = "https://connectivitycheck.gstatic.com/generate_204"
23+
MiuiGenerate204URL = "https://connect.rom.miui.com/generate_204"
24+
V2exGenerate204URL = "https://captive.v2ex.co/generate_204"
2125
)
2226

2327
const Logo = `Open Source Cli Tools For Senior Restart Engineer`

go.mod

+13
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ require (
4040
)
4141

4242
require (
43+
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
4344
github.com/6tail/lunar-go v1.3.3 // indirect
4445
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
4546
github.com/MakeNowJust/heredoc v1.0.0 // indirect
4647
github.com/Microsoft/go-winio v0.6.0 // indirect
4748
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
4849
github.com/VividCortex/ewma v1.2.0 // indirect
50+
github.com/akutz/memconn v0.1.0 // indirect
51+
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 // indirect
4952
github.com/blang/semver/v4 v4.0.0 // indirect
5053
github.com/chai2010/gettext-go v1.0.2 // indirect
5154
github.com/cloudflare/circl v1.3.3 // indirect
@@ -57,6 +60,7 @@ require (
5760
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
5861
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
5962
github.com/fatih/color v1.15.0 // indirect
63+
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
6064
github.com/getsentry/sentry-go v0.20.0 // indirect
6165
github.com/go-errors/errors v1.4.2 // indirect
6266
github.com/go-logr/logr v1.2.4 // indirect
@@ -76,9 +80,12 @@ require (
7680
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
7781
github.com/google/uuid v1.3.0 // indirect
7882
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
83+
github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3 // indirect
7984
github.com/imdario/mergo v0.3.12 // indirect
8085
github.com/inconshreveable/mousetrap v1.1.0 // indirect
8186
github.com/josharian/intern v1.0.0 // indirect
87+
github.com/josharian/native v1.1.1-0.20230202152459-5c7d0dd6ab86 // indirect
88+
github.com/jsimonetti/rtnetlink v1.1.2-0.20220408201609-d380b505068b // indirect
8289
github.com/json-iterator/go v1.1.12 // indirect
8390
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
8491
github.com/kr/pretty v0.3.1 // indirect
@@ -88,7 +95,10 @@ require (
8895
github.com/mailru/easyjson v0.7.7 // indirect
8996
github.com/mattn/go-colorable v0.1.13 // indirect
9097
github.com/mattn/go-runewidth v0.0.14 // indirect
98+
github.com/mdlayher/netlink v1.7.1 // indirect
99+
github.com/mdlayher/socket v0.4.0 // indirect
91100
github.com/mitchellh/go-homedir v1.1.0 // indirect
101+
github.com/mitchellh/go-ps v1.0.0 // indirect
92102
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
93103
github.com/moby/spdystream v0.2.0 // indirect
94104
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@@ -106,9 +116,11 @@ require (
106116
github.com/russross/blackfriday/v2 v2.1.0 // indirect
107117
github.com/tklauser/go-sysconf v0.3.11 // indirect
108118
github.com/tklauser/numcpus v0.6.0 // indirect
119+
github.com/x448/float16 v0.8.4 // indirect
109120
github.com/yusufpapurcu/wmi v1.2.2 // indirect
110121
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
111122
go4.org/mem v0.0.0-20220726221520-4f986261bf13 // indirect
123+
go4.org/netipx v0.0.0-20220725152314-7e7bdc8411bf // indirect
112124
golang.org/x/crypto v0.9.0 // indirect
113125
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
114126
golang.org/x/mod v0.10.0 // indirect
@@ -120,6 +132,7 @@ require (
120132
golang.org/x/text v0.9.0 // indirect
121133
golang.org/x/time v0.3.0 // indirect
122134
golang.org/x/tools v0.9.1 // indirect
135+
golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
123136
google.golang.org/appengine v1.6.7 // indirect
124137
google.golang.org/protobuf v1.30.0 // indirect
125138
gopkg.in/inf.v0 v0.9.1 // indirect

0 commit comments

Comments
 (0)