From 5585304d6885cd623426df7d363df93d06a45556 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 21 Dec 2025 10:28:05 +0800 Subject: [PATCH] chore: allow custom path for gRPC (`grpc-service-name` start with `/`) --- transport/gun/gun.go | 13 +++++++++++-- transport/gun/server.go | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/transport/gun/gun.go b/transport/gun/gun.go index 35639c12..3ac548dd 100644 --- a/transport/gun/gun.go +++ b/transport/gun/gun.go @@ -12,6 +12,7 @@ import ( "io" "net" "net/url" + "strings" "sync" "time" @@ -331,11 +332,19 @@ func NewHTTP2Client(dialFn DialFn, tlsConfig *tls.Config, clientFingerprint stri return wrap } +func ServiceNameToPath(serviceName string) string { + if strings.HasPrefix(serviceName, "/") { // custom paths + return serviceName + } + return "/" + serviceName + "/Tun" +} + func StreamGunWithTransport(transport *TransportWrap, cfg *Config) (net.Conn, error) { serviceName := "GunService" if cfg.ServiceName != "" { serviceName = cfg.ServiceName } + path := ServiceNameToPath(serviceName) reader, writer := io.Pipe() request := &http.Request{ @@ -344,9 +353,9 @@ func StreamGunWithTransport(transport *TransportWrap, cfg *Config) (net.Conn, er URL: &url.URL{ Scheme: "https", Host: cfg.Host, - Path: fmt.Sprintf("/%s/Tun", serviceName), + Path: path, // for unescape path - Opaque: fmt.Sprintf("//%s/%s/Tun", cfg.Host, serviceName), + Opaque: "//" + cfg.Host + path, }, Proto: "HTTP/2", ProtoMajor: 2, diff --git a/transport/gun/server.go b/transport/gun/server.go index 16605b43..c240459f 100644 --- a/transport/gun/server.go +++ b/transport/gun/server.go @@ -24,7 +24,7 @@ type ServerOption struct { } func NewServerHandler(options ServerOption) http.Handler { - path := "/" + options.ServiceName + "/Tun" + path := ServiceNameToPath(options.ServiceName) connHandler := options.ConnHandler httpHandler := options.HttpHandler if httpHandler == nil {