1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 08:35:57 +01:00

Don't start TURN server by default if ice-servers.json exists.

This can be overridden by explicitly specifying the -turn option.
This commit is contained in:
Juliusz Chroboczek 2021-01-19 01:18:11 +01:00
parent 5a7937b198
commit c1b689bccf
3 changed files with 54 additions and 14 deletions

View file

@ -44,9 +44,15 @@ func main() {
flag.BoolVar(&ice.ICERelayOnly, "relay-only", false, flag.BoolVar(&ice.ICERelayOnly, "relay-only", false,
"require use of TURN relays for all media traffic") "require use of TURN relays for all media traffic")
flag.StringVar(&turnserver.Address, "turn", ":1194", flag.StringVar(&turnserver.Address, "turn", ":1194",
"built-in TURN server address (\"\" to disable)") "built-in TURN server `address` (\"\" to disable)")
flag.Parse() flag.Parse()
flag.Visit(func(f *flag.Flag) {
if f.Name == "turn" {
turnserver.Force = true
}
})
if cpuprofile != "" { if cpuprofile != "" {
f, err := os.Create(cpuprofile) f, err := os.Create(cpuprofile)
if err != nil { if err != nil {
@ -89,10 +95,8 @@ func main() {
go group.ReadPublicGroups() go group.ReadPublicGroups()
err := turnserver.Start() // causes the built-in server to start if required
if err != nil { ice.Update()
log.Printf("TURN: %v", err)
}
defer turnserver.Stop() defer turnserver.Stop()
serverDone := make(chan struct{}) serverDone := make(chan struct{})

View file

@ -74,15 +74,19 @@ type configuration struct {
var conf atomic.Value var conf atomic.Value
func updateICEConfiguration() *configuration { func Update() *configuration {
now := time.Now() now := time.Now()
var cf webrtc.Configuration var cf webrtc.Configuration
found := false
if ICEFilename != "" { if ICEFilename != "" {
found = true
file, err := os.Open(ICEFilename) file, err := os.Open(ICEFilename)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
log.Printf("Open %v: %v", ICEFilename, err) log.Printf("Open %v: %v", ICEFilename, err)
} else {
found = false
} }
} else { } else {
defer file.Close() 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()...) cf.ICEServers = append(cf.ICEServers, turnserver.ICEServers()...)
if ICERelayOnly { if ICERelayOnly {
@ -120,9 +129,9 @@ func updateICEConfiguration() *configuration {
func ICEConfiguration() *webrtc.Configuration { func ICEConfiguration() *webrtc.Configuration {
conf, ok := conf.Load().(*configuration) conf, ok := conf.Load().(*configuration)
if !ok || time.Since(conf.timestamp) > 5*time.Minute { if !ok || time.Since(conf.timestamp) > 5*time.Minute {
conf = updateICEConfiguration() conf = Update()
} else if time.Since(conf.timestamp) > 2*time.Minute { } else if time.Since(conf.timestamp) > 2*time.Minute {
go updateICEConfiguration() go Update()
} }
return &conf.conf return &conf.conf

View file

@ -7,6 +7,7 @@ import (
"log" "log"
"net" "net"
"strconv" "strconv"
"sync"
"github.com/pion/turn/v2" "github.com/pion/turn/v2"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
@ -14,9 +15,12 @@ import (
var username string var username string
var password string var password string
var server *turn.Server
var Address string var Address string
var Force bool
var mu sync.Mutex
var addresses []net.Addr var addresses []net.Addr
var server *turn.Server
func publicAddresses() ([]net.IP, error) { func publicAddresses() ([]net.IP, error) {
addrs, err := net.InterfaceAddrs() 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 { func Start() error {
mu.Lock()
defer mu.Unlock()
if server != nil { if server != nil {
return errors.New("TURN server already started") return nil
} }
if Address == "" { if Address == "" {
@ -94,6 +101,8 @@ func Start() error {
return err return err
} }
log.Printf("Starting built-in TURN server")
username = "galene" username = "galene"
buf := make([]byte, 6) buf := make([]byte, 6)
_, err = rand.Read(buf) _, err = rand.Read(buf)
@ -182,6 +191,9 @@ func Start() error {
} }
func ICEServers() []webrtc.ICEServer { func ICEServers() []webrtc.ICEServer {
mu.Lock()
defer mu.Unlock()
if len(addresses) == 0 { if len(addresses) == 0 {
return nil return nil
} }
@ -208,12 +220,27 @@ func ICEServers() []webrtc.ICEServer {
} }
func Stop() { func Stop() error {
mu.Lock()
defer mu.Unlock()
addresses = nil addresses = nil
if server == nil { if server == nil {
return return nil
} }
server.Close() log.Printf("Stopping built-in TURN server")
err := server.Close()
server = nil 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
} }