subController.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "github.com/mhsanaei/3x-ui/v2/config"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // SUBController handles HTTP requests for subscription links and JSON configurations.
  11. type SUBController struct {
  12. subTitle string
  13. subSupportUrl string
  14. subProfileUrl string
  15. subAnnounce string
  16. subEnableRouting bool
  17. subRoutingRules string
  18. subPath string
  19. subJsonPath string
  20. subClashPath string
  21. jsonEnabled bool
  22. clashEnabled bool
  23. subEncrypt bool
  24. updateInterval string
  25. subService *SubService
  26. subJsonService *SubJsonService
  27. subClashService *SubClashService
  28. }
  29. // NewSUBController creates a new subscription controller with the given configuration.
  30. func NewSUBController(
  31. g *gin.RouterGroup,
  32. subPath string,
  33. jsonPath string,
  34. clashPath string,
  35. jsonEnabled bool,
  36. clashEnabled bool,
  37. encrypt bool,
  38. showInfo bool,
  39. rModel string,
  40. update string,
  41. jsonFragment string,
  42. jsonNoise string,
  43. jsonMux string,
  44. jsonRules string,
  45. subTitle string,
  46. subSupportUrl string,
  47. subProfileUrl string,
  48. subAnnounce string,
  49. subEnableRouting bool,
  50. subRoutingRules string,
  51. ) *SUBController {
  52. sub := NewSubService(showInfo, rModel)
  53. a := &SUBController{
  54. subTitle: subTitle,
  55. subSupportUrl: subSupportUrl,
  56. subProfileUrl: subProfileUrl,
  57. subAnnounce: subAnnounce,
  58. subEnableRouting: subEnableRouting,
  59. subRoutingRules: subRoutingRules,
  60. subPath: subPath,
  61. subJsonPath: jsonPath,
  62. subClashPath: clashPath,
  63. jsonEnabled: jsonEnabled,
  64. clashEnabled: clashEnabled,
  65. subEncrypt: encrypt,
  66. updateInterval: update,
  67. subService: sub,
  68. subJsonService: NewSubJsonService(jsonFragment, jsonNoise, jsonMux, jsonRules, sub),
  69. subClashService: NewSubClashService(sub),
  70. }
  71. a.initRouter(g)
  72. return a
  73. }
  74. // initRouter registers HTTP routes for subscription links and JSON endpoints
  75. // on the provided router group.
  76. func (a *SUBController) initRouter(g *gin.RouterGroup) {
  77. gLink := g.Group(a.subPath)
  78. gLink.GET(":subid", a.subs)
  79. if a.jsonEnabled {
  80. gJson := g.Group(a.subJsonPath)
  81. gJson.GET(":subid", a.subJsons)
  82. }
  83. if a.clashEnabled {
  84. gClash := g.Group(a.subClashPath)
  85. gClash.GET(":subid", a.subClashs)
  86. }
  87. }
  88. // subs handles HTTP requests for subscription links, returning either HTML page or base64-encoded subscription data.
  89. func (a *SUBController) subs(c *gin.Context) {
  90. subId := c.Param("subid")
  91. scheme, host, hostWithPort, hostHeader := a.subService.ResolveRequest(c)
  92. subs, lastOnline, traffic, err := a.subService.GetSubs(subId, host)
  93. if err != nil || len(subs) == 0 {
  94. c.String(400, "Error!")
  95. } else {
  96. result := ""
  97. for _, sub := range subs {
  98. result += sub + "\n"
  99. }
  100. // If the request expects HTML (e.g., browser) or explicitly asked (?html=1 or ?view=html), render the info page here
  101. accept := c.GetHeader("Accept")
  102. if strings.Contains(strings.ToLower(accept), "text/html") || c.Query("html") == "1" || strings.EqualFold(c.Query("view"), "html") {
  103. // Build page data in service
  104. subURL, subJsonURL, subClashURL := a.subService.BuildURLs(scheme, hostWithPort, a.subPath, a.subJsonPath, a.subClashPath, subId)
  105. if !a.jsonEnabled {
  106. subJsonURL = ""
  107. }
  108. if !a.clashEnabled {
  109. subClashURL = ""
  110. }
  111. // Get base_path from context (set by middleware)
  112. basePath, exists := c.Get("base_path")
  113. if !exists {
  114. basePath = "/"
  115. }
  116. // Add subId to base_path for asset URLs
  117. basePathStr := basePath.(string)
  118. if basePathStr == "/" {
  119. basePathStr = "/" + subId + "/"
  120. } else {
  121. // Remove trailing slash if exists, add subId, then add trailing slash
  122. basePathStr = strings.TrimRight(basePathStr, "/") + "/" + subId + "/"
  123. }
  124. page := a.subService.BuildPageData(subId, hostHeader, traffic, lastOnline, subs, subURL, subJsonURL, subClashURL, basePathStr)
  125. c.HTML(200, "subpage.html", gin.H{
  126. "title": "subscription.title",
  127. "cur_ver": config.GetVersion(),
  128. "host": page.Host,
  129. "base_path": page.BasePath,
  130. "sId": page.SId,
  131. "download": page.Download,
  132. "upload": page.Upload,
  133. "total": page.Total,
  134. "used": page.Used,
  135. "remained": page.Remained,
  136. "expire": page.Expire,
  137. "lastOnline": page.LastOnline,
  138. "datepicker": page.Datepicker,
  139. "downloadByte": page.DownloadByte,
  140. "uploadByte": page.UploadByte,
  141. "totalByte": page.TotalByte,
  142. "subUrl": page.SubUrl,
  143. "subJsonUrl": page.SubJsonUrl,
  144. "subClashUrl": page.SubClashUrl,
  145. "result": page.Result,
  146. })
  147. return
  148. }
  149. // Add headers
  150. header := fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
  151. profileUrl := a.subProfileUrl
  152. if profileUrl == "" {
  153. profileUrl = fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
  154. }
  155. a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle, a.subSupportUrl, profileUrl, a.subAnnounce, a.subEnableRouting, a.subRoutingRules)
  156. if a.subEncrypt {
  157. c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
  158. } else {
  159. c.String(200, result)
  160. }
  161. }
  162. }
  163. // subJsons handles HTTP requests for JSON subscription configurations.
  164. func (a *SUBController) subJsons(c *gin.Context) {
  165. subId := c.Param("subid")
  166. scheme, host, hostWithPort, _ := a.subService.ResolveRequest(c)
  167. jsonSub, header, err := a.subJsonService.GetJson(subId, host)
  168. if err != nil || len(jsonSub) == 0 {
  169. c.String(400, "Error!")
  170. } else {
  171. profileUrl := a.subProfileUrl
  172. if profileUrl == "" {
  173. profileUrl = fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
  174. }
  175. a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle, a.subSupportUrl, profileUrl, a.subAnnounce, a.subEnableRouting, a.subRoutingRules)
  176. c.String(200, jsonSub)
  177. }
  178. }
  179. func (a *SUBController) subClashs(c *gin.Context) {
  180. subId := c.Param("subid")
  181. scheme, host, hostWithPort, _ := a.subService.ResolveRequest(c)
  182. clashSub, header, err := a.subClashService.GetClash(subId, host)
  183. if err != nil || len(clashSub) == 0 {
  184. c.String(400, "Error!")
  185. } else {
  186. profileUrl := a.subProfileUrl
  187. if profileUrl == "" {
  188. profileUrl = fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
  189. }
  190. a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle, a.subSupportUrl, profileUrl, a.subAnnounce, a.subEnableRouting, a.subRoutingRules)
  191. c.Data(200, "application/yaml; charset=utf-8", []byte(clashSub))
  192. }
  193. }
  194. // ApplyCommonHeaders sets common HTTP headers for subscription responses including user info, update interval, and profile title.
  195. func (a *SUBController) ApplyCommonHeaders(
  196. c *gin.Context,
  197. header,
  198. updateInterval,
  199. profileTitle string,
  200. profileSupportUrl string,
  201. profileUrl string,
  202. profileAnnounce string,
  203. profileEnableRouting bool,
  204. profileRoutingRules string,
  205. ) {
  206. c.Writer.Header().Set("Subscription-Userinfo", header)
  207. c.Writer.Header().Set("Profile-Update-Interval", updateInterval)
  208. //Basics
  209. if profileTitle != "" {
  210. c.Writer.Header().Set("Profile-Title", "base64:"+base64.StdEncoding.EncodeToString([]byte(profileTitle)))
  211. }
  212. if profileSupportUrl != "" {
  213. c.Writer.Header().Set("Support-Url", profileSupportUrl)
  214. }
  215. if profileUrl != "" {
  216. c.Writer.Header().Set("Profile-Web-Page-Url", profileUrl)
  217. }
  218. if profileAnnounce != "" {
  219. c.Writer.Header().Set("Announce", "base64:"+base64.StdEncoding.EncodeToString([]byte(profileAnnounce)))
  220. }
  221. //Advanced (Happ)
  222. c.Writer.Header().Set("Routing-Enable", strconv.FormatBool(profileEnableRouting))
  223. if profileRoutingRules != "" {
  224. c.Writer.Header().Set("Routing", profileRoutingRules)
  225. }
  226. }