From 55dc26f22840c83045e6f34221533aa3cbe977a2 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Mon, 27 Feb 2023 19:52:01 +0000 Subject: [PATCH] Add REALITY support to gRPC client and server Now you are able to configure REALITY gRPC client and server Duplicate of REALITY H2, perhaps, just for fun --- go.mod | 2 +- go.sum | 4 ++-- infra/conf/transport_internet.go | 4 ++-- transport/internet/grpc/dial.go | 8 +++++++- transport/internet/grpc/hub.go | 5 +++++ 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 6b6f7c13..b318b6bb 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/stretchr/testify v1.8.1 github.com/v2fly/ss-bloomring v0.0.0-20210312155135-28617310f63e github.com/xtls/go v0.0.0-20230107031059-4610f88d00f3 - github.com/xtls/reality v0.0.0-20230227150228-9e83b0bee167 + github.com/xtls/reality v0.0.0-20230227192902-524506d97551 go.starlark.net v0.0.0-20230128213706-3f75dec8e403 golang.org/x/crypto v0.6.0 golang.org/x/net v0.7.0 diff --git a/go.sum b/go.sum index cf6d2507..02f4cb2e 100644 --- a/go.sum +++ b/go.sum @@ -191,8 +191,8 @@ github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49u github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= github.com/xtls/go v0.0.0-20230107031059-4610f88d00f3 h1:a3Y4WVjCxwoyO4E2xdNvq577tW8lkSBgyrA8E9+2NtM= github.com/xtls/go v0.0.0-20230107031059-4610f88d00f3/go.mod h1:YJTRELIWrGxR1s8xcEBgxcxBfwQfMGjdvNLTjN9XFgY= -github.com/xtls/reality v0.0.0-20230227150228-9e83b0bee167 h1:kC3gtMw5yxMb9pJyKD4ZobwtiF9DgdubkSd5BhWTX5M= -github.com/xtls/reality v0.0.0-20230227150228-9e83b0bee167/go.mod h1:rkuAY1S9F8eI8gDiPDYvACE8e2uwkyg8qoOTuwWov7Y= +github.com/xtls/reality v0.0.0-20230227192902-524506d97551 h1:zOP9NvpCMa1Y58UmA9EhbWs5/FNKvqwD5EyDLVit2LI= +github.com/xtls/reality v0.0.0-20230227192902-524506d97551/go.mod h1:rkuAY1S9F8eI8gDiPDYvACE8e2uwkyg8qoOTuwWov7Y= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA= go.starlark.net v0.0.0-20230128213706-3f75dec8e403 h1:jPeC7Exc+m8OBJUlWbBLh0O5UZPM7yU5W4adnhhbG4U= diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index 92abb688..0af0f1e5 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -849,8 +849,8 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) { config.SecurityType = tm.Type } if strings.EqualFold(c.Security, "reality") { - if config.ProtocolName != "tcp" && config.ProtocolName != "http" && config.ProtocolName != "domainsocket" { - return nil, newError("REALITY only supports TCP, H2 and DomainSocket for now.") + if config.ProtocolName != "tcp" && config.ProtocolName != "http" && config.ProtocolName != "grpc" && config.ProtocolName != "domainsocket" { + return nil, newError("REALITY only supports TCP, H2, gRPC and DomainSocket for now.") } if c.REALITYSettings == nil { return nil, newError(`REALITY: Empty "realitySettings".`) diff --git a/transport/internet/grpc/dial.go b/transport/internet/grpc/dial.go index afc270bd..4ab4b615 100644 --- a/transport/internet/grpc/dial.go +++ b/transport/internet/grpc/dial.go @@ -11,6 +11,7 @@ import ( "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/grpc/encoding" + "github.com/xtls/xray-core/transport/internet/reality" "github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/tls" "google.golang.org/grpc" @@ -77,6 +78,7 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in globalDialerMap = make(map[dialerConf]*grpc.ClientConn) } tlsConfig := tls.ConfigFromStreamSettings(streamSettings) + realityConfig := reality.ConfigFromStreamSettings(streamSettings) sockopt := streamSettings.SocketSettings grpcSettings := streamSettings.ProtocolSettings.(*Config) @@ -116,7 +118,11 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in return nil, err } address := net.ParseAddress(rawHost) - return internet.DialSystem(gctx, net.TCPDestination(address, port), sockopt) + c, err := internet.DialSystem(gctx, net.TCPDestination(address, port), sockopt) + if err == nil && realityConfig != nil { + return reality.UClient(c, realityConfig, ctx, dest) + } + return c, err }), } diff --git a/transport/internet/grpc/hub.go b/transport/internet/grpc/hub.go index 4f553070..9bce2274 100644 --- a/transport/internet/grpc/hub.go +++ b/transport/internet/grpc/hub.go @@ -4,11 +4,13 @@ import ( "context" "time" + goreality "github.com/xtls/reality" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/grpc/encoding" + "github.com/xtls/xray-core/transport/internet/reality" "github.com/xtls/xray-core/transport/internet/tls" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -125,6 +127,9 @@ func Listen(ctx context.Context, address net.Address, port net.Port, settings *i encoding.RegisterGRPCServiceServerX(s, listener, grpcSettings.getNormalizedName()) + if config := reality.ConfigFromStreamSettings(settings); config != nil { + streamListener = goreality.NewListener(streamListener, config.GetREALITYConfig()) + } if err = s.Serve(streamListener); err != nil { newError("Listener for gRPC ended").Base(err).WriteToLog() }