Choose a random subset from given STUN servers

Only chooses a subset as long as we have over 2 STUN servers to choose
from.
This commit is contained in:
Cecylia Bocovich 2020-07-23 09:28:21 -04:00
parent eaac9f5b6b
commit 92520f681d
3 changed files with 12 additions and 3 deletions

View file

@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"os"
"os/signal"
@ -77,6 +78,7 @@ func socksAcceptLoop(ln *pt.SocksListener, snowflakes sf.SnowflakeCollector) {
}
// s is a comma-separated list of ICE server URLs.
// chooses a random subset of servers from inputs
func parseIceServers(s string) []webrtc.ICEServer {
var servers []webrtc.ICEServer
s = strings.TrimSpace(s)
@ -90,6 +92,13 @@ func parseIceServers(s string) []webrtc.ICEServer {
URLs: []string{url},
})
}
rand.Seed(time.Now().Unix())
rand.Shuffle(len(servers), func(i, j int) {
servers[i], servers[j] = servers[j], servers[i]
})
if len(servers) > 2 {
servers = servers[:len(servers)/2]
}
return servers
}