Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Authority to gRPC Transport #3076

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion infra/conf/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type GRPCConfig struct {
ServiceName string `json:"serviceName" `
Authority string `json:"authority"`
ServiceName string `json:"serviceName"`
MultiMode bool `json:"multiMode"`
IdleTimeout int32 `json:"idle_timeout"`
HealthCheckTimeout int32 `json:"health_check_timeout"`
Expand All @@ -28,6 +29,7 @@ func (g *GRPCConfig) Build() (proto.Message, error) {
}

return &grpc.Config{
Authority: g.Authority,
ServiceName: g.ServiceName,
MultiMode: g.MultiMode,
IdleTimeout: g.IdleTimeout,
Expand Down
56 changes: 28 additions & 28 deletions transport/internet/grpc/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion transport/internet/grpc/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package xray.transport.internet.grpc.encoding;
option go_package = "github.com/xtls/xray-core/transport/internet/grpc";

message Config {
string host = 1;
string authority = 1;
string service_name = 2;
bool multi_mode = 3;
int32 idle_timeout = 4;
Expand Down
38 changes: 25 additions & 13 deletions transport/internet/grpc/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)

Expand Down Expand Up @@ -122,24 +121,37 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
gctx = session.ContextWithTimeoutOnly(gctx, true)

c, err := internet.DialSystem(gctx, net.TCPDestination(address, port), sockopt)
if err == nil && realityConfig != nil {
return reality.UClient(c, realityConfig, gctx, dest)
if err == nil {
if tlsConfig != nil {
config := tlsConfig.GetTLSConfig()
if config.ServerName == "" && address.Family().IsDomain() {
config.ServerName = address.Domain()
}
if fingerprint := tls.GetFingerprint(tlsConfig.Fingerprint); fingerprint != nil {
return tls.UClient(c, config, fingerprint), nil
} else { // Fallback to normal gRPC TLS
return tls.Client(c, config), nil
}
}
if realityConfig != nil {
return reality.UClient(c, realityConfig, gctx, dest)
}
}
return c, err
}),
}

if tlsConfig != nil {
var transportCredential credentials.TransportCredentials
if fingerprint := tls.GetFingerprint(tlsConfig.Fingerprint); fingerprint != nil {
transportCredential = tls.NewGrpcUtls(tlsConfig.GetTLSConfig(), fingerprint)
} else { // Fallback to normal gRPC TLS
transportCredential = credentials.NewTLS(tlsConfig.GetTLSConfig())
}
dialOptions = append(dialOptions, grpc.WithTransportCredentials(transportCredential))
} else {
dialOptions = append(dialOptions, grpc.WithInsecure())
dialOptions = append(dialOptions, grpc.WithInsecure())

authority := ""
if grpcSettings.Authority != "" {
authority = grpcSettings.Authority
} else if tlsConfig != nil && tlsConfig.ServerName != "" {
authority = tlsConfig.ServerName
} else if realityConfig == nil && dest.Address.Family().IsDomain() {
authority = dest.Address.Domain()
}
dialOptions = append(dialOptions, grpc.WithAuthority(authority))

if grpcSettings.IdleTimeout > 0 || grpcSettings.HealthCheckTimeout > 0 || grpcSettings.PermitWithoutStream {
dialOptions = append(dialOptions, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Expand Down