forked from l7mp/stunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
167 lines (136 loc) · 4.49 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
package stunner
import (
"crypto/tls"
"fmt"
"net"
// "strings"
// "github.com/pion/logging"
"github.com/pion/dtls/v2"
"github.com/pion/turn/v2"
// "github.com/pion/transport/vnet"
"github.com/l7mp/stunner/pkg/apis/v1alpha1"
)
// Start starts the STUNner server and starts listining on all requested server sockets
func (s *Stunner) Start() error {
s.log.Infof("STUNner server (re)starting with API version %q", s.version)
auth := s.GetAuth()
// start listeners
var pconn []turn.PacketConnConfig
var conn []turn.ListenerConfig
listeners := s.listenerManager.Keys()
for _, name := range listeners {
l := s.GetListener(name)
relay := &turn.RelayAddressGeneratorPortRange{
RelayAddress: l.Addr,
Address: l.Addr.String(),
MinPort: uint16(l.MinPort),
MaxPort: uint16(l.MaxPort),
Net: l.Net,
}
addr := fmt.Sprintf("%s:%d", l.Addr.String(), l.Port)
switch l.Proto {
case v1alpha1.ListenerProtocolUDP:
s.log.Debugf("setting up UDP listener at %s", addr)
udpListener, err := l.Net.ListenPacket("udp", addr)
if err != nil {
return fmt.Errorf("failed to create UDP listener at %s: %s",
addr, err)
}
l.Conn = turn.PacketConnConfig{
PacketConn: udpListener,
RelayAddressGenerator: relay,
PermissionHandler: s.NewPermissionHandler(l),
}
pconn = append(pconn, l.Conn.(turn.PacketConnConfig))
// cannot test this on vnet, no Listen/ListenTCP in vnet.Net
case v1alpha1.ListenerProtocolTCP:
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)
}
l.Conn = turn.ListenerConfig{
Listener: tcpListener,
RelayAddressGenerator: relay,
PermissionHandler: s.NewPermissionHandler(l),
}
conn = append(conn, l.Conn.(turn.ListenerConfig))
// cannot test this on vnet, no TLS in vnet.Net
case v1alpha1.ListenerProtocolTLS:
s.log.Debugf("setting up TLS/TCP listener at %s", addr)
cer, errTls := tls.LoadX509KeyPair(l.Cert, l.Key)
if errTls != nil {
return fmt.Errorf("cannot load cert/key pair for creating TLS listener at %s: %s",
addr, errTls)
}
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)
}
l.Conn = turn.ListenerConfig{
Listener: tlsListener,
RelayAddressGenerator: relay,
PermissionHandler: s.NewPermissionHandler(l),
}
conn = append(conn, l.Conn.(turn.ListenerConfig))
// cannot test this on vnet, no DTLS in vnet.Net
case v1alpha1.ListenerProtocolDTLS:
s.log.Debugf("setting up DTLS/UDP listener at %s", addr)
cer, errTls := tls.LoadX509KeyPair(l.Cert, l.Key)
if errTls != nil {
return fmt.Errorf("cannot load cert/key pair for creating DTLS listener at %s: %s",
addr, errTls)
}
// for some reason dtls.Listen requires a UDPAddr and not an addr string
udpAddr := &net.UDPAddr{IP: l.Addr, Port: l.Port}
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)
}
l.Conn = turn.ListenerConfig{
Listener: dtlsListener,
RelayAddressGenerator: relay,
PermissionHandler: s.NewPermissionHandler(l),
}
conn = append(conn, l.Conn.(turn.ListenerConfig))
default:
return fmt.Errorf("internal error: unknown listener protocol " + l.Proto.String())
}
}
// start the DNS resolver threads
if s.resolver == nil {
s.resolver.Start()
}
// start the TURN server if there are actual listeners configured
if len(conn) == 0 && len(pconn) == 0 {
s.server = nil
} else {
t, err := turn.NewServer(turn.ServerConfig{
Realm: auth.Realm,
AuthHandler: s.NewAuthHandler(),
LoggerFactory: s.logger,
PacketConnConfigs: pconn,
ListenerConfigs: conn,
})
if err != nil {
return fmt.Errorf("cannot set up TURN server: %s", err)
}
s.server = t
}
s.log.Infof("TURN server running: %s", s.String())
return nil
}
// Close stops the TURN server underneath STUNner
func (s *Stunner) Stop() {
s.log.Info("stopping the STUNner server")
if s.server != nil {
s.server.Close()
}
s.server = nil
}