From 98c92521c9b23c25c2da5d97d480cc23557169f2 Mon Sep 17 00:00:00 2001 From: Kartikay Date: Mon, 3 Mar 2025 01:01:41 +0530 Subject: [PATCH 1/3] add proxy support Signed-off-by: Kartikay --- internal/client/client.go | 23 +++++++++++++++++++++++ internal/cmd/cmd.go | 1 + 2 files changed, 24 insertions(+) diff --git a/internal/client/client.go b/internal/client/client.go index 5cdc72d5..4852f51c 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -1,10 +1,15 @@ package client import ( + "context" "fmt" + "net" + "net/url" "os" "path/filepath" + "golang.org/x/net/proxy" + v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" "github.com/authzed/authzed-go/v1" "github.com/authzed/grpcutil" @@ -188,6 +193,24 @@ func DialOptsFromFlags(cmd *cobra.Command, token storage.Token) ([]grpc.DialOpti grpc.WithChainStreamInterceptor(zgrpcutil.StreamLogDispatchTrailers), } + proxyAddr := cobrautil.MustGetString(cmd, "proxy") + + if proxyAddr != "" { + addr, err := url.Parse(proxyAddr) + if err != nil { + return nil, fmt.Errorf("failed to parse socks5 proxy addr: %w", err) + } + + dialer, err := proxy.SOCKS5("tcp", addr.Host, nil, proxy.Direct) + if err != nil { + return nil, fmt.Errorf("failed to create socks5 proxy dialer: %w", err) + } + + opts = append(opts, grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) { + return dialer.Dial("tcp", addr) + })) + } + if token.IsInsecure() { opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) opts = append(opts, grpcutil.WithInsecureBearerToken(token.APIToken)) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 554a8cd6..de4e7916 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -76,6 +76,7 @@ func Run() { rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging") rootCmd.PersistentFlags().String("request-id", "", "optional id to send along with SpiceDB requests for tracing") rootCmd.PersistentFlags().Int("max-message-size", 0, "maximum size *in bytes* (defaults to 4_194_304 bytes ~= 4MB) of a gRPC message that can be sent or received by zed") + rootCmd.PersistentFlags().String("proxy", "", "specify a SOCKS5 proxy address") _ = rootCmd.PersistentFlags().MarkHidden("debug") // This cannot return its error. versionCmd := &cobra.Command{ From c1ce958f7df72ad4235638209e641e6d7286ebcf Mon Sep 17 00:00:00 2001 From: Kartikay Date: Mon, 3 Mar 2025 01:21:30 +0530 Subject: [PATCH 2/3] gofumpt Signed-off-by: Kartikay --- go.mod | 2 +- internal/client/client.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 8749d843..74faf578 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/xlab/treeprint v1.2.0 golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 golang.org/x/mod v0.22.0 + golang.org/x/net v0.34.0 golang.org/x/sync v0.10.0 golang.org/x/term v0.28.0 google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f @@ -239,7 +240,6 @@ require ( go.uber.org/goleak v1.3.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.32.0 // indirect - golang.org/x/net v0.34.0 // indirect golang.org/x/oauth2 v0.25.0 // indirect golang.org/x/sys v0.29.0 // indirect golang.org/x/text v0.21.0 // indirect diff --git a/internal/client/client.go b/internal/client/client.go index 4852f51c..a5e93a0e 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -206,7 +206,7 @@ func DialOptsFromFlags(cmd *cobra.Command, token storage.Token) ([]grpc.DialOpti return nil, fmt.Errorf("failed to create socks5 proxy dialer: %w", err) } - opts = append(opts, grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) { + opts = append(opts, grpc.WithContextDialer(func(_ context.Context, addr string) (net.Conn, error) { return dialer.Dial("tcp", addr) })) } From 62205016e7ab172fd249db97e573ab431da2f219 Mon Sep 17 00:00:00 2001 From: Kartikay Date: Tue, 4 Mar 2025 02:22:13 +0530 Subject: [PATCH 3/3] gofumpt Signed-off-by: Kartikay --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 50312594..e9f4f5a8 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,7 @@ require ( github.com/xlab/treeprint v1.2.0 golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 golang.org/x/mod v0.23.0 - golang.org/x/net v0.34.0 + golang.org/x/net v0.34.0 golang.org/x/sync v0.11.0 golang.org/x/term v0.29.0 google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f