feat(nathole): use serverUDPPort in nathole discovery when available (#3382)

This commit is contained in:
fatedier
2023-03-30 21:49:12 +08:00
committed by GitHub
parent a22d6c9504
commit 3faae194d0
3 changed files with 153 additions and 99 deletions

View File

@@ -53,8 +53,12 @@ var natholeDiscoveryCmd = &cobra.Command{
os.Exit(1)
}
serverAddr := ""
if cfg.ServerUDPPort != 0 {
serverAddr = net.JoinHostPort(cfg.ServerAddr, strconv.Itoa(cfg.ServerUDPPort))
}
addresses, err := nathole.Discover(
net.JoinHostPort(cfg.ServerAddr, strconv.Itoa(cfg.ServerUDPPort)),
serverAddr,
[]string{cfg.NatHoleSTUNServer},
[]byte(cfg.Token),
)
@@ -62,6 +66,10 @@ var natholeDiscoveryCmd = &cobra.Command{
fmt.Println("discover error:", err)
os.Exit(1)
}
if len(addresses) < 2 {
fmt.Printf("discover error: can not get enough addresses, need 2, got: %v\n", addresses)
os.Exit(1)
}
natType, behavior, err := nathole.ClassifyNATType(addresses)
if err != nil {
@@ -79,8 +87,5 @@ func validateForNatHoleDiscovery(cfg config.ClientCommonConf) error {
if cfg.NatHoleSTUNServer == "" {
return fmt.Errorf("nat_hole_stun_server can not be empty")
}
if cfg.ServerUDPPort == 0 {
return fmt.Errorf("server udp port can not be empty")
}
return nil
}