forked from jech/galene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturnserver.go
263 lines (228 loc) · 4.76 KB
/
turnserver.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package turnserver
import (
"crypto/rand"
"encoding/base64"
"errors"
"log"
"net"
"strconv"
"sync"
"github.com/pion/turn/v2"
"github.com/pion/webrtc/v3"
)
var username string
var password string
var Address string
var server struct {
mu sync.Mutex
addresses []net.Addr
server *turn.Server
}
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(),
}
}
p, err := net.ListenPacket("udp4", s)
if err == nil {
pcc = &turn.PacketConnConfig{
PacketConn: p,
RelayAddressGenerator: g,
}
} 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,
}
} else {
log.Printf("TURN: listen(%v): %v", s, err)
}
return pcc, lc
}
func Start() error {
server.mu.Lock()
defer server.mu.Unlock()
if server.server != nil {
return nil
}
if Address == "" {
return errors.New("built-in TURN server disabled")
}
ad := Address
if Address == "auto" {
ad = ":1194"
}
addr, err := net.ResolveUDPAddr("udp4", ad)
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)
server.addresses = append(server.addresses, &net.UDPAddr{
IP: a,
Port: addr.Port,
})
}
if lc != nil {
lcs = append(lcs, *lc)
server.addresses = append(server.addresses, &net.TCPAddr{
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)
if pcc != nil {
pccs = append(pccs, *pcc)
server.addresses = append(server.addresses,
&net.UDPAddr{
IP: a,
Port: addr.Port,
},
)
}
if lc != nil {
lcs = append(lcs, *lc)
server.addresses = append(server.addresses,
&net.TCPAddr{
IP: a,
Port: addr.Port,
},
)
}
}
}
if len(pccs) == 0 && len(lcs) == 0 {
return errors.New("couldn't establish any listeners")
}
log.Printf("Starting built-in TURN server on %v", addr.String())
server.server, err = turn.NewServer(turn.ServerConfig{
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 {
server.addresses = nil
return err
}
return nil
}
func ICEServers() []webrtc.ICEServer {
server.mu.Lock()
defer server.mu.Unlock()
if len(server.addresses) == 0 {
return nil
}
var urls []string
for _, a := range server.addresses {
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 {
server.mu.Lock()
defer server.mu.Unlock()
server.addresses = nil
if server.server == nil {
return nil
}
log.Printf("Stopping built-in TURN server")
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()
}