3x-ui/web/controller/setting.go

124 lines
4.1 KiB
Go
Raw Permalink Normal View History

2023-02-09 19:18:06 +00:00
package controller
import (
"errors"
"time"
2025-09-19 08:05:43 +00:00
"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"
2023-03-17 16:07:49 +00:00
"github.com/gin-gonic/gin"
2023-02-09 19:18:06 +00:00
)
2025-09-20 07:35:50 +00:00
// updateUserForm represents the form for updating user credentials.
2023-02-09 19:18:06 +00: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"`
}
2025-09-20 07:35:50 +00:00
// SettingController handles settings and user management operations.
2023-02-09 19:18:06 +00:00
type SettingController struct {
settingService service.SettingService
userService service.UserService
panelService service.PanelService
}
2025-09-20 07:35:50 +00:00
// NewSettingController creates a new SettingController and initializes its routes.
2023-02-09 19:18:06 +00:00
func NewSettingController(g *gin.RouterGroup) *SettingController {
a := &SettingController{}
a.initRouter(g)
return a
}
2025-09-20 07:35:50 +00:00
// initRouter sets up the routes for settings management.
2023-02-09 19:18:06 +00:00
func (a *SettingController) initRouter(g *gin.RouterGroup) {
g = g.Group("/setting")
g.POST("/all", a.getAllSetting)
g.POST("/defaultSettings", a.getDefaultSettings)
2023-02-09 19:18:06 +00:00
g.POST("/update", a.updateSetting)
g.POST("/updateUser", a.updateUser)
g.POST("/restartPanel", a.restartPanel)
2023-12-04 18:20:46 +00:00
g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// getAllSetting retrieves all current settings.
2023-02-09 19:18:06 +00:00
func (a *SettingController) getAllSetting(c *gin.Context) {
allSetting, err := a.settingService.GetAllSetting()
if err != nil {
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
2023-02-09 19:18:06 +00:00
return
}
jsonObj(c, allSetting, nil)
}
2025-09-20 07:35:50 +00:00
// getDefaultSettings retrieves the default settings based on the host.
func (a *SettingController) getDefaultSettings(c *gin.Context) {
2023-12-08 16:18:51 +00:00
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)
}
2025-09-20 07:35:50 +00:00
// updateSetting updates all settings with the provided data.
2023-02-09 19:18:06 +00:00
func (a *SettingController) updateSetting(c *gin.Context) {
allSetting := &entity.AllSetting{}
err := c.ShouldBind(allSetting)
if err != nil {
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
2023-02-09 19:18:06 +00:00
return
}
err = a.settingService.UpdateAllSetting(allSetting)
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// updateUser updates the current user's username and password.
2023-02-09 19:18:06 +00:00
func (a *SettingController) updateUser(c *gin.Context) {
form := &updateUserForm{}
err := c.ShouldBind(form)
if err != nil {
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
2023-02-09 19:18:06 +00:00
return
}
user := session.GetLoginUser(c)
if user.Username != form.OldUsername || !crypto.CheckPasswordHash(user.Password, form.OldPassword) {
2025-05-09 03:46:29 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.originalUserPassIncorrect")))
2023-02-09 19:18:06 +00:00
return
}
if form.NewUsername == "" || form.NewPassword == "" {
2025-05-09 03:46:29 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.userPassMustBeNotEmpty")))
2023-02-09 19:18:06 +00:00
return
}
err = a.userService.UpdateUser(user.Id, form.NewUsername, form.NewPassword)
if err == nil {
user.Username = form.NewUsername
user.Password, _ = crypto.HashPasswordAsBcrypt(form.NewPassword)
ws/inbounds: realtime fixes + perf for 10k+ client inbounds (#4123) * ws/inbounds: realtime fixes + perf for 10k+ client inbounds - hub: dedup, throttle, panic-restart, deadlock fix, race tests - client: backoff cap + slow-retry instead of giving up - broadcast: delta-only payload, count-based invalidate fallback - filter: fix empty online list (Inbound has no .id, use dbInbound.toInbound) - perf: O(N²)→O(N) traffic merge, bulk delete, /setEnable endpoint - traffic: monotonic all_time + UI clamp + propagate in delta handler - session: persist on update/logout (fixes logout-after-password-change) - ui: protocol tags flex, traffic bar normalize * Remove hub_test.go file * fix: ws hub, inbound service, and frontend correctness - propagate DelInbound error on disable path in SetInboundEnable - skip empty emails in updateClientTraffics to avoid constraint violations - use consistent IN ? clause, drop redundant ErrRecordNotFound guards - Hub.Unregister: direct removeClient fallback when channel is full - applyClientStatsDelta: O(1) email lookup via per-inbound Map cache - WS payload size check: Blob.size instead of .length for real byte count * fix: chunk large IN ? queries and fix IPv6 same-origin check * fix: chunk large IN ? queries and fix IPv6 same-origin check * fix: unify clientStats cache, throttle clarity, hub constants * fix(ui): align traffic/expiry cell columns across all rows * style(ui): redesign outbounds table for visual consistency * style(ui): redesign routing table for visual consistency * fix: * fix: * fix: * fix: * fix: * fix: font * refactor: simplify outbound tone functions for consistency and maintainability --------- Co-authored-by: lolka1333 <test123@gmail.com>
2026-05-05 15:27:49 +00:00
if saveErr := session.SetLoginUser(c, user); saveErr != nil {
err = saveErr
}
2023-02-09 19:18:06 +00:00
}
2023-05-20 22:59:27 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// restartPanel restarts the panel service after a delay.
2023-02-09 19:18:06 +00:00
func (a *SettingController) restartPanel(c *gin.Context) {
err := a.panelService.RestartPanel(time.Second * 3)
2025-05-09 03:46:29 +00:00
jsonMsg(c, I18nWeb(c, "pages.settings.restartPanelSuccess"), err)
2023-02-09 19:18:06 +00:00
}
2025-09-20 07:35:50 +00:00
// getDefaultXrayConfig retrieves the default Xray configuration.
2023-12-04 18:20:46 +00:00
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)
}