Skip to content

Commit

Permalink
echo server in publicIPTest update (#39)
Browse files Browse the repository at this point in the history
* Update nat.go
func publicIPTest echo server
  • Loading branch information
W192547975 authored Aug 24, 2023
1 parent 276a443 commit 9b05252
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions core/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 9b05252

Please sign in to comment.