From 0de19420f64ba8ac34416d76626fbf625f5fcda0 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sun, 11 Mar 2018 23:03:11 -0700 Subject: [PATCH] cmd/tendermint/commands/lite: add tcp scheme to address URLs (#1297) Noticed while investigating https://github.com/tendermint/tendermint/issues/970 As reported by @zramsay, we'd get the warning from tendermint/rpc/lib because we were passing in scheme-less addresses, so by default use "tcp". Also by default, "node" (nodeAddr) has been set to: "tcp://localhost:46657" instead of the bare: "localhost:46657" This change is just to clean up such warnings as they spuriously would spook users for a package "lite" that claims to be secure. --- cmd/tendermint/commands/lite.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/cmd/tendermint/commands/lite.go b/cmd/tendermint/commands/lite.go index d94f95ba4..5ea20a785 100644 --- a/cmd/tendermint/commands/lite.go +++ b/cmd/tendermint/commands/lite.go @@ -1,6 +1,9 @@ package commands import ( + "fmt" + "net/url" + "github.com/spf13/cobra" cmn "github.com/tendermint/tmlibs/common" @@ -32,12 +35,36 @@ var ( func init() { LiteCmd.Flags().StringVar(&listenAddr, "laddr", ":8888", "Serve the proxy on the given port") - LiteCmd.Flags().StringVar(&nodeAddr, "node", "localhost:46657", "Connect to a Tendermint node at this address") + LiteCmd.Flags().StringVar(&nodeAddr, "node", "tcp://localhost:46657", "Connect to a Tendermint node at this address") LiteCmd.Flags().StringVar(&chainID, "chain-id", "tendermint", "Specify the Tendermint chain ID") LiteCmd.Flags().StringVar(&home, "home-dir", ".tendermint-lite", "Specify the home directory") } +func ensureAddrHasSchemeOrDefaultToTCP(addr string) (string, error) { + u, err := url.Parse(nodeAddr) + if err != nil { + return "", err + } + switch u.Scheme { + case "tcp", "unix": + case "": + u.Scheme = "tcp" + default: + return "", fmt.Errorf("unknown scheme %q, use either tcp or unix", u.Scheme) + } + return u.String(), nil +} + func runProxy(cmd *cobra.Command, args []string) error { + nodeAddr, err := ensureAddrHasSchemeOrDefaultToTCP(nodeAddr) + if err != nil { + return err + } + listenAddr, err := ensureAddrHasSchemeOrDefaultToTCP(listenAddr) + if err != nil { + return err + } + // First, connect a client node := rpcclient.NewHTTP(nodeAddr, "/websocket")