-
Notifications
You must be signed in to change notification settings - Fork 11
/
nodeurl.go
226 lines (196 loc) · 4.91 KB
/
nodeurl.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package storj
import (
"net/url"
"strconv"
"strings"
"github.com/zeebo/errs"
"storj.io/common/base58"
)
var (
// ErrNodeURL is used when something goes wrong with a node url.
ErrNodeURL = errs.Class("node URL")
)
// NodeURL defines a structure for connecting to a node.
type NodeURL struct {
ID NodeID
Address string
NoiseInfo NoiseInfo
DebounceLimit int
Features uint64 // this is a bitmask of pb.NodeAddress_Feature values.
}
// ParseNodeURL parses node URL string.
//
// Examples:
//
// raw IP:
// 33.20.0.1:7777
// [2001:db8:1f70::999:de8:7648:6e8]:7777
//
// with NodeID:
// 12vha9oTFnerxYRgeQ2BZqoFrLrnmmf5UWTCY2jA77dF3YvWew7@33.20.0.1:7777
// 12vha9oTFnerxYRgeQ2BZqoFrLrnmmf5UWTCY2jA77dF3YvWew7@[2001:db8:1f70::999:de8:7648:6e8]:7777
//
// without host:
// 12vha9oTFnerxYRgeQ2BZqoFrLrnmmf5UWTCY2jA77dF3YvWew7@
//
// with noise information:
// 12vha9oTFnerxYRgeQ2BZqoFrLrnmmf5UWTCY2jA77dF3YvWew7@33.20.0.1:7777?noise_pub=12vha9oTFnerxY&noise_proto=1
func ParseNodeURL(s string) (NodeURL, error) {
if s == "" {
return NodeURL{}, nil
}
if !strings.HasPrefix(s, "storj://") {
if !strings.Contains(s, "://") {
s = "storj://" + s
}
}
u, err := url.Parse(s)
if err != nil {
return NodeURL{}, ErrNodeURL.Wrap(err)
}
if u.Scheme != "" && u.Scheme != "storj" {
return NodeURL{}, ErrNodeURL.New("unknown scheme %q", u.Scheme)
}
var node NodeURL
if u.User != nil {
node.ID, err = NodeIDFromString(u.User.String())
if err != nil {
return NodeURL{}, ErrNodeURL.Wrap(err)
}
}
node.Address = u.Host
query := u.Query()
if query.Get("noise_pub") != "" {
pubKey, _, err := base58.CheckDecode(query.Get("noise_pub"))
if err != nil {
return NodeURL{}, ErrNodeURL.Wrap(err)
}
node.NoiseInfo.PublicKey = string(pubKey)
}
if query.Get("noise_proto") != "" {
protoInt, err := strconv.Atoi(query.Get("noise_proto"))
if err != nil {
return NodeURL{}, ErrNodeURL.Wrap(err)
}
node.NoiseInfo.Proto = NoiseProto(protoInt)
}
if query.Get("debounce") != "" {
debounceInt, err := strconv.Atoi(query.Get("debounce"))
if err != nil {
return NodeURL{}, ErrNodeURL.Wrap(err)
}
node.DebounceLimit = debounceInt
}
if query.Get("f") != "" {
features, err := strconv.ParseUint(query.Get("f"), 16, 64)
if err != nil {
return NodeURL{}, ErrNodeURL.Wrap(err)
}
node.Features = features
}
return node, nil
}
// IsZero returns whether the url is empty.
func (u NodeURL) IsZero() bool {
return u == NodeURL{}
}
// String converts NodeURL to a string.
func (u NodeURL) String() string {
hasSuffix := u.DebounceLimit > 0 || u.Features > 0 || !u.NoiseInfo.IsZero()
if !hasSuffix {
if u.ID.IsZero() {
return u.Address
}
return u.ID.String() + "@" + u.Address
}
var s strings.Builder
s.Grow(160)
if !u.ID.IsZero() {
s.WriteString(u.ID.String())
s.WriteString("@")
}
s.WriteString(u.Address)
const maxIntBytes = 22
writeInt := func(v int64, base int) {
var buf [maxIntBytes]byte
s.Write(strconv.AppendInt(buf[:0], v, base))
}
writeUint := func(v uint64, base int) {
var buf [maxIntBytes]byte
s.Write(strconv.AppendUint(buf[:0], v, base))
}
delim := byte('?')
writeKey := func(key string) {
s.WriteByte(delim)
delim = '&'
s.WriteString(key)
}
if u.DebounceLimit > 0 {
writeKey("debounce=")
writeInt(int64(u.DebounceLimit), 10)
}
if u.Features > 0 {
writeKey("f=")
writeUint(u.Features, 16)
}
info := &u.NoiseInfo
if info.Proto != NoiseProto_Unset {
writeKey("noise_proto=")
writeInt(int64(info.Proto), 10)
}
if info.PublicKey != "" {
writeKey("noise_pub=")
// base58 is URL safe
s.WriteString(base58.CheckEncode([]byte(info.PublicKey), 0))
}
return s.String()
}
// Set implements flag.Value interface.
func (u *NodeURL) Set(s string) error {
parsed, err := ParseNodeURL(s)
if err != nil {
return ErrNodeURL.Wrap(err)
}
*u = parsed
return nil
}
// Type implements pflag.Value.
func (NodeURL) Type() string { return "storj.NodeURL" }
// NodeURLs defines a comma delimited flag for defining a list node url-s.
type NodeURLs []NodeURL
// ParseNodeURLs parses comma delimited list of node urls.
func ParseNodeURLs(s string) (NodeURLs, error) {
var urls NodeURLs
if s == "" {
return nil, nil
}
for _, s := range strings.Split(s, ",") {
u, err := ParseNodeURL(s)
if err != nil {
return nil, ErrNodeURL.Wrap(err)
}
urls = append(urls, u)
}
return urls, nil
}
// String converts NodeURLs to a string.
func (urls NodeURLs) String() string {
var xs []string
for _, u := range urls {
xs = append(xs, u.String())
}
return strings.Join(xs, ",")
}
// Set implements flag.Value interface.
func (urls *NodeURLs) Set(s string) error {
parsed, err := ParseNodeURLs(s)
if err != nil {
return ErrNodeURL.Wrap(err)
}
*urls = parsed
return nil
}
// Type implements pflag.Value.
func (NodeURLs) Type() string { return "storj.NodeURLs" }