Browse Source

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>
lucifer 5 years ago
parent
commit
4c9210cf11
1 changed files with 9 additions and 3 deletions
  1. 9 3
      transport/internet/http/hub.go

+ 9 - 3
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),