summaryrefslogtreecommitdiff
path: root/network/addr.go
blob: 1e2b04a447b8ac3c2ccd1bcc728a66dd2326c7f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package network

import (
	"errors"
	"net/url"
	"path"
	"regexp"
	"strings"

	"github.com/multiformats/go-multiaddr"
)

var errURLNoHost = errors.New("could not parse a host from url")

var errURLColonHost = errors.New("host name starts with a colon")

// HostColonPortPattern matches "^[-a-zA-Z0-9.]+:\\d+$" e.g. "foo.com.:1234"
var HostColonPortPattern = regexp.MustCompile(`^[-a-zA-Z0-9.]+:\d+$`)

// ParseHostOrURL handles "host:port" or a full URL.
// Standard library net/url.Parse chokes on "host:port".
func ParseHostOrURL(addr string) (*url.URL, error) {
	// If the entire addr is "host:port" grab that right away.
	// Don't try url.Parse() because that will grab "host:" as if it were "scheme:"
	if HostColonPortPattern.MatchString(addr) {
		return &url.URL{Scheme: "http", Host: addr}, nil
	}
	parsed, err := url.Parse(addr)
	if err == nil {
		if parsed.Host == "" {
			return nil, errURLNoHost
		}
		return parsed, nil
	}
	if strings.HasPrefix(addr, "http:") || strings.HasPrefix(addr, "https:") || strings.HasPrefix(addr, "ws:") || strings.HasPrefix(addr, "wss:") || strings.HasPrefix(addr, "://") || strings.HasPrefix(addr, "//") {
		return parsed, err
	}
	// This turns "[::]:4601" into "http://[::]:4601" which url.Parse can do
	parsed, e2 := url.Parse("http://" + addr)
	if e2 == nil {
		// https://datatracker.ietf.org/doc/html/rfc1123#section-2
		// first character is relaxed to allow either a letter or a digit
		if parsed.Host[0] == ':' && (len(parsed.Host) < 2 || parsed.Host[1] != ':') {
			return nil, errURLColonHost
		}
		return parsed, nil
	}
	return parsed, err /* return original err, not our prefix altered try */
}

// ParseHostOrURLOrMultiaddr returns an error if it could not parse the provided
// string as a valid "host:port", full URL, or multiaddr. If no error, it returns
// a host:port address, or a multiaddr.
func ParseHostOrURLOrMultiaddr(addr string) (string, error) {
	if strings.HasPrefix(addr, "/") && !strings.HasPrefix(addr, "//") { // multiaddr starts with '/' but not '//' which is possible for scheme relative URLS
		_, err := multiaddr.NewMultiaddr(addr)
		return addr, err
	}
	url, err := ParseHostOrURL(addr)
	if err != nil {
		return "", err
	}
	return url.Host, nil
}

// addrToGossipAddr parses host:port or a URL and returns the URL to the websocket interface at that address.
func (wn *WebsocketNetwork) addrToGossipAddr(addr string) (string, error) {
	parsedURL, err := ParseHostOrURL(addr)
	if err != nil {
		wn.log.Warnf("could not parse addr %#v: %s", addr, err)
		return "", errBadAddr
	}
	parsedURL.Scheme = websocketsScheme[parsedURL.Scheme]
	if parsedURL.Scheme == "" {
		parsedURL.Scheme = "ws"
	}
	parsedURL.Path = strings.Replace(path.Join(parsedURL.Path, GossipNetworkPath), "{genesisID}", wn.GenesisID, -1)
	return parsedURL.String(), nil
}