subController.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "strings"
  5. "github.com/gin-gonic/gin"
  6. )
  7. type SUBController struct {
  8. subTitle string
  9. subPath string
  10. subJsonPath string
  11. subEncrypt bool
  12. updateInterval string
  13. subService *SubService
  14. subJsonService *SubJsonService
  15. }
  16. func NewSUBController(
  17. g *gin.RouterGroup,
  18. subPath string,
  19. jsonPath string,
  20. encrypt bool,
  21. showInfo bool,
  22. rModel string,
  23. update string,
  24. jsonFragment string,
  25. jsonNoise string,
  26. jsonMux string,
  27. jsonRules string,
  28. subTitle string,
  29. ) *SUBController {
  30. sub := NewSubService(showInfo, rModel)
  31. a := &SUBController{
  32. subTitle: subTitle,
  33. subPath: subPath,
  34. subJsonPath: jsonPath,
  35. subEncrypt: encrypt,
  36. updateInterval: update,
  37. subService: sub,
  38. subJsonService: NewSubJsonService(jsonFragment, jsonNoise, jsonMux, jsonRules, sub),
  39. }
  40. a.initRouter(g)
  41. return a
  42. }
  43. func (a *SUBController) initRouter(g *gin.RouterGroup) {
  44. gLink := g.Group(a.subPath)
  45. gJson := g.Group(a.subJsonPath)
  46. gLink.GET(":subid", a.subs)
  47. gJson.GET(":subid", a.subJsons)
  48. }
  49. func (a *SUBController) subs(c *gin.Context) {
  50. subId := c.Param("subid")
  51. scheme, host, hostWithPort, hostHeader := a.subService.ResolveRequest(c)
  52. subs, header, lastOnline, err := a.subService.GetSubs(subId, host)
  53. if err != nil || len(subs) == 0 {
  54. c.String(400, "Error!")
  55. } else {
  56. result := ""
  57. for _, sub := range subs {
  58. result += sub + "\n"
  59. }
  60. // Add headers via service
  61. a.subService.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle)
  62. a.subService.ApplyBase64ContentHeader(c, result)
  63. // If the request expects HTML (e.g., browser) or explicitly asked (?html=1 or ?view=html), render the info page here
  64. accept := c.GetHeader("Accept")
  65. if strings.Contains(strings.ToLower(accept), "text/html") || c.Query("html") == "1" || strings.EqualFold(c.Query("view"), "html") {
  66. // Build page data in service
  67. subURL, subJsonURL := a.subService.BuildURLs(scheme, hostWithPort, a.subPath, a.subJsonPath, subId)
  68. page := a.subService.BuildPageData(subId, hostHeader, header, lastOnline, subs, subURL, subJsonURL)
  69. c.HTML(200, "subscription.html", gin.H{
  70. "title": "subscription.title",
  71. "host": page.Host,
  72. "base_path": page.BasePath,
  73. "sId": page.SId,
  74. "download": page.Download,
  75. "upload": page.Upload,
  76. "total": page.Total,
  77. "used": page.Used,
  78. "remained": page.Remained,
  79. "expire": page.Expire,
  80. "lastOnline": page.LastOnline,
  81. "datepicker": page.Datepicker,
  82. "downloadByte": page.DownloadByte,
  83. "uploadByte": page.UploadByte,
  84. "totalByte": page.TotalByte,
  85. "subUrl": page.SubUrl,
  86. "subJsonUrl": page.SubJsonUrl,
  87. "result": page.Result,
  88. })
  89. return
  90. }
  91. if a.subEncrypt {
  92. c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
  93. } else {
  94. c.String(200, result)
  95. }
  96. }
  97. }
  98. func (a *SUBController) subJsons(c *gin.Context) {
  99. subId := c.Param("subid")
  100. _, host, _, _ := a.subService.ResolveRequest(c)
  101. jsonSub, header, err := a.subJsonService.GetJson(subId, host)
  102. if err != nil || len(jsonSub) == 0 {
  103. c.String(400, "Error!")
  104. } else {
  105. // Add headers via service
  106. a.subService.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle)
  107. c.String(200, jsonSub)
  108. }
  109. }
  110. // Note: host parsing and page data preparation moved to SubService