forked from pion/turn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_vnet_test.go
293 lines (247 loc) · 6.34 KB
/
server_vnet_test.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package turn
import (
"net"
"testing"
"time"
"github.com/pion/logging"
"github.com/pion/transport/vnet"
"github.com/stretchr/testify/assert"
)
type VNet struct {
wan *vnet.Router
net0 *vnet.Net // net (0) on the WAN
net1 *vnet.Net // net (1) on the WAN
netL0 *vnet.Net // net (0) on the LAN
server *Server
}
func (v *VNet) Close() error {
err := v.server.Close()
v.wan.Stop() // nolint:errcheck,gosec
if err != nil {
return err
}
return nil
}
func buildVNet() (*VNet, error) {
loggerFactory := logging.NewDefaultLoggerFactory()
// WAN
wan, err := vnet.NewRouter(&vnet.RouterConfig{
CIDR: "0.0.0.0/0",
LoggerFactory: loggerFactory,
})
if err != nil {
return nil, err
}
net0 := vnet.NewNet(&vnet.NetConfig{
StaticIP: "1.2.3.4", // will be assigned to eth0
})
err = wan.AddNet(net0)
if err != nil {
return nil, err
}
net1 := vnet.NewNet(&vnet.NetConfig{
StaticIP: "1.2.3.5", // will be assigned to eth0
})
err = wan.AddNet(net1)
if err != nil {
return nil, err
}
// LAN
lan, err := vnet.NewRouter(&vnet.RouterConfig{
StaticIP: "5.6.7.8", // this router's external IP on eth0
CIDR: "192.168.0.0/24",
NATType: &vnet.NATType{
MappingBehavior: vnet.EndpointIndependent,
FilteringBehavior: vnet.EndpointIndependent,
},
LoggerFactory: loggerFactory,
})
if err != nil {
return nil, err
}
netL0 := vnet.NewNet(&vnet.NetConfig{})
err = lan.AddNet(netL0)
if err != nil {
return nil, err
}
err = wan.AddRouter(lan)
if err != nil {
return nil, err
}
err = wan.Start()
if err != nil {
return nil, err
}
// start server...
credMap := map[string]string{}
credMap["user"] = "pass"
server := NewServer(&ServerConfig{
AuthHandler: func(username string, srcAddr net.Addr) (password string, ok bool) {
if pw, ok := credMap[username]; ok {
return pw, true
}
return "", false
},
Realm: "pion.ly",
Net: net0,
LoggerFactory: loggerFactory,
})
err = server.AddListeningIPAddr("1.2.3.4")
if err != nil {
return nil, err
}
// register host names
err = wan.AddHost("stun.pion.ly", "1.2.3.4")
if err != nil {
return nil, err
}
err = wan.AddHost("turn.pion.ly", "1.2.3.4")
if err != nil {
return nil, err
}
err = wan.AddHost("echo.pion.ly", "1.2.3.5")
if err != nil {
return nil, err
}
err = server.Start()
if err != nil {
wan.Stop() // nolint:errcheck,gosec
return nil, err
}
return &VNet{
wan: wan,
net0: net0,
net1: net1,
netL0: netL0,
server: server,
}, nil
}
func TestServerVNet(t *testing.T) {
loggerFactory := logging.NewDefaultLoggerFactory()
log := loggerFactory.NewLogger("test")
t.Run("SendBindingRequest", func(t *testing.T) {
v, err := buildVNet()
if !assert.NoError(t, err, "should succeed") {
return
}
defer v.Close() // nolint:errcheck
lconn, err := v.netL0.ListenPacket("udp4", "0.0.0.0:0")
if !assert.NoError(t, err, "should succeed") {
return
}
defer lconn.Close() // nolint:errcheck,gosec
log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
STUNServerAddr: "1.2.3.4:3478",
Conn: lconn,
Net: v.netL0,
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
return
}
err = client.Listen()
if !assert.NoError(t, err, "should succeed") {
return
}
defer client.Close()
log.Debug("sending a binding request.")
reflAddr, err := client.SendBindingRequest()
if !assert.NoError(t, err, "should succeed") {
return
}
log.Debugf("mapped-address: %v", reflAddr.String())
udpAddr := reflAddr.(*net.UDPAddr)
// The mapped-address should have IP address that was assigned
// to the LAN router.
assert.True(t, udpAddr.IP.Equal(net.IPv4(5, 6, 7, 8)), "should match")
})
t.Run("Echo via relay", func(t *testing.T) {
v, err := buildVNet()
if !assert.NoError(t, err, "should succeed") {
return
}
lconn, err := v.netL0.ListenPacket("udp4", "0.0.0.0:0")
if !assert.NoError(t, err, "should succeed") {
return
}
log.Debug("creating a client.")
client, err := NewClient(&ClientConfig{
STUNServerAddr: "stun.pion.ly:3478",
TURNServerAddr: "turn.pion.ly:3478",
Username: "user",
Password: "pass",
Conn: lconn,
Net: v.netL0,
LoggerFactory: loggerFactory,
})
if !assert.NoError(t, err, "should succeed") {
return
}
err = client.Listen()
if !assert.NoError(t, err, "should succeed") {
return
}
log.Debug("sending a binding request.")
conn, err := client.Allocate()
if !assert.NoError(t, err, "should succeed") {
return
}
log.Debugf("laddr: %s", conn.LocalAddr().String())
echoConn, err := v.net1.ListenPacket("udp4", "1.2.3.5:5678")
if !assert.NoError(t, err, "should succeed") {
return
}
go func() {
buf := make([]byte, 1500)
for {
n, from, err2 := echoConn.ReadFrom(buf)
if err2 != nil {
break
}
log.Debugf("echo: received %d bytes from %s: %s", n, from.String(), string(buf[:n]))
// verify the message was received from the relay address
if !assert.Equal(t, conn.LocalAddr().String(), from.String(), "should match") {
break
}
// verify the message received is correct
if !assert.Equal(t, "Hello", string(buf[:n]), "should match") {
break
}
// echo the data
_, err2 = echoConn.WriteTo(buf[:n], from)
if !assert.NoError(t, err2, "should succeed") {
break
}
}
}()
buf := make([]byte, 1500)
for i := 0; i < 10; i++ {
log.Debug("sending \"Hello\"..")
_, err = conn.WriteTo([]byte("Hello"), echoConn.LocalAddr())
if !assert.NoError(t, err, "should succeed") {
return
}
_, from, err2 := conn.ReadFrom(buf)
assert.NoError(t, err2, "should succeed")
// verify the message was received from the relay address
assert.Equal(t, echoConn.LocalAddr().String(), from.String(), "should match")
time.Sleep(100 * time.Millisecond)
}
time.Sleep(100 * time.Millisecond)
err = conn.Close()
assert.NoError(t, err, "should succeed")
log.Debug("RELAY CONN CLOSED")
err = echoConn.Close()
assert.NoError(t, err, "should succeed")
log.Debug("ECHO SERVER CLOSED")
client.Close()
log.Debug("TURN CLIENT CLOSED")
err = lconn.Close()
assert.NoError(t, err, "should succeed")
log.Debug("LOCAL CONN CLOSED")
err = v.Close()
assert.NoError(t, err, "should succeed")
log.Debug("VNET CLOSED")
})
}