1
0

subController.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package sub
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "github.com/mhsanaei/3x-ui/v3/web/service"
  13. "github.com/gin-gonic/gin"
  14. )
  15. // writeSubError translates a service-layer result into an HTTP response.
  16. // A nil error with no rows means the subId doesn't match anything (deleted
  17. // client, never-existed id) and becomes 404. A real error becomes 500. No
  18. // body — VPN clients only look at the status.
  19. func writeSubError(c *gin.Context, err error) {
  20. if err == nil {
  21. c.Status(http.StatusNotFound)
  22. return
  23. }
  24. c.Status(http.StatusInternalServerError)
  25. }
  26. // SUBController handles HTTP requests for subscription links and JSON configurations.
  27. type SUBController struct {
  28. subTitle string
  29. subSupportUrl string
  30. subProfileUrl string
  31. subAnnounce string
  32. subEnableRouting bool
  33. subRoutingRules string
  34. subPath string
  35. subJsonPath string
  36. subClashPath string
  37. jsonEnabled bool
  38. clashEnabled bool
  39. subEncrypt bool
  40. updateInterval string
  41. subService *SubService
  42. subJsonService *SubJsonService
  43. subClashService *SubClashService
  44. settingService service.SettingService
  45. }
  46. // NewSUBController creates a new subscription controller with the given configuration.
  47. func NewSUBController(
  48. g *gin.RouterGroup,
  49. subPath string,
  50. jsonPath string,
  51. clashPath string,
  52. jsonEnabled bool,
  53. clashEnabled bool,
  54. encrypt bool,
  55. showInfo bool,
  56. rModel string,
  57. update string,
  58. jsonFragment string,
  59. jsonNoise string,
  60. jsonMux string,
  61. jsonRules string,
  62. clashEnableRouting bool,
  63. clashRules string,
  64. subTitle string,
  65. subSupportUrl string,
  66. subProfileUrl string,
  67. subAnnounce string,
  68. subEnableRouting bool,
  69. subRoutingRules string,
  70. ) *SUBController {
  71. sub := NewSubService(showInfo, rModel)
  72. a := &SUBController{
  73. subTitle: subTitle,
  74. subSupportUrl: subSupportUrl,
  75. subProfileUrl: subProfileUrl,
  76. subAnnounce: subAnnounce,
  77. subEnableRouting: subEnableRouting,
  78. subRoutingRules: subRoutingRules,
  79. subPath: subPath,
  80. subJsonPath: jsonPath,
  81. subClashPath: clashPath,
  82. jsonEnabled: jsonEnabled,
  83. clashEnabled: clashEnabled,
  84. subEncrypt: encrypt,
  85. updateInterval: update,
  86. subService: sub,
  87. subJsonService: NewSubJsonService(jsonFragment, jsonNoise, jsonMux, jsonRules, sub),
  88. subClashService: NewSubClashService(clashEnableRouting, clashRules, sub),
  89. }
  90. a.initRouter(g)
  91. return a
  92. }
  93. // initRouter registers HTTP routes for subscription links and JSON endpoints
  94. // on the provided router group.
  95. func (a *SUBController) initRouter(g *gin.RouterGroup) {
  96. gLink := g.Group(a.subPath)
  97. gLink.GET(":subid", a.subs)
  98. gLink.HEAD(":subid", a.subs)
  99. if a.jsonEnabled {
  100. gJson := g.Group(a.subJsonPath)
  101. gJson.GET(":subid", a.subJsons)
  102. gJson.HEAD(":subid", a.subJsons)
  103. }
  104. if a.clashEnabled {
  105. gClash := g.Group(a.subClashPath)
  106. gClash.GET(":subid", a.subClashs)
  107. gClash.HEAD(":subid", a.subClashs)
  108. }
  109. }
  110. // subs handles HTTP requests for subscription links, returning either HTML page or base64-encoded subscription data.
  111. func (a *SUBController) subs(c *gin.Context) {
  112. subId := c.Param("subid")
  113. scheme, host, hostWithPort, hostHeader := a.subService.ResolveRequest(c)
  114. subs, emails, lastOnline, traffic, err := a.subService.GetSubs(subId, host)
  115. if err != nil || len(subs) == 0 {
  116. writeSubError(c, err)
  117. } else {
  118. result := ""
  119. for _, sub := range subs {
  120. result += sub + "\n"
  121. }
  122. // If the request expects HTML (e.g., browser) or explicitly asked (?html=1 or ?view=html), render the info page here
  123. accept := c.GetHeader("Accept")
  124. if strings.Contains(strings.ToLower(accept), "text/html") || c.Query("html") == "1" || strings.EqualFold(c.Query("view"), "html") {
  125. subURL, subJsonURL, subClashURL := a.subService.BuildURLs(a.subPath, a.subJsonPath, a.subClashPath, subId)
  126. if !a.jsonEnabled {
  127. subJsonURL = ""
  128. }
  129. if !a.clashEnabled {
  130. subClashURL = ""
  131. }
  132. basePath, exists := c.Get("base_path")
  133. if !exists {
  134. basePath = "/"
  135. }
  136. basePathStr := basePath.(string)
  137. page := a.subService.BuildPageData(subId, hostHeader, traffic, lastOnline, subs, emails, subURL, subJsonURL, subClashURL, basePathStr, a.subTitle, a.subSupportUrl)
  138. a.serveSubPage(c, basePathStr, page)
  139. return
  140. }
  141. // Add headers
  142. header := fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
  143. profileUrl := a.subProfileUrl
  144. if profileUrl == "" {
  145. profileUrl = fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
  146. }
  147. a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle, a.subSupportUrl, profileUrl, a.subAnnounce, a.subEnableRouting, a.subRoutingRules)
  148. if a.subEncrypt {
  149. c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
  150. } else {
  151. c.String(200, result)
  152. }
  153. }
  154. }
  155. // serveSubPage renders web/dist/subpage.html for the current subscription
  156. // request. The Vite-built SPA reads window.__SUB_PAGE_DATA__ on mount —
  157. // we inject that here, along with window.X_UI_BASE_PATH so the
  158. // page's static asset references resolve correctly when the panel runs
  159. // behind a URL prefix.
  160. func (a *SUBController) serveSubPage(c *gin.Context, basePath string, page PageData) {
  161. var body []byte
  162. if diskBody, diskErr := os.ReadFile("web/dist/subpage.html"); diskErr == nil {
  163. body = diskBody
  164. } else {
  165. readBody, err := distFS.ReadFile("dist/subpage.html")
  166. if err != nil {
  167. c.String(http.StatusInternalServerError, "missing embedded subpage")
  168. return
  169. }
  170. body = readBody
  171. }
  172. // Vite emits absolute asset URLs (`/assets/...`); when the panel is
  173. // installed under a custom URL prefix, rewrite them so the bundle
  174. // loads from `<basePath>assets/...` where the static handler is
  175. // actually mounted.
  176. if basePath != "/" && basePath != "" {
  177. body = bytes.ReplaceAll(body, []byte(`src="/assets/`), []byte(`src="`+basePath+`assets/`))
  178. body = bytes.ReplaceAll(body, []byte(`href="/assets/`), []byte(`href="`+basePath+`assets/`))
  179. }
  180. // JSON-marshal the view-model so the SPA can read it as a plain
  181. // The panel's "Calendar Type" setting decides whether the SubPage
  182. // renders dates in Gregorian or Jalali — surface it here so the SPA
  183. // can match the rest of the panel without a round-trip.
  184. datepicker, _ := a.settingService.GetDatepicker()
  185. if datepicker == "" {
  186. datepicker = "gregorian"
  187. }
  188. subData := map[string]any{
  189. "sId": page.SId,
  190. "enabled": page.Enabled,
  191. "download": page.Download,
  192. "upload": page.Upload,
  193. "total": page.Total,
  194. "used": page.Used,
  195. "remained": page.Remained,
  196. "expire": page.Expire,
  197. "lastOnline": page.LastOnline,
  198. "downloadByte": page.DownloadByte,
  199. "uploadByte": page.UploadByte,
  200. "totalByte": page.TotalByte,
  201. "subUrl": page.SubUrl,
  202. "subJsonUrl": page.SubJsonUrl,
  203. "subClashUrl": page.SubClashUrl,
  204. "links": page.Result,
  205. "emails": page.Emails,
  206. "datepicker": datepicker,
  207. }
  208. subDataJSON, err := json.Marshal(subData)
  209. if err != nil {
  210. subDataJSON = []byte("{}")
  211. }
  212. // Defense-in-depth string-escape for the basePath embed — admin-
  213. // controlled but cheap to harden.
  214. jsEscape := strings.NewReplacer(
  215. `\`, `\\`,
  216. `"`, `\"`,
  217. "\n", `\n`,
  218. "\r", `\r`,
  219. "<", `<`,
  220. ">", `>`,
  221. "&", `&`,
  222. )
  223. escapedBase := jsEscape.Replace(basePath)
  224. inject := []byte(`<script>window.X_UI_BASE_PATH="` + escapedBase + `";` +
  225. `window.__SUB_PAGE_DATA__=` + string(subDataJSON) + `;</script></head>`)
  226. out := bytes.Replace(body, []byte("</head>"), inject, 1)
  227. c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
  228. c.Header("Pragma", "no-cache")
  229. c.Header("Expires", "0")
  230. c.Data(http.StatusOK, "text/html; charset=utf-8", out)
  231. }
  232. // subJsons handles HTTP requests for JSON subscription configurations.
  233. func (a *SUBController) subJsons(c *gin.Context) {
  234. subId := c.Param("subid")
  235. scheme, host, hostWithPort, _ := a.subService.ResolveRequest(c)
  236. jsonSub, header, err := a.subJsonService.GetJson(subId, host)
  237. if err != nil || len(jsonSub) == 0 {
  238. writeSubError(c, err)
  239. } else {
  240. profileUrl := a.subProfileUrl
  241. if profileUrl == "" {
  242. profileUrl = fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
  243. }
  244. a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle, a.subSupportUrl, profileUrl, a.subAnnounce, a.subEnableRouting, a.subRoutingRules)
  245. c.String(200, jsonSub)
  246. }
  247. }
  248. func (a *SUBController) subClashs(c *gin.Context) {
  249. subId := c.Param("subid")
  250. scheme, host, hostWithPort, _ := a.subService.ResolveRequest(c)
  251. clashSub, header, err := a.subClashService.GetClash(subId, host)
  252. if err != nil || len(clashSub) == 0 {
  253. writeSubError(c, err)
  254. } else {
  255. profileUrl := a.subProfileUrl
  256. if profileUrl == "" {
  257. profileUrl = fmt.Sprintf("%s://%s%s", scheme, hostWithPort, c.Request.RequestURI)
  258. }
  259. a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle, a.subSupportUrl, profileUrl, a.subAnnounce, a.subEnableRouting, a.subRoutingRules)
  260. if a.subTitle != "" {
  261. // Clash clients commonly use Content-Disposition to choose the imported profile name.
  262. c.Writer.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename*=UTF-8''%s`, url.PathEscape(a.subTitle)))
  263. }
  264. c.Data(200, "application/yaml; charset=utf-8", []byte(clashSub))
  265. }
  266. }
  267. // ApplyCommonHeaders sets common HTTP headers for subscription responses including user info, update interval, and profile title.
  268. func (a *SUBController) ApplyCommonHeaders(
  269. c *gin.Context,
  270. header,
  271. updateInterval,
  272. profileTitle string,
  273. profileSupportUrl string,
  274. profileUrl string,
  275. profileAnnounce string,
  276. profileEnableRouting bool,
  277. profileRoutingRules string,
  278. ) {
  279. c.Writer.Header().Set("Subscription-Userinfo", header)
  280. c.Writer.Header().Set("Profile-Update-Interval", updateInterval)
  281. //Basics
  282. if profileTitle != "" {
  283. c.Writer.Header().Set("Profile-Title", "base64:"+base64.StdEncoding.EncodeToString([]byte(profileTitle)))
  284. }
  285. if profileSupportUrl != "" {
  286. c.Writer.Header().Set("Support-Url", profileSupportUrl)
  287. }
  288. if profileUrl != "" {
  289. c.Writer.Header().Set("Profile-Web-Page-Url", profileUrl)
  290. }
  291. if profileAnnounce != "" {
  292. c.Writer.Header().Set("Announce", "base64:"+base64.StdEncoding.EncodeToString([]byte(profileAnnounce)))
  293. }
  294. //Advanced (Happ)
  295. c.Writer.Header().Set("Routing-Enable", strconv.FormatBool(profileEnableRouting))
  296. if profileRoutingRules != "" {
  297. c.Writer.Header().Set("Routing", profileRoutingRules)
  298. }
  299. }