From 2cafb3ef89990e3b1088d6b0a3ce16d7c924a78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=81=E3=82=BB?= <123655015+chise0713@users.noreply.github.com> Date: Sun, 24 Mar 2024 01:34:51 +0800 Subject: [PATCH] Fix(httpupgrade): `X-Forwarded-For` header not read. (#3172) --- transport/internet/httpupgrade/connection.go | 19 +++++++++++++++++++ transport/internet/httpupgrade/hub.go | 15 +++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 transport/internet/httpupgrade/connection.go diff --git a/transport/internet/httpupgrade/connection.go b/transport/internet/httpupgrade/connection.go new file mode 100644 index 00000000..c7a33af9 --- /dev/null +++ b/transport/internet/httpupgrade/connection.go @@ -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 +} diff --git a/transport/internet/httpupgrade/hub.go b/transport/internet/httpupgrade/hub.go index f87d020f..f78f8442 100644 --- a/transport/internet/httpupgrade/hub.go +++ b/transport/internet/httpupgrade/hub.go @@ -9,6 +9,7 @@ import ( "github.com/xtls/xray-core/common" "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/transport/internet" "github.com/xtls/xray-core/transport/internet/stat" @@ -68,6 +69,20 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) { _ = conn.Close() 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 }