engine

package
v0.0.0-...-44d3390 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package engine runs Xray-core in-process from a generated config.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseConnections

func CloseConnections() int

CloseConnections closes every connection the cores have open to the network and returns how many were open. Closing one that is closed already does nothing.

func ForgetHysteria

func ForgetHysteria()

ForgetHysteria drops the cached Hysteria clients, so that the next core dials its servers with its own settings: noise before QUIC in the smart mode only. Call it with no core running, after CloseConnections has closed the old clients' sockets.

func Load

func Load(config []byte) (*core.Config, error)

Load parses and builds an Xray JSON config without starting it.

func RefuseIPv6

func RefuseIPv6(inst *core.Instance) int

RefuseIPv6 makes the TUN inbounds of inst refuse IPv6 at once: a TCP connection gets a reset, a UDP packet "port unreachable". Xray's own stack takes every connection before routing it, so an IPv6 one looked open and was then closed by the routing rule that blocks IPv6. A browser that resolves names itself, over DNS-over-HTTPS, gets IPv6 addresses for sites like YouTube and kept failing on them instead of falling back to IPv4. It returns how many stacks it changed.

func SetAssetDir

func SetAssetDir(dir string) error

SetAssetDir points Xray at the directory with geosite.dat and geoip.dat.

func SetLink(l Link)

SetLink tunes the TCP connections dialled from now on for the network.

func Start

func Start(config []byte, setup ...func(*core.Instance) error) (*core.Instance, error)

Start builds the config and starts Xray. Close the instance to stop it. setup hooks run on the created instance before it starts, to add what a JSON config cannot describe, such as the transport picker.

func Stop

func Stop(inst *core.Instance) error

Stop closes the instance and the network stacks of its TUN inbounds.

Xray's TUN inbound has no Close: closing the instance leaves its gVisor stack running and still reading the TUN descriptor. On Android the app then closes that descriptor and opens a new TUN for the next start, which usually gets the same number, so the stale stack takes packets from the new tunnel and connections stall. Every reconnect (a mode switch, a new network) added one more such stack.

On Windows the stack's Wintun device stays open too: its session holds the adapter, and the next start failed with "initialization has already been completed". There the device is closed with the stack, which also removes the adapter; on Android the app closes its TUN descriptor itself.

func Track

func Track(c any)

Track remembers a connection to the network for CloseConnections. The cores' own dials are tracked already; a handler that dials by itself, like TUIC's, tracks what it opens. Kinds of connections it does not know are left alone.

Types

type Link struct {
	// Mobile is the LTE mode: TCP keepalives every 10 s after 30 s of
	// silence hold the operator's NAT open, and a connection whose data goes
	// unacknowledged for 30 s, lost in a handover, is dropped and redialled
	// instead of hanging.
	Mobile bool
	// MSS, when set, caps the TCP segments so that a path narrower than
	// 1500 bytes never has to split them (netauto.MSS).
	MSS int
}

Link describes the network the phone is on now. The panel's own socket options always win.

Jump to

Keyboard shortcuts

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