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 {