This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
udp_test.go
346 lines (281 loc) · 8.03 KB
/
udp_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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package v2ray_simple_test
import (
"fmt"
"net"
"testing"
"github.com/e1732a364fed/v2ray_simple"
"github.com/e1732a364fed/v2ray_simple/netLayer"
"github.com/e1732a364fed/v2ray_simple/proxy"
"github.com/e1732a364fed/v2ray_simple/proxy/socks5"
"github.com/e1732a364fed/v2ray_simple/utils"
"github.com/miekg/dns"
_ "github.com/e1732a364fed/v2ray_simple/advLayer/grpcSimple"
_ "github.com/e1732a364fed/v2ray_simple/advLayer/ws"
_ "github.com/e1732a364fed/v2ray_simple/proxy/dokodemo"
_ "github.com/e1732a364fed/v2ray_simple/proxy/shadowsocks"
_ "github.com/e1732a364fed/v2ray_simple/proxy/simplesocks"
_ "github.com/e1732a364fed/v2ray_simple/proxy/socks5http"
_ "github.com/e1732a364fed/v2ray_simple/proxy/trojan"
_ "github.com/e1732a364fed/v2ray_simple/proxy/vless"
_ "github.com/e1732a364fed/v2ray_simple/proxy/vmess"
)
func TestUDP_vless(t *testing.T) {
testUDP(t, "vless", 0, "tcp", false, false, false)
}
//v0 没有fullcone
func TestUDP_vless_v1(t *testing.T) {
testUDP(t, "vless", 1, "tcp", false, false, false)
}
func TestUDP_vless_v1_fullcone(t *testing.T) {
testUDP(t, "vless", 1, "tcp", false, true, false)
}
func TestUDP_vless_v1_udpMulti(t *testing.T) {
testUDP(t, "vless", 1, "tcp", true, false, false)
}
func TestUDP_vless_v1_udpMulti_fullcone(t *testing.T) {
testUDP(t, "vless", 1, "tcp", true, true, false)
}
func TestUDP_trojan(t *testing.T) {
testUDP(t, "trojan", 0, "tcp", false, false, false)
}
func TestUDP_trojan_mux(t *testing.T) {
testUDP(t, "trojan", 0, "tcp", false, false, true)
}
func TestUDP_trojan_fullcone(t *testing.T) {
testUDP(t, "trojan", 0, "tcp", false, true, false)
}
func TestUDP_trojan_through_udp(t *testing.T) {
testUDP(t, "trojan", 0, "udp", false, false, false)
}
// udp测试我们直接使用dns请求来测试.
func testUDP(t *testing.T, protocol string, version int, network string, multi bool, fullcone bool, mux bool) {
utils.LogLevel = utils.Log_debug
utils.InitLog("")
//同时监听两个dokodemo, 发向不同raddr, 这样就可以模拟 多raddr目标时的 情况
//vless v1的udp_multi的dialfunc 需要单一 client 拨号多个raddr 才能被触发, 所以还要使用socks5测试两次
isTestInChina := utils.IsTimezoneCN()
var testClientConfFormatStr = `
[[listen]]
protocol = "dokodemo"
network = "udp"
host = "127.0.0.1"
port = %s
target = "udp://8.8.8.8:53"
[[listen]]
protocol = "dokodemo"
network = "udp"
host = "127.0.0.1"
port = %s
target = "udp://%s:53"
[[listen]]
protocol = "socks5"
host = "127.0.0.1"
port = %s
[[dial]]
protocol = "%s"
uuid = "a684455c-b14f-11ea-bf0d-42010aaa0003"
host = "127.0.0.1"
port = %s
version = %d
insecure = true
network = "%s"
`
if mux {
testClientConfFormatStr += "\nuse_mux = true\n"
}
if multi {
testClientConfFormatStr += "\nextra = { vless1_udp_multi = true }\n"
}
clientListenPort := netLayer.RandPortStr(true, true)
clientListen2Port := netLayer.RandPortStr(true, true)
clientDialPort := netLayer.RandPortStr(true, false)
socks5Port, socks5PortStr := netLayer.RandPort_andStr(true, false)
var secondDnsServerStr string
if isTestInChina {
secondDnsServerStr = "114.114.114.114"
} else {
secondDnsServerStr = "1.1.1.1"
}
testClientConfStr := fmt.Sprintf(testClientConfFormatStr, clientListenPort,
clientListen2Port, secondDnsServerStr, socks5PortStr, protocol, clientDialPort, version, network)
var testServerConfFormatStr = `
[[listen]]
protocol = "%s"
uuid = "a684455c-b14f-11ea-bf0d-42010aaa0003"
host = "127.0.0.1"
port = %s
version = %d
insecure = true
cert = "cert.pem"
key = "cert.key"
network = "%s"
[[dial]]
protocol = "direct"
`
if fullcone {
testServerConfFormatStr += "fullcone = true"
}
testServerConfStr := fmt.Sprintf(testServerConfFormatStr, protocol, clientDialPort, version, network)
clientConf, err := proxy.LoadStandardConfFromTomlStr(testClientConfStr)
if err != nil {
t.Log(err)
t.FailNow()
}
serverConf, err := proxy.LoadStandardConfFromTomlStr(testServerConfStr)
if err != nil {
t.Log(err)
t.FailNow()
}
//先建立服务端监听和客户端监听,最后自定义dns查询 并导向 客户端的 dokodemo监听端口
//domodemo in
clientEndInServer, err := proxy.NewServer(clientConf.Listen[0])
if err != nil {
t.Log("can not create clientEndInServer: ", err)
t.FailNow()
}
//domodemo in2
clientEndInServer2, err := proxy.NewServer(clientConf.Listen[1])
if err != nil {
t.Log("can not create clientEndInServer: ", err)
t.FailNow()
}
//socks5 in
clientEndInServer3, err := proxy.NewServer(clientConf.Listen[2])
if err != nil {
t.Log("can not create clientEndInServer: ", err)
t.FailNow()
}
// vless out
clientEndOutClient, err := proxy.NewClient(clientConf.Dial[0])
if err != nil {
t.Log("can not create clientEndOutClient: ", err)
t.FailNow()
}
//vless in
serverEndInServer, err := proxy.NewServer(serverConf.Listen[0])
if err != nil {
t.Log("can not create serverEndInServer: ", err)
t.FailNow()
}
// direct out
serverEndOutClient, err := proxy.NewClient(serverConf.Dial[0])
if err != nil {
t.Log("can not create serverEndOutClient: ", err)
t.FailNow()
}
c1 := v2ray_simple.ListenSer(clientEndInServer, clientEndOutClient, nil, nil)
c2 := v2ray_simple.ListenSer(clientEndInServer2, clientEndOutClient, nil, nil)
c3 := v2ray_simple.ListenSer(clientEndInServer3, clientEndOutClient, nil, nil)
c4 := v2ray_simple.ListenSer(serverEndInServer, serverEndOutClient, nil, nil)
if c1 != nil {
defer c1.Close()
}
if c2 != nil {
defer c2.Close()
}
if c3 != nil {
defer c3.Close()
}
if c4 != nil {
defer c4.Close()
}
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn("www.qq.com"), dns.TypeA)
c := new(dns.Client)
// server 1 测试 /////////////////////////////////////////
r, _, err := c.Exchange(m, "127.0.0.1:"+clientListenPort)
if r == nil {
t.Log("error: ", err.Error())
t.FailNow()
}
if r.Rcode != dns.RcodeSuccess {
t.Log("err2 ", r.Rcode, r)
t.FailNow()
}
for _, a := range r.Answer {
t.Log("header is", a.Header())
t.Log("string is", a.String())
t.Log("a is ", a)
if aa, ok := a.(*dns.A); ok {
t.Log("arecord is ", aa.A)
}
}
// server 2 测试 /////////////////////////////////////////
r, _, err = c.Exchange(m, "127.0.0.1:"+clientListen2Port)
if r == nil {
t.Log("test2, error: ", err.Error())
t.FailNow()
}
if r.Rcode != dns.RcodeSuccess {
t.Log("test2, err2 ", r.Rcode, r)
t.FailNow()
}
for _, a := range r.Answer {
t.Log("test2, header is", a.Header())
t.Log("test2, string is", a.String())
t.Log("test2, a is ", a)
if aa, ok := a.(*dns.A); ok {
t.Log("test2, arecord is ", aa.A)
}
}
// server 3 socks5 udp 测试 /////////////////////////////////////////
//向不同地址写入两次,以测试 vless v1 udp multi
socks5ClientConn := &socks5.ClientUDPConn{
ServerAddr: &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: socks5Port,
},
WriteUDP_Target: &net.UDPAddr{
IP: net.IPv4(8, 8, 8, 8),
Port: 53,
},
}
err = socks5ClientConn.Associate()
if err != nil {
t.Log("test3, Associate error: ", err.Error())
t.FailNow()
}
r, _, err = c.ExchangeWithConn(m, &dns.Conn{
Conn: socks5ClientConn,
})
if r == nil {
t.Log("test3, error: ", err.Error())
t.FailNow()
}
if r.Rcode != dns.RcodeSuccess {
t.Log("test3, err2 ", r.Rcode, r)
t.FailNow()
}
for _, a := range r.Answer {
t.Log("test3, header is", a.Header())
t.Log("test3, string is", a.String())
t.Log("test3, a is ", a)
if aa, ok := a.(*dns.A); ok {
t.Log("test3, arecord is ", aa.A)
}
}
if isTestInChina {
socks5ClientConn.WriteUDP_Target.IP = net.IPv4(114, 114, 114, 114)
} else {
socks5ClientConn.WriteUDP_Target.IP = net.IPv4(1, 1, 1, 1)
}
r, _, err = c.ExchangeWithConn(m, &dns.Conn{
Conn: socks5ClientConn,
})
if r == nil {
t.Log("test3_2, error: ", err.Error())
t.FailNow()
}
if r.Rcode != dns.RcodeSuccess {
t.Log("test3_2, err2 ", r.Rcode, r)
t.FailNow()
}
for _, a := range r.Answer {
t.Log("test3_2, header is", a.Header())
t.Log("test3_2, string is", a.String())
t.Log("test3_2, a is ", a)
if aa, ok := a.(*dns.A); ok {
t.Log("test3_2, arecord is ", aa.A)
}
}
}