-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoint.go
76 lines (61 loc) · 1.81 KB
/
endpoint.go
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
package p2p
import (
"fmt"
"net"
"regexp"
"strconv"
)
var hostnameRegex *regexp.Regexp
func init() {
// built after https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames
// ( optional labels ) ( required label )
hostnameRegex = regexp.MustCompile(`^([0-9A-Za-z][0-9A-Za-z\-]{0,62}\.)*[0-9A-Za-z][0-9A-Za-z\-]{0,62}$`)
}
type Endpoint struct {
IP string `json:"ip"`
Port string `json:"port"`
}
// NewEndpoint creates an Endpoint struct from a given ip and port, throws error if ip could not be resolved
func NewEndpoint(ip, port string) (Endpoint, error) {
ep := Endpoint{ip, port}
if !ep.Valid() {
return Endpoint{}, fmt.Errorf("(%s:%s) is not a valid endpoint", ip, port)
}
return ep, nil
}
// ParseEndpoint takes input in the form of "ip:port" and returns its IP
func ParseEndpoint(s string) (Endpoint, error) {
ip, port, err := net.SplitHostPort(s)
if err != nil {
return Endpoint{}, err
}
p, err := strconv.Atoi(port)
if err != nil {
return Endpoint{}, err
}
if p < 1 || p > 65535 {
return Endpoint{}, fmt.Errorf("port out of range")
}
return NewEndpoint(ip, port)
}
func (ep Endpoint) String() string {
return fmt.Sprintf("%s:%s", ep.IP, ep.Port)
}
// Verify checks if the data is usable. Does not check if the remote address works
func (ep Endpoint) Valid() bool {
if ep.IP == "" || ep.Port == "" || len(ep.IP) > 253 { // 253 is max according to rfc 952
return false
}
if p, err := strconv.Atoi(ep.Port); err != nil || p == 0 {
return false
}
if parse := net.ParseIP(ep.IP); parse == nil {
// check hostname
return hostnameRegex.MatchString(ep.IP)
}
return true
}
// Equals returns true if both endpoints are the same
func (ep Endpoint) Equal(o Endpoint) bool {
return ep.IP == o.IP && ep.Port == o.Port
}