mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-05-29 16:39:35 +00:00
checkLogin middleware now detects default admin/admin credentials and redirects every panel route to /panel/settings until they are changed. The settings page auto-opens the Authentication tab, shows a non-dismissible error banner, and lists 'Default credentials' first in the security checklist. Login response includes mustChangeCredentials so the login page can redirect directly. Logout is now POST-only. Password must be at least 10 characters and cannot be admin/admin.
193 lines
6.4 KiB
Go
193 lines
6.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/util/crypto"
|
|
"github.com/mhsanaei/3x-ui/v3/web/entity"
|
|
"github.com/mhsanaei/3x-ui/v3/web/service"
|
|
"github.com/mhsanaei/3x-ui/v3/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"`
|
|
}
|
|
|
|
type verifyTwoFactorForm struct {
|
|
Code string `json:"code" form:"code"`
|
|
}
|
|
|
|
type updateSecretForm struct {
|
|
Key string `json:"key" form:"key"`
|
|
Value string `json:"value" form:"value"`
|
|
}
|
|
|
|
// 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("/secret", a.updateSecret)
|
|
g.POST("/updateUser", a.updateUser)
|
|
g.POST("/verifyTwoFactor", a.verifyTwoFactor)
|
|
g.POST("/restartPanel", a.restartPanel)
|
|
g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
|
|
g.GET("/getApiToken", a.getApiToken)
|
|
g.POST("/regenerateApiToken", a.regenerateApiToken)
|
|
}
|
|
|
|
// getAllSetting retrieves all current settings.
|
|
func (a *SettingController) getAllSetting(c *gin.Context) {
|
|
allSetting, err := a.settingService.GetAllSettingView()
|
|
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)
|
|
}
|
|
|
|
func (a *SettingController) updateSecret(c *gin.Context) {
|
|
form := &updateSecretForm{}
|
|
if err := c.ShouldBind(form); err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
|
|
return
|
|
}
|
|
err := a.settingService.UpdateSecret(form.Key, form.Value)
|
|
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 strings.TrimSpace(form.NewUsername) == "" || form.NewPassword == "" {
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.userPassMustBeNotEmpty")))
|
|
return
|
|
}
|
|
if len(form.NewPassword) < 10 {
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New("new password must be at least 10 characters"))
|
|
return
|
|
}
|
|
if strings.TrimSpace(form.NewUsername) == "admin" && form.NewPassword == "admin" {
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New("default admin/admin credentials are not allowed"))
|
|
return
|
|
}
|
|
err = a.userService.UpdateUser(user.Id, form.NewUsername, form.NewPassword)
|
|
if err == nil {
|
|
user.Username = form.NewUsername
|
|
user.Password, _ = crypto.HashPasswordAsBcrypt(form.NewPassword)
|
|
if saveErr := session.SetLoginUser(c, user); saveErr != nil {
|
|
err = saveErr
|
|
}
|
|
}
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
|
|
}
|
|
|
|
func (a *SettingController) verifyTwoFactor(c *gin.Context) {
|
|
form := &verifyTwoFactorForm{}
|
|
if err := c.ShouldBind(form); err != nil {
|
|
jsonMsg(c, I18nWeb(c, "somethingWentWrong"), err)
|
|
return
|
|
}
|
|
ok, err := a.userService.VerifyTwoFactorCode(form.Code)
|
|
if err == nil && !ok {
|
|
err = errors.New("invalid 2fa code")
|
|
}
|
|
jsonObj(c, ok, 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)
|
|
}
|
|
|
|
// getApiToken returns the panel's API token used by remote central
|
|
// panels to authenticate as Bearer tokens. The token is auto-generated
|
|
// on first read so existing installs upgrade transparently.
|
|
func (a *SettingController) getApiToken(c *gin.Context) {
|
|
tok, err := a.settingService.GetApiToken()
|
|
if err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
|
|
return
|
|
}
|
|
jsonObj(c, tok, nil)
|
|
}
|
|
|
|
// regenerateApiToken rotates the API token. Any central panel that had
|
|
// the old value cached will start failing heartbeats until it is updated
|
|
// with the new token — that's intentional, it's the whole point of rotation.
|
|
func (a *SettingController) regenerateApiToken(c *gin.Context) {
|
|
tok, err := a.settingService.RegenerateApiToken()
|
|
if err != nil {
|
|
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
|
|
return
|
|
}
|
|
jsonObj(c, tok, nil)
|
|
}
|