From 3dc9fba20d9274d004652330e3dce72401aca087 Mon Sep 17 00:00:00 2001 From: hmol233 <82594500+hmol233@users.noreply.github.com> Date: Sun, 27 Jun 2021 16:41:19 +0800 Subject: [PATCH] gRPC: Use `PathEscape` encoded service name --- transport/internet/grpc/config.go | 6 ++++++ transport/internet/grpc/dial.go | 4 ++-- transport/internet/grpc/encoding/customSeviceName.go | 2 +- transport/internet/grpc/hub.go | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/transport/internet/grpc/config.go b/transport/internet/grpc/config.go index 4be47ec2..d87722a4 100644 --- a/transport/internet/grpc/config.go +++ b/transport/internet/grpc/config.go @@ -1,6 +1,8 @@ package grpc import ( + "net/url" + "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/transport/internet" ) @@ -12,3 +14,7 @@ func init() { return new(Config) })) } + +func (c *Config) getNormalizedName() string { + return url.PathEscape(c.ServiceName) +} diff --git a/transport/internet/grpc/dial.go b/transport/internet/grpc/dial.go index 99c483be..d75f2c91 100644 --- a/transport/internet/grpc/dial.go +++ b/transport/internet/grpc/dial.go @@ -57,14 +57,14 @@ func dialgRPC(ctx context.Context, dest net.Destination, streamSettings *interne client := encoding.NewGRPCServiceClient(conn) if grpcSettings.MultiMode { newError("using gRPC multi mode").AtDebug().WriteToLog() - grpcService, err := client.(encoding.GRPCServiceClientX).TunMultiCustomName(ctx, grpcSettings.ServiceName) + grpcService, err := client.(encoding.GRPCServiceClientX).TunMultiCustomName(ctx, grpcSettings.getNormalizedName()) if err != nil { return nil, newError("Cannot dial gRPC").Base(err) } return encoding.NewMultiHunkConn(grpcService, nil), nil } - grpcService, err := client.(encoding.GRPCServiceClientX).TunCustomName(ctx, grpcSettings.ServiceName) + grpcService, err := client.(encoding.GRPCServiceClientX).TunCustomName(ctx, grpcSettings.getNormalizedName()) if err != nil { return nil, newError("Cannot dial gRPC").Base(err) } diff --git a/transport/internet/grpc/encoding/customSeviceName.go b/transport/internet/grpc/encoding/customSeviceName.go index d37d61f8..aa098835 100644 --- a/transport/internet/grpc/encoding/customSeviceName.go +++ b/transport/internet/grpc/encoding/customSeviceName.go @@ -39,7 +39,7 @@ func (c *gRPCServiceClient) TunCustomName(ctx context.Context, name string, opts } func (c *gRPCServiceClient) TunMultiCustomName(ctx context.Context, name string, opts ...grpc.CallOption) (GRPCService_TunMultiClient, error) { - stream, err := c.cc.NewStream(ctx, &ServerDesc(name).Streams[0], "/"+name+"/TunMulti", opts...) + stream, err := c.cc.NewStream(ctx, &ServerDesc(name).Streams[1], "/"+name+"/TunMulti", opts...) if err != nil { return nil, err } diff --git a/transport/internet/grpc/hub.go b/transport/internet/grpc/hub.go index 0f7ab6ba..9e1ef345 100644 --- a/transport/internet/grpc/hub.go +++ b/transport/internet/grpc/hub.go @@ -114,7 +114,7 @@ func Listen(ctx context.Context, address net.Address, port net.Port, settings *i } } - encoding.RegisterGRPCServiceServerX(s, listener, grpcSettings.ServiceName) + encoding.RegisterGRPCServiceServerX(s, listener, grpcSettings.getNormalizedName()) if err = s.Serve(streamListener); err != nil { newError("Listener for gRPC ended").Base(err).WriteToLog()