From 8fbb5abc7ccef36ebeba6d3ae4f5b03fed19878f Mon Sep 17 00:00:00 2001 From: bbassingthwaite Date: Tue, 3 Aug 2021 13:34:22 -0600 Subject: Add support for the gRPC client to connect on a unix domain socket --- cmd/gobgp/common.go | 12 ++++++++++-- cmd/gobgp/root.go | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cmd/gobgp/common.go b/cmd/gobgp/common.go index 0d5a0953..a0d73938 100644 --- a/cmd/gobgp/common.go +++ b/cmd/gobgp/common.go @@ -23,6 +23,7 @@ import ( "net" "os" "strconv" + "strings" "time" "google.golang.org/grpc" @@ -207,11 +208,18 @@ func newClient(ctx context.Context) (api.GobgpApiClient, context.CancelFunc, err grpcOpts = append(grpcOpts, grpc.WithInsecure()) } - target := net.JoinHostPort(globalOpts.Host, strconv.Itoa(globalOpts.Port)) + target := globalOpts.Target if target == "" { - target = ":50051" + target = net.JoinHostPort(globalOpts.Host, strconv.Itoa(globalOpts.Port)) + } else if strings.HasPrefix(target, "unix://") { + target = target[len("unix://"):] + dialer := func(addr string, t time.Duration) (net.Conn, error) { + return net.Dial("unix", addr) + } + grpcOpts = append(grpcOpts, grpc.WithDialer(dialer)) } cc, cancel := context.WithTimeout(ctx, time.Second) + conn, err := grpc.DialContext(cc, target, grpcOpts...) if err != nil { return nil, cancel, err diff --git a/cmd/gobgp/root.go b/cmd/gobgp/root.go index 6c90f0ab..eb63d0c1 100644 --- a/cmd/gobgp/root.go +++ b/cmd/gobgp/root.go @@ -28,6 +28,7 @@ import ( var globalOpts struct { Host string Port int + Target string Debug bool Quiet bool Json bool @@ -83,6 +84,7 @@ func newRootCmd() *cobra.Command { rootCmd.PersistentFlags().StringVarP(&globalOpts.Host, "host", "u", "127.0.0.1", "host") rootCmd.PersistentFlags().IntVarP(&globalOpts.Port, "port", "p", 50051, "port") + rootCmd.PersistentFlags().StringVarP(&globalOpts.Target, "target", "", "", "alternative to host/port when using UDS. Ex: unix:///var/run/go-bgp.sock if running gobgpd with a UDS socket.") rootCmd.PersistentFlags().BoolVarP(&globalOpts.Json, "json", "j", false, "use json format to output format") rootCmd.PersistentFlags().BoolVarP(&globalOpts.Debug, "debug", "d", false, "use debug") rootCmd.PersistentFlags().BoolVarP(&globalOpts.Quiet, "quiet", "q", false, "use quiet") -- cgit v1.2.3