This is a wrapper around Xray-core to improve the client development experience.
- This repository has few maintainers. If you do not report a bug or initiate a PR, your issue will be ignored.
- This repository does not guarantee API stability, you need to adapt it yourself.
- This repository is only compatible with the latest release of Xray-core.
Releases use CalVer in the form v<YY>.<M>.<D> (e.g. v26.3.27 = 2026-03-27).
Because Go modules require any module with major version >= 2 to encode the
major in its import path, every CalVer release is mirrored onto a Go-friendly
SemVer tag on the same commit:
| CalVer tag | Go-import tag |
|---|---|
v26.3.27 |
v1.260327.0 |
Go consumers should pin against the SemVer mirror:
go get github.com/xtls/libxray@v1.260327.0The mirror tag is created automatically by
.github/workflows/release-go-mirror.yml
on every CalVer push. Existing CalVer tags can be backfilled with
scripts/backfill-semver-tags.sh.
Compile script. It is recommended to always use this script to compile libXray. We will not answer questions caused by using other compilation methods.
depends on git and go.
By default, the build script does not clone Xray-core. It uses Go modules and pins Xray-core to tag v26.6.27 (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.
Linux and Windows builds only produce the libXray shared library. Applications that need a standalone Xray executable should use official Xray-core release binaries.
# 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
use gomobile .
Need "iOS Simulator Runtime".
This is the best choice for general scenarios and will not conflict with other frameworks.
Supports iOS, iOSSimulator, macOS, macCatalyst.
But it is not possible to set the minimum macOS version, which will cause some warnings when compiling. And it does not support tvOS.
Need "iOS Simulator Runtime" and "tvOS Simulator Runtime".
Support more compilation options, output c header files.
This works well when you use ffi for integration. For example, integration with swift, kotlin, dart.
Support iOS, iOSSimulator, macOS, tvOS.
The product LibXray.xcframework contains module.modulemap. When using
Swift, import it as module LibXray.
depend on gcc and g++.
depend on LLVM MinGW.
you can use winget to install LLVM MinGW.
winget install MartinStorsjo.LLVM-MinGW.UCRTlibXray exposes a single structured entrypoint:
func Invoke(requestJSON string) stringThe C export is:
char* CGoInvoke(char* requestJSON);The request is a JSON object:
{
"apiVersion": 1,
"method": "runXray",
"env": {
"xray.location.config": "/path/to/config.json",
"xray.location.asset": "/path/to/dat",
"xray.location.cert": "/path/to/dat",
"xray.tun.fd": "123"
},
"payload": {
"configPath": "/path/to/config.json"
}
}The response is a JSON object:
{
"success": true,
"data": {},
"error": ""
}env is optional and only supports Xray-core environment variables that are
explicitly modeled by libXray:
| JSON key | Meaning |
|---|---|
xray.location.config |
Xray config file location |
xray.location.confdir |
Xray config directory location |
xray.location.asset |
Directory containing geosite.dat, geoip.dat, and custom GeoData files |
xray.location.cert |
Certificate directory used by Xray-core |
xray.buf.readv |
Xray-core readv buffer switch |
xray.buf.splice |
Xray-core splice buffer switch |
xray.vmess.padding |
VMess padding switch |
xray.cone.disabled |
Cone behavior switch |
xray.json.strict |
Strict JSON parsing switch |
xray.ray.buffer.size |
Ray buffer size |
xray.browser.dialer |
Browser dialer address |
xray.xudp.show |
XUDP log display switch |
xray.xudp.basekey |
XUDP base key |
xray.tun.fd |
TUN file descriptor for Android, iOS, and macOS packet tunnel integrations |
Design notes:
envis modeled as fixed fields in Go and Dart. It is not a free-form map.- Unknown
envkeys are ignored and are not written to the process environment. envonly sets modeled, non-empty fields. Missing fields are not unset.- libXray does not restore previous environment values after a method returns. Callers must pass the required env fields on every request that depends on them. This avoids concurrent calls restoring stale values over newer values.
SetTunFdhas been removed. Passxray.tun.fdin theenvobject of therunXrayrequest.
Supported methods:
getFreePorts
convertShareLinksToXrayJson
convertXrayJsonToShareLinks
countGeoData
ping
testXray
runXray
runXrayFromJson
stopXray
xrayVersion
getXrayState
Used to solve the socket protect problem on Android.
Read geo files and count the categories and rules.
Download geosite.dat and geoip.dat and count them.
Only executed on iOS, GC is initiated once a second. This can alleviate memory pressure on iOS.
Write data to a file.
Speed test the Xray configuration.
Get free ports.
libXray uses sendThrough to store outbound names.
Parse Clash.Meta configuration.
convert Xray Json to VMessAEAD/VLESS sharing protocol.
convert VMessAEAD/VLESS sharing protocol to Xray Json.
convert VMessQRCode to Xray Json.
convert VMessQRCode to Xray Json.
Some tools used to parse shared links.
Latency testing.
Refer to the following configuration:
{
"metrics" : {
"listen": "127.0.0.1:49227"
},
"policy" : {
"system" : {
"statsInboundDownlink" : true,
"statsInboundUplink" : true,
"statsOutboundDownlink" : true,
"statsOutboundUplink" : true
}
},
"stats" : {}
}The metrics server exposes the Xray runtime counters through HTTP. For example,
when listen is 127.0.0.1:49227, read:
http://localhost:49227/debug/vars
Note:
-
When testing latency or validating configuration, make sure
metricsisnull. -
Metrics only needs the
listenfield in this wrapper. Query/debug/varsdirectly with an HTTP client instead of going through libXray.
Verify the Xray configuration.
Start and stop Xray instances.
This repository is based on the MIT License.