Add second STUN server from Cloudflare

This commit is contained in:
Alex X
2025-12-01 14:18:45 +03:00
parent fbd5215669
commit 7eb5fe0355
6 changed files with 30 additions and 17 deletions
+13 -12
View File
@@ -185,8 +185,8 @@ func LookupIP(address string) (string, error) {
}
// GetPublicIP example from https://github.com/pion/stun
func GetPublicIP() (net.IP, error) {
conn, err := net.Dial("udp", "stun.l.google.com:19302")
func GetPublicIP(address string) (net.IP, error) {
conn, err := net.Dial("udp", address)
if err != nil {
return nil, err
}
@@ -225,18 +225,19 @@ func GetPublicIP() (net.IP, error) {
var cachedIP net.IP
var cachedTS time.Time
func GetCachedPublicIP() (net.IP, error) {
now := time.Now()
if now.After(cachedTS) {
newIP, err := GetPublicIP()
if err == nil {
cachedIP = newIP
cachedTS = now.Add(time.Minute * 5)
} else if cachedIP == nil {
return nil, err
func GetCachedPublicIP(stuns ...string) (net.IP, error) {
if now := time.Now(); now.After(cachedTS) {
for _, addr := range stuns {
if ip, _ := GetPublicIP(addr); ip != nil {
cachedIP = ip
cachedTS = now.Add(time.Minute * 5)
return ip, nil
}
}
}
if cachedIP == nil {
return nil, errors.New("webrtc: can't get public IP")
}
return cachedIP, nil
}