libXray

package module
v1.260627.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 7 Imported by: 2

README

libXray

简体中文

This is a wrapper around Xray-core to improve the client development experience.

Note

  1. This repository has few maintainers. If you do not report a bug or initiate a PR, your issue will be ignored.
  2. This repository does not guarantee API stability, you need to adapt it yourself.
  3. This repository is only compatible with the latest release of Xray-core.

Versioning

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.0

The 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.

Features

build

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.

Usage
# 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

Android

use gomobile .

iOS && macOS
1. 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.

2. use cgo

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.

Linux

depend on gcc and g++.

Windows

depend on MinGW.

you can use winget to install LLVM MinGW or WinLibs .

winget install MartinStorsjo.LLVM-MinGW.UCRT
winget install BrechtSanders.WinLibs.POSIX.UCRT

controller

Used to solve the socket protect problem on Android.

geo

count

Read geo files and count the categories and rules.

read

Read the Xray Json configuration and extract the geo file name used.

main

Download geosite.dat and geoip.dat and count them.

memory

Only executed on iOS, GC is initiated once a second. This can alleviate memory pressure on iOS.

nodep

file

Write data to a file.

measure

Speed ​​test the Xray configuration.

model

The response body of the wrapper interface.

port

Get free ports.

share

libXray uses sendThrough to store outbound names.

clash_meta

Parse Clash.Meta configuration.

generate_share

convert Xray Json to VMessAEAD/VLESS sharing protocol.

parse_share

convert VMessAEAD/VLESS sharing protocol to Xray Json.

convert VMessQRCode to Xray Json.

vmess

convert VMessQRCode to Xray Json.

xray_json

Some tools used to parse shared links.

xray

ping

Latency testing.

metrics

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:

  1. When testing latency or validating configuration, make sure metrics is null.

  2. Metrics only needs the listen field in this wrapper. Query /debug/vars directly with an HTTP client instead of going through libXray.

validation

Verify the Xray configuration.

xray

Start and stop Xray instances.

nodep_wrapper

export nodep.

xray_wrapper

export xray.

Credits

Project X

VMessPing

FreePort

License

This repository is based on the MIT License.

Documentation

Overview

libXray is an Xray wrapper focusing on improving the experience of Xray-core mobile development.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertShareLinksToXrayJson added in v1.260601.0

func ConvertShareLinksToXrayJson(base64Text string) string

Convert share text to XrayJson support XrayJson, v2rayN plain text, v2rayN base64 text, Clash.Meta yaml

func ConvertXrayJsonToShareLinks(base64Text string) string

Convert XrayJson to share links. VMess will generate VMessAEAD link.

func CountGeoData added in v1.260601.0

func CountGeoData(base64Text string) string

Read geo data and write all codes to text file.

func GetFreePorts

func GetFreePorts(count int) string

Wrapper of nodep.GetFreePorts count means how many ports you need.

func GetXrayState added in v1.260601.0

func GetXrayState() bool

Get Xray State

func NewXrayRunFromJSONRequest added in v1.260601.0

func NewXrayRunFromJSONRequest(datDir, configJSON string) (string, error)

Create Xray Run From JSON Request

func NewXrayRunRequest added in v1.260601.0

func NewXrayRunRequest(datDir, configPath string) (string, error)

Create Xray Run Request

func Ping

func Ping(base64Text string) string

Ping Xray config and get the delay of its outbound.

func ReadGeoFiles added in v1.260601.0

func ReadGeoFiles(base64Text string) string

thin geo data

func RunXray

func RunXray(base64Text string) string

Run Xray instance.

func RunXrayFromJSON added in v1.260601.0

func RunXrayFromJSON(base64Text string) string

Run Xray instance with JSON configuration.

func SetTunFd added in v1.260601.0

func SetTunFd(fd int32)

SetTunFd sets the TUN file descriptor. Call this BEFORE RunXray/RunXrayFromJSON.

func StopXray

func StopXray() string

Stop Xray instance.

func TestXray added in v1.260601.0

func TestXray(base64Text string) string

Test Xray Config.

func XrayVersion

func XrayVersion() string

Xray's version

Types

type CountGeoDataRequest added in v1.260601.0

type CountGeoDataRequest struct {
	DatDir  string `json:"datDir,omitempty"`
	Name    string `json:"name,omitempty"`
	GeoType string `json:"geoType,omitempty"`
}

type RunXrayFromJSONRequest added in v1.260601.0

type RunXrayFromJSONRequest struct {
	DatDir     string `json:"datDir,omitempty"`
	ConfigJSON string `json:"configJSON,omitempty"`
}

type RunXrayRequest added in v1.260601.0

type RunXrayRequest struct {
	DatDir     string `json:"datDir,omitempty"`
	ConfigPath string `json:"configPath,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL