rpc: fix RPC client doesn't handle url's without ports (#6507)

This commit is contained in:
JayT106
2021-06-14 04:34:02 -04:00
committed by GitHub
parent 66926d31ca
commit cb63ab4ac0
3 changed files with 39 additions and 2 deletions

View File

@@ -126,5 +126,6 @@ Friendly reminder: We have a [bug bounty program](https://hackerone.com/tendermi
- [privval] \#5638 Increase read/write timeout to 5s and calculate ping interval based on it (@JoeKash)
- [blockchain/v1] [\#5701](https://github.com/tendermint/tendermint/pull/5701) Handle peers without blocks (@melekes)
- [blockchain/v1] \#5711 Fix deadlock (@melekes)
- [evidence] \#6375 Fix bug with inconsistent LightClientAttackEvidence hashing (@cmwaters)
- [evidence] \#6375 Fix bug with inconsistent LightClientAttackEvidence hashing (cmwaters)
- [rpc] \#6507 fix RPC client doesn't handle url's without ports (@JayT106)
- [statesync] \#6463 Adds Reverse Sync feature to fetch historical light blocks after state sync in order to verify any evidence (@cmwaters)

View File

@@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
tmsync "github.com/tendermint/tendermint/internal/libs/sync"
types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
@@ -370,6 +371,7 @@ func makeHTTPDialer(remoteAddr string) (func(string, string) (net.Conn, error),
}
protocol := u.Scheme
padding := u.Scheme
// accept http(s) as an alias for tcp
switch protocol {
@@ -378,7 +380,13 @@ func makeHTTPDialer(remoteAddr string) (func(string, string) (net.Conn, error),
}
dialFn := func(proto, addr string) (net.Conn, error) {
return net.Dial(protocol, u.GetDialAddress())
var timeout = 10 * time.Second
if !u.isUnixSocket && strings.LastIndex(u.Host, ":") == -1 {
u.Host = fmt.Sprintf("%s:%s", u.Host, padding)
return net.DialTimeout(protocol, u.GetDialAddress(), timeout)
}
return net.DialTimeout(protocol, u.GetDialAddress(), timeout)
}
return dialFn, nil

View File

@@ -84,3 +84,31 @@ func Test_parsedURL(t *testing.T) {
})
}
}
func TestMakeHTTPDialerURL(t *testing.T) {
remotes := []string{"https://foo-bar.com", "http://foo-bar.com"}
for _, remote := range remotes {
u, err := newParsedURL(remote)
require.NoError(t, err)
dialFn, err := makeHTTPDialer(remote)
require.Nil(t, err)
addr, err := dialFn(u.Scheme, u.GetHostWithPath())
require.NoError(t, err)
require.NotNil(t, addr)
}
errorURLs := []string{"tcp://foo-bar.com", "ftp://foo-bar.com"}
for _, errorURL := range errorURLs {
u, err := newParsedURL(errorURL)
require.NoError(t, err)
dialFn, err := makeHTTPDialer(errorURL)
require.Nil(t, err)
addr, err := dialFn(u.Scheme, u.GetHostWithPath())
require.Error(t, err)
require.Nil(t, addr)
}
}