Xray is a platform for building proxies. This fork was created from XTLS/Xray-core to address the issue where Xray resets all user statistics upon restart.
Before stopping Xray, you need to call xray api statsquery and save its output to a file like this:
xray api statsquery > xray_stats.jsonThen, when restarting Xray, use special flags to restore statistics from that file.
These flags were added for the xray run command:
-restore-stats: Restores statistics from a JSON file.-statsfile=file: Specifies the path to the JSON file used for restoring statistics. The default isxray_stats.json, located in the same directory as the Xray executable.
To run Xray with a specific configuration file and restore statistics from a JSON file:
xray run -c config.json -restore-stats -statsfile /path/to/stats.jsonFirst, you need to install this version of Xray.
Typically, xray run is not called manually; instead, it is managed via a systemd unit. To set up statistics restoring, modify the ExecStart line in the unit file by adding the -restore-stats and (optionally) -statsfile flags:
ExecStart=/usr/local/bin/xray run -config /usr/local/etc/xray/config.json -restore-stats -statsfile /path/to/stats.json
/etc/systemd/system/xray.service) is not recommended, as updates may overwrite your changes.
A better approach is to use a Drop-In Override. If you installed Xray via the official script, you likely already have a drop-in override file, such as:
/etc/systemd/system/xray.service.d/10-donot_touch_single_conf.conf
Modify this file instead of the main service file.
If no override exists, create it:
sudo mkdir -p /etc/systemd/system/xray.service.d
sudo nano /etc/systemd/system/xray.service.d/override.confYour override file should look like this:
[Service]
ExecStart=
ExecStart=/usr/local/bin/xray run -config /usr/local/etc/xray/config.json -restore-stats -statsfile /path/to/stats.json
The first ExecStart= clears the existing entry before adding the modified one.
You can either use the -statsfile flag to specify the path to your stats file or name your file xray_stats.json and place it in the same directory as the Xray executable (since that’s the default location).
Make sure xray can read the file and has access to it's directory.
After modifying the systemd unit configuration, reload systemd:
sudo systemctl daemon-reloadYou need Go 1.24 or later. If this version is unavailable in your package manager, install it manually (Linux):
-
Download & Install Go 1.24+:
wget https://go.dev/dl/go1.24.0.linux-amd64.tar.gz sudo tar -C /usr/local -xzf go1.24.0.linux-amd64.tar.gz
-
Update Your PATH:
Add this to your shell configuration file (~/.bashrc,~/.profile, or~/.zshrc):export PATH=$PATH:/usr/local/go/bin
Apply the changes:
source ~/.bashrc # or source ~/.profile
-
Verify Installation:
go version
git clone https://github.com/five9th/Xray-core.git
cd Xray-corego mod tidyWindows (PowerShell):
$env:CGO_ENABLED=0
go build -o xray.exe -trimpath -buildvcs=false -ldflags="-s -w -buildid=" -v ./mainLinux/macOS:
CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -ldflags="-s -w -buildid=" -v ./mainAfter a successful compilation:
- Stop the Xray service (don't forget to save statistics first).
- Replace the existing Xray executable with the newly compiled one (consider making a backup before replacing it).
- Update the systemd unit file as described earlier and restart the Xray service.
- Verify the service status:
sudo systemctl status xrayXray will start even if statistics restoration fails, but an error message will be logged.
Successful restoration:
xray[110416]: Restoring stats from the previous session.
xray[110416]: Read stat entries: 18; Counters registered: 16.
Error example:
xray[110416]: error opening stats file: permission denied.