-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnodeuri.go
More file actions
87 lines (73 loc) · 1.88 KB
/
Copy pathnodeuri.go
File metadata and controls
87 lines (73 loc) · 1.88 KB
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
package ethnode
import (
"errors"
"net"
"net/url"
"strings"
)
// ParseNodeURI takes an "enode://..." string (Ethereum Node URI) and parses it to
// the relevant components.
func ParseNodeURI(enode string) (*NodeURI, error) {
if !strings.HasPrefix(enode, "enode://") && !strings.Contains(enode, "://") {
enode = "enode://" + enode
}
u, err := url.Parse(enode)
if err != nil {
return nil, err
}
if u.Scheme != "enode" {
return nil, errors.New("invalid enode scheme: " + u.Scheme)
}
r := NodeURI(*u)
return &r, nil
}
// NodeURI is a representation of an Ethereum Node URI, represented as an
// "enode://" string
type NodeURI url.URL
// ID returns the EnodeID
func (u *NodeURI) ID() string {
if u.Scheme == "" {
// "<ID>"
return u.Path
}
if u.User == nil {
// "enode://<ID>"
return u.Host
}
// "enode://<ID>@<Host>"
return u.User.Username()
}
func (u *NodeURI) hasRemote() bool {
if u.User == nil {
return false
}
// Future versions of Ethereum might support DNS-resolved hostnames instead
// of IPs, so we avoid stripping out hosts.
if hostname := (*url.URL)(u).Hostname(); hostname == "localhost" {
return false
} else if ip := net.ParseIP(hostname); ip.IsUnspecified() || ip.IsLoopback() {
return false
}
return true
}
// RemoteAddress returns the remote host:port component required to connect to
// the node, if included in the enode URI. If no remote address is provided,
// then empty string is returned.
func (u *NodeURI) RemoteAddress() string {
if !u.hasRemote() {
return ""
}
return u.Host
}
// RemoteHost returns the host of the remote NodeURI, if a remote address is
// defined. Otherwise empty string is returned.
func (u *NodeURI) RemoteHost() string {
if !u.hasRemote() {
return ""
}
return (*url.URL)(u).Hostname()
}
// String returns a fully qualified enode:// URI
func (u *NodeURI) String() string {
return (*url.URL)(u).String()
}