diff --git a/README.md b/README.md index 102cb5c3..96c6763e 100644 --- a/README.md +++ b/README.md @@ -40,21 +40,29 @@ Compile script. It is recommended to always use this script to compile libXray. depends on git and go. +By default, the build script does not clone [Xray-core](https://github.com/XTLS/Xray-core). It uses Go modules and pins Xray-core to tag `v26.5.9` (recorded by Go as the matching pseudo-version). +Pass the optional `local` argument to use an existing local checkout at `../Xray-core` through a Go module `replace`. + ### Usage ```shell # Android (min Android API level is 21) python3 build/main.py android +python3 build/main.py android local # Apple (gomobile or go) python3 build/main.py apple gomobile python3 build/main.py apple go +python3 build/main.py apple gomobile local +python3 build/main.py apple go local # Linux python3 build/main.py linux +python3 build/main.py linux local # Windows python3 build/main.py windows +python3 build/main.py windows local ``` diff --git a/build/app/apple_go.py b/build/app/apple_go.py index 7eb3e307..d01dab40 100644 --- a/build/app/apple_go.py +++ b/build/app/apple_go.py @@ -23,8 +23,8 @@ def __init__( class AppleGoBuilder(Builder): - def __init__(self, build_dir: str): - super().__init__(build_dir) + def __init__(self, build_dir: str, use_local_xray_core: bool = False): + super().__init__(build_dir, use_local_xray_core) self.framework_dir = os.path.join(self.lib_dir, "apple_xcframework") delete_dir_if_exists(self.framework_dir) create_dir_if_not_exists(self.framework_dir) diff --git a/build/app/build.py b/build/app/build.py index 53194a8b..57154cd2 100644 --- a/build/app/build.py +++ b/build/app/build.py @@ -10,13 +10,24 @@ ) LIBXRAY_MOD_NAME = "github.com/xtls/libxray" +XRAY_CORE_MOD_NAME = "github.com/xtls/xray-core" +DEFAULT_XRAY_CORE_TAG = "v26.5.9" +# Xray-core CalVer tags cannot be used directly as Go module versions because +# its module path does not include /v26. This pseudo-version points to the tag above. +DEFAULT_XRAY_CORE_VERSION = "v1.260327.1-0.20260509173629-1bdb488c9ec0" +LOCAL_XRAY_CORE_DIR_NAME = "Xray-core" class Builder(object): - def __init__(self, build_dir: str): + def __init__(self, build_dir: str, use_local_xray_core: bool = False): self.build_dir = build_dir - self.lib_dir = os.path.join(self.build_dir, "..") + self.lib_dir = os.path.abspath(os.path.join(self.build_dir, "..")) self.bin_file = "xray" + self.use_local_xray_core = use_local_xray_core + self.xray_core_replace_path = f"../{LOCAL_XRAY_CORE_DIR_NAME}" + self.xray_core_dir = os.path.abspath( + os.path.join(self.lib_dir, self.xray_core_replace_path) + ) def clean_lib_files(self, files: list[str]): for file in files: @@ -28,6 +39,11 @@ def clean_lib_dirs(self, dirs: list[str]): dir_path = os.path.join(self.lib_dir, dir_name) delete_dir_if_exists(dir_path) + def prepare_xray_core(self): + if self.use_local_xray_core: + if not os.path.isdir(self.xray_core_dir): + raise Exception(f"local Xray-core dir not found: {self.xray_core_dir}") + def init_go_env(self): os.chdir(self.lib_dir) if not os.path.exists(os.path.join(self.lib_dir, "go.mod")): @@ -35,6 +51,30 @@ def init_go_env(self): if ret.returncode != 0: raise Exception("go mod init failed") + if self.use_local_xray_core: + ret = subprocess.run( + [ + "go", + "mod", + "edit", + f"-replace={XRAY_CORE_MOD_NAME}={self.xray_core_replace_path}", + ] + ) + if ret.returncode != 0: + raise Exception("go mod edit replace failed") + else: + ret = subprocess.run( + ["go", "mod", "edit", f"-dropreplace={XRAY_CORE_MOD_NAME}"] + ) + if ret.returncode != 0: + raise Exception("go mod edit dropreplace failed") + + ret = subprocess.run( + ["go", "get", f"{XRAY_CORE_MOD_NAME}@{DEFAULT_XRAY_CORE_VERSION}"] + ) + if ret.returncode != 0: + raise Exception("go get xray-core failed") + ret = subprocess.run( [ "go", @@ -97,6 +137,7 @@ def replace_package_name(self, file_name: str): f.writelines(new_lines) def before_build(self): + self.prepare_xray_core() self.init_go_env() self.download_geo() @@ -151,11 +192,17 @@ def build_desktop_bin(self): def revert_go_env(self): os.chdir(self.lib_dir) ret = subprocess.run( - [ - "go", - "mod", - "tidy", - ] + ["go", "mod", "edit", f"-dropreplace={XRAY_CORE_MOD_NAME}"] ) + if ret.returncode != 0: + raise Exception("go mod edit dropreplace failed") + + ret = subprocess.run( + ["go", "get", f"{XRAY_CORE_MOD_NAME}@{DEFAULT_XRAY_CORE_VERSION}"] + ) + if ret.returncode != 0: + raise Exception("go get xray-core failed") + + ret = subprocess.run(["go", "mod", "tidy"]) if ret.returncode != 0: raise Exception("go mod tidy failed") diff --git a/build/app/linux.py b/build/app/linux.py index 1b619006..5b5b8415 100644 --- a/build/app/linux.py +++ b/build/app/linux.py @@ -6,8 +6,8 @@ class LinuxBuilder(Builder): - def __init__(self, build_dir: str): - super().__init__(build_dir) + def __init__(self, build_dir: str, use_local_xray_core: bool = False): + super().__init__(build_dir, use_local_xray_core) self.framework_dir = os.path.join(self.lib_dir, "linux_so") delete_dir_if_exists(self.framework_dir) create_dir_if_not_exists(self.framework_dir) diff --git a/build/app/windows.py b/build/app/windows.py index c1112c3d..e13ee204 100644 --- a/build/app/windows.py +++ b/build/app/windows.py @@ -6,8 +6,8 @@ class WindowsBuilder(Builder): - def __init__(self, build_dir: str): - super().__init__(build_dir) + def __init__(self, build_dir: str, use_local_xray_core: bool = False): + super().__init__(build_dir, use_local_xray_core) self.framework_dir = os.path.join(self.lib_dir, "windows_dll") delete_dir_if_exists(self.framework_dir) create_dir_if_not_exists(self.framework_dir) diff --git a/build/main.py b/build/main.py index 80ac6de1..19b77fab 100644 --- a/build/main.py +++ b/build/main.py @@ -8,6 +8,8 @@ from app.linux import LinuxBuilder from app.windows import WindowsBuilder +LOCAL_ARG = "local" + def build_dir_path(): file_dir = os.path.dirname(__file__) @@ -15,31 +17,43 @@ def build_dir_path(): return dir_path +def parse_local_arg(args: list[str]) -> bool: + if not args: + return False + if args == [LOCAL_ARG]: + return True + raise Exception(f"unsupported args: {args}") + + if __name__ == "__main__": print(sys.argv) platform = sys.argv[1] if platform == "apple": tool = sys.argv[2] + use_local_xray_core = parse_local_arg(sys.argv[3:]) if tool == "go": - builder = AppleGoBuilder(build_dir_path()) + builder = AppleGoBuilder(build_dir_path(), use_local_xray_core) builder.build() elif tool == "gomobile": - builder = AppleGoMobileBuilder(build_dir_path()) + builder = AppleGoMobileBuilder(build_dir_path(), use_local_xray_core) builder.build() else: raise Exception(f"platform {platform} tool {tool} not supported") elif platform == "android": - builder = AndroidBuilder(build_dir_path()) + use_local_xray_core = parse_local_arg(sys.argv[2:]) + builder = AndroidBuilder(build_dir_path(), use_local_xray_core) builder.build() elif platform == "linux": - builder = LinuxBuilder(build_dir_path()) + use_local_xray_core = parse_local_arg(sys.argv[2:]) + builder = LinuxBuilder(build_dir_path(), use_local_xray_core) builder.build() elif platform == "windows": - builder = WindowsBuilder(build_dir_path()) + use_local_xray_core = parse_local_arg(sys.argv[2:]) + builder = WindowsBuilder(build_dir_path(), use_local_xray_core) builder.build() else: diff --git a/build/template/main.gotemplate b/build/template/main.gotemplate index 62644ca4..31fd8c43 100644 --- a/build/template/main.gotemplate +++ b/build/template/main.gotemplate @@ -88,9 +88,3 @@ func CGoStopXray() *C.char { func CGoXrayVersion() *C.char { return C.CString(XrayVersion()) } - -//export CGoBuildMphCache -func CGoBuildMphCache(base64Text *C.char) *C.char { - text := C.GoString(base64Text) - return C.CString(BuildMphCache(text)) -} diff --git a/desktop_bin/run.go b/desktop_bin/run.go index 2d89e29d..71440454 100644 --- a/desktop_bin/run.go +++ b/desktop_bin/run.go @@ -16,9 +16,8 @@ type runXrayConfig struct { Dns string `json:"dns,omitempty"` BindInterface string `json:"bindInterface,omitempty"` // xray - DatDir string `json:"datDir,omitempty"` - MphCachePath string `json:"mphCachePath,omitempty"` - ConfigPath string `json:"configPath,omitempty"` + DatDir string `json:"datDir,omitempty"` + ConfigPath string `json:"configPath,omitempty"` } func runXray(configPath string) error { @@ -32,7 +31,7 @@ func runXray(configPath string) error { return err } - err = xray.RunXray(config.DatDir, config.MphCachePath, config.ConfigPath) + err = xray.RunXray(config.DatDir, config.ConfigPath) if err != nil { return err } diff --git a/go.mod b/go.mod index 7ee1e879..645fa049 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/xtls/libxray -go 1.26.2 +go 1.26.3 require ( github.com/stretchr/testify v1.11.1 @@ -33,20 +33,20 @@ require ( github.com/vishvananda/netns v0.0.5 // indirect github.com/xtls/reality v0.0.0-20260322125925-9234c772ba8f // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect - golang.org/x/crypto v0.50.0 // indirect + golang.org/x/crypto v0.51.0 // indirect golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect - golang.org/x/mod v0.34.0 // indirect - golang.org/x/net v0.53.0 // indirect + golang.org/x/mod v0.35.0 // indirect + golang.org/x/net v0.54.0 // indirect golang.org/x/sync v0.20.0 // indirect - golang.org/x/sys v0.43.0 // indirect - golang.org/x/text v0.36.0 // indirect + golang.org/x/sys v0.44.0 // indirect + golang.org/x/text v0.37.0 // indirect golang.org/x/time v0.12.0 // indirect - golang.org/x/tools v0.43.0 // indirect + golang.org/x/tools v0.44.0 // indirect golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb // indirect golang.zx2c4.com/wireguard/windows v1.0.1 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171 // indirect - google.golang.org/grpc v1.81.0 // indirect + google.golang.org/grpc v1.81.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gvisor.dev/gvisor v0.0.0-20260122175437-89a5d21be8f0 // indirect lukechampine.com/blake3 v1.4.1 // indirect diff --git a/go.sum b/go.sum index 15b1e5b3..106b540c 100644 --- a/go.sum +++ b/go.sum @@ -82,26 +82,26 @@ go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko= go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o= go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBseWJUpBw5I82+2U4M= go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y= -golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI= -golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q= +golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI= +golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8= golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= -golang.org/x/mod v0.34.0 h1:xIHgNUUnW6sYkcM5Jleh05DvLOtwc6RitGHbDk4akRI= -golang.org/x/mod v0.34.0/go.mod h1:ykgH52iCZe79kzLLMhyCUzhMci+nQj+0XkbXpNYtVjY= -golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA= -golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs= +golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM= +golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU= +golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w= +golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ= golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4= golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI= -golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg= -golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164= +golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ= +golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc= +golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38= golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE= golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= -golang.org/x/tools v0.43.0 h1:12BdW9CeB3Z+J/I/wj34VMl8X+fEXBxVR90JeMX5E7s= -golang.org/x/tools v0.43.0/go.mod h1:uHkMso649BX2cZK6+RpuIPXS3ho2hZo4FVwfoy1vIk0= +golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c= +golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI= golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg= golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI= golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb h1:whnFRlWMcXI9d+ZbWg+4sHnLp52d5yiIPUxMBSt4X9A= @@ -112,8 +112,8 @@ gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171 h1:ggcbiqK8WWh6l1dnltU4BgWGIGo+EVYxCaAPih/zQXQ= google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= -google.golang.org/grpc v1.81.0 h1:W3G9N3KQf3BU+YuCtGKJk0CmxQNbAISICD/9AORxLIw= -google.golang.org/grpc v1.81.0/go.mod h1:xGH9GfzOyMTGIOXBJmXt+BX/V0kcdQbdcuwQ/zNw42I= +google.golang.org/grpc v1.81.1 h1:VnnIIZ88UzOOKLukQi+ImGz8O1Wdp8nAGGnvOfEIWQQ= +google.golang.org/grpc v1.81.1/go.mod h1:xGH9GfzOyMTGIOXBJmXt+BX/V0kcdQbdcuwQ/zNw42I= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/readme/README.zh_CN.md b/readme/README.zh_CN.md index 735eed84..27b260bd 100644 --- a/readme/README.zh_CN.md +++ b/readme/README.zh_CN.md @@ -16,14 +16,22 @@ 依赖 git 和 go。 +默认情况下,编译脚本不会 clone [Xray-core](https://github.com/XTLS/Xray-core),而是通过 Go modules 将 Xray-core 固定到 tag `v26.5.9`(Go 会记录为对应的 pseudo-version)。 +传入可选参数 `local` 时,会通过 Go module `replace` 改用已有的本地仓库 `../Xray-core`。 + ### 使用方式 ```shell python3 build/main.py android +python3 build/main.py android local python3 build/main.py apple gomobile python3 build/main.py apple go +python3 build/main.py apple gomobile local +python3 build/main.py apple go local python3 build/main.py linux +python3 build/main.py linux local python3 build/main.py windows +python3 build/main.py windows local ``` ### Android diff --git a/share/generate_share.go b/share/generate_share.go index d8b91383..d695ab94 100644 --- a/share/generate_share.go +++ b/share/generate_share.go @@ -246,11 +246,6 @@ func streamSettingsQuery(proxy conf.OutboundDetourConfig, link *url.URL) { query = addQuery(query, "down", string(qp.BrutalDown)) } if len(qp.UdpHop.PortList.Range) > 0 { - if jsonData, err := json.Marshal(qp.UdpHop.PortList.Range); err == nil { - query = addQuery(query, "ports", string(jsonData)) - } - } - if qp.UdpHop.Interval.From >= 5 { query = addQuery(query, "ports", qp.UdpHop.PortList.String()) } if qp.UdpHop.Interval.From != 0 || qp.UdpHop.Interval.To != 0 { diff --git a/share/hysteria_mask.go b/share/hysteria_mask.go index 5ff238e3..cbdc5e5b 100644 --- a/share/hysteria_mask.go +++ b/share/hysteria_mask.go @@ -23,14 +23,14 @@ func buildHy2FinalMask(up, down, ports string, hopInterval *int32, obfsType, obf if ports != "" { var portList conf.PortList udpHop := conf.UdpHop{} - err := json.Unmarshal([]byte(ports), &portList) + portListJSON, err := json.Marshal(ports) if err != nil { return nil, err } - udpHop.PortList = portList - if err := json.Unmarshal(portListJSON, &udpHop.PortList); err != nil { + if err := json.Unmarshal(portListJSON, &portList); err != nil { return nil, err } + udpHop.PortList = portList if hopInterval != nil { i := *hopInterval udpHop.Interval = conf.Int32Range{Left: i, Right: i, From: i, To: i} diff --git a/xray/ping.go b/xray/ping.go index 1a4a0081..ff03b331 100644 --- a/xray/ping.go +++ b/xray/ping.go @@ -11,7 +11,7 @@ import ( // url means the website we use to test speed. "https://www.google.com" is a good choice for most cases. // proxy means the local http/socks5 proxy, like "socks5://[::1]:1080". func Ping(datDir string, configPath string, timeout int, url string, proxy string) (int64, error) { - InitEnv(datDir, "") + InitEnv(datDir) server, err := StartXray(configPath) if err != nil { return nodep.PingDelayError, err diff --git a/xray/validation.go b/xray/validation.go index d4b7b1df..e6e3f21e 100644 --- a/xray/validation.go +++ b/xray/validation.go @@ -4,7 +4,7 @@ package xray // datDir means the dir which geosite.dat and geoip.dat are in. // configPath means the config.json file path. func TestXray(datDir string, configPath string) error { - InitEnv(datDir, "") + InitEnv(datDir) server, err := StartXray(configPath) if err != nil { return err diff --git a/xray/xray.go b/xray/xray.go index 672ac004..03a949a8 100644 --- a/xray/xray.go +++ b/xray/xray.go @@ -1,7 +1,6 @@ package xray import ( - "errors" "os" "runtime/debug" "strconv" @@ -51,17 +50,16 @@ func SetTunFd(fd int32) { os.Setenv(platform.TunFdKey, strconv.Itoa(int(fd))) } -func InitEnv(datDir string, mphCachePath string) { +func InitEnv(datDir string) { os.Setenv(platform.AssetLocation, datDir) os.Setenv(platform.CertLocation, datDir) } // Run Xray instance. // datDir means the dir which geosite.dat and geoip.dat are in. -// mphCachePath means the path of mph cache file. leave it empty if you don't use mph cache. // configPath means the config.json file path. -func RunXray(datDir string, mphCachePath string, configPath string) (err error) { - InitEnv(datDir, mphCachePath) +func RunXray(datDir, configPath string) (err error) { + InitEnv(datDir) memory.InitForceFree() coreServer, err = StartXray(configPath) if err != nil { @@ -78,10 +76,9 @@ func RunXray(datDir string, mphCachePath string, configPath string) (err error) // Run Xray instance with JSON configuration string. // datDir means the dir which geosite.dat and geoip.dat are in. -// mphCachePath means the path of mph cache file. leave it empty if you don't use mph cache. // configJSON means the JSON configuration string. -func RunXrayFromJSON(datDir string, mphCachePath string, configJSON string) (err error) { - InitEnv(datDir, mphCachePath) +func RunXrayFromJSON(datDir, configJSON string) (err error) { + InitEnv(datDir) memory.InitForceFree() coreServer, err = StartXrayFromJSON(configJSON) if err != nil { @@ -113,7 +110,3 @@ func StopXray() error { func XrayVersion() string { return core.Version() } - -func BuildMphCache(datDir string, mphCachePath string, configPath string) error { - return errors.New("MPH cache building is not supported by xray-core v26.5.9") -} diff --git a/xray/xray_test.go b/xray/xray_test.go deleted file mode 100644 index a986c73f..00000000 --- a/xray/xray_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package xray - -import ( - "strings" - "testing" -) - -func TestBuildMphCacheUnsupportedByXrayCoreVersion(t *testing.T) { - err := BuildMphCache("/tmp", "/tmp/mph.cache", "/tmp/config.json") - if err == nil { - t.Fatal("expected BuildMphCache to return an unsupported error") - } - if !strings.Contains(err.Error(), "not supported") { - t.Fatalf("expected unsupported error, got %v", err) - } -} diff --git a/xray_wrapper.go b/xray_wrapper.go index e4e22e50..b5db084d 100644 --- a/xray_wrapper.go +++ b/xray_wrapper.go @@ -113,23 +113,20 @@ func TestXray(base64Text string) string { } type RunXrayRequest struct { - DatDir string `json:"datDir,omitempty"` - MphCachePath string `json:"mphCachePath,omitempty"` - ConfigPath string `json:"configPath,omitempty"` + DatDir string `json:"datDir,omitempty"` + ConfigPath string `json:"configPath,omitempty"` } type RunXrayFromJSONRequest struct { - DatDir string `json:"datDir,omitempty"` - MphCachePath string `json:"mphCachePath,omitempty"` - ConfigJSON string `json:"configJSON,omitempty"` + DatDir string `json:"datDir,omitempty"` + ConfigJSON string `json:"configJSON,omitempty"` } // Create Xray Run Request -func NewXrayRunRequest(datDir, mphCachePath, configPath string) (string, error) { +func NewXrayRunRequest(datDir, configPath string) (string, error) { request := RunXrayRequest{ - DatDir: datDir, - MphCachePath: mphCachePath, - ConfigPath: configPath, + DatDir: datDir, + ConfigPath: configPath, } requestBytes, err := json.Marshal(&request) if err != nil { @@ -141,11 +138,10 @@ func NewXrayRunRequest(datDir, mphCachePath, configPath string) (string, error) } // Create Xray Run From JSON Request -func NewXrayRunFromJSONRequest(datDir, mphCachePath, configJSON string) (string, error) { +func NewXrayRunFromJSONRequest(datDir, configJSON string) (string, error) { request := RunXrayFromJSONRequest{ - DatDir: datDir, - MphCachePath: mphCachePath, - ConfigJSON: configJSON, + DatDir: datDir, + ConfigJSON: configJSON, } requestBytes, err := json.Marshal(&request) if err != nil { @@ -168,7 +164,7 @@ func RunXray(base64Text string) string { if err != nil { return response.EncodeToBase64("", err) } - err = xray.RunXray(request.DatDir, request.MphCachePath, request.ConfigPath) + err = xray.RunXray(request.DatDir, request.ConfigPath) return response.EncodeToBase64("", err) } @@ -184,7 +180,7 @@ func RunXrayFromJSON(base64Text string) string { if err != nil { return response.EncodeToBase64("", err) } - err = xray.RunXrayFromJSON(request.DatDir, request.MphCachePath, request.ConfigJSON) + err = xray.RunXrayFromJSON(request.DatDir, request.ConfigJSON) return response.EncodeToBase64("", err) } @@ -205,19 +201,3 @@ func XrayVersion() string { var response nodep.CallResponse[string] return response.EncodeToBase64(xray.XrayVersion(), nil) } - -// Build Mph Cache -func BuildMphCache(base64Text string) string { - var response nodep.CallResponse[string] - req, err := base64.StdEncoding.DecodeString(base64Text) - if err != nil { - return response.EncodeToBase64("", err) - } - var request RunXrayRequest - err = json.Unmarshal(req, &request) - if err != nil { - return response.EncodeToBase64("", err) - } - err = xray.BuildMphCache(request.DatDir, request.MphCachePath, request.ConfigPath) - return response.EncodeToBase64("", err) -}