setting.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package controller
  2. import (
  3. "errors"
  4. "time"
  5. "x-ui/web/entity"
  6. "x-ui/web/service"
  7. "x-ui/web/session"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type updateUserForm struct {
  11. OldUsername string `json:"oldUsername" form:"oldUsername"`
  12. OldPassword string `json:"oldPassword" form:"oldPassword"`
  13. NewUsername string `json:"newUsername" form:"newUsername"`
  14. NewPassword string `json:"newPassword" form:"newPassword"`
  15. }
  16. type updateSecretForm struct {
  17. LoginSecret string `json:"loginSecret" form:"loginSecret"`
  18. }
  19. type SettingController struct {
  20. settingService service.SettingService
  21. userService service.UserService
  22. panelService service.PanelService
  23. }
  24. func NewSettingController(g *gin.RouterGroup) *SettingController {
  25. a := &SettingController{}
  26. a.initRouter(g)
  27. return a
  28. }
  29. func (a *SettingController) initRouter(g *gin.RouterGroup) {
  30. g = g.Group("/setting")
  31. g.POST("/all", a.getAllSetting)
  32. g.POST("/defaultSettings", a.getDefaultSettings)
  33. g.POST("/update", a.updateSetting)
  34. g.POST("/updateUser", a.updateUser)
  35. g.POST("/restartPanel", a.restartPanel)
  36. g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
  37. g.POST("/updateUserSecret", a.updateSecret)
  38. g.POST("/getUserSecret", a.getUserSecret)
  39. }
  40. func (a *SettingController) getAllSetting(c *gin.Context) {
  41. allSetting, err := a.settingService.GetAllSetting()
  42. if err != nil {
  43. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  44. return
  45. }
  46. jsonObj(c, allSetting, nil)
  47. }
  48. func (a *SettingController) getDefaultSettings(c *gin.Context) {
  49. result, err := a.settingService.GetDefaultSettings(c.Request.Host)
  50. if err != nil {
  51. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  52. return
  53. }
  54. jsonObj(c, result, nil)
  55. }
  56. func (a *SettingController) updateSetting(c *gin.Context) {
  57. allSetting := &entity.AllSetting{}
  58. err := c.ShouldBind(allSetting)
  59. if err != nil {
  60. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  61. return
  62. }
  63. err = a.settingService.UpdateAllSetting(allSetting)
  64. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  65. }
  66. func (a *SettingController) updateUser(c *gin.Context) {
  67. form := &updateUserForm{}
  68. err := c.ShouldBind(form)
  69. if err != nil {
  70. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  71. return
  72. }
  73. user := session.GetLoginUser(c)
  74. if user.Username != form.OldUsername || user.Password != form.OldPassword {
  75. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), errors.New(I18nWeb(c, "pages.settings.toasts.originalUserPassIncorrect")))
  76. return
  77. }
  78. if form.NewUsername == "" || form.NewPassword == "" {
  79. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), errors.New(I18nWeb(c, "pages.settings.toasts.userPassMustBeNotEmpty")))
  80. return
  81. }
  82. err = a.userService.UpdateUser(user.Id, form.NewUsername, form.NewPassword)
  83. if err == nil {
  84. user.Username = form.NewUsername
  85. user.Password = form.NewPassword
  86. session.SetLoginUser(c, user)
  87. }
  88. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
  89. }
  90. func (a *SettingController) restartPanel(c *gin.Context) {
  91. err := a.panelService.RestartPanel(time.Second * 3)
  92. jsonMsg(c, I18nWeb(c, "pages.settings.restartPanel"), err)
  93. }
  94. func (a *SettingController) updateSecret(c *gin.Context) {
  95. form := &updateSecretForm{}
  96. err := c.ShouldBind(form)
  97. if err != nil {
  98. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  99. }
  100. user := session.GetLoginUser(c)
  101. err = a.userService.UpdateUserSecret(user.Id, form.LoginSecret)
  102. if err == nil {
  103. user.LoginSecret = form.LoginSecret
  104. session.SetLoginUser(c, user)
  105. }
  106. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
  107. }
  108. func (a *SettingController) getUserSecret(c *gin.Context) {
  109. loginUser := session.GetLoginUser(c)
  110. user := a.userService.GetUserSecret(loginUser.Id)
  111. if user != nil {
  112. jsonObj(c, user, nil)
  113. }
  114. }
  115. func (a *SettingController) getDefaultXrayConfig(c *gin.Context) {
  116. defaultJsonConfig, err := a.settingService.GetDefaultXrayConfig()
  117. if err != nil {
  118. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  119. return
  120. }
  121. jsonObj(c, defaultJsonConfig, nil)
  122. }