From 2be4b0fe050f04ca5126d51e9bec0fc7ea8aaa25 Mon Sep 17 00:00:00 2001 From: yk Date: Wed, 13 Nov 2019 22:26:29 +0800 Subject: [PATCH] rpc/lib: fix RPC client, which was previously resolving https protocol to http (#4131) Fixes #4051 Function `parseRemoteAddr` is forcing protocol HTTP and protocol HTTPs to tcp. This causes the bug in the issue #4051. I find that the tcp is only needed where `net.Dial`. So I moved the switch to makeHTTPDialer. --- CHANGELOG_PENDING.md | 1 + rpc/lib/client/http_client.go | 12 ++++++------ rpc/lib/client/http_client_test.go | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 rpc/lib/client/http_client_test.go diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 0fa986f0d..037b90c33 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -36,3 +36,4 @@ program](https://hackerone.com/tendermint). - [tools] [\#4023](https://github.com/tendermint/tendermint/issues/4023) Refresh `tm-monitor` health when validator count is updated (@erikgrinaker) - [state] [\#4104](https://github.com/tendermint/tendermint/pull/4104) txindex/kv: Fsync data to disk immediately after receiving it (@guagualvcha) - [state] [\#4095](https://github.com/tendermint/tendermint/pull/4095) txindex/kv: Return an error if there's one when the user searches for a tx (hash=X) (@hsyis) +- [rpc/lib] [\#4051](https://github.com/tendermint/tendermint/pull/4131) Fix RPC client, which was previously resolving https protocol to http (@yenkhoon) diff --git a/rpc/lib/client/http_client.go b/rpc/lib/client/http_client.go index 5eb5382e7..0673177c3 100644 --- a/rpc/lib/client/http_client.go +++ b/rpc/lib/client/http_client.go @@ -82,12 +82,6 @@ func parseRemoteAddr(remoteAddr string) (network string, s string, err error) { return "", "", fmt.Errorf("invalid addr: %s", remoteAddr) } - // accept http(s) as an alias for tcp - switch protocol { - case protoHTTP, protoHTTPS: - protocol = protoTCP - } - return protocol, address, nil } @@ -103,6 +97,12 @@ func makeHTTPDialer(remoteAddr string) func(string, string) (net.Conn, error) { return makeErrorDialer(err) } + // accept http(s) as an alias for tcp + switch protocol { + case protoHTTP, protoHTTPS: + protocol = protoTCP + } + return func(proto, addr string) (net.Conn, error) { return net.Dial(protocol, address) } diff --git a/rpc/lib/client/http_client_test.go b/rpc/lib/client/http_client_test.go new file mode 100644 index 000000000..b74425a54 --- /dev/null +++ b/rpc/lib/client/http_client_test.go @@ -0,0 +1,22 @@ +package rpcclient + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestHTTPClientMakeHTTPDialer(t *testing.T) { + remote := []string{"https://foo-bar.com:80", "http://foo-bar.net:80"} + + for _, f := range remote { + protocol, address, err := parseRemoteAddr(f) + require.NoError(t, err) + dialFn := makeHTTPDialer(f) + + addr, err := dialFn(protocol, address) + require.NoError(t, err) + require.NotNil(t, addr) + } + +}