From 4c9210cf11d9324700f4d8de7f7d38e86118b8f5 Mon Sep 17 00:00:00 2001 From: lucifer Date: Sun, 30 Aug 2020 12:30:05 +0800 Subject: [PATCH] Parse X-Forwarded-For in http transport (#142) * Parse X-Forwarded-For * Update hub.go Co-authored-by: RPRX <63339210+rprx@users.noreply.github.com> --- transport/internet/http/hub.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index 43a9039a..4b75a1a2 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -9,16 +9,17 @@ import ( "strings" "time" + "golang.org/x/net/http2" + "golang.org/x/net/http2/h2c" + "v2ray.com/core/common" "v2ray.com/core/common/net" + http_proto "v2ray.com/core/common/protocol/http" "v2ray.com/core/common/serial" "v2ray.com/core/common/session" "v2ray.com/core/common/signal/done" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/tls" - - "golang.org/x/net/http2" - "golang.org/x/net/http2/h2c" ) type Listener struct { @@ -82,6 +83,11 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) } } + forwardedAddrs := http_proto.ParseXForwardedFor(request.Header) + if len(forwardedAddrs) > 0 && forwardedAddrs[0].Family().IsIP() { + remoteAddr.(*net.TCPAddr).IP = forwardedAddrs[0].IP() + } + done := done.New() conn := net.NewConnection( net.ConnectionOutput(request.Body),