1
1
package router_test
2
2
3
3
import (
4
+ "errors"
4
5
"os"
5
6
"path/filepath"
6
7
"strconv"
8
+ "strings"
7
9
"testing"
8
10
9
- "github.com/ golang/protobuf/proto"
11
+ "google. golang.org /protobuf/proto"
10
12
11
- . "github.com/v2fly/v2ray-core/v4/app/router"
13
+ "github.com/v2fly/v2ray-core/v4/app/router"
12
14
"github.com/v2fly/v2ray-core/v4/common"
13
- "github.com/v2fly/v2ray-core/v4/common/errors"
14
15
"github.com/v2fly/v2ray-core/v4/common/net"
15
16
"github.com/v2fly/v2ray-core/v4/common/platform"
16
17
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
@@ -25,11 +26,28 @@ func init() {
25
26
wd , err := os .Getwd ()
26
27
common .Must (err )
27
28
28
- if _ , err := os .Stat (platform .GetAssetLocation ("geoip.dat" )); err != nil && os .IsNotExist (err ) {
29
- common .Must (filesystem .CopyFile (platform .GetAssetLocation ("geoip.dat" ), filepath .Join (wd , ".." , ".." , "release" , "config" , "geoip.dat" )))
29
+ tempPath := filepath .Join (wd , ".." , ".." , "testing" , "temp" )
30
+ geoipPath := filepath .Join (tempPath , "geoip.dat" )
31
+ geositePath := filepath .Join (tempPath , "geosite.dat" )
32
+
33
+ os .Setenv ("v2ray.location.asset" , tempPath )
34
+
35
+ if _ , err := os .Stat (platform .GetAssetLocation ("geoip.dat" )); err != nil && errors .Is (err , os .ErrNotExist ) {
36
+ if _ , err := os .Stat (geoipPath ); err != nil && errors .Is (err , os .ErrNotExist ) {
37
+ common .Must (os .MkdirAll (tempPath , 0755 ))
38
+ geoipBytes , err := common .FetchHTTPContent (geoipURL )
39
+ common .Must (err )
40
+ common .Must (filesystem .WriteFile (geoipPath , geoipBytes ))
41
+ }
30
42
}
31
- if _ , err := os .Stat (platform .GetAssetLocation ("geosite.dat" )); err != nil && os .IsNotExist (err ) {
32
- common .Must (filesystem .CopyFile (platform .GetAssetLocation ("geosite.dat" ), filepath .Join (wd , ".." , ".." , "release" , "config" , "geosite.dat" )))
43
+
44
+ if _ , err := os .Stat (platform .GetAssetLocation ("geosite.dat" )); err != nil && errors .Is (err , os .ErrNotExist ) {
45
+ if _ , err := os .Stat (geositePath ); err != nil && errors .Is (err , os .ErrNotExist ) {
46
+ common .Must (os .MkdirAll (tempPath , 0755 ))
47
+ geositeBytes , err := common .FetchHTTPContent (geositeURL )
48
+ common .Must (err )
49
+ common .Must (filesystem .WriteFile (geositePath , geositeBytes ))
50
+ }
33
51
}
34
52
}
35
53
@@ -56,23 +74,23 @@ func TestRoutingRule(t *testing.T) {
56
74
}
57
75
58
76
cases := []struct {
59
- rule * RoutingRule
77
+ rule * router. RoutingRule
60
78
test []ruleTest
61
79
}{
62
80
{
63
- rule : & RoutingRule {
64
- Domain : []* Domain {
81
+ rule : & router. RoutingRule {
82
+ Domain : []* router. Domain {
65
83
{
66
84
Value : "v2fly.org" ,
67
- Type : Domain_Plain ,
85
+ Type : router . Domain_Plain ,
68
86
},
69
87
{
70
88
Value : "google.com" ,
71
- Type : Domain_Domain ,
89
+ Type : router . Domain_Domain ,
72
90
},
73
91
{
74
92
Value : "^facebook\\ .com$" ,
75
- Type : Domain_Regex ,
93
+ Type : router . Domain_Regex ,
76
94
},
77
95
},
78
96
},
@@ -108,8 +126,8 @@ func TestRoutingRule(t *testing.T) {
108
126
},
109
127
},
110
128
{
111
- rule : & RoutingRule {
112
- Cidr : []* CIDR {
129
+ rule : & router. RoutingRule {
130
+ Cidr : []* router. CIDR {
113
131
{
114
132
Ip : []byte {8 , 8 , 8 , 8 },
115
133
Prefix : 32 ,
@@ -144,10 +162,10 @@ func TestRoutingRule(t *testing.T) {
144
162
},
145
163
},
146
164
{
147
- rule : & RoutingRule {
148
- Geoip : []* GeoIP {
165
+ rule : & router. RoutingRule {
166
+ Geoip : []* router. GeoIP {
149
167
{
150
- Cidr : []* CIDR {
168
+ Cidr : []* router. CIDR {
151
169
{
152
170
Ip : []byte {8 , 8 , 8 , 8 },
153
171
Prefix : 32 ,
@@ -184,8 +202,8 @@ func TestRoutingRule(t *testing.T) {
184
202
},
185
203
},
186
204
{
187
- rule : & RoutingRule {
188
- SourceCidr : []* CIDR {
205
+ rule : & router. RoutingRule {
206
+ SourceCidr : []* router. CIDR {
189
207
{
190
208
Ip : []byte {192 , 168 , 0 , 0 },
191
209
Prefix : 16 ,
@@ -204,7 +222,7 @@ func TestRoutingRule(t *testing.T) {
204
222
},
205
223
},
206
224
{
207
- rule : & RoutingRule {
225
+ rule : & router. RoutingRule {
208
226
UserEmail : []string {
209
227
"admin@v2fly.org" ,
210
228
},
@@ -225,7 +243,7 @@ func TestRoutingRule(t *testing.T) {
225
243
},
226
244
},
227
245
{
228
- rule : & RoutingRule {
246
+ rule : & router. RoutingRule {
229
247
Protocol : []string {"http" },
230
248
},
231
249
test : []ruleTest {
@@ -236,7 +254,7 @@ func TestRoutingRule(t *testing.T) {
236
254
},
237
255
},
238
256
{
239
- rule : & RoutingRule {
257
+ rule : & router. RoutingRule {
240
258
InboundTag : []string {"test" , "test1" },
241
259
},
242
260
test : []ruleTest {
@@ -251,7 +269,7 @@ func TestRoutingRule(t *testing.T) {
251
269
},
252
270
},
253
271
{
254
- rule : & RoutingRule {
272
+ rule : & router. RoutingRule {
255
273
PortList : & net.PortList {
256
274
Range : []* net.PortRange {
257
275
{From : 443 , To : 443 },
@@ -279,7 +297,7 @@ func TestRoutingRule(t *testing.T) {
279
297
},
280
298
},
281
299
{
282
- rule : & RoutingRule {
300
+ rule : & router. RoutingRule {
283
301
SourcePortList : & net.PortList {
284
302
Range : []* net.PortRange {
285
303
{From : 123 , To : 123 },
@@ -307,7 +325,7 @@ func TestRoutingRule(t *testing.T) {
307
325
},
308
326
},
309
327
{
310
- rule : & RoutingRule {
328
+ rule : & router. RoutingRule {
311
329
Protocol : []string {"http" },
312
330
Attributes : "attrs[':path'].startswith('/test')" ,
313
331
},
@@ -333,32 +351,31 @@ func TestRoutingRule(t *testing.T) {
333
351
}
334
352
}
335
353
336
- func loadGeoSite (country string ) ([]* Domain , error ) {
354
+ func loadGeoSite (country string ) ([]* router. Domain , error ) {
337
355
geositeBytes , err := filesystem .ReadAsset ("geosite.dat" )
338
356
if err != nil {
339
357
return nil , err
340
358
}
341
- var geositeList GeoSiteList
359
+ var geositeList router. GeoSiteList
342
360
if err := proto .Unmarshal (geositeBytes , & geositeList ); err != nil {
343
361
return nil , err
344
362
}
345
363
346
364
for _ , site := range geositeList .Entry {
347
- if site .CountryCode == country {
365
+ if strings . EqualFold ( site .CountryCode , country ) {
348
366
return site .Domain , nil
349
367
}
350
368
}
351
369
352
370
return nil , errors .New ("country not found: " + country )
353
371
}
354
-
355
372
func TestChinaSites (t * testing.T ) {
356
373
domains , err := loadGeoSite ("CN" )
357
374
common .Must (err )
358
375
359
- matcher , err := NewDomainMatcher (domains )
376
+ matcher , err := router . NewDomainMatcher (domains )
360
377
common .Must (err )
361
- acMatcher , err := NewMphMatcherGroup (domains )
378
+ acMatcher , err := router . NewMphMatcherGroup (domains )
362
379
common .Must (err )
363
380
364
381
type TestCase struct {
@@ -403,7 +420,7 @@ func BenchmarkMphDomainMatcher(b *testing.B) {
403
420
domains , err := loadGeoSite ("CN" )
404
421
common .Must (err )
405
422
406
- matcher , err := NewMphMatcherGroup (domains )
423
+ matcher , err := router . NewMphMatcherGroup (domains )
407
424
common .Must (err )
408
425
409
426
type TestCase struct {
@@ -445,7 +462,7 @@ func BenchmarkDomainMatcher(b *testing.B) {
445
462
domains , err := loadGeoSite ("CN" )
446
463
common .Must (err )
447
464
448
- matcher , err := NewDomainMatcher (domains )
465
+ matcher , err := router . NewDomainMatcher (domains )
449
466
common .Must (err )
450
467
451
468
type TestCase struct {
@@ -484,12 +501,12 @@ func BenchmarkDomainMatcher(b *testing.B) {
484
501
}
485
502
486
503
func BenchmarkMultiGeoIPMatcher (b * testing.B ) {
487
- var geoips []* GeoIP
504
+ var geoips []* router. GeoIP
488
505
489
506
{
490
507
ips , err := loadGeoIP ("CN" )
491
508
common .Must (err )
492
- geoips = append (geoips , & GeoIP {
509
+ geoips = append (geoips , & router. GeoIP {
493
510
CountryCode : "CN" ,
494
511
Cidr : ips ,
495
512
})
@@ -498,7 +515,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
498
515
{
499
516
ips , err := loadGeoIP ("JP" )
500
517
common .Must (err )
501
- geoips = append (geoips , & GeoIP {
518
+ geoips = append (geoips , & router. GeoIP {
502
519
CountryCode : "JP" ,
503
520
Cidr : ips ,
504
521
})
@@ -507,7 +524,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
507
524
{
508
525
ips , err := loadGeoIP ("CA" )
509
526
common .Must (err )
510
- geoips = append (geoips , & GeoIP {
527
+ geoips = append (geoips , & router. GeoIP {
511
528
CountryCode : "CA" ,
512
529
Cidr : ips ,
513
530
})
@@ -516,13 +533,13 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
516
533
{
517
534
ips , err := loadGeoIP ("US" )
518
535
common .Must (err )
519
- geoips = append (geoips , & GeoIP {
536
+ geoips = append (geoips , & router. GeoIP {
520
537
CountryCode : "US" ,
521
538
Cidr : ips ,
522
539
})
523
540
}
524
541
525
- matcher , err := NewMultiGeoIPMatcher (geoips , false )
542
+ matcher , err := router . NewMultiGeoIPMatcher (geoips , false )
526
543
common .Must (err )
527
544
528
545
ctx := withOutbound (& session.Outbound {Target : net .TCPDestination (net .ParseAddress ("8.8.8.8" ), 80 )})
0 commit comments