| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 | 
							- package controller
 
- import (
 
- 	"errors"
 
- 	"time"
 
- 	"github.com/mhsanaei/3x-ui/v2/util/crypto"
 
- 	"github.com/mhsanaei/3x-ui/v2/web/entity"
 
- 	"github.com/mhsanaei/3x-ui/v2/web/service"
 
- 	"github.com/mhsanaei/3x-ui/v2/web/session"
 
- 	"github.com/gin-gonic/gin"
 
- )
 
- // updateUserForm represents the form for updating user credentials.
 
- 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"`
 
- }
 
- // SettingController handles settings and user management operations.
 
- type SettingController struct {
 
- 	settingService service.SettingService
 
- 	userService    service.UserService
 
- 	panelService   service.PanelService
 
- }
 
- // NewSettingController creates a new SettingController and initializes its routes.
 
- func NewSettingController(g *gin.RouterGroup) *SettingController {
 
- 	a := &SettingController{}
 
- 	a.initRouter(g)
 
- 	return a
 
- }
 
- // initRouter sets up the routes for settings management.
 
- func (a *SettingController) initRouter(g *gin.RouterGroup) {
 
- 	g = g.Group("/setting")
 
- 	g.POST("/all", a.getAllSetting)
 
- 	g.POST("/defaultSettings", a.getDefaultSettings)
 
- 	g.POST("/update", a.updateSetting)
 
- 	g.POST("/updateUser", a.updateUser)
 
- 	g.POST("/restartPanel", a.restartPanel)
 
- 	g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
 
- }
 
- // getAllSetting retrieves all current settings.
 
- func (a *SettingController) getAllSetting(c *gin.Context) {
 
- 	allSetting, err := a.settingService.GetAllSetting()
 
- 	if err != nil {
 
- 		jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
 
- 		return
 
- 	}
 
- 	jsonObj(c, allSetting, nil)
 
- }
 
- // getDefaultSettings retrieves the default settings based on the host.
 
- func (a *SettingController) getDefaultSettings(c *gin.Context) {
 
- 	result, err := a.settingService.GetDefaultSettings(c.Request.Host)
 
- 	if err != nil {
 
- 		jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
 
- 		return
 
- 	}
 
- 	jsonObj(c, result, nil)
 
- }
 
- // updateSetting updates all settings with the provided data.
 
- func (a *SettingController) updateSetting(c *gin.Context) {
 
- 	allSetting := &entity.AllSetting{}
 
- 	err := c.ShouldBind(allSetting)
 
- 	if err != nil {
 
- 		jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
 
- 		return
 
- 	}
 
- 	err = a.settingService.UpdateAllSetting(allSetting)
 
- 	jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
 
- }
 
- // updateUser updates the current user's username and password.
 
- func (a *SettingController) updateUser(c *gin.Context) {
 
- 	form := &updateUserForm{}
 
- 	err := c.ShouldBind(form)
 
- 	if err != nil {
 
- 		jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
 
- 		return
 
- 	}
 
- 	user := session.GetLoginUser(c)
 
- 	if user.Username != form.OldUsername || !crypto.CheckPasswordHash(user.Password, form.OldPassword) {
 
- 		jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.originalUserPassIncorrect")))
 
- 		return
 
- 	}
 
- 	if form.NewUsername == "" || form.NewPassword == "" {
 
- 		jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.userPassMustBeNotEmpty")))
 
- 		return
 
- 	}
 
- 	err = a.userService.UpdateUser(user.Id, form.NewUsername, form.NewPassword)
 
- 	if err == nil {
 
- 		user.Username = form.NewUsername
 
- 		user.Password, _ = crypto.HashPasswordAsBcrypt(form.NewPassword)
 
- 		session.SetLoginUser(c, user)
 
- 	}
 
- 	jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
 
- }
 
- // restartPanel restarts the panel service after a delay.
 
- func (a *SettingController) restartPanel(c *gin.Context) {
 
- 	err := a.panelService.RestartPanel(time.Second * 3)
 
- 	jsonMsg(c, I18nWeb(c, "pages.settings.restartPanelSuccess"), err)
 
- }
 
- // getDefaultXrayConfig retrieves the default Xray configuration.
 
- func (a *SettingController) getDefaultXrayConfig(c *gin.Context) {
 
- 	defaultJsonConfig, err := a.settingService.GetDefaultXrayConfig()
 
- 	if err != nil {
 
- 		jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
 
- 		return
 
- 	}
 
- 	jsonObj(c, defaultJsonConfig, nil)
 
- }
 
 
  |