Skip to content

Commit

Permalink
Fix #2470 (The subscription service gives two ports for VLESS) (#2479)
Browse files Browse the repository at this point in the history
* Fix #2470
  • Loading branch information
voronin9032 committed Aug 11, 2024
1 parent a6000f2 commit 438a968
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions sub/subController.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sub
import (
"encoding/base64"
"net"
"strings"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -54,7 +55,10 @@ func (a *SUBController) initRouter(g *gin.RouterGroup) {

func (a *SUBController) subs(c *gin.Context) {
subId := c.Param("subid")
host := c.GetHeader("X-Forwarded-Host")
var host string
if h, err := getHostFromXFH(c.GetHeader("X-Forwarded-Host")); err == nil {
host = h
}
if host == "" {
host = c.GetHeader("X-Real-IP")
}
Expand Down Expand Up @@ -89,7 +93,10 @@ func (a *SUBController) subs(c *gin.Context) {

func (a *SUBController) subJsons(c *gin.Context) {
subId := c.Param("subid")
host := c.GetHeader("X-Forwarded-Host")
var host string
if h, err := getHostFromXFH(c.GetHeader("X-Forwarded-Host")); err == nil {
host = h
}
if host == "" {
host = c.GetHeader("X-Real-IP")
}
Expand All @@ -113,3 +120,14 @@ func (a *SUBController) subJsons(c *gin.Context) {
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
}

0 comments on commit 438a968

Please sign in to comment.