Skip to content

Commit 10ca68f

Browse files
authored
Feat: remove geo files from repo & refine tests (v2fly#869)
1 parent 3f9b5e2 commit 10ca68f

16 files changed

+194
-11387
lines changed

.github/workflows/release.yml

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ jobs:
118118
mv v2ray v2ray.exe
119119
mv v2ctl v2ctl.exe
120120
121+
- name: Download geo files
122+
run: |
123+
wget -O release/config/geoip.dat "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
124+
wget -O release/config/geosite.dat "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
125+
121126
- name: Prepare package
122127
run: cp -v ./release/config/*.* ./build_assets
123128

.github/workflows/updateGeofile.yml

-25
This file was deleted.

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ vprotogen
2525
!infra/vprotogen/
2626
errorgen
2727
!common/errors/errorgen/
28-
infra/conf/*.dat
28+
*.dat

app/router/condition_geoip_test.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package router_test
22

33
import (
4+
"errors"
45
"os"
56
"path/filepath"
7+
"strings"
68
"testing"
79

8-
"github.com/golang/protobuf/proto"
10+
"google.golang.org/protobuf/proto"
911

1012
"github.com/v2fly/v2ray-core/v4/app/router"
1113
"github.com/v2fly/v2ray-core/v4/common"
@@ -18,11 +20,18 @@ func init() {
1820
wd, err := os.Getwd()
1921
common.Must(err)
2022

21-
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
22-
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
23-
}
24-
if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && os.IsNotExist(err) {
25-
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
23+
tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
24+
geoipPath := filepath.Join(tempPath, "geoip.dat")
25+
26+
os.Setenv("v2ray.location.asset", tempPath)
27+
28+
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, os.ErrNotExist) {
29+
if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, os.ErrNotExist) {
30+
common.Must(os.MkdirAll(tempPath, 0755))
31+
geoipBytes, err := common.FetchHTTPContent(geoipURL)
32+
common.Must(err)
33+
common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
34+
}
2635
}
2736
}
2837

@@ -195,7 +204,7 @@ func loadGeoIP(country string) ([]*router.CIDR, error) {
195204
}
196205

197206
for _, geoip := range geoipList.Entry {
198-
if geoip.CountryCode == country {
207+
if strings.EqualFold(geoip.CountryCode, country) {
199208
return geoip.Cidr, nil
200209
}
201210
}

app/router/condition_test.go

+57-40
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package router_test
22

33
import (
4+
"errors"
45
"os"
56
"path/filepath"
67
"strconv"
8+
"strings"
79
"testing"
810

9-
"github.com/golang/protobuf/proto"
11+
"google.golang.org/protobuf/proto"
1012

11-
. "github.com/v2fly/v2ray-core/v4/app/router"
13+
"github.com/v2fly/v2ray-core/v4/app/router"
1214
"github.com/v2fly/v2ray-core/v4/common"
13-
"github.com/v2fly/v2ray-core/v4/common/errors"
1415
"github.com/v2fly/v2ray-core/v4/common/net"
1516
"github.com/v2fly/v2ray-core/v4/common/platform"
1617
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
@@ -25,11 +26,28 @@ func init() {
2526
wd, err := os.Getwd()
2627
common.Must(err)
2728

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+
}
3042
}
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+
}
3351
}
3452
}
3553

@@ -56,23 +74,23 @@ func TestRoutingRule(t *testing.T) {
5674
}
5775

5876
cases := []struct {
59-
rule *RoutingRule
77+
rule *router.RoutingRule
6078
test []ruleTest
6179
}{
6280
{
63-
rule: &RoutingRule{
64-
Domain: []*Domain{
81+
rule: &router.RoutingRule{
82+
Domain: []*router.Domain{
6583
{
6684
Value: "v2fly.org",
67-
Type: Domain_Plain,
85+
Type: router.Domain_Plain,
6886
},
6987
{
7088
Value: "google.com",
71-
Type: Domain_Domain,
89+
Type: router.Domain_Domain,
7290
},
7391
{
7492
Value: "^facebook\\.com$",
75-
Type: Domain_Regex,
93+
Type: router.Domain_Regex,
7694
},
7795
},
7896
},
@@ -108,8 +126,8 @@ func TestRoutingRule(t *testing.T) {
108126
},
109127
},
110128
{
111-
rule: &RoutingRule{
112-
Cidr: []*CIDR{
129+
rule: &router.RoutingRule{
130+
Cidr: []*router.CIDR{
113131
{
114132
Ip: []byte{8, 8, 8, 8},
115133
Prefix: 32,
@@ -144,10 +162,10 @@ func TestRoutingRule(t *testing.T) {
144162
},
145163
},
146164
{
147-
rule: &RoutingRule{
148-
Geoip: []*GeoIP{
165+
rule: &router.RoutingRule{
166+
Geoip: []*router.GeoIP{
149167
{
150-
Cidr: []*CIDR{
168+
Cidr: []*router.CIDR{
151169
{
152170
Ip: []byte{8, 8, 8, 8},
153171
Prefix: 32,
@@ -184,8 +202,8 @@ func TestRoutingRule(t *testing.T) {
184202
},
185203
},
186204
{
187-
rule: &RoutingRule{
188-
SourceCidr: []*CIDR{
205+
rule: &router.RoutingRule{
206+
SourceCidr: []*router.CIDR{
189207
{
190208
Ip: []byte{192, 168, 0, 0},
191209
Prefix: 16,
@@ -204,7 +222,7 @@ func TestRoutingRule(t *testing.T) {
204222
},
205223
},
206224
{
207-
rule: &RoutingRule{
225+
rule: &router.RoutingRule{
208226
UserEmail: []string{
209227
"admin@v2fly.org",
210228
},
@@ -225,7 +243,7 @@ func TestRoutingRule(t *testing.T) {
225243
},
226244
},
227245
{
228-
rule: &RoutingRule{
246+
rule: &router.RoutingRule{
229247
Protocol: []string{"http"},
230248
},
231249
test: []ruleTest{
@@ -236,7 +254,7 @@ func TestRoutingRule(t *testing.T) {
236254
},
237255
},
238256
{
239-
rule: &RoutingRule{
257+
rule: &router.RoutingRule{
240258
InboundTag: []string{"test", "test1"},
241259
},
242260
test: []ruleTest{
@@ -251,7 +269,7 @@ func TestRoutingRule(t *testing.T) {
251269
},
252270
},
253271
{
254-
rule: &RoutingRule{
272+
rule: &router.RoutingRule{
255273
PortList: &net.PortList{
256274
Range: []*net.PortRange{
257275
{From: 443, To: 443},
@@ -279,7 +297,7 @@ func TestRoutingRule(t *testing.T) {
279297
},
280298
},
281299
{
282-
rule: &RoutingRule{
300+
rule: &router.RoutingRule{
283301
SourcePortList: &net.PortList{
284302
Range: []*net.PortRange{
285303
{From: 123, To: 123},
@@ -307,7 +325,7 @@ func TestRoutingRule(t *testing.T) {
307325
},
308326
},
309327
{
310-
rule: &RoutingRule{
328+
rule: &router.RoutingRule{
311329
Protocol: []string{"http"},
312330
Attributes: "attrs[':path'].startswith('/test')",
313331
},
@@ -333,32 +351,31 @@ func TestRoutingRule(t *testing.T) {
333351
}
334352
}
335353

336-
func loadGeoSite(country string) ([]*Domain, error) {
354+
func loadGeoSite(country string) ([]*router.Domain, error) {
337355
geositeBytes, err := filesystem.ReadAsset("geosite.dat")
338356
if err != nil {
339357
return nil, err
340358
}
341-
var geositeList GeoSiteList
359+
var geositeList router.GeoSiteList
342360
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
343361
return nil, err
344362
}
345363

346364
for _, site := range geositeList.Entry {
347-
if site.CountryCode == country {
365+
if strings.EqualFold(site.CountryCode, country) {
348366
return site.Domain, nil
349367
}
350368
}
351369

352370
return nil, errors.New("country not found: " + country)
353371
}
354-
355372
func TestChinaSites(t *testing.T) {
356373
domains, err := loadGeoSite("CN")
357374
common.Must(err)
358375

359-
matcher, err := NewDomainMatcher(domains)
376+
matcher, err := router.NewDomainMatcher(domains)
360377
common.Must(err)
361-
acMatcher, err := NewMphMatcherGroup(domains)
378+
acMatcher, err := router.NewMphMatcherGroup(domains)
362379
common.Must(err)
363380

364381
type TestCase struct {
@@ -403,7 +420,7 @@ func BenchmarkMphDomainMatcher(b *testing.B) {
403420
domains, err := loadGeoSite("CN")
404421
common.Must(err)
405422

406-
matcher, err := NewMphMatcherGroup(domains)
423+
matcher, err := router.NewMphMatcherGroup(domains)
407424
common.Must(err)
408425

409426
type TestCase struct {
@@ -445,7 +462,7 @@ func BenchmarkDomainMatcher(b *testing.B) {
445462
domains, err := loadGeoSite("CN")
446463
common.Must(err)
447464

448-
matcher, err := NewDomainMatcher(domains)
465+
matcher, err := router.NewDomainMatcher(domains)
449466
common.Must(err)
450467

451468
type TestCase struct {
@@ -484,12 +501,12 @@ func BenchmarkDomainMatcher(b *testing.B) {
484501
}
485502

486503
func BenchmarkMultiGeoIPMatcher(b *testing.B) {
487-
var geoips []*GeoIP
504+
var geoips []*router.GeoIP
488505

489506
{
490507
ips, err := loadGeoIP("CN")
491508
common.Must(err)
492-
geoips = append(geoips, &GeoIP{
509+
geoips = append(geoips, &router.GeoIP{
493510
CountryCode: "CN",
494511
Cidr: ips,
495512
})
@@ -498,7 +515,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
498515
{
499516
ips, err := loadGeoIP("JP")
500517
common.Must(err)
501-
geoips = append(geoips, &GeoIP{
518+
geoips = append(geoips, &router.GeoIP{
502519
CountryCode: "JP",
503520
Cidr: ips,
504521
})
@@ -507,7 +524,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
507524
{
508525
ips, err := loadGeoIP("CA")
509526
common.Must(err)
510-
geoips = append(geoips, &GeoIP{
527+
geoips = append(geoips, &router.GeoIP{
511528
CountryCode: "CA",
512529
Cidr: ips,
513530
})
@@ -516,13 +533,13 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) {
516533
{
517534
ips, err := loadGeoIP("US")
518535
common.Must(err)
519-
geoips = append(geoips, &GeoIP{
536+
geoips = append(geoips, &router.GeoIP{
520537
CountryCode: "US",
521538
Cidr: ips,
522539
})
523540
}
524541

525-
matcher, err := NewMultiGeoIPMatcher(geoips, false)
542+
matcher, err := router.NewMultiGeoIPMatcher(geoips, false)
526543
common.Must(err)
527544

528545
ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})

app/router/constant_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package router_test
2+
3+
const (
4+
geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
5+
geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
6+
)

0 commit comments

Comments
 (0)