-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathturn.go
74 lines (65 loc) · 1.67 KB
/
turn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"fmt"
"log"
"net"
"time"
"github.com/pion/turn/v4"
)
func (p *PluginImpl) waitForIP(ctx context.Context) (net.IP, error) {
ticker := time.NewTicker(100 * time.Millisecond)
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("context cancelled")
case <-ticker.C:
// log.Printf("Waiting for Tailscale to obtain an ip...")
ipv4Addr, _ := p.tailscaleServer.TailscaleIPs()
slice := ipv4Addr.AsSlice()
if slice == nil {
continue
}
return net.IP(slice), nil
}
}
}
func (p *PluginImpl) CreateTurnServer(ctx context.Context) (*turn.Server, error) {
// Wait for a valid ip address
ipv4, err := p.waitForIP(ctx)
if err != nil {
return nil, err
}
log.Printf("Tailscale IPs: %v", ipv4)
listenerAddress := fmt.Sprintf("%s:3478", ipv4.String())
udpListener, err := p.tailscaleServer.ListenPacket("udp", listenerAddress)
if err != nil {
return nil, err
}
// TODO: this could be dynamic depending on the JetKVM side of things
key := turn.GenerateAuthKey("username", "pion.ly", "password")
s, err := turn.NewServer(turn.ServerConfig{
Realm: "pion.ly",
AuthHandler: func(username string, realm string, srcAddr net.Addr) ([]byte, bool) {
log.Printf("Authenticating %s", username)
if username == "username" {
return key, true
}
return nil, false
},
PacketConnConfigs: []turn.PacketConnConfig{
{
PacketConn: udpListener,
RelayAddressGenerator: &turn.RelayAddressGeneratorStatic{
RelayAddress: ipv4, // IPv4 only for now
Address: "0.0.0.0",
},
},
},
})
if err != nil {
return nil, err
}
log.Printf("TURN server listening on %s", listenerAddress)
return s, nil
}