1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 02:05:59 +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,
"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{})

View file

@ -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

View file

@ -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
}