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"`
|
|
|
|
}
|
|
|
|
|
2023-04-21 18:30:14 +03:00
|
|
|
type updateSecretForm struct {
|
|
|
|
LoginSecret string `json:"loginSecret" form:"loginSecret"`
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:18:06 +03:00
|
|
|
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-04-21 18:30:14 +03:00
|
|
|
g.POST("/updateUserSecret", a.updateSecret)
|
|
|
|
g.POST("/getUserSecret", a.getUserSecret)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *SettingController) getAllSetting(c *gin.Context) {
|
|
|
|
allSetting, err := a.settingService.GetAllSetting()
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), 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 {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
|
2023-04-18 19:51:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonObj(c, defaultJsonConfig, nil)
|
|
|
|
}
|
|
|
|
|
2023-04-09 22:43:18 +03:00
|
|
|
func (a *SettingController) getDefaultSettings(c *gin.Context) {
|
2023-05-31 00:01:56 +03:00
|
|
|
type settingFunc func() (interface{}, error)
|
|
|
|
|
|
|
|
settings := map[string]settingFunc{
|
|
|
|
"expireDiff": func() (interface{}, error) { return a.settingService.GetExpireDiff() },
|
|
|
|
"trafficDiff": func() (interface{}, error) { return a.settingService.GetTrafficDiff() },
|
|
|
|
"defaultCert": func() (interface{}, error) { return a.settingService.GetCertFile() },
|
|
|
|
"defaultKey": func() (interface{}, error) { return a.settingService.GetKeyFile() },
|
|
|
|
"tgBotEnable": func() (interface{}, error) { return a.settingService.GetTgbotenabled() },
|
|
|
|
"subEnable": func() (interface{}, error) { return a.settingService.GetSubEnable() },
|
|
|
|
"subPort": func() (interface{}, error) { return a.settingService.GetSubPort() },
|
|
|
|
"subPath": func() (interface{}, error) { return a.settingService.GetSubPath() },
|
|
|
|
"subDomain": func() (interface{}, error) { return a.settingService.GetSubDomain() },
|
|
|
|
"subKeyFile": func() (interface{}, error) { return a.settingService.GetSubKeyFile() },
|
|
|
|
"subCertFile": func() (interface{}, error) { return a.settingService.GetSubCertFile() },
|
2023-05-22 17:36:34 +03:00
|
|
|
}
|
2023-05-31 00:01:56 +03:00
|
|
|
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
|
|
|
|
for key, fn := range settings {
|
|
|
|
value, err := fn()
|
|
|
|
if err != nil {
|
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result[key] = value
|
2023-05-22 17:36:34 +03:00
|
|
|
}
|
2023-05-31 00:01:56 +03:00
|
|
|
|
2023-05-22 17:36:34 +03:00
|
|
|
subTLS := false
|
2023-05-31 01:59:59 +03:00
|
|
|
if result["subKeyFile"] != "" || result["subCertFile"] != "" {
|
2023-05-22 17:36:34 +03:00
|
|
|
subTLS = true
|
|
|
|
}
|
2023-05-31 00:01:56 +03:00
|
|
|
result["subTLS"] = subTLS
|
|
|
|
|
|
|
|
delete(result, "subKeyFile")
|
|
|
|
delete(result, "subCertFile")
|
|
|
|
|
2023-04-09 22:43:18 +03:00
|
|
|
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-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = a.settingService.UpdateAllSetting(allSetting)
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), 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-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user := session.GetLoginUser(c)
|
|
|
|
if user.Username != form.OldUsername || user.Password != form.OldPassword {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), errors.New(I18nWeb(c, "pages.settings.toasts.originalUserPassIncorrect")))
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if form.NewUsername == "" || form.NewPassword == "" {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), errors.New(I18nWeb(c, "pages.settings.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-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.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-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.restartPanel"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
2023-04-21 18:30:14 +03:00
|
|
|
|
|
|
|
func (a *SettingController) updateSecret(c *gin.Context) {
|
|
|
|
form := &updateSecretForm{}
|
|
|
|
err := c.ShouldBind(form)
|
|
|
|
if err != nil {
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
|
2023-04-21 18:30:14 +03:00
|
|
|
}
|
|
|
|
user := session.GetLoginUser(c)
|
|
|
|
err = a.userService.UpdateUserSecret(user.Id, form.LoginSecret)
|
|
|
|
if err == nil {
|
|
|
|
user.LoginSecret = form.LoginSecret
|
|
|
|
session.SetLoginUser(c, user)
|
|
|
|
}
|
2023-05-21 01:59:27 +03:00
|
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
|
2023-04-21 18:30:14 +03:00
|
|
|
}
|
2023-05-12 18:02:04 +03:00
|
|
|
|
2023-04-21 18:30:14 +03:00
|
|
|
func (a *SettingController) getUserSecret(c *gin.Context) {
|
|
|
|
loginUser := session.GetLoginUser(c)
|
|
|
|
user := a.userService.GetUserSecret(loginUser.Id)
|
|
|
|
if user != nil {
|
|
|
|
jsonObj(c, user, nil)
|
|
|
|
}
|
|
|
|
}
|