Fix(httpupgrade): X-Forwarded-For header not read. (#3172)

This commit is contained in:
チセ 2024-03-24 01:34:51 +08:00 committed by GitHub
parent 70a5fe9a25
commit 2cafb3ef89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package httpupgrade
import "net"
type connnection struct {
net.Conn
remoteAddr net.Addr
}
func newConnection(conn net.Conn, remoteAddr net.Addr) *connnection {
return &connnection{
Conn: conn,
remoteAddr: remoteAddr,
}
}
func (c *connnection) RemoteAddr() net.Addr {
return c.remoteAddr
}

View File

@ -9,6 +9,7 @@ import (
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
http_proto "github.com/xtls/xray-core/common/protocol/http"
"github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/stat"
@ -68,6 +69,20 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) {
_ = conn.Close() _ = conn.Close()
return nil, err return nil, err
} }
forwardedAddrs := http_proto.ParseXForwardedFor(req.Header)
remoteAddr := conn.RemoteAddr()
if len(forwardedAddrs) > 0 && forwardedAddrs[0].Family().IsIP() {
remoteAddr = &net.TCPAddr{
IP: forwardedAddrs[0].IP(),
Port: int(0),
}
}
if remoteAddr == nil {
return nil, newError("remoteAddr is nil")
}
conn = newConnection(conn, remoteAddr)
return stat.Connection(conn), nil return stat.Connection(conn), nil
} }