diff --git a/transport/v2raygrpc/server.go b/transport/v2raygrpc/server.go index 875cf197..c852a9fa 100644 --- a/transport/v2raygrpc/server.go +++ b/transport/v2raygrpc/server.go @@ -4,6 +4,7 @@ import ( "context" "net" "os" + "strings" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/tls" @@ -13,6 +14,8 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials" + gM "google.golang.org/grpc/metadata" + "google.golang.org/grpc/peer" ) var _ adapter.V2RayServerTransport = (*Server)(nil) @@ -41,7 +44,22 @@ func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig t func (s *Server) Tun(server GunService_TunServer) error { ctx, cancel := context.WithCancel(s.ctx) conn := NewGRPCConn(server, cancel) - go s.handler.NewConnection(ctx, conn, M.Metadata{}) + var metadata M.Metadata + if remotePeer, loaded := peer.FromContext(server.Context()); loaded { + metadata.Source = M.SocksaddrFromNet(remotePeer.Addr) + } + if grpcMetadata, loaded := gM.FromIncomingContext(server.Context()); loaded { + forwardFrom := strings.Join(grpcMetadata.Get("X-Forwarded-For"), ",") + if forwardFrom != "" { + for _, from := range strings.Split(forwardFrom, ",") { + originAddr := M.ParseSocksaddr(from) + if originAddr.IsValid() { + metadata.Source = originAddr.Unwrap() + } + } + } + } + go s.handler.NewConnection(ctx, conn, metadata) <-ctx.Done() return nil }