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

Move TURN server into structure.

This commit is contained in:
Juliusz Chroboczek 2022-04-30 22:37:06 +02:00
parent fd228b09c2
commit 340c2900cc

View file

@ -17,9 +17,11 @@ var username string
var password string var password string
var Address string var Address string
var mu sync.Mutex var server struct {
var addresses []net.Addr mu sync.Mutex
var server *turn.Server addresses []net.Addr
server *turn.Server
}
func publicAddresses() ([]net.IP, error) { func publicAddresses() ([]net.IP, error) {
addrs, err := net.InterfaceAddrs() addrs, err := net.InterfaceAddrs()
@ -91,10 +93,10 @@ func listener(a net.IP, port int, relay net.IP) (*turn.PacketConnConfig, *turn.L
} }
func Start() error { func Start() error {
mu.Lock() server.mu.Lock()
defer mu.Unlock() defer server.mu.Unlock()
if server != nil { if server.server != nil {
return nil return nil
} }
@ -111,8 +113,6 @@ func Start() error {
return err return err
} }
log.Printf("Starting built-in TURN server on %v", addr.String())
username = "galene" username = "galene"
buf := make([]byte, 6) buf := make([]byte, 6)
_, err = rand.Read(buf) _, err = rand.Read(buf)
@ -135,14 +135,14 @@ func Start() error {
pcc, lc := listener(net.IP{0, 0, 0, 0}, addr.Port, a) pcc, lc := listener(net.IP{0, 0, 0, 0}, addr.Port, a)
if pcc != nil { if pcc != nil {
pccs = append(pccs, *pcc) pccs = append(pccs, *pcc)
addresses = append(addresses, &net.UDPAddr{ server.addresses = append(server.addresses, &net.UDPAddr{
IP: a, IP: a,
Port: addr.Port, Port: addr.Port,
}) })
} }
if lc != nil { if lc != nil {
lcs = append(lcs, *lc) lcs = append(lcs, *lc)
addresses = append(addresses, &net.TCPAddr{ server.addresses = append(server.addresses, &net.TCPAddr{
IP: a, IP: a,
Port: addr.Port, Port: addr.Port,
}) })
@ -161,17 +161,21 @@ func Start() error {
pcc, lc := listener(a, addr.Port, nil) pcc, lc := listener(a, addr.Port, nil)
if pcc != nil { if pcc != nil {
pccs = append(pccs, *pcc) pccs = append(pccs, *pcc)
addresses = append(addresses, &net.UDPAddr{ server.addresses = append(server.addresses,
&net.UDPAddr{
IP: a, IP: a,
Port: addr.Port, Port: addr.Port,
}) },
)
} }
if lc != nil { if lc != nil {
lcs = append(lcs, *lc) lcs = append(lcs, *lc)
addresses = append(addresses, &net.TCPAddr{ server.addresses = append(server.addresses,
&net.TCPAddr{
IP: a, IP: a,
Port: addr.Port, Port: addr.Port,
}) },
)
} }
} }
} }
@ -180,7 +184,9 @@ func Start() error {
return errors.New("couldn't establish any listeners") return errors.New("couldn't establish any listeners")
} }
server, err = turn.NewServer(turn.ServerConfig{ log.Printf("Starting built-in TURN server on %v", addr.String())
server.server, err = turn.NewServer(turn.ServerConfig{
Realm: "galene.org", Realm: "galene.org",
AuthHandler: func(u, r string, src net.Addr) ([]byte, bool) { AuthHandler: func(u, r string, src net.Addr) ([]byte, bool) {
if u != username || r != "galene.org" { if u != username || r != "galene.org" {
@ -193,7 +199,7 @@ func Start() error {
}) })
if err != nil { if err != nil {
addresses = nil server.addresses = nil
return err return err
} }
@ -201,15 +207,15 @@ func Start() error {
} }
func ICEServers() []webrtc.ICEServer { func ICEServers() []webrtc.ICEServer {
mu.Lock() server.mu.Lock()
defer mu.Unlock() defer server.mu.Unlock()
if len(addresses) == 0 { if len(server.addresses) == 0 {
return nil return nil
} }
var urls []string var urls []string
for _, a := range addresses { for _, a := range server.addresses {
switch a := a.(type) { switch a := a.(type) {
case *net.UDPAddr: case *net.UDPAddr:
urls = append(urls, "turn:"+a.String()) urls = append(urls, "turn:"+a.String())
@ -231,16 +237,16 @@ func ICEServers() []webrtc.ICEServer {
} }
func Stop() error { func Stop() error {
mu.Lock() server.mu.Lock()
defer mu.Unlock() defer server.mu.Unlock()
addresses = nil server.addresses = nil
if server == nil { if server.server == nil {
return nil return nil
} }
log.Printf("Stopping built-in TURN server") log.Printf("Stopping built-in TURN server")
err := server.Close() err := server.server.Close()
server = nil server.server = nil
return err return err
} }