2023-02-09 22:18:06 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
"x-ui/web/entity"
|
|
|
|
"x-ui/web/service"
|
|
|
|
"x-ui/web/session"
|
2023-03-17 19:07:49 +03:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-02-09 22:18:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
a := &SettingController{}
|
|
|
|
a.initRouter(g)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *SettingController) initRouter(g *gin.RouterGroup) {
|
|
|
|
g = g.Group("/setting")
|
|
|
|
|
|
|
|
g.POST("/all", a.getAllSetting)
|
2023-04-09 22:43:18 +03:00
|
|
|
g.POST("/defaultSettings", a.getDefaultSettings)
|
2023-02-09 22:18:06 +03:00
|
|
|
g.POST("/update", a.updateSetting)
|
|
|
|
g.POST("/updateUser", a.updateUser)
|
|
|
|
g.POST("/restartPanel", a.restartPanel)
|
2023-04-18 19:51:09 +03:00
|
|
|
g.GET("/getDefaultJsonConfig", a.getDefaultJsonConfig)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *SettingController) getAllSetting(c *gin.Context) {
|
|
|
|
allSetting, err := a.settingService.GetAllSetting()
|
|
|
|
if err != nil {
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.getSetting"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonObj(c, allSetting, nil)
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:51:09 +03:00
|
|
|
func (a *SettingController) getDefaultJsonConfig(c *gin.Context) {
|
|
|
|
defaultJsonConfig, err := a.settingService.GetDefaultJsonConfig()
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.getSetting"), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonObj(c, defaultJsonConfig, nil)
|
|
|
|
}
|
|
|
|
|
2023-04-09 22:43:18 +03:00
|
|
|
func (a *SettingController) getDefaultSettings(c *gin.Context) {
|
|
|
|
expireDiff, err := a.settingService.GetExpireDiff()
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.getSetting"), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
trafficDiff, err := a.settingService.GetTrafficDiff()
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.getSetting"), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defaultCert, err := a.settingService.GetCertFile()
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.getSetting"), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defaultKey, err := a.settingService.GetKeyFile()
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.getSetting"), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result := map[string]interface{}{
|
|
|
|
"expireDiff": expireDiff,
|
|
|
|
"trafficDiff": trafficDiff,
|
|
|
|
"defaultCert": defaultCert,
|
|
|
|
"defaultKey": defaultKey,
|
|
|
|
}
|
|
|
|
jsonObj(c, result, nil)
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:18:06 +03:00
|
|
|
func (a *SettingController) updateSetting(c *gin.Context) {
|
|
|
|
allSetting := &entity.AllSetting{}
|
|
|
|
err := c.ShouldBind(allSetting)
|
|
|
|
if err != nil {
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.modifySetting"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = a.settingService.UpdateAllSetting(allSetting)
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.modifySetting"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *SettingController) updateUser(c *gin.Context) {
|
|
|
|
form := &updateUserForm{}
|
|
|
|
err := c.ShouldBind(form)
|
|
|
|
if err != nil {
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.modifySetting"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user := session.GetLoginUser(c)
|
|
|
|
if user.Username != form.OldUsername || user.Password != form.OldPassword {
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.modifyUser"), errors.New(I18n(c, "pages.setting.toasts.originalUserPassIncorrect")))
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if form.NewUsername == "" || form.NewPassword == "" {
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.modifyUser"), errors.New(I18n(c, "pages.setting.toasts.userPassMustBeNotEmpty")))
|
2023-02-09 22:18:06 +03:00
|
|
|
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)
|
|
|
|
}
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.toasts.modifyUser"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *SettingController) restartPanel(c *gin.Context) {
|
|
|
|
err := a.panelService.RestartPanel(time.Second * 3)
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.setting.restartPanel"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|