From 9b0525294a5759599324b4f70b9f239e48b4475d Mon Sep 17 00:00:00 2001 From: W192547975 <138874985+W192547975@users.noreply.github.com> Date: Thu, 24 Aug 2023 10:55:09 +0800 Subject: [PATCH] echo server in publicIPTest update (#39) * Update nat.go func publicIPTest echo server --- core/nat.go | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/core/nat.go b/core/nat.go index eab5574..6a57a26 100644 --- a/core/nat.go +++ b/core/nat.go @@ -113,21 +113,18 @@ func getNATType(host string, udp1 int, udp2 int) (publicIP string, NATType int, func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATPMP int) { var echoConn *net.UDPConn - var wg sync.WaitGroup - wg.Add(1) + gLog.Println(LvDEBUG, "echo server start") + var err error + echoConn, err = net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: echoPort}) + if err != nil { // listen error + gLog.Println(LvERROR, "echo server listen error:", err) + return + } + defer echoConn.Close() go func() { - gLog.Println(LvDEBUG, "echo server start") - var err error - echoConn, err = net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: echoPort}) - if err != nil { - gLog.Println(LvERROR, "echo server listen error:", err) - wg.Done() - return - } - buf := make([]byte, 1600) // close outside for breaking the ReadFromUDP - // wait 5s for echo testing - wg.Done() + // 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 { @@ -136,11 +133,6 @@ func publicIPTest(publicIP string, echoPort int) (hasPublicIP int, hasUPNPorNATP echoConn.WriteToUDP(buf[0:n], addr) gLog.Println(LvDEBUG, "echo server end") }() - wg.Wait() // wait echo udp - if echoConn == nil { // listen error - return - } - defer echoConn.Close() // testing for public ip for i := 0; i < 2; i++ { if i == 1 {