From 080e6af779af8ed595e09511f9389bf724691a1e Mon Sep 17 00:00:00 2001 From: TenderIronh Date: Mon, 2 Dec 2024 21:10:15 +0800 Subject: [PATCH] fix public ip detect bug --- core/daemon.go | 9 +++++++-- core/nat.go | 26 +++++++------------------- core/protocol.go | 4 ++-- go.mod | 23 +++++++++++------------ 4 files changed, 27 insertions(+), 35 deletions(-) diff --git a/core/daemon.go b/core/daemon.go index 533fdc4..4e7bbec 100644 --- a/core/daemon.go +++ b/core/daemon.go @@ -60,6 +60,7 @@ func (d *daemon) run() { break } } + args = append(args, "-nv") for { // start worker @@ -72,6 +73,7 @@ func (d *daemon) run() { } gLog.Println(LvINFO, "start worker process, args:", args) execSpec := &os.ProcAttr{Env: append(os.Environ(), "GOTRACEBACK=crash"), Files: []*os.File{os.Stdin, os.Stdout, f}} + lastRebootTime := time.Now() p, err := os.StartProcess(binPath, args, execSpec) if err != nil { gLog.Printf(LvERROR, "start worker error:%s", err) @@ -88,8 +90,11 @@ func (d *daemon) run() { if !d.running { return } - gLog.Printf(LvERROR, "worker stop, restart it after 10s") - time.Sleep(time.Second * 10) + if time.Since(lastRebootTime) < time.Second*10 { + gLog.Printf(LvERROR, "worker stop, restart it after 10s") + time.Sleep(time.Second * 10) + } + } } diff --git a/core/nat.go b/core/nat.go index 3748d25..9cc8c71 100644 --- a/core/nat.go +++ b/core/nat.go @@ -121,18 +121,6 @@ func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATP return } defer echoConn.Close() - go func() { - // close outside for breaking the ReadFromUDP - // wait 30s for echo testing - buf := make([]byte, 1600) - echoConn.SetReadDeadline(time.Now().Add(time.Second * 30)) - n, addr, err := echoConn.ReadFromUDP(buf) - if err != nil { - return - } - echoConn.WriteToUDP(buf[0:n], addr) - gLog.Println(LvDEBUG, "echo server end") - }() // testing for public ip for i := 0; i < 2; i++ { if i == 1 { @@ -173,22 +161,22 @@ func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATP msg, _ := newMessage(MsgNATDetect, MsgPublicIP, NatDetectReq{EchoPort: echoPort}) _, err = conn.WriteTo(msg, dst) if err != nil { - return + continue } buf := make([]byte, 1600) // wait for echo testing - conn.SetReadDeadline(time.Now().Add(PublicIPEchoTimeout)) - nRead, _, err := conn.ReadFromUDP(buf) + echoConn.SetReadDeadline(time.Now().Add(PublicIPEchoTimeout)) + nRead, _, err := echoConn.ReadFromUDP(buf) if err != nil { - gLog.Println(LvERROR, "PublicIP detect error:", err) - break + gLog.Println(LvDEBUG, "PublicIP detect error:", err) + continue } natRsp := NatDetectRsp{} err = json.Unmarshal(buf[openP2PHeaderSize:nRead], &natRsp) if err != nil { - gLog.Println(LvERROR, "PublicIP detect error:", err) - break + gLog.Println(LvDEBUG, "PublicIP detect error:", err) + continue } if natRsp.Port == echoPort { if i == 1 { diff --git a/core/protocol.go b/core/protocol.go index 6519ab2..8656c26 100644 --- a/core/protocol.go +++ b/core/protocol.go @@ -10,7 +10,7 @@ import ( "time" ) -const OpenP2PVersion = "3.21.10" +const OpenP2PVersion = "3.21.12" const ProductName string = "openp2p" const LeastSupportVersion = "3.0.0" const SyncServerTimeVersion = "3.9.0" @@ -167,7 +167,7 @@ const ( MaxRetry = 10 Cone2ConeTCPPunchMaxRetry = 1 Cone2ConeUDPPunchMaxRetry = 1 - PublicIPEchoTimeout = time.Second * 1 + PublicIPEchoTimeout = time.Second * 3 NatTestTimeout = time.Second * 5 UDPReadTimeout = time.Second * 5 ClientAPITimeout = time.Second * 10 diff --git a/go.mod b/go.mod index 44e7997..00d9a3d 100644 --- a/go.mod +++ b/go.mod @@ -8,17 +8,16 @@ require ( github.com/openp2p-cn/go-reuseport v0.3.2 github.com/openp2p-cn/service v1.0.0 github.com/openp2p-cn/totp v0.0.0-20230421034602-0f3320ffb25e - github.com/openp2p-cn/wireguard-go v0.0.20240223 github.com/quic-go/quic-go v0.34.0 - github.com/vishvananda/netlink v1.1.0 + github.com/vishvananda/netlink v1.1.1-0.20211118161826-650dca95af54 github.com/xtaci/kcp-go/v5 v5.5.17 - golang.org/x/sys v0.21.0 + golang.org/x/sys v0.26.0 golang.zx2c4.com/wireguard/windows v0.5.3 ) require ( github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect - github.com/golang/mock v1.6.0 // indirect + github.com/golang/mock v1.7.0-rc.1 // indirect github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/kardianos/service v1.2.2 // indirect github.com/klauspost/cpuid/v2 v2.2.5 // indirect @@ -30,13 +29,13 @@ require ( github.com/templexxx/cpu v0.1.0 // indirect github.com/templexxx/xorsimd v0.4.2 // indirect github.com/tjfoc/gmsm v1.4.1 // indirect - github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect - golang.org/x/crypto v0.24.0 // indirect - golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect - golang.org/x/mod v0.18.0 // indirect - golang.org/x/net v0.26.0 // indirect - golang.org/x/tools v0.22.0 // indirect + github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect + golang.org/x/crypto v0.28.0 // indirect + golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 // indirect + golang.org/x/mod v0.21.0 // indirect + golang.org/x/net v0.30.0 // indirect + golang.org/x/tools v0.26.0 // indirect golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect - golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173 // indirect - google.golang.org/protobuf v1.28.1 // indirect + google.golang.org/protobuf v1.33.0 // indirect + gvisor.dev/gvisor v0.0.0-20241128011400-745828301c93 // indirect )