add xray-core restart option in cli

This commit is contained in:
Alireza Ahmadi
2026-02-20 00:08:36 +01:00
parent 9d93468332
commit 6781b0f7ae
4 changed files with 50 additions and 28 deletions

15
main.go
View File

@@ -22,7 +22,6 @@ import (
"github.com/op/go-logging"
"github.com/shirou/gopsutil/v4/net"
xrayCore "github.com/xtls/xray-core/core"
)
func runWebServer() {
@@ -68,7 +67,7 @@ func runWebServer() {
sigCh := make(chan os.Signal, 1)
// Trap shutdown signals
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGUSR1)
for {
sig := <-sigCh
@@ -99,6 +98,12 @@ func runWebServer() {
log.Println(err)
return
}
case syscall.SIGUSR1:
logger.Info("Received USR1 signal, restarting xray-core...")
err := server.RestartXray()
if err != nil {
logger.Error("Failed to restart xray-core:", err)
}
default:
server.Stop()
subServer.Stop()
@@ -473,9 +478,3 @@ func main() {
settingCmd.Usage()
}
}
func startXray() {
conf := xrayCore.Config{}
core, _ := xrayCore.New(&conf)
core.Start()
}

View File

@@ -392,3 +392,7 @@ func (s *Server) GetCtx() context.Context {
func (s *Server) GetCron() *cron.Cron {
return s.cron
}
func (s *Server) RestartXray() error {
return s.xrayService.RestartXray(true)
}

View File

@@ -8,6 +8,7 @@ Environment="XRAY_VMESS_AEAD_FORCED=false"
Type=simple
WorkingDirectory=/usr/local/x-ui/
ExecStart=/usr/local/x-ui/x-ui
ExecReload=kill -USR1 $MAINPID
Restart=on-failure
[Install]

58
x-ui.sh
View File

@@ -286,6 +286,16 @@ restart() {
fi
}
restart_xray() {
systemctl reload x-ui
LOGI "xray-core Restart signal sent successfully, Please check the log information to confirm whether xray restarted successfully"
sleep 2
show_xray_status
if [[ $# == 0 ]]; then
before_show_menu
fi
}
status() {
systemctl status x-ui -l
if [[ $# == 0 ]]; then
@@ -1052,6 +1062,7 @@ show_usage() {
echo "x-ui start - Start"
echo "x-ui stop - Stop"
echo "x-ui restart - Restart"
echo "x-ui restart-xray - Restart xray-core"
echo "x-ui status - Current Status"
echo "x-ui settings - Current Settings"
echo "x-ui enable - Enable Autostart on OS Startup"
@@ -1084,19 +1095,20 @@ show_menu() {
${green}10.${plain} Start
${green}11.${plain} Stop
${green}12.${plain} Restart
${green}13.${plain} Check State
${green}14.${plain} Check Logs
${green}13.${plain} Restart Xray
${green}14.${plain} Check State
${green}15.${plain} Check Logs
————————————————
${green}15.${plain} Enable Autostart
${green}16.${plain} Disable Autostart
${green}16.${plain} Enable Autostart
${green}17.${plain} Disable Autostart
————————————————
${green}17.${plain} SSL Certificate Management
${green}18.${plain} Cloudflare SSL Certificate
${green}19.${plain} Firewall Management
${green}18.${plain} SSL Certificate Management
${green}19.${plain} Cloudflare SSL Certificate
${green}20.${plain} Firewall Management
————————————————
${green}20.${plain} Enable or Disable BBR
${green}21.${plain} Update Geo Files
${green}22.${plain} Speedtest by Ookla
${green}21.${plain} Enable or Disable BBR
${green}22.${plain} Update Geo Files
${green}23.${plain} Speedtest by Ookla
"
show_status
echo && read -p "Please enter your selection [0-22]: " num
@@ -1142,37 +1154,40 @@ show_menu() {
check_install && restart
;;
13)
check_install && status
check_install && restart_xray
;;
14)
check_install && show_log
check_install && status
;;
15)
check_install && enable
check_install && show_log
;;
16)
check_install && disable
check_install && enable
;;
17)
ssl_cert_issue_main
check_install && disable
;;
18)
ssl_cert_issue_CF
ssl_cert_issue_main
;;
19)
firewall_menu
ssl_cert_issue_CF
;;
20)
bbr_menu
firewall_menu
;;
21)
update_geo
bbr_menu
;;
22)
update_geo
;;
23)
run_speedtest
;;
*)
LOGE "Please enter the correct number [0-22]"
LOGE "Please enter the correct number [0-23]"
;;
esac
}
@@ -1188,6 +1203,9 @@ if [[ $# > 0 ]]; then
"restart")
check_install 0 && restart 0
;;
"restart-xray")
check_install 0 && restart_xray 0
;;
"status")
check_install 0 && status 0
;;