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

pull/3179/head
チセ 8 months ago committed by GitHub
parent 70a5fe9a25
commit 2cafb3ef89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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
}

@ -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
} }

Loading…
Cancel
Save