setting.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package controller
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. "github.com/mhsanaei/3x-ui/v3/internal/util/crypto"
  8. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  9. "github.com/mhsanaei/3x-ui/v3/internal/web/middleware"
  10. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  11. "github.com/mhsanaei/3x-ui/v3/internal/web/service/email"
  12. "github.com/mhsanaei/3x-ui/v3/internal/web/service/panel"
  13. "github.com/mhsanaei/3x-ui/v3/internal/web/session"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // updateUserForm represents the form for updating user credentials.
  17. type updateUserForm struct {
  18. OldUsername string `json:"oldUsername" form:"oldUsername"`
  19. OldPassword string `json:"oldPassword" form:"oldPassword"`
  20. NewUsername string `json:"newUsername" form:"newUsername"`
  21. NewPassword string `json:"newPassword" form:"newPassword"`
  22. }
  23. // SettingController handles settings and user management operations.
  24. type SettingController struct {
  25. settingService service.SettingService
  26. userService panel.UserService
  27. panelService panel.PanelService
  28. apiTokenService panel.ApiTokenService
  29. xrayService service.XrayService
  30. }
  31. // NewSettingController creates a new SettingController and initializes its routes.
  32. func NewSettingController(g *gin.RouterGroup) *SettingController {
  33. a := &SettingController{}
  34. a.initRouter(g)
  35. return a
  36. }
  37. // initRouter sets up the routes for settings management.
  38. func (a *SettingController) initRouter(g *gin.RouterGroup) {
  39. g = g.Group("/setting")
  40. g.POST("/all", a.getAllSetting)
  41. g.POST("/defaultSettings", a.getDefaultSettings)
  42. g.POST("/update", a.updateSetting)
  43. g.POST("/updateUser", a.updateUser)
  44. g.POST("/restartPanel", a.restartPanel)
  45. g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
  46. g.GET("/apiTokens", a.listApiTokens)
  47. g.POST("/apiTokens/create", a.createApiToken)
  48. g.POST("/apiTokens/delete/:id", a.deleteApiToken)
  49. g.POST("/apiTokens/setEnabled/:id", a.setApiTokenEnabled)
  50. g.POST("/testSmtp", a.testSmtp)
  51. g.POST("/testTgBot", a.testTgBot)
  52. }
  53. // getAllSetting retrieves all current settings as the browser-safe view:
  54. // secret values are redacted and surfaced as has* presence flags instead.
  55. func (a *SettingController) getAllSetting(c *gin.Context) {
  56. allSetting, err := a.settingService.GetAllSettingView()
  57. if err != nil {
  58. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  59. return
  60. }
  61. jsonObj(c, allSetting, nil)
  62. }
  63. // getDefaultSettings retrieves the default settings based on the host.
  64. func (a *SettingController) getDefaultSettings(c *gin.Context) {
  65. result, err := a.settingService.GetDefaultSettings(c.Request.Host)
  66. if err != nil {
  67. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  68. return
  69. }
  70. jsonObj(c, result, nil)
  71. }
  72. // updateSetting updates all settings with the provided data.
  73. func (a *SettingController) updateSetting(c *gin.Context) {
  74. allSetting, ok := middleware.BindAndValidate[entity.AllSetting](c)
  75. if !ok {
  76. return
  77. }
  78. oldTwoFactor, twoFactorErr := a.settingService.GetTwoFactorEnable()
  79. oldPanelOutbound, _ := a.settingService.GetPanelOutbound()
  80. err := a.settingService.UpdateAllSetting(allSetting)
  81. if err == nil && twoFactorErr == nil && !oldTwoFactor && allSetting.TwoFactorEnable {
  82. if bumpErr := a.userService.BumpLoginEpoch(); bumpErr != nil {
  83. err = bumpErr
  84. }
  85. }
  86. if err == nil && allSetting.PanelOutbound != oldPanelOutbound {
  87. // The egress bridge lives in the generated config; reconcile the
  88. // running core. One SOCKS inbound plus one routing rule — both
  89. // hot-appliable, so this normally does not restart Xray.
  90. if applyErr := a.xrayService.RestartXray(false); applyErr != nil {
  91. logger.Warning("apply panel outbound change failed:", applyErr)
  92. }
  93. }
  94. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  95. }
  96. // updateUser updates the current user's username and password.
  97. func (a *SettingController) updateUser(c *gin.Context) {
  98. form := &updateUserForm{}
  99. err := c.ShouldBind(form)
  100. if err != nil {
  101. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  102. return
  103. }
  104. user := session.GetLoginUser(c)
  105. if user.Username != form.OldUsername || !crypto.CheckPasswordHash(user.Password, form.OldPassword) {
  106. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.originalUserPassIncorrect")))
  107. return
  108. }
  109. if form.NewUsername == "" || form.NewPassword == "" {
  110. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUserError"), errors.New(I18nWeb(c, "pages.settings.toasts.userPassMustBeNotEmpty")))
  111. return
  112. }
  113. err = a.userService.UpdateUser(user.Id, form.NewUsername, form.NewPassword)
  114. if err == nil {
  115. user.Username = form.NewUsername
  116. user.Password, _ = crypto.HashPasswordAsBcrypt(form.NewPassword)
  117. if saveErr := session.SetLoginUser(c, user); saveErr != nil {
  118. err = saveErr
  119. }
  120. }
  121. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifyUser"), err)
  122. }
  123. // restartPanel restarts the panel service after a delay.
  124. func (a *SettingController) restartPanel(c *gin.Context) {
  125. err := a.panelService.RestartPanel(time.Second * 3)
  126. jsonMsg(c, I18nWeb(c, "pages.settings.restartPanelSuccess"), err)
  127. }
  128. // getDefaultXrayConfig retrieves the default Xray configuration.
  129. func (a *SettingController) getDefaultXrayConfig(c *gin.Context) {
  130. defaultJsonConfig, err := a.settingService.GetDefaultXrayConfig()
  131. if err != nil {
  132. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  133. return
  134. }
  135. jsonObj(c, defaultJsonConfig, nil)
  136. }
  137. type apiTokenCreateForm struct {
  138. Name string `json:"name" form:"name"`
  139. }
  140. type apiTokenEnabledForm struct {
  141. Enabled bool `json:"enabled" form:"enabled"`
  142. }
  143. func (a *SettingController) listApiTokens(c *gin.Context) {
  144. rows, err := a.apiTokenService.List()
  145. if err != nil {
  146. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
  147. return
  148. }
  149. jsonObj(c, rows, nil)
  150. }
  151. func (a *SettingController) createApiToken(c *gin.Context) {
  152. form := &apiTokenCreateForm{}
  153. if err := c.ShouldBind(form); err != nil {
  154. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  155. return
  156. }
  157. row, err := a.apiTokenService.Create(form.Name)
  158. if err != nil {
  159. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  160. return
  161. }
  162. jsonObj(c, row, nil)
  163. }
  164. func (a *SettingController) deleteApiToken(c *gin.Context) {
  165. id, err := strconv.Atoi(c.Param("id"))
  166. if err != nil {
  167. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  168. return
  169. }
  170. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), a.apiTokenService.Delete(id))
  171. }
  172. func (a *SettingController) setApiTokenEnabled(c *gin.Context) {
  173. id, err := strconv.Atoi(c.Param("id"))
  174. if err != nil {
  175. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err)
  176. return
  177. }
  178. form := &apiTokenEnabledForm{}
  179. if bindErr := c.ShouldBind(form); bindErr != nil {
  180. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), bindErr)
  181. return
  182. }
  183. jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), a.apiTokenService.SetEnabled(id, form.Enabled))
  184. }
  185. func (a *SettingController) testSmtp(c *gin.Context) {
  186. if emailService == nil {
  187. jsonMsg(c, I18nWeb(c, "pages.settings.smtpNotInitialized"), errors.New("email service not available"))
  188. return
  189. }
  190. logger.Info("SMTP test: starting...")
  191. result := emailService.TestConnection()
  192. if !result.Success {
  193. logger.Warning("SMTP test failed at", result.Stage+":", result.Message)
  194. c.JSON(200, gin.H{
  195. "success": false,
  196. "stage": result.Stage,
  197. "msg": result.Message,
  198. })
  199. return
  200. }
  201. logger.Info("SMTP test: success")
  202. c.JSON(200, gin.H{
  203. "success": true,
  204. "stage": result.Stage,
  205. "msg": result.Message,
  206. })
  207. }
  208. func (a *SettingController) testTgBot(c *gin.Context) {
  209. enabled, err := a.settingService.GetTgbotEnabled()
  210. if err != nil || !enabled {
  211. jsonMsg(c, I18nWeb(c, "pages.settings.tgBotNotEnabled"), errors.New("telegram bot disabled"))
  212. return
  213. }
  214. // Import tgbot package would create a circular dependency, so we call
  215. // the test through the global function registered at startup.
  216. if testTgFunc != nil {
  217. if err := testTgFunc(); err != nil {
  218. jsonMsg(c, I18nWeb(c, "pages.settings.tgTestFailed")+": "+err.Error(), err)
  219. return
  220. }
  221. jsonMsg(c, I18nWeb(c, "pages.settings.tgTestSuccess"), nil)
  222. return
  223. }
  224. jsonMsg(c, I18nWeb(c, "pages.settings.tgBotNotRunning"), errors.New("bot not started"))
  225. }
  226. // testTgFunc is set from web layer to test Telegram sending without circular imports.
  227. var testTgFunc func() error
  228. // SetTestTgFunc registers the function used to test Telegram sending.
  229. func SetTestTgFunc(fn func() error) { testTgFunc = fn }
  230. // emailService is set from web layer.
  231. var emailService *email.EmailService
  232. // SetEmailService registers the email service for test endpoints.
  233. func SetEmailService(s *email.EmailService) { emailService = s }