subController.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "net"
  5. "github.com/gin-gonic/gin"
  6. )
  7. type SUBController struct {
  8. subPath string
  9. subJsonPath string
  10. subEncrypt bool
  11. updateInterval string
  12. subService *SubService
  13. subJsonService *SubJsonService
  14. }
  15. func NewSUBController(
  16. g *gin.RouterGroup,
  17. subPath string,
  18. jsonPath string,
  19. encrypt bool,
  20. showInfo bool,
  21. rModel string,
  22. update string,
  23. jsonFragment string,
  24. jsonMux string,
  25. jsonRules string,
  26. ) *SUBController {
  27. sub := NewSubService(showInfo, rModel)
  28. a := &SUBController{
  29. subPath: subPath,
  30. subJsonPath: jsonPath,
  31. subEncrypt: encrypt,
  32. updateInterval: update,
  33. subService: sub,
  34. subJsonService: NewSubJsonService(jsonFragment, jsonMux, jsonRules, sub),
  35. }
  36. a.initRouter(g)
  37. return a
  38. }
  39. func (a *SUBController) initRouter(g *gin.RouterGroup) {
  40. gLink := g.Group(a.subPath)
  41. gJson := g.Group(a.subJsonPath)
  42. gLink.GET(":subid", a.subs)
  43. gJson.GET(":subid", a.subJsons)
  44. }
  45. func (a *SUBController) subs(c *gin.Context) {
  46. subId := c.Param("subid")
  47. host := c.GetHeader("X-Forwarded-Host")
  48. if host == "" {
  49. host = c.GetHeader("X-Real-IP")
  50. }
  51. if host == "" {
  52. var err error
  53. host, _, err = net.SplitHostPort(c.Request.Host)
  54. if err != nil {
  55. host = c.Request.Host
  56. }
  57. }
  58. subs, header, err := a.subService.GetSubs(subId, host)
  59. if err != nil || len(subs) == 0 {
  60. c.String(400, "Error!")
  61. } else {
  62. result := ""
  63. for _, sub := range subs {
  64. result += sub + "\n"
  65. }
  66. // Add headers
  67. c.Writer.Header().Set("Subscription-Userinfo", header)
  68. c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
  69. c.Writer.Header().Set("Profile-Title", subId)
  70. if a.subEncrypt {
  71. c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
  72. } else {
  73. c.String(200, result)
  74. }
  75. }
  76. }
  77. func (a *SUBController) subJsons(c *gin.Context) {
  78. subId := c.Param("subid")
  79. host := c.GetHeader("X-Forwarded-Host")
  80. if host == "" {
  81. host = c.GetHeader("X-Real-IP")
  82. }
  83. if host == "" {
  84. var err error
  85. host, _, err = net.SplitHostPort(c.Request.Host)
  86. if err != nil {
  87. host = c.Request.Host
  88. }
  89. }
  90. jsonSub, header, err := a.subJsonService.GetJson(subId, host)
  91. if err != nil || len(jsonSub) == 0 {
  92. c.String(400, "Error!")
  93. } else {
  94. // Add headers
  95. c.Writer.Header().Set("Subscription-Userinfo", header)
  96. c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
  97. c.Writer.Header().Set("Profile-Title", subId)
  98. c.String(200, jsonSub)
  99. }
  100. }