From 073b2ee8a871cb7fc04f9dd0d55b45df1a5af2e1 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 13 Mar 2023 22:32:24 +0300 Subject: [PATCH] add Xray stop & Xray restart --- web/controller/server.go | 22 ++++++++++++++++++++++ web/html/xui/index.html | 22 +++++++++++++++++++++- web/service/server.go | 24 ++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/web/controller/server.go b/web/controller/server.go index 2dd40a0a..673a96d8 100644 --- a/web/controller/server.go +++ b/web/controller/server.go @@ -34,6 +34,8 @@ func (a *ServerController) initRouter(g *gin.RouterGroup) { g.Use(a.checkLogin) g.POST("/status", a.status) g.POST("/getXrayVersion", a.getXrayVersion) + g.POST("/stopXrayService", a.stopXrayService) + g.POST("/restartXrayService", a.restartXrayService) g.POST("/installXray/:version", a.installXray) } @@ -83,3 +85,23 @@ func (a *ServerController) installXray(c *gin.Context) { err := a.serverService.UpdateXray(version) jsonMsg(c, I18n(c, "install")+" xray", err) } + +func (a *ServerController) stopXrayService(c *gin.Context) { + a.lastGetStatusTime = time.Now() + err := a.serverService.StopXrayService() + if err != nil { + jsonMsg(c, "", err) + return + } + jsonMsg(c, "Xray stoped",err) + +} +func (a *ServerController) restartXrayService(c *gin.Context) { + err := a.serverService.RestartXrayService() + if err != nil { + jsonMsg(c, "", err) + return + } + jsonMsg(c, "Xray restarted",err) + +} \ No newline at end of file diff --git a/web/html/xui/index.html b/web/html/xui/index.html index b4e5be7f..af1104c7 100644 --- a/web/html/xui/index.html +++ b/web/html/xui/index.html @@ -77,7 +77,9 @@ [[ status.xray.version ]] - {{ i18n "pages.index.xraySwitch"}} + {{ i18n "pages.index.stopXray" }} + {{ i18n "pages.index.restartXray" }} + {{ i18n "pages.index.xraySwitch" }} @@ -316,6 +318,24 @@ }, }); }, + //here add stop xray function + async stopXrayService() { + this.loading(true); + const msg = await HttpUtil.post('server/stopXrayService'); + this.loading(false); + if (!msg.success) { + return; + } + }, + //here add restart xray function + async restartXrayService() { + this.loading(true); + const msg = await HttpUtil.post('server/restartXrayService'); + this.loading(false); + if (!msg.success) { + return; + } + }, }, async mounted() { while (true) { diff --git a/web/service/server.go b/web/service/server.go index 968df398..c0cff794 100644 --- a/web/service/server.go +++ b/web/service/server.go @@ -198,6 +198,30 @@ func (s *ServerService) GetXrayVersions() ([]string, error) { return versions, nil } +func (s *ServerService) StopXrayService() (string error) { + + err := s.xrayService.StopXray() + if err != nil { + logger.Error("stop xray failed:", err) + return err + } + + return nil +} + +func (s *ServerService) RestartXrayService() (string error) { + + s.xrayService.StopXray() + defer func() { + err := s.xrayService.RestartXray(true) + if err != nil { + logger.Error("start xray failed:", err) + } + }() + + return nil +} + func (s *ServerService) downloadXRay(version string) (string, error) { osName := runtime.GOOS arch := runtime.GOARCH