1
0

subController.go 2.7 KB

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