Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```

Expand Down
4 changes: 2 additions & 2 deletions build/app/apple_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 54 additions & 7 deletions build/app/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,13 +39,42 @@ 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")):
ret = subprocess.run(["go", "mod", "init", LIBXRAY_MOD_NAME])
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",
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -151,11 +192,17 @@ def build_desktop_bin(self):
def revert_go_env(self):
os.chdir(self.lib_dir)
Comment thread
yiguodev marked this conversation as resolved.
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")
4 changes: 2 additions & 2 deletions build/app/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions build/app/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 19 additions & 5 deletions build/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,52 @@
from app.linux import LinuxBuilder
from app.windows import WindowsBuilder

LOCAL_ARG = "local"


def build_dir_path():
file_dir = os.path.dirname(__file__)
dir_path = os.path.abspath(file_dir)
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:
Expand Down
6 changes: 0 additions & 6 deletions build/template/main.gotemplate
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
7 changes: 3 additions & 4 deletions desktop_bin/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
8 changes: 8 additions & 0 deletions readme/README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions share/generate_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions share/hysteria_mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Loading