mirror of
https://github.com/alireza0/x-ui.git
synced 2026-03-21 00:05:49 +00:00
0.0.1
This commit is contained in:
@@ -3,7 +3,6 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/atomic"
|
||||
"strconv"
|
||||
"x-ui/database/model"
|
||||
"x-ui/logger"
|
||||
@@ -15,8 +14,6 @@ import (
|
||||
type InboundController struct {
|
||||
inboundService service.InboundService
|
||||
xrayService service.XrayService
|
||||
|
||||
isNeedXrayRestart atomic.Bool
|
||||
}
|
||||
|
||||
func NewInboundController(g *gin.RouterGroup) *InboundController {
|
||||
@@ -39,12 +36,12 @@ func (a *InboundController) startTask() {
|
||||
webServer := global.GetWebServer()
|
||||
c := webServer.GetCron()
|
||||
c.AddFunc("@every 10s", func() {
|
||||
if a.isNeedXrayRestart.Load() {
|
||||
if a.xrayService.IsNeedRestart() {
|
||||
a.xrayService.SetIsNeedRestart(false)
|
||||
err := a.xrayService.RestartXray()
|
||||
if err != nil {
|
||||
logger.Error("restart xray failed:", err)
|
||||
}
|
||||
a.isNeedXrayRestart.Store(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -73,7 +70,7 @@ func (a *InboundController) addInbound(c *gin.Context) {
|
||||
err = a.inboundService.AddInbound(inbound)
|
||||
jsonMsg(c, "添加", err)
|
||||
if err == nil {
|
||||
a.isNeedXrayRestart.Store(true)
|
||||
a.xrayService.SetIsNeedRestart(true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +83,7 @@ func (a *InboundController) delInbound(c *gin.Context) {
|
||||
err = a.inboundService.DelInbound(id)
|
||||
jsonMsg(c, "删除", err)
|
||||
if err == nil {
|
||||
a.isNeedXrayRestart.Store(true)
|
||||
a.xrayService.SetIsNeedRestart(true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +104,6 @@ func (a *InboundController) updateInbound(c *gin.Context) {
|
||||
err = a.inboundService.UpdateInbound(inbound)
|
||||
jsonMsg(c, "修改", err)
|
||||
if err == nil {
|
||||
a.isNeedXrayRestart.Store(true)
|
||||
a.xrayService.SetIsNeedRestart(true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,28 +38,19 @@ func (a *ServerController) initRouter(g *gin.RouterGroup) {
|
||||
}
|
||||
|
||||
func (a *ServerController) refreshStatus() {
|
||||
status := a.serverService.GetStatus(a.lastStatus)
|
||||
a.lastStatus = status
|
||||
a.lastStatus = a.serverService.GetStatus(a.lastStatus)
|
||||
}
|
||||
|
||||
func (a *ServerController) startTask() {
|
||||
webServer := global.GetWebServer()
|
||||
ctx := webServer.GetCtx()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
now := time.Now()
|
||||
if now.Sub(a.lastGetStatusTime) > time.Minute*3 {
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
}
|
||||
a.refreshStatus()
|
||||
c := webServer.GetCron()
|
||||
c.AddFunc("@every 2s", func() {
|
||||
now := time.Now()
|
||||
if now.Sub(a.lastGetStatusTime) > time.Minute*3 {
|
||||
return
|
||||
}
|
||||
}()
|
||||
a.refreshStatus()
|
||||
})
|
||||
}
|
||||
|
||||
func (a *ServerController) status(c *gin.Context) {
|
||||
|
||||
@@ -1,13 +1,25 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"time"
|
||||
"x-ui/web/entity"
|
||||
"x-ui/web/service"
|
||||
"x-ui/web/session"
|
||||
)
|
||||
|
||||
type updateUserForm struct {
|
||||
OldUsername string `json:"oldUsername" form:"oldUsername"`
|
||||
OldPassword string `json:"oldPassword" form:"oldPassword"`
|
||||
NewUsername string `json:"newUsername" form:"newUsername"`
|
||||
NewPassword string `json:"newPassword" form:"newPassword"`
|
||||
}
|
||||
|
||||
type SettingController struct {
|
||||
settingService service.SettingService
|
||||
userService service.UserService
|
||||
panelService service.PanelService
|
||||
}
|
||||
|
||||
func NewSettingController(g *gin.RouterGroup) *SettingController {
|
||||
@@ -21,6 +33,8 @@ func (a *SettingController) initRouter(g *gin.RouterGroup) {
|
||||
|
||||
g.POST("/all", a.getAllSetting)
|
||||
g.POST("/update", a.updateSetting)
|
||||
g.POST("/updateUser", a.updateUser)
|
||||
g.POST("/restartPanel", a.restartPanel)
|
||||
}
|
||||
|
||||
func (a *SettingController) getAllSetting(c *gin.Context) {
|
||||
@@ -42,3 +56,33 @@ func (a *SettingController) updateSetting(c *gin.Context) {
|
||||
err = a.settingService.UpdateAllSetting(allSetting)
|
||||
jsonMsg(c, "修改设置", err)
|
||||
}
|
||||
|
||||
func (a *SettingController) updateUser(c *gin.Context) {
|
||||
form := &updateUserForm{}
|
||||
err := c.ShouldBind(form)
|
||||
if err != nil {
|
||||
jsonMsg(c, "修改用户", err)
|
||||
return
|
||||
}
|
||||
user := session.GetLoginUser(c)
|
||||
if user.Username != form.OldUsername || user.Password != form.OldPassword {
|
||||
jsonMsg(c, "修改用户", errors.New("原用户名或原密码错误"))
|
||||
return
|
||||
}
|
||||
if form.NewUsername == "" || form.NewPassword == "" {
|
||||
jsonMsg(c, "修改用户", errors.New("新用户名和新密码不能为空"))
|
||||
return
|
||||
}
|
||||
err = a.userService.UpdateUser(user.Id, form.NewUsername, form.NewPassword)
|
||||
if err == nil {
|
||||
user.Username = form.NewUsername
|
||||
user.Password = form.NewPassword
|
||||
session.SetLoginUser(c, user)
|
||||
}
|
||||
jsonMsg(c, "修改用户", err)
|
||||
}
|
||||
|
||||
func (a *SettingController) restartPanel(c *gin.Context) {
|
||||
err := a.panelService.RestartPanel(time.Second * 3)
|
||||
jsonMsg(c, "重启面板", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user