custom_geo.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package controller
  2. import (
  3. "errors"
  4. "net/http"
  5. "strconv"
  6. "github.com/mhsanaei/3x-ui/v2/database/model"
  7. "github.com/mhsanaei/3x-ui/v2/logger"
  8. "github.com/mhsanaei/3x-ui/v2/web/entity"
  9. "github.com/mhsanaei/3x-ui/v2/web/service"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type CustomGeoController struct {
  13. BaseController
  14. customGeoService *service.CustomGeoService
  15. }
  16. func NewCustomGeoController(g *gin.RouterGroup, customGeo *service.CustomGeoService) *CustomGeoController {
  17. a := &CustomGeoController{customGeoService: customGeo}
  18. a.initRouter(g)
  19. return a
  20. }
  21. func (a *CustomGeoController) initRouter(g *gin.RouterGroup) {
  22. g.GET("/list", a.list)
  23. g.GET("/aliases", a.aliases)
  24. g.POST("/add", a.add)
  25. g.POST("/update/:id", a.update)
  26. g.POST("/delete/:id", a.delete)
  27. g.POST("/download/:id", a.download)
  28. g.POST("/update-all", a.updateAll)
  29. }
  30. func mapCustomGeoErr(c *gin.Context, err error) error {
  31. if err == nil {
  32. return nil
  33. }
  34. switch {
  35. case errors.Is(err, service.ErrCustomGeoInvalidType):
  36. return errors.New(I18nWeb(c, "pages.index.customGeoErrInvalidType"))
  37. case errors.Is(err, service.ErrCustomGeoAliasRequired):
  38. return errors.New(I18nWeb(c, "pages.index.customGeoErrAliasRequired"))
  39. case errors.Is(err, service.ErrCustomGeoAliasPattern):
  40. return errors.New(I18nWeb(c, "pages.index.customGeoErrAliasPattern"))
  41. case errors.Is(err, service.ErrCustomGeoAliasReserved):
  42. return errors.New(I18nWeb(c, "pages.index.customGeoErrAliasReserved"))
  43. case errors.Is(err, service.ErrCustomGeoURLRequired):
  44. return errors.New(I18nWeb(c, "pages.index.customGeoErrUrlRequired"))
  45. case errors.Is(err, service.ErrCustomGeoInvalidURL):
  46. return errors.New(I18nWeb(c, "pages.index.customGeoErrInvalidUrl"))
  47. case errors.Is(err, service.ErrCustomGeoURLScheme):
  48. return errors.New(I18nWeb(c, "pages.index.customGeoErrUrlScheme"))
  49. case errors.Is(err, service.ErrCustomGeoURLHost):
  50. return errors.New(I18nWeb(c, "pages.index.customGeoErrUrlHost"))
  51. case errors.Is(err, service.ErrCustomGeoDuplicateAlias):
  52. return errors.New(I18nWeb(c, "pages.index.customGeoErrDuplicateAlias"))
  53. case errors.Is(err, service.ErrCustomGeoNotFound):
  54. return errors.New(I18nWeb(c, "pages.index.customGeoErrNotFound"))
  55. case errors.Is(err, service.ErrCustomGeoDownload):
  56. logger.Warning("custom geo download:", err)
  57. return errors.New(I18nWeb(c, "pages.index.customGeoErrDownload"))
  58. default:
  59. return err
  60. }
  61. }
  62. func (a *CustomGeoController) list(c *gin.Context) {
  63. list, err := a.customGeoService.GetAll()
  64. if err != nil {
  65. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastList"), mapCustomGeoErr(c, err))
  66. return
  67. }
  68. jsonObj(c, list, nil)
  69. }
  70. func (a *CustomGeoController) aliases(c *gin.Context) {
  71. out, err := a.customGeoService.GetAliasesForUI()
  72. if err != nil {
  73. jsonMsg(c, I18nWeb(c, "pages.index.customGeoAliasesError"), mapCustomGeoErr(c, err))
  74. return
  75. }
  76. jsonObj(c, out, nil)
  77. }
  78. type customGeoForm struct {
  79. Type string `json:"type" form:"type"`
  80. Alias string `json:"alias" form:"alias"`
  81. Url string `json:"url" form:"url"`
  82. }
  83. func (a *CustomGeoController) add(c *gin.Context) {
  84. var form customGeoForm
  85. if err := c.ShouldBind(&form); err != nil {
  86. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastAdd"), err)
  87. return
  88. }
  89. r := &model.CustomGeoResource{
  90. Type: form.Type,
  91. Alias: form.Alias,
  92. Url: form.Url,
  93. }
  94. err := a.customGeoService.Create(r)
  95. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastAdd"), mapCustomGeoErr(c, err))
  96. }
  97. func parseCustomGeoID(c *gin.Context, idStr string) (int, bool) {
  98. id, err := strconv.Atoi(idStr)
  99. if err != nil {
  100. jsonMsg(c, I18nWeb(c, "pages.index.customGeoInvalidId"), err)
  101. return 0, false
  102. }
  103. if id <= 0 {
  104. jsonMsg(c, I18nWeb(c, "pages.index.customGeoInvalidId"), errors.New(""))
  105. return 0, false
  106. }
  107. return id, true
  108. }
  109. func (a *CustomGeoController) update(c *gin.Context) {
  110. id, ok := parseCustomGeoID(c, c.Param("id"))
  111. if !ok {
  112. return
  113. }
  114. var form customGeoForm
  115. if bindErr := c.ShouldBind(&form); bindErr != nil {
  116. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastUpdate"), bindErr)
  117. return
  118. }
  119. r := &model.CustomGeoResource{
  120. Type: form.Type,
  121. Alias: form.Alias,
  122. Url: form.Url,
  123. }
  124. err := a.customGeoService.Update(id, r)
  125. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastUpdate"), mapCustomGeoErr(c, err))
  126. }
  127. func (a *CustomGeoController) delete(c *gin.Context) {
  128. id, ok := parseCustomGeoID(c, c.Param("id"))
  129. if !ok {
  130. return
  131. }
  132. name, err := a.customGeoService.Delete(id)
  133. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastDelete", "fileName=="+name), mapCustomGeoErr(c, err))
  134. }
  135. func (a *CustomGeoController) download(c *gin.Context) {
  136. id, ok := parseCustomGeoID(c, c.Param("id"))
  137. if !ok {
  138. return
  139. }
  140. name, err := a.customGeoService.TriggerUpdate(id)
  141. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastDownload", "fileName=="+name), mapCustomGeoErr(c, err))
  142. }
  143. func (a *CustomGeoController) updateAll(c *gin.Context) {
  144. res, err := a.customGeoService.TriggerUpdateAll()
  145. if err != nil {
  146. jsonMsg(c, I18nWeb(c, "pages.index.customGeoToastUpdateAll"), mapCustomGeoErr(c, err))
  147. return
  148. }
  149. if len(res.Failed) > 0 {
  150. c.JSON(http.StatusOK, entity.Msg{
  151. Success: false,
  152. Msg: I18nWeb(c, "pages.index.customGeoErrUpdateAllIncomplete"),
  153. Obj: res,
  154. })
  155. return
  156. }
  157. jsonMsgObj(c, I18nWeb(c, "pages.index.customGeoToastUpdateAll"), res, nil)
  158. }