gRPC: Parse X-Real-IP header, fix #766 (#769)

This commit is contained in:
lucifer 2021-10-14 12:10:19 +08:00 committed by GitHub
parent 3f3b54f673
commit 5e606169f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -5,9 +5,11 @@ import (
"io" "io"
"net" "net"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer" "google.golang.org/grpc/peer"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
xnet "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/common/net/cnc"
"github.com/xtls/xray-core/common/signal/done" "github.com/xtls/xray-core/common/signal/done"
) )
@ -49,6 +51,19 @@ func NewHunkConn(hc HunkConn, cancel context.CancelFunc) net.Conn {
} }
} }
md, ok := metadata.FromIncomingContext(hc.Context())
if ok {
header := md.Get("x-real-ip")
if len(header) > 0 {
realip := xnet.ParseAddress(header[0])
if realip.Family().IsIP() {
rAddr = &net.TCPAddr{
IP: realip.IP(),
Port: 0,
}
}
}
}
wrc := NewHunkReadWriter(hc, cancel) wrc := NewHunkReadWriter(hc, cancel)
return cnc.NewConnection( return cnc.NewConnection(
cnc.ConnectionInput(wrc), cnc.ConnectionInput(wrc),

View File

@ -5,9 +5,11 @@ import (
"io" "io"
"net" "net"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer" "google.golang.org/grpc/peer"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
xnet "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/common/net/cnc"
"github.com/xtls/xray-core/common/signal/done" "github.com/xtls/xray-core/common/signal/done"
) )
@ -44,6 +46,19 @@ func NewMultiHunkConn(hc MultiHunkConn, cancel context.CancelFunc) net.Conn {
} }
} }
md, ok := metadata.FromIncomingContext(hc.Context())
if ok {
header := md.Get("x-real-ip")
if len(header) > 0 {
realip := xnet.ParseAddress(header[0])
if realip.Family().IsIP() {
rAddr = &net.TCPAddr{
IP: realip.IP(),
Port: 0,
}
}
}
}
wrc := NewMultiHunkReadWriter(hc, cancel) wrc := NewMultiHunkReadWriter(hc, cancel)
return cnc.NewConnection( return cnc.NewConnection(
cnc.ConnectionInputMulti(wrc), cnc.ConnectionInputMulti(wrc),