Skip to content

Commit

Permalink
Fix(httpupgrade): X-Forwarded-For header not read. (#3172)
Browse files Browse the repository at this point in the history
  • Loading branch information
chise0713 authored Mar 23, 2024
1 parent 70a5fe9 commit 2cafb3e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions transport/internet/httpupgrade/connection.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 15 additions & 0 deletions transport/internet/httpupgrade/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 2cafb3e

Please sign in to comment.