1
0

subController.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. host = host
  59. subs, header, err := a.subService.GetSubs(subId, host)
  60. if err != nil || len(subs) == 0 {
  61. c.String(400, "Error!")
  62. } else {
  63. result := ""
  64. for _, sub := range subs {
  65. result += sub + "\n"
  66. }
  67. // Add headers
  68. c.Writer.Header().Set("Subscription-Userinfo", header)
  69. c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
  70. c.Writer.Header().Set("Profile-Title", subId)
  71. if a.subEncrypt {
  72. c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
  73. } else {
  74. c.String(200, result)
  75. }
  76. }
  77. }
  78. func (a *SUBController) subJsons(c *gin.Context) {
  79. subId := c.Param("subid")
  80. host := c.GetHeader("X-Forwarded-Host")
  81. if host == "" {
  82. host = c.GetHeader("X-Real-IP")
  83. }
  84. if host == "" {
  85. var err error
  86. host, _, err = net.SplitHostPort(c.Request.Host)
  87. if err != nil {
  88. host = c.Request.Host
  89. }
  90. }
  91. host = host
  92. jsonSub, header, err := a.subJsonService.GetJson(subId, host)
  93. if err != nil || len(jsonSub) == 0 {
  94. c.String(400, "Error!")
  95. } else {
  96. // Add headers
  97. c.Writer.Header().Set("Subscription-Userinfo", header)
  98. c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
  99. c.Writer.Header().Set("Profile-Title", subId)
  100. c.String(200, jsonSub)
  101. }
  102. }