mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-05-04 22:28:49 +00:00
Update subController.go
This commit is contained in:
parent
bf134ab52b
commit
590567bfa0
1 changed files with 60 additions and 3 deletions
|
@ -2,7 +2,10 @@ package sub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -78,12 +81,37 @@ func (a *SUBController) subs(c *gin.Context) {
|
||||||
for _, sub := range subs {
|
for _, sub := range subs {
|
||||||
result += sub + "\n"
|
result += sub + "\n"
|
||||||
}
|
}
|
||||||
|
resultSlice := strings.Split(strings.TrimSpace(result), "\n")
|
||||||
|
|
||||||
// Add headers
|
// Add headers
|
||||||
c.Writer.Header().Set("Subscription-Userinfo", header)
|
c.Writer.Header().Set("Subscription-Userinfo", header)
|
||||||
c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
|
c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
|
||||||
c.Writer.Header().Set("Profile-Title", subId)
|
c.Writer.Header().Set("Profile-Title", subId)
|
||||||
|
|
||||||
|
acceptHeader := c.GetHeader("Accept")
|
||||||
|
headerMap := parseHeaderString(header)
|
||||||
|
expireValue := headerMap["expire"]
|
||||||
|
upValue := formatBytes(headerMap["upload"], 2)
|
||||||
|
downValue := formatBytes(headerMap["download"], 2)
|
||||||
|
totalValue := formatBytes(headerMap["total"], 2)
|
||||||
|
|
||||||
|
currentURL := "https://" + c.Request.Host + c.Request.RequestURI
|
||||||
|
|
||||||
|
if strings.Contains(acceptHeader, "text/html") {
|
||||||
|
if a.subEncrypt {
|
||||||
|
c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
|
||||||
|
} else {
|
||||||
|
c.HTML(200, "sub.html", gin.H{
|
||||||
|
"result": resultSlice,
|
||||||
|
"total": totalValue,
|
||||||
|
"expire": expireValue,
|
||||||
|
"upload": upValue,
|
||||||
|
"download": downValue,
|
||||||
|
"sId": subId,
|
||||||
|
"subUrl": currentURL,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if a.subEncrypt {
|
if a.subEncrypt {
|
||||||
c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
|
c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
|
||||||
} else {
|
} else {
|
||||||
|
@ -91,6 +119,7 @@ func (a *SUBController) subs(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (a *SUBController) subJsons(c *gin.Context) {
|
func (a *SUBController) subJsons(c *gin.Context) {
|
||||||
subId := c.Param("subid")
|
subId := c.Param("subid")
|
||||||
|
@ -132,3 +161,31 @@ func getHostFromXFH(s string) (string, error) {
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseHeaderString(header string) map[string]string {
|
||||||
|
headerMap := make(map[string]string)
|
||||||
|
pairs := strings.Split(header, ";")
|
||||||
|
for _, pair := range pairs {
|
||||||
|
kv := strings.Split(strings.TrimSpace(pair), "=")
|
||||||
|
if len(kv) == 2 {
|
||||||
|
headerMap[kv[0]] = kv[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return headerMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatBytes(sizeStr string, precision int) string {
|
||||||
|
// Convert the string input to a float64
|
||||||
|
size, _ := strconv.ParseFloat(sizeStr, 64)
|
||||||
|
|
||||||
|
if size == 0 {
|
||||||
|
return "0 B"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate base and suffix
|
||||||
|
base := math.Log(size) / math.Log(1024)
|
||||||
|
suffixes := []string{"B", "K", "M", "G", "T"}
|
||||||
|
|
||||||
|
value := math.Pow(1024, base-math.Floor(base))
|
||||||
|
return fmt.Sprintf("%.*f %s", precision, value, suffixes[int(math.Floor(base))])
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue