Files
trihuy-russian/sub/subController.go
T

116 lines
2.4 KiB
Go
Raw Normal View History

2023-05-22 18:06:34 +03:30
package sub
2023-04-09 23:13:18 +03:30
import (
"encoding/base64"
2024-05-22 17:51:55 +02:00
"net"
2023-04-09 23:13:18 +03:30
"github.com/gin-gonic/gin"
)
type SUBController struct {
2024-02-21 14:17:52 +03:30
subPath string
subJsonPath string
subEncrypt bool
updateInterval string
subService *SubService
subJsonService *SubJsonService
2023-04-09 23:13:18 +03:30
}
2024-02-21 14:17:52 +03:30
func NewSUBController(
g *gin.RouterGroup,
subPath string,
jsonPath string,
encrypt bool,
showInfo bool,
rModel string,
update string,
2024-03-11 01:01:24 +03:30
jsonFragment string,
2024-03-12 19:44:51 +03:30
jsonMux string,
jsonRules string,
2024-03-11 01:01:24 +03:30
) *SUBController {
2024-03-11 16:14:24 +03:30
sub := NewSubService(showInfo, rModel)
2024-02-21 14:17:52 +03:30
a := &SUBController{
subPath: subPath,
subJsonPath: jsonPath,
subEncrypt: encrypt,
updateInterval: update,
2024-03-11 16:14:24 +03:30
subService: sub,
2024-03-12 19:44:51 +03:30
subJsonService: NewSubJsonService(jsonFragment, jsonMux, jsonRules, sub),
2024-02-21 14:17:52 +03:30
}
2023-04-09 23:13:18 +03:30
a.initRouter(g)
return a
}
func (a *SUBController) initRouter(g *gin.RouterGroup) {
2024-02-21 14:17:52 +03:30
gLink := g.Group(a.subPath)
gJson := g.Group(a.subJsonPath)
2023-04-09 23:13:18 +03:30
2024-02-21 14:17:52 +03:30
gLink.GET(":subid", a.subs)
gJson.GET(":subid", a.subJsons)
2023-04-09 23:13:18 +03:30
}
func (a *SUBController) subs(c *gin.Context) {
subId := c.Param("subid")
host := c.GetHeader("X-Forwarded-Host")
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
}
}
2024-02-21 14:17:52 +03:30
subs, header, err := a.subService.GetSubs(subId, host)
2023-04-18 21:34:06 +03:30
if err != nil || len(subs) == 0 {
2023-04-09 23:13:18 +03:30
c.String(400, "Error!")
} else {
result := ""
for _, sub := range subs {
result += sub + "\n"
}
2023-04-18 21:34:06 +03:30
2023-05-17 01:07:35 +03:30
// Add headers
2024-02-21 14:17:52 +03:30
c.Writer.Header().Set("Subscription-Userinfo", header)
c.Writer.Header().Set("Profile-Update-Interval", a.updateInterval)
c.Writer.Header().Set("Profile-Title", subId)
2023-04-18 21:34:06 +03:30
2024-02-21 14:17:52 +03:30
if a.subEncrypt {
2023-08-26 16:54:01 +03:30
c.String(200, base64.StdEncoding.EncodeToString([]byte(result)))
} else {
c.String(200, result)
}
2023-04-09 23:13:18 +03:30
}
}
2024-02-21 14:17:52 +03:30
func (a *SUBController) subJsons(c *gin.Context) {
subId := c.Param("subid")
host := c.GetHeader("X-Forwarded-Host")
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
}
}
2024-02-21 14:17:52 +03:30
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", subId)
c.String(200, jsonSub)
}
}