From c1b689bccf1827cd68c1981e5a499fb0418fe695 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Tue, 19 Jan 2021 01:18:11 +0100 Subject: [PATCH] Don't start TURN server by default if ice-servers.json exists. This can be overridden by explicitly specifying the -turn option. --- galene.go | 14 +++++++++----- ice/ice.go | 15 ++++++++++++--- turnserver/turnserver.go | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/galene.go b/galene.go index 6c26a06..57dc659 100644 --- a/galene.go +++ b/galene.go @@ -44,9 +44,15 @@ func main() { flag.BoolVar(&ice.ICERelayOnly, "relay-only", false, "require use of TURN relays for all media traffic") flag.StringVar(&turnserver.Address, "turn", ":1194", - "built-in TURN server address (\"\" to disable)") + "built-in TURN server `address` (\"\" to disable)") flag.Parse() + flag.Visit(func(f *flag.Flag) { + if f.Name == "turn" { + turnserver.Force = true + } + }) + if cpuprofile != "" { f, err := os.Create(cpuprofile) if err != nil { @@ -89,10 +95,8 @@ func main() { go group.ReadPublicGroups() - err := turnserver.Start() - if err != nil { - log.Printf("TURN: %v", err) - } + // causes the built-in server to start if required + ice.Update() defer turnserver.Stop() serverDone := make(chan struct{}) diff --git a/ice/ice.go b/ice/ice.go index e11e23f..bb97b41 100644 --- a/ice/ice.go +++ b/ice/ice.go @@ -74,15 +74,19 @@ type configuration struct { var conf atomic.Value -func updateICEConfiguration() *configuration { +func Update() *configuration { now := time.Now() var cf webrtc.Configuration + found := false if ICEFilename != "" { + found = true file, err := os.Open(ICEFilename) if err != nil { if !os.IsNotExist(err) { log.Printf("Open %v: %v", ICEFilename, err) + } else { + found = false } } else { defer file.Close() @@ -103,6 +107,11 @@ func updateICEConfiguration() *configuration { } } + err := turnserver.StartStop(found) + if err != nil { + log.Printf("TURN: %v", err) + } + cf.ICEServers = append(cf.ICEServers, turnserver.ICEServers()...) if ICERelayOnly { @@ -120,9 +129,9 @@ func updateICEConfiguration() *configuration { func ICEConfiguration() *webrtc.Configuration { conf, ok := conf.Load().(*configuration) if !ok || time.Since(conf.timestamp) > 5*time.Minute { - conf = updateICEConfiguration() + conf = Update() } else if time.Since(conf.timestamp) > 2*time.Minute { - go updateICEConfiguration() + go Update() } return &conf.conf diff --git a/turnserver/turnserver.go b/turnserver/turnserver.go index 44a8bbc..38c4bc1 100644 --- a/turnserver/turnserver.go +++ b/turnserver/turnserver.go @@ -7,6 +7,7 @@ import ( "log" "net" "strconv" + "sync" "github.com/pion/turn/v2" "github.com/pion/webrtc/v3" @@ -14,9 +15,12 @@ import ( var username string var password string -var server *turn.Server var Address string +var Force bool + +var mu sync.Mutex var addresses []net.Addr +var server *turn.Server func publicAddresses() ([]net.IP, error) { addrs, err := net.InterfaceAddrs() @@ -82,8 +86,11 @@ func listener(a net.IP, port int, relay net.IP) (*turn.PacketConnConfig, *turn.L } func Start() error { + mu.Lock() + defer mu.Unlock() + if server != nil { - return errors.New("TURN server already started") + return nil } if Address == "" { @@ -94,6 +101,8 @@ func Start() error { return err } + log.Printf("Starting built-in TURN server") + username = "galene" buf := make([]byte, 6) _, err = rand.Read(buf) @@ -182,6 +191,9 @@ func Start() error { } func ICEServers() []webrtc.ICEServer { + mu.Lock() + defer mu.Unlock() + if len(addresses) == 0 { return nil } @@ -208,12 +220,27 @@ func ICEServers() []webrtc.ICEServer { } -func Stop() { +func Stop() error { + mu.Lock() + defer mu.Unlock() + addresses = nil if server == nil { - return + return nil } - server.Close() + log.Printf("Stopping built-in TURN server") + err := server.Close() server = nil - return + return err +} + +func StartStop(found bool) error { + if Force && Address != "" { + return Start() + } else if found { + return Stop() + } else if Address != "" { + return Start() + } + return nil }