Skip to content

Commit 4eb2b5e

Browse files
committed
merge ext into core
1 parent d84166b commit 4eb2b5e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6346
-18
lines changed

app/router/condition_geoip_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ import (
1010
"v2ray.com/core/common"
1111
"v2ray.com/core/common/net"
1212
"v2ray.com/core/common/platform"
13-
"v2ray.com/ext/sysio"
13+
"v2ray.com/core/common/platform/filesystem"
1414
)
1515

16+
func init() {
17+
wd, err := os.Getwd()
18+
common.Must(err)
19+
20+
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
21+
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
22+
}
23+
1624
func TestGeoIPMatcherContainer(t *testing.T) {
1725
container := &router.GeoIPMatcherContainer{}
1826

@@ -112,8 +120,6 @@ func TestGeoIPMatcher(t *testing.T) {
112120
}
113121

114122
func TestGeoIPMatcher4CN(t *testing.T) {
115-
common.Must(sysio.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geoip.dat")))
116-
117123
ips, err := loadGeoIP("CN")
118124
common.Must(err)
119125

@@ -126,8 +132,6 @@ func TestGeoIPMatcher4CN(t *testing.T) {
126132
}
127133

128134
func TestGeoIPMatcher6US(t *testing.T) {
129-
common.Must(sysio.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geoip.dat")))
130-
131135
ips, err := loadGeoIP("US")
132136
common.Must(err)
133137

@@ -140,7 +144,7 @@ func TestGeoIPMatcher6US(t *testing.T) {
140144
}
141145

142146
func loadGeoIP(country string) ([]*router.CIDR, error) {
143-
geoipBytes, err := sysio.ReadAsset("geoip.dat")
147+
geoipBytes, err := filesystem.ReadAsset("geoip.dat")
144148
if err != nil {
145149
return nil, err
146150
}
@@ -159,8 +163,6 @@ func loadGeoIP(country string) ([]*router.CIDR, error) {
159163
}
160164

161165
func BenchmarkGeoIPMatcher4CN(b *testing.B) {
162-
common.Must(sysio.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geoip.dat")))
163-
164166
ips, err := loadGeoIP("CN")
165167
common.Must(err)
166168

@@ -175,8 +177,6 @@ func BenchmarkGeoIPMatcher4CN(b *testing.B) {
175177
}
176178

177179
func BenchmarkGeoIPMatcher6US(b *testing.B) {
178-
common.Must(sysio.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geoip.dat")))
179-
180180
ips, err := loadGeoIP("US")
181181
common.Must(err)
182182

app/router/condition_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ import (
1515
"v2ray.com/core/common/errors"
1616
"v2ray.com/core/common/net"
1717
"v2ray.com/core/common/platform"
18+
"v2ray.com/core/common/platform/filesystem"
1819
"v2ray.com/core/common/protocol"
1920
"v2ray.com/core/common/protocol/http"
2021
"v2ray.com/core/common/session"
21-
"v2ray.com/ext/sysio"
2222
)
2323

24+
func init() {
25+
wd, err := os.Getwd()
26+
common.Must(err)
27+
28+
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
29+
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
30+
}
31+
2432
func withOutbound(outbound *session.Outbound) context.Context {
2533
return session.ContextWithOutbound(context.Background(), outbound)
2634
}
@@ -246,7 +254,7 @@ func TestRoutingRule(t *testing.T) {
246254
}
247255

248256
func loadGeoSite(country string) ([]*Domain, error) {
249-
geositeBytes, err := sysio.ReadAsset("geosite.dat")
257+
geositeBytes, err := filesystem.ReadAsset("geosite.dat")
250258
if err != nil {
251259
return nil, err
252260
}
@@ -265,8 +273,6 @@ func loadGeoSite(country string) ([]*Domain, error) {
265273
}
266274

267275
func TestChinaSites(t *testing.T) {
268-
common.Must(sysio.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geosite.dat")))
269-
270276
domains, err := loadGeoSite("CN")
271277
common.Must(err)
272278

@@ -309,8 +315,6 @@ func TestChinaSites(t *testing.T) {
309315
}
310316

311317
func BenchmarkMultiGeoIPMatcher(b *testing.B) {
312-
common.Must(sysio.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geoip.dat")))
313-
314318
var geoips []*GeoIP
315319

316320
{

common/platform/filesystem/file.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package filesystem
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"v2ray.com/core/common/buf"
8+
"v2ray.com/core/common/platform"
9+
)
10+
11+
type FileReaderFunc func(path string) (io.ReadCloser, error)
12+
13+
var NewFileReader FileReaderFunc = func(path string) (io.ReadCloser, error) {
14+
return os.Open(path)
15+
}
16+
17+
func ReadFile(path string) ([]byte, error) {
18+
reader, err := NewFileReader(path)
19+
if err != nil {
20+
return nil, err
21+
}
22+
defer reader.Close()
23+
24+
return buf.ReadAllToBytes(reader)
25+
}
26+
27+
func ReadAsset(file string) ([]byte, error) {
28+
return ReadFile(platform.GetAssetLocation(file))
29+
}
30+
31+
func CopyFile(dst string, src string) error {
32+
bytes, err := ReadFile(src)
33+
if err != nil {
34+
return err
35+
}
36+
f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0644)
37+
if err != nil {
38+
return err
39+
}
40+
defer f.Close()
41+
42+
_, err = f.Write(bytes)
43+
return err
44+
}

infra/bazel/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filegroup(
2+
name = "rules",
3+
srcs = glob(["*.bzl"]),
4+
visibility = ["//visibility:public"],
5+
)

infra/bazel/build.bzl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
def _go_command(ctx):
2+
output = ctx.attr.output
3+
if ctx.attr.os == "windows":
4+
output = output + ".exe"
5+
6+
output_file = ctx.actions.declare_file(ctx.attr.os + "/" + ctx.attr.arch + "/" + output)
7+
pkg = ctx.attr.pkg
8+
9+
ld_flags = "-s -w"
10+
if ctx.attr.ld:
11+
ld_flags = ld_flags + " " + ctx.attr.ld
12+
13+
options = [
14+
"go",
15+
"build",
16+
"-o", output_file.path,
17+
"-compiler", "gc",
18+
"-gcflags", '"all=-trimpath=${GOPATH}/src"',
19+
"-asmflags", '"all=-trimpath=${GOPATH}/src"',
20+
"-ldflags", "'%s'" % ld_flags,
21+
"-tags", "'%s'" % ctx.attr.gotags,
22+
pkg,
23+
]
24+
25+
command = " ".join(options)
26+
27+
envs = [
28+
"CGO_ENABLED=0",
29+
"GOOS="+ctx.attr.os,
30+
"GOARCH="+ctx.attr.arch,
31+
"GOROOT_FINAL=/go"
32+
]
33+
34+
if ctx.attr.mips: # https://github.com/golang/go/issues/27260
35+
envs+=["GOMIPS="+ctx.attr.mips]
36+
envs+=["GOMIPS64="+ctx.attr.mips]
37+
envs+=["GOMIPSLE="+ctx.attr.mips]
38+
envs+=["GOMIPS64LE="+ctx.attr.mips]
39+
if ctx.attr.arm:
40+
envs+=["GOARM="+ctx.attr.arm]
41+
42+
command = " ".join(envs) + " " + command
43+
44+
ctx.actions.run_shell(
45+
outputs = [output_file],
46+
command = command,
47+
use_default_shell_env = True,
48+
)
49+
runfiles = ctx.runfiles(files = [output_file])
50+
return [DefaultInfo(executable = output_file, runfiles = runfiles)]
51+
52+
53+
foreign_go_binary = rule(
54+
_go_command,
55+
attrs = {
56+
'pkg': attr.string(),
57+
'output': attr.string(),
58+
'os': attr.string(mandatory=True),
59+
'arch': attr.string(mandatory=True),
60+
'mips': attr.string(),
61+
'arm': attr.string(),
62+
'ld': attr.string(),
63+
'gotags': attr.string(),
64+
},
65+
executable = True,
66+
)

infra/bazel/gpg.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def _gpg_sign_impl(ctx):
2+
output_file = ctx.actions.declare_file(ctx.file.base.basename + ctx.attr.suffix, sibling = ctx.file.base)
3+
if not ctx.configuration.default_shell_env.get("GPG_PASS"):
4+
ctx.actions.write(output_file, "")
5+
else:
6+
command = "echo ${GPG_PASS} | gpg --pinentry-mode loopback --digest-algo SHA512 --passphrase-fd 0 --output %s --detach-sig %s" % (output_file.path, ctx.file.base.path)
7+
ctx.actions.run_shell(
8+
command = command,
9+
use_default_shell_env = True,
10+
inputs = [ctx.file.base],
11+
outputs = [output_file],
12+
progress_message = "Signing binary",
13+
mnemonic = "gpg",
14+
)
15+
return [DefaultInfo(files = depset([output_file]))]
16+
17+
gpg_sign = rule(
18+
implementation = _gpg_sign_impl,
19+
attrs = {
20+
"base": attr.label(allow_single_file=True),
21+
"suffix": attr.string(default=".sig"),
22+
},
23+
)

infra/bazel/matrix.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
SUPPORTED_MATRIX = [
2+
("windows", "amd64"),
3+
("windows", "386"),
4+
("darwin", "amd64"),
5+
("linux", "amd64"),
6+
("linux", "386"),
7+
("linux", "arm64"),
8+
("linux", "arm"),
9+
("linux", "mips64"),
10+
("linux", "mips"),
11+
("linux", "mips64le"),
12+
("linux", "mipsle"),
13+
("linux", "ppc64"),
14+
("linux", "ppc64le"),
15+
("linux", "s390x"),
16+
("freebsd", "amd64"),
17+
("freebsd", "386"),
18+
("openbsd", "amd64"),
19+
("openbsd", "386"),
20+
("dragonfly", "amd64"),
21+
]

0 commit comments

Comments
 (0)