diff options
author | bbassingthwaite <bbassingthwaite@digitalocean.com> | 2021-08-03 13:34:22 -0600 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2021-08-08 20:46:10 +0900 |
commit | 8fbb5abc7ccef36ebeba6d3ae4f5b03fed19878f (patch) | |
tree | 9e2126d31de894a4ed5d40216592638421f0e193 | |
parent | f99c0674039227bf47a7d02c9fe038ee57229da3 (diff) |
Add support for the gRPC client to connect on a unix domain socket
-rw-r--r-- | cmd/gobgp/common.go | 12 | ||||
-rw-r--r-- | 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") |