1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-27 02:55:58 +01:00
galene/turnserver/turnserver.go

264 lines
4.8 KiB
Go
Raw Normal View History

2021-01-18 20:24:52 +01:00
package turnserver
import (
"crypto/rand"
"encoding/base64"
"errors"
"log"
"net"
"strconv"
"sync"
2021-01-18 20:24:52 +01:00
"github.com/pion/turn/v2"
"github.com/pion/webrtc/v3"
)
var username string
var password string
var Address string
2022-04-30 22:37:06 +02:00
var server struct {
mu sync.Mutex
addresses []net.Addr
server *turn.Server
}
2021-01-18 20:24:52 +01:00
func publicAddresses() ([]net.IP, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
var as []net.IP
for _, addr := range addrs {
switch addr := addr.(type) {
case *net.IPNet:
a := addr.IP.To4()
if a == nil {
continue
}
if !a.IsGlobalUnicast() {
continue
}
if a[0] == 10 ||
a[0] == 172 && a[1] >= 16 && a[1] < 32 ||
a[0] == 192 && a[1] == 168 {
continue
}
as = append(as, a)
}
}
return as, nil
}
func listener(a net.IP, port int, relay net.IP) (*turn.PacketConnConfig, *turn.ListenerConfig) {
var pcc *turn.PacketConnConfig
var lc *turn.ListenerConfig
s := net.JoinHostPort(a.String(), strconv.Itoa(port))
var g turn.RelayAddressGenerator
if relay == nil || relay.IsUnspecified() {
g = &turn.RelayAddressGeneratorNone{
Address: a.String(),
}
} else {
g = &turn.RelayAddressGeneratorStatic{
RelayAddress: relay,
Address: a.String(),
}
}
2021-01-18 20:24:52 +01:00
p, err := net.ListenPacket("udp4", s)
if err == nil {
pcc = &turn.PacketConnConfig{
PacketConn: p,
RelayAddressGenerator: g,
2021-01-18 20:24:52 +01:00
}
} else {
log.Printf("TURN: listenPacket(%v): %v", s, err)
}
l, err := net.Listen("tcp4", s)
if err == nil {
lc = &turn.ListenerConfig{
Listener: l,
RelayAddressGenerator: g,
2021-01-18 20:24:52 +01:00
}
} else {
log.Printf("TURN: listen(%v): %v", s, err)
}
return pcc, lc
}
func Start() error {
2022-04-30 22:37:06 +02:00
server.mu.Lock()
defer server.mu.Unlock()
2022-04-30 22:37:06 +02:00
if server.server != nil {
return nil
2021-01-18 20:24:52 +01:00
}
if Address == "" {
return errors.New("built-in TURN server disabled")
}
ad := Address
if Address == "auto" {
ad = ":1194"
}
addr, err := net.ResolveUDPAddr("udp4", ad)
2021-01-18 20:24:52 +01:00
if err != nil {
return err
}
username = "galene"
buf := make([]byte, 6)
_, err = rand.Read(buf)
if err != nil {
return err
}
buf2 := make([]byte, 8)
base64.RawStdEncoding.Encode(buf2, buf)
password = string(buf2)
var lcs []turn.ListenerConfig
var pccs []turn.PacketConnConfig
if addr.IP != nil && !addr.IP.IsUnspecified() {
a := addr.IP.To4()
if a == nil {
return errors.New("couldn't parse address")
}
pcc, lc := listener(net.IP{0, 0, 0, 0}, addr.Port, a)
if pcc != nil {
pccs = append(pccs, *pcc)
2022-04-30 22:37:06 +02:00
server.addresses = append(server.addresses, &net.UDPAddr{
2021-01-18 20:24:52 +01:00
IP: a,
Port: addr.Port,
})
}
if lc != nil {
lcs = append(lcs, *lc)
2022-04-30 22:37:06 +02:00
server.addresses = append(server.addresses, &net.TCPAddr{
2021-01-18 20:24:52 +01:00
IP: a,
Port: addr.Port,
})
}
} else {
as, err := publicAddresses()
if err != nil {
return err
}
if len(as) == 0 {
return errors.New("no public addresses")
}
for _, a := range as {
pcc, lc := listener(a, addr.Port, nil)
2021-01-18 20:24:52 +01:00
if pcc != nil {
pccs = append(pccs, *pcc)
2022-04-30 22:37:06 +02:00
server.addresses = append(server.addresses,
&net.UDPAddr{
IP: a,
Port: addr.Port,
},
)
2021-01-18 20:24:52 +01:00
}
if lc != nil {
lcs = append(lcs, *lc)
2022-04-30 22:37:06 +02:00
server.addresses = append(server.addresses,
&net.TCPAddr{
IP: a,
Port: addr.Port,
},
)
2021-01-18 20:24:52 +01:00
}
}
}
if len(pccs) == 0 && len(lcs) == 0 {
return errors.New("couldn't establish any listeners")
}
2022-04-30 22:37:06 +02:00
log.Printf("Starting built-in TURN server on %v", addr.String())
server.server, err = turn.NewServer(turn.ServerConfig{
2021-01-18 20:24:52 +01:00
Realm: "galene.org",
AuthHandler: func(u, r string, src net.Addr) ([]byte, bool) {
if u != username || r != "galene.org" {
return nil, false
}
return turn.GenerateAuthKey(u, r, password), true
},
ListenerConfigs: lcs,
PacketConnConfigs: pccs,
})
if err != nil {
2022-04-30 22:37:06 +02:00
server.addresses = nil
2021-01-18 20:24:52 +01:00
return err
}
return nil
}
func ICEServers() []webrtc.ICEServer {
2022-04-30 22:37:06 +02:00
server.mu.Lock()
defer server.mu.Unlock()
2022-04-30 22:37:06 +02:00
if len(server.addresses) == 0 {
2021-01-18 20:24:52 +01:00
return nil
}
var urls []string
2022-04-30 22:37:06 +02:00
for _, a := range server.addresses {
2021-01-18 20:24:52 +01:00
switch a := a.(type) {
case *net.UDPAddr:
urls = append(urls, "turn:"+a.String())
case *net.TCPAddr:
urls = append(urls, "turn:"+a.String()+"?transport=tcp")
default:
log.Printf("unexpected TURN address %T", a)
}
}
return []webrtc.ICEServer{
{
URLs: urls,
Username: username,
Credential: password,
},
}
}
func Stop() error {
2022-04-30 22:37:06 +02:00
server.mu.Lock()
defer server.mu.Unlock()
2022-04-30 22:37:06 +02:00
server.addresses = nil
if server.server == nil {
return nil
2021-01-18 20:24:52 +01:00
}
log.Printf("Stopping built-in TURN server")
2022-04-30 22:37:06 +02:00
err := server.server.Close()
server.server = nil
return err
}
func StartStop(start bool) error {
if Address == "auto" {
if start {
return Start()
}
return Stop()
} else if Address == "" {
return Stop()
}
return Start()
2021-01-18 20:24:52 +01:00
}