From 10025ffa66c011fd756af780772260d833460795 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sun, 14 Sep 2025 01:22:42 +0200 Subject: Subscription --- sub/subController.go | 86 +++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 48 deletions(-) (limited to 'sub/subController.go') diff --git a/sub/subController.go b/sub/subController.go index 3f053740..ac0c09a1 100644 --- a/sub/subController.go +++ b/sub/subController.go @@ -2,7 +2,6 @@ package sub import ( "encoding/base64" - "net" "strings" "github.com/gin-gonic/gin" @@ -58,21 +57,8 @@ func (a *SUBController) initRouter(g *gin.RouterGroup) { func (a *SUBController) subs(c *gin.Context) { subId := c.Param("subid") - var host string - if h, err := getHostFromXFH(c.GetHeader("X-Forwarded-Host")); err == nil { - host = h - } - if host == "" { - host = c.GetHeader("X-Real-IP") - } - if host == "" { - var err error - host, _, err = net.SplitHostPort(c.Request.Host) - if err != nil { - host = c.Request.Host - } - } - subs, header, err := a.subService.GetSubs(subId, host) + scheme, host, hostWithPort, hostHeader := a.subService.ResolveRequest(c) + subs, header, lastOnline, err := a.subService.GetSubs(subId, host) if err != nil || len(subs) == 0 { c.String(400, "Error!") } else { @@ -81,10 +67,38 @@ func (a *SUBController) subs(c *gin.Context) { result += sub + "\n" } - // Add headers - c.Writer.Header().Set("Subscription-Userinfo", header) - c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval) - c.Writer.Header().Set("Profile-Title", "base64:"+base64.StdEncoding.EncodeToString([]byte(a.subTitle))) + // Add headers via service + a.subService.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle) + a.subService.ApplyBase64ContentHeader(c, result) + + // If the request expects HTML (e.g., browser) or explicitly asked (?html=1 or ?view=html), render the info page here + accept := c.GetHeader("Accept") + if strings.Contains(strings.ToLower(accept), "text/html") || c.Query("html") == "1" || strings.EqualFold(c.Query("view"), "html") { + // Build page data in service + subURL, subJsonURL := a.subService.BuildURLs(scheme, hostWithPort, a.subPath, a.subJsonPath, subId) + page := a.subService.BuildPageData(subId, hostHeader, header, lastOnline, subs, subURL, subJsonURL) + c.HTML(200, "subscription.html", gin.H{ + "title": "subscription.title", + "host": page.Host, + "base_path": page.BasePath, + "sId": page.SId, + "download": page.Download, + "upload": page.Upload, + "total": page.Total, + "used": page.Used, + "remained": page.Remained, + "expire": page.Expire, + "lastOnline": page.LastOnline, + "datepicker": page.Datepicker, + "downloadByte": page.DownloadByte, + "uploadByte": page.UploadByte, + "totalByte": page.TotalByte, + "subUrl": page.SubUrl, + "subJsonUrl": page.SubJsonUrl, + "result": page.Result, + }) + return + } if a.subEncrypt { c.String(200, base64.StdEncoding.EncodeToString([]byte(result))) @@ -96,41 +110,17 @@ func (a *SUBController) subs(c *gin.Context) { func (a *SUBController) subJsons(c *gin.Context) { subId := c.Param("subid") - var host string - if h, err := getHostFromXFH(c.GetHeader("X-Forwarded-Host")); err == nil { - host = h - } - if host == "" { - host = c.GetHeader("X-Real-IP") - } - if host == "" { - var err error - host, _, err = net.SplitHostPort(c.Request.Host) - if err != nil { - host = c.Request.Host - } - } + _, host, _, _ := a.subService.ResolveRequest(c) jsonSub, header, err := a.subJsonService.GetJson(subId, host) if err != nil || len(jsonSub) == 0 { c.String(400, "Error!") } else { - // Add headers - c.Writer.Header().Set("Subscription-Userinfo", header) - c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval) - c.Writer.Header().Set("Profile-Title", "base64:"+base64.StdEncoding.EncodeToString([]byte(a.subTitle))) + // Add headers via service + a.subService.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle) c.String(200, jsonSub) } } -func getHostFromXFH(s string) (string, error) { - if strings.Contains(s, ":") { - realHost, _, err := net.SplitHostPort(s) - if err != nil { - return "", err - } - return realHost, nil - } - return s, nil -} +// Note: host parsing and page data preparation moved to SubService -- cgit v1.2.3 From 3ac1d7f5460b4e89d8057c27d7772bd34519269d Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Sun, 14 Sep 2025 19:44:26 +0200 Subject: enhancements --- sub/subController.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'sub/subController.go') diff --git a/sub/subController.go b/sub/subController.go index ac0c09a1..c37ff5a9 100644 --- a/sub/subController.go +++ b/sub/subController.go @@ -3,6 +3,7 @@ package sub import ( "encoding/base64" "strings" + "x-ui/config" "github.com/gin-gonic/gin" ) @@ -67,10 +68,6 @@ func (a *SUBController) subs(c *gin.Context) { result += sub + "\n" } - // Add headers via service - a.subService.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle) - a.subService.ApplyBase64ContentHeader(c, result) - // If the request expects HTML (e.g., browser) or explicitly asked (?html=1 or ?view=html), render the info page here accept := c.GetHeader("Accept") if strings.Contains(strings.ToLower(accept), "text/html") || c.Query("html") == "1" || strings.EqualFold(c.Query("view"), "html") { @@ -79,6 +76,7 @@ func (a *SUBController) subs(c *gin.Context) { page := a.subService.BuildPageData(subId, hostHeader, header, lastOnline, subs, subURL, subJsonURL) c.HTML(200, "subscription.html", gin.H{ "title": "subscription.title", + "cur_ver": config.GetVersion(), "host": page.Host, "base_path": page.BasePath, "sId": page.SId, @@ -100,6 +98,9 @@ func (a *SUBController) subs(c *gin.Context) { return } + // Add headers + a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle) + if a.subEncrypt { c.String(200, base64.StdEncoding.EncodeToString([]byte(result))) } else { @@ -116,11 +117,15 @@ func (a *SUBController) subJsons(c *gin.Context) { c.String(400, "Error!") } else { - // Add headers via service - a.subService.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle) + // Add headers + a.ApplyCommonHeaders(c, header, a.updateInterval, a.subTitle) c.String(200, jsonSub) } } -// Note: host parsing and page data preparation moved to SubService +func (a *SUBController) ApplyCommonHeaders(c *gin.Context, header, updateInterval, profileTitle string) { + c.Writer.Header().Set("Subscription-Userinfo", header) + c.Writer.Header().Set("Profile-Update-Interval", updateInterval) + c.Writer.Header().Set("Profile-Title", "base64:"+base64.StdEncoding.EncodeToString([]byte(profileTitle))) +} -- cgit v1.2.3