forked from l7mp/stunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
171 lines (136 loc) · 4.88 KB
/
server.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package stunner
import (
"crypto/tls"
"fmt"
"net"
"github.com/pion/dtls/v3"
"github.com/pion/turn/v4"
"golang.org/x/time/rate"
"github.com/l7mp/stunner/internal/object"
"github.com/l7mp/stunner/internal/telemetry"
"github.com/l7mp/stunner/internal/util"
stnrv1 "github.com/l7mp/stunner/pkg/apis/v1"
"github.com/l7mp/stunner/pkg/logger"
)
// Number of log events per second reported at ERROR, WARN and INFO loglevel (logging at DEBUG and
// TRACE levels is not rate-limited).
var LogRateLimit rate.Limit = 1.0
// Burst size for rate-limited logging at ERROR, WARN and INFO loglevel (logging at DEBUG and TRACE
// levels is not rate-limited).
var LogBurst = 3
// Start will start the TURN server that belongs to a listener.
func (s *Stunner) StartServer(l *object.Listener) error {
s.log.Infof("listener %s (re)starting", l.String())
// start listeners
var pConns []turn.PacketConnConfig
var lConns []turn.ListenerConfig
relay := NewRelayGen(l, s.telemetry, s.logger)
relay.PortRangeChecker = s.GenPortRangeChecker(relay)
permissionHandler := s.NewPermissionHandler(l)
addr := fmt.Sprintf("0.0.0.0:%d", l.Port)
switch l.Proto {
case stnrv1.ListenerProtocolTURNUDP:
socketPool := util.NewPacketConnPool(l.Name, l.Net, s.udpThreadNum, s.telemetry)
s.log.Infof("setting up UDP listener socket pool at %s with %d readloop threads",
addr, socketPool.Size())
conns, err := socketPool.Make("udp", addr)
if err != nil {
return err
}
for _, c := range conns {
conn := turn.PacketConnConfig{
PacketConn: c,
RelayAddressGenerator: relay,
PermissionHandler: permissionHandler,
}
l.Conns = append(l.Conns, conn)
pConns = append(pConns, conn)
}
case stnrv1.ListenerProtocolTURNTCP:
s.log.Debugf("setting up TCP listener at %s", addr)
tcpListener, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("failed to create TCP listener at %s: %s", addr, err)
}
tcpListener = telemetry.NewListener(tcpListener, l.Name, telemetry.ListenerType, s.telemetry)
conn := turn.ListenerConfig{
Listener: tcpListener,
RelayAddressGenerator: relay,
PermissionHandler: permissionHandler,
}
lConns = append(lConns, conn)
l.Conns = append(l.Conns, conn)
// cannot test this on vnet, no TLS in vnet.Net
case stnrv1.ListenerProtocolTURNTLS:
s.log.Debugf("setting up TLS/TCP listener at %s", addr)
cer, err := tls.X509KeyPair(l.Cert, l.Key)
if err != nil {
return fmt.Errorf("cannot load cert/key pair for creating TLS listener at %s: %s",
addr, err)
}
tlsListener, err := tls.Listen("tcp", addr, &tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{cer},
})
if err != nil {
return fmt.Errorf("failed to create TLS listener at %s: %s", addr, err)
}
tlsListener = telemetry.NewListener(tlsListener, l.Name, telemetry.ListenerType, s.telemetry)
conn := turn.ListenerConfig{
Listener: tlsListener,
RelayAddressGenerator: relay,
PermissionHandler: permissionHandler,
}
lConns = append(lConns, conn)
l.Conns = append(l.Conns, conn)
case stnrv1.ListenerProtocolTURNDTLS:
s.log.Debugf("setting up DTLS/UDP listener at %s", addr)
cer, err := tls.X509KeyPair(l.Cert, l.Key)
if err != nil {
return fmt.Errorf("cannot load cert/key pair for creating DTLS listener at %s: %s",
addr, err)
}
// for some reason dtls.Listen requires a UDPAddr and not an addr string
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return fmt.Errorf("failed to parse DTLS listener address %s: %s", addr, err)
}
dtlsListener, err := dtls.Listen("udp", udpAddr, &dtls.Config{
Certificates: []tls.Certificate{cer},
// ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
})
if err != nil {
return fmt.Errorf("failed to create DTLS listener at %s: %s", addr, err)
}
dtlsListener = telemetry.NewListener(dtlsListener, l.Name, telemetry.ListenerType, s.telemetry)
conn := turn.ListenerConfig{
Listener: dtlsListener,
RelayAddressGenerator: relay,
PermissionHandler: permissionHandler,
}
lConns = append(lConns, conn)
l.Conns = append(l.Conns, conn)
default:
return fmt.Errorf("internal error: unknown listener protocol %q", l.Proto.String())
}
// start the TURN server if there are actual listeners configured
if len(pConns) == 0 && len(lConns) == 0 {
l.Server = nil
return nil
}
t, err := turn.NewServer(turn.ServerConfig{
Realm: s.GetRealm(),
AuthHandler: s.NewAuthHandler(),
EventHandlers: s.NewLifecycleEventHandler(),
PacketConnConfigs: pConns,
ListenerConfigs: lConns,
LoggerFactory: logger.NewRateLimitedLoggerFactory(s.logger, LogRateLimit, LogBurst),
})
if err != nil {
return fmt.Errorf("cannot set up TURN server for listener %s: %w",
l.Name, err)
}
l.Server = t
s.log.Infof("listener %s: TURN server running", l.Name)
return nil
}