subController.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "net"
  5. "strings"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type SUBController struct {
  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. ) *SUBController {
  29. sub := NewSubService(showInfo, rModel)
  30. a := &SUBController{
  31. subPath: subPath,
  32. subJsonPath: jsonPath,
  33. subEncrypt: encrypt,
  34. updateInterval: update,
  35. subService: sub,
  36. subJsonService: NewSubJsonService(jsonFragment, jsonNoise, jsonMux, jsonRules, sub),
  37. }
  38. a.initRouter(g)
  39. return a
  40. }
  41. func (a *SUBController) initRouter(g *gin.RouterGroup) {
  42. gLink := g.Group(a.subPath)
  43. gJson := g.Group(a.subJsonPath)
  44. gLink.GET(":subid", a.subs)
  45. gJson.GET(":subid", a.subJsons)
  46. }
  47. func (a *SUBController) subs(c *gin.Context) {
  48. subId := c.Param("subid")
  49. var host string
  50. if h, err := getHostFromXFH(c.GetHeader("X-Forwarded-Host")); err == nil {
  51. host = h
  52. }
  53. if host == "" {
  54. host = c.GetHeader("X-Real-IP")
  55. }
  56. if host == "" {
  57. var err error
  58. host, _, err = net.SplitHostPort(c.Request.Host)
  59. if err != nil {
  60. host = c.Request.Host
  61. }
  62. }
  63. subs, header, err := a.subService.GetSubs(subId, host)
  64. if err != nil || len(subs) == 0 {
  65. c.String(400, "Error!")
  66. } else {
  67. result := ""
  68. for _, sub := range subs {
  69. result += sub + "\n"
  70. }
  71. // Add headers
  72. c.Writer.Header().Set("Subscription-Userinfo", header)
  73. c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
  74. c.Writer.Header().Set("Profile-Title", subId)
  75. if a.subEncrypt {
  76. c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
  77. } else {
  78. c.String(200, result)
  79. }
  80. }
  81. }
  82. func (a *SUBController) subJsons(c *gin.Context) {
  83. subId := c.Param("subid")
  84. var host string
  85. if h, err := getHostFromXFH(c.GetHeader("X-Forwarded-Host")); err == nil {
  86. host = h
  87. }
  88. if host == "" {
  89. host = c.GetHeader("X-Real-IP")
  90. }
  91. if host == "" {
  92. var err error
  93. host, _, err = net.SplitHostPort(c.Request.Host)
  94. if err != nil {
  95. host = c.Request.Host
  96. }
  97. }
  98. jsonSub, header, err := a.subJsonService.GetJson(subId, host)
  99. if err != nil || len(jsonSub) == 0 {
  100. c.String(400, "Error!")
  101. } else {
  102. // Add headers
  103. c.Writer.Header().Set("Subscription-Userinfo", header)
  104. c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
  105. c.Writer.Header().Set("Profile-Title", subId)
  106. c.String(200, jsonSub)
  107. }
  108. }
  109. func getHostFromXFH(s string) (string, error) {
  110. if strings.Contains(s, ":") {
  111. realHost, _, err := net.SplitHostPort(s)
  112. if err != nil {
  113. return "", err
  114. }
  115. return realHost, nil
  116. }
  117. return s, nil
  118. }