p2p: enable scheme-less parsing of IPv6 strings (#6158)

This commit is contained in:
Callum Waters
2021-02-22 16:24:56 +01:00
committed by GitHub
parent 6912c34b58
commit 42f6c40751
2 changed files with 15 additions and 3 deletions

View File

@@ -24,8 +24,10 @@ var (
// reNodeID is a regexp for valid node IDs.
reNodeID = regexp.MustCompile(`^[0-9a-f]{40}$`)
// reHasScheme tries to detect URLs with schemes. It looks for a : before a / (if any).
reHasScheme = regexp.MustCompile(`^[^/]+:`)
// stringHasScheme tries to detect URLs with schemes. It looks for a : before a / (if any).
stringHasScheme = func(str string) bool {
return strings.Contains(str, "://")
}
// reSchemeIsHost tries to detect URLs where the scheme part is instead a
// hostname, i.e. of the form "host:80/path" where host: is a hostname.
@@ -95,7 +97,7 @@ func ParseNodeAddress(urlString string) (NodeAddress, error) {
// we try to apply a default scheme.
url, err := url.Parse(urlString)
if (err != nil || url.Scheme == "") &&
(!reHasScheme.MatchString(urlString) || reSchemeIsHost.MatchString(urlString)) {
(!stringHasScheme(urlString) || reSchemeIsHost.MatchString(urlString)) {
url, err = url.Parse(string(defaultProtocol) + "://" + urlString)
}
if err != nil {

View File

@@ -161,6 +161,16 @@ func TestParseNodeAddress(t *testing.T) {
p2p.NodeAddress{Protocol: "memory", NodeID: id},
true,
},
{
user + "@[1::]",
p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "1::"},
true,
},
{
"mconn://" + user + "@[fd80:b10c::2]:26657",
p2p.NodeAddress{Protocol: "mconn", NodeID: id, Hostname: "fd80:b10c::2", Port: 26657},
true,
},
// Invalid addresses.
{"", p2p.NodeAddress{}, false},