From 5db6c112784bf8e70edb3c8d6c0bcd902cac60e3 Mon Sep 17 00:00:00 2001 From: Morgan Date: Thu, 5 Oct 2023 18:44:44 +0200 Subject: [PATCH] fix(tm2): make HTTPClient support https (#1158) Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com> --- tm2/pkg/bft/rpc/lib/client/http_client.go | 12 ++--- .../bft/rpc/lib/client/http_client_test.go | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 tm2/pkg/bft/rpc/lib/client/http_client_test.go diff --git a/tm2/pkg/bft/rpc/lib/client/http_client.go b/tm2/pkg/bft/rpc/lib/client/http_client.go index d5708f97d40..5a9da9ec052 100644 --- a/tm2/pkg/bft/rpc/lib/client/http_client.go +++ b/tm2/pkg/bft/rpc/lib/client/http_client.go @@ -78,12 +78,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 } @@ -99,6 +93,12 @@ func makeHTTPDialer(remoteAddr string) func(string, string) (net.Conn, error) { return makeErrorDialer(err) } + // net.Dial doesn't understand http/https, so change it to TCP + switch protocol { + case protoHTTP, protoHTTPS: + protocol = protoTCP + } + return func(proto, addr string) (net.Conn, error) { return net.Dial(protocol, address) } diff --git a/tm2/pkg/bft/rpc/lib/client/http_client_test.go b/tm2/pkg/bft/rpc/lib/client/http_client_test.go new file mode 100644 index 00000000000..9546a0c1d72 --- /dev/null +++ b/tm2/pkg/bft/rpc/lib/client/http_client_test.go @@ -0,0 +1,52 @@ +package rpcclient + +import ( + "testing" + + "github.com/jaekwon/testify/assert" +) + +func Test_parseRemoteAddr(t *testing.T) { + tt := []struct { + remoteAddr string + network, s, errContains string + }{ + {"127.0.0.1", "tcp", "127.0.0.1", ""}, + {"https://example.com", "https", "example.com", ""}, + {"wss://[::1]", "wss", "[::1]", ""}, + // no error cases - they cannot happen! + } + + for _, tc := range tt { + n, s, err := parseRemoteAddr(tc.remoteAddr) + if tc.errContains != "" { + _ = assert.NotNil(t, err) && assert.Contains(t, err.Error(), tc.errContains) + } + assert.NoError(t, err) + assert.Equal(t, n, tc.network) + assert.Equal(t, s, tc.s) + } +} + +// Following tests check that we correctly translate http/https to tcp, +// and other protocols are left intact from parseRemoteAddr() + +func Test_makeHTTPDialer(t *testing.T) { + dl := makeHTTPDialer("https://.") + _, err := dl("hello", "world") + if assert.NotNil(t, err) { + e := err.Error() + assert.Contains(t, e, "dial tcp:", "should convert https to tcp") + assert.Contains(t, e, "address .:", "should have parsed the address (as incorrect)") + } +} + +func Test_makeHTTPDialer_noConvert(t *testing.T) { + dl := makeHTTPDialer("udp://.") + _, err := dl("hello", "world") + if assert.NotNil(t, err) { + e := err.Error() + assert.Contains(t, e, "dial udp:", "udp protocol should remain the same") + assert.Contains(t, e, "address .:", "should have parsed the address (as incorrect)") + } +}