Refine nat 1to1 mapping setting (#1094)

Now only set mapping when user_external_ip enabled or node_ip is
explicitly set. If multiple local address resolved to same external
ip, only the first one will be mapped to external, avoid candidate
conflict between different clients.
This commit is contained in:
cnderrauber
2022-10-17 16:11:52 +08:00
committed by GitHub
parent 4161768530
commit 759e3bb1f2
4 changed files with 114 additions and 31 deletions
+11 -3
View File
@@ -19,7 +19,7 @@ func (conf *Config) determineIP() (string, error) {
var err error
for i := 0; i < 3; i++ {
var ip string
ip, err = GetExternalIP(stunServers)
ip, err = GetExternalIP(stunServers, nil)
if err == nil {
return ip, nil
} else {
@@ -79,11 +79,19 @@ func GetLocalIPAddresses() ([]string, error) {
return nil, fmt.Errorf("could not find local IP address")
}
func GetExternalIP(stunServers []string) (string, error) {
// GetExternalIP return external IP for localAddr from stun server. If localAddr is nil, a local address is chosen automatically.
func GetExternalIP(stunServers []string, localAddr net.Addr) (string, error) {
if len(stunServers) == 0 {
return "", errors.New("STUN servers are required but not defined")
}
c, err := stun.Dial("udp4", stunServers[0])
dialer := &net.Dialer{
LocalAddr: localAddr,
}
conn, err := dialer.Dial("udp4", stunServers[0])
if err != nil {
return "", err
}
c, err := stun.NewClient(conn)
if err != nil {
return "", err
}