Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add default ports for net.Dial if missing in RPC URL #2360

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions tm2/pkg/bft/rpc/lib/client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
const (
protoHTTP = "http"
protoHTTPS = "https"
protoWSS = "wss"
protoWS = "ws"
protoTCP = "tcp"
)

Expand Down Expand Up @@ -173,12 +171,7 @@ func defaultHTTPClient(remoteAddr string) *http.Client {
}

func makeHTTPDialer(remoteAddr string) func(string, string) (net.Conn, error) {
protocol, address, err := parseRemoteAddr(remoteAddr)
if err != nil {
return func(_ string, _ string) (net.Conn, error) {
return nil, err
}
}
protocol, address := parseRemoteAddr(remoteAddr)

// net.Dial doesn't understand http/https, so change it to TCP
switch protocol {
Expand All @@ -193,17 +186,14 @@ func makeHTTPDialer(remoteAddr string) func(string, string) (net.Conn, error) {

// protocol - client's protocol (for example, "http", "https", "wss", "ws", "tcp")
// trimmedS - rest of the address (for example, "192.0.2.1:25", "[2001:db8::1]:80") with "/" replaced with "."
func toClientAddrAndParse(remoteAddr string) (string, string, error) {
protocol, address, err := parseRemoteAddr(remoteAddr)
if err != nil {
return "", "", err
}
func toClientAddrAndParse(remoteAddr string) (string, string) {
protocol, address := parseRemoteAddr(remoteAddr)

// protocol to use for http operations, to support both http and https
var clientProtocol string
// default to http for unknown protocols (ex. tcp)
switch protocol {
case protoHTTP, protoHTTPS, protoWS, protoWSS:
case protoHTTP, protoHTTPS:
clientProtocol = protocol
default:
clientProtocol = protoHTTP
Expand All @@ -212,23 +202,21 @@ func toClientAddrAndParse(remoteAddr string) (string, string, error) {
// replace / with . for http requests (kvstore domain)
trimmedAddress := strings.Replace(address, "/", ".", -1)

return clientProtocol, trimmedAddress, nil
return clientProtocol, trimmedAddress
}

func toClientAddress(remoteAddr string) (string, error) {
clientProtocol, trimmedAddress, err := toClientAddrAndParse(remoteAddr)
if err != nil {
return "", err
}
clientProtocol, trimmedAddress := toClientAddrAndParse(remoteAddr)

return clientProtocol + "://" + trimmedAddress, nil
}

// network - name of the network (for example, "tcp", "unix")
// s - rest of the address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
// TODO: Deprecate support for IP:PORT or /path/to/socket
func parseRemoteAddr(remoteAddr string) (network string, s string, err error) {
func parseRemoteAddr(remoteAddr string) (string, string) {
parts := strings.SplitN(remoteAddr, "://", 2)

var protocol, address string
switch len(parts) {
case 1:
Expand All @@ -237,7 +225,19 @@ func parseRemoteAddr(remoteAddr string) (network string, s string, err error) {
case 2:
protocol, address = parts[0], parts[1]
}
return protocol, address, nil

// Append default ports if not specified
if !strings.Contains(address, ":") {
switch protocol {
case protoHTTPS:
address += ":443"
case protoHTTP, protoTCP:
address += ":80"
default: // noop
}
}

return protocol, address
}

// isOKStatus returns a boolean indicating if the response
Expand Down
40 changes: 26 additions & 14 deletions tm2/pkg/bft/rpc/lib/client/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,39 @@ func TestClient_parseRemoteAddr(t *testing.T) {
t.Parallel()

testTable := []struct {
remoteAddr string
network string
rest string
remoteAddr string
expectedNetwork string
expectedRest string
}{
{
"127.0.0.1",
"tcp",
"127.0.0.1",
"127.0.0.1:80",
},
{
"127.0.0.1:5000",
"tcp",
"127.0.0.1:5000",
},
{
"http://example.com",
"http",
"example.com:80",
},
{
"https://example.com",
"https",
"example.com",
"example.com:443",
},
{
"wss://[::1]",
"wss",
"[::1]",
"http://example.com:5000",
"http",
"example.com:5000",
},
{
"https://example.com:5000",
"https",
"example.com:5000",
},
}

Expand All @@ -44,11 +59,10 @@ func TestClient_parseRemoteAddr(t *testing.T) {
t.Run(testCase.remoteAddr, func(t *testing.T) {
t.Parallel()

n, r, err := parseRemoteAddr(testCase.remoteAddr)
require.NoError(t, err)
n, r := parseRemoteAddr(testCase.remoteAddr)

assert.Equal(t, n, testCase.network)
assert.Equal(t, r, testCase.rest)
assert.Equal(t, testCase.expectedNetwork, n)
assert.Equal(t, testCase.expectedRest, r)
})
}
}
Expand All @@ -66,7 +80,6 @@ func TestClient_makeHTTPDialer(t *testing.T) {
require.Error(t, err)

assert.Contains(t, err.Error(), "dial tcp:", "should convert https to tcp")
assert.Contains(t, err.Error(), "address .:", "should have parsed the address (as incorrect)")
})

t.Run("udp", func(t *testing.T) {
Expand All @@ -76,7 +89,6 @@ func TestClient_makeHTTPDialer(t *testing.T) {
require.Error(t, err)

assert.Contains(t, err.Error(), "dial udp:", "udp protocol should remain the same")
assert.Contains(t, err.Error(), "address .:", "should have parsed the address (as incorrect)")
})
}

Expand Down
Loading