Skip to content

Commit f69edef

Browse files
committed
fix: 增强gostun的检测能力,更新版本
1 parent 6e865f6 commit f69edef

3 files changed

Lines changed: 43 additions & 34 deletions

File tree

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ require (
77
github.com/jaypipes/ghw v0.12.0
88
github.com/json-iterator/go v1.1.12
99
github.com/libp2p/go-nat v0.2.0
10-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
1110
github.com/oneclickvirt/defaultset v0.0.2-20240624082446
12-
github.com/oneclickvirt/gostun v0.0.3-20250329105202
11+
github.com/oneclickvirt/gostun v0.0.5-20250722144757
1312
github.com/shirou/gopsutil/v4 v4.24.5
1413
github.com/yusufpapurcu/wmi v1.2.4
1514
golang.org/x/sys v0.21.0

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
6262
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
6363
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
6464
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
65-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
66-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
6765
github.com/oneclickvirt/defaultset v0.0.2-20240624082446 h1:5Pg3mK/u/vQvSz7anu0nxzrNdELi/AcDAU1mMsmPzyc=
6866
github.com/oneclickvirt/defaultset v0.0.2-20240624082446/go.mod h1:e9Jt4tf2sbemCtc84/XgKcHy9EZ2jkc5x2sW1NiJS+E=
69-
github.com/oneclickvirt/gostun v0.0.3-20250329105202 h1:aJ6E91Lp94lq8iWRcCaxpXTjqOOaWvufr5oras6cFtM=
70-
github.com/oneclickvirt/gostun v0.0.3-20250329105202/go.mod h1:f7DPEXAxbmwXSW33dbxtb0/KzqvOBWhTs2Or5xBerQA=
67+
github.com/oneclickvirt/gostun v0.0.5-20250722144757 h1:RCEouk5WIJoF52JfsC60Zkqz66JY0JQKrhfd9kqP1OI=
68+
github.com/oneclickvirt/gostun v0.0.5-20250722144757/go.mod h1:f7DPEXAxbmwXSW33dbxtb0/KzqvOBWhTs2Or5xBerQA=
7169
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
7270
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
7371
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=

system/nat.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,48 @@ import (
77

88
func getNatType() string {
99
model.EnableLoger = false
10-
addrStrPtrList := []string{
11-
"stun.voipgate.com:3478",
12-
"stun.miwifi.com:3478",
13-
"stunserver.stunprotocol.org:3478",
14-
}
15-
checkStatus := true
16-
for _, addrStr := range addrStrPtrList {
17-
err1 := stuncheck.MappingTests(addrStr)
18-
if err1 != nil {
19-
model.NatMappingBehavior = "inconclusive"
20-
if model.EnableLoger {
21-
model.Log.Warn("NAT mapping behavior: inconclusive")
22-
}
23-
checkStatus = false
10+
model.Verbose = 0
11+
model.Timeout = 3
12+
model.IPVersion = "ipv4"
13+
addrStrList := model.GetDefaultServers(model.IPVersion)
14+
rfcMethods := []string{"RFC5780", "RFC5389", "RFC3489"}
15+
successfulDetection := false
16+
for _, rfcMethod := range rfcMethods {
17+
if successfulDetection {
18+
break
2419
}
25-
err2 := stuncheck.FilteringTests(addrStr)
26-
if err2 != nil {
27-
model.NatFilteringBehavior = "inconclusive"
28-
if model.EnableLoger {
29-
model.Log.Warn("NAT filtering behavior: inconclusive")
20+
for _, addrStr := range addrStrList {
21+
model.NatMappingBehavior = ""
22+
model.NatFilteringBehavior = ""
23+
var err1, err2 error
24+
switch rfcMethod {
25+
case "RFC5780":
26+
err1 = stuncheck.MappingTests(addrStr)
27+
if err1 != nil {
28+
model.NatMappingBehavior = "inconclusive"
29+
}
30+
err2 = stuncheck.FilteringTests(addrStr)
31+
if err2 != nil {
32+
model.NatFilteringBehavior = "inconclusive"
33+
}
34+
case "RFC5389":
35+
err1 = stuncheck.MappingTestsRFC5389(addrStr)
36+
if err1 != nil {
37+
model.NatMappingBehavior = "inconclusive"
38+
model.NatFilteringBehavior = "inconclusive"
39+
}
40+
case "RFC3489":
41+
err1 = stuncheck.MappingTestsRFC3489(addrStr)
42+
if err1 != nil {
43+
model.NatMappingBehavior = "inconclusive"
44+
model.NatFilteringBehavior = "inconclusive"
45+
}
46+
}
47+
if model.NatMappingBehavior != "inconclusive" && model.NatFilteringBehavior != "inconclusive" &&
48+
model.NatMappingBehavior != "" && model.NatFilteringBehavior != "" {
49+
successfulDetection = true
50+
break
3051
}
31-
checkStatus = false
32-
}
33-
if model.NatMappingBehavior == "inconclusive" || model.NatFilteringBehavior == "inconclusive" {
34-
checkStatus = false
35-
} else if model.NatMappingBehavior != "inconclusive" && model.NatFilteringBehavior != "inconclusive" {
36-
checkStatus = true
37-
}
38-
if checkStatus {
39-
break
4052
}
4153
}
4254
return stuncheck.CheckType()

0 commit comments

Comments
 (0)