-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplithostport.go
279 lines (238 loc) · 5.59 KB
/
splithostport.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package core
import (
"net"
"net/netip"
"regexp"
"strconv"
"strings"
"golang.org/x/net/idna"
)
// MakeHostPort produces a validated host:port from an input string
// optionally using the given default port when the string doesn't
// specify one.
// port 0 on the string input isn't considered valid.
func MakeHostPort(hostPort string, defaultPort uint16) (string, error) {
host, port, err := SplitHostPort(hostPort)
if err != nil {
// bad input
return "", err
}
// port is either valid or empty
// host is either a valid hostname or a valid IP address
if ip, _ := ParseAddr(host); ip.IsValid() {
// IP address
if port == "" && defaultPort == 0 {
// portless IP
return ip.String(), nil
}
host = ipForHostPort(ip)
}
return doMakeHostPort(host, port, defaultPort)
}
func doMakeHostPort(host, port string, defaultPort uint16) (string, error) {
var ok bool
switch {
case port == "":
if defaultPort == 0 {
// portless hostname
return host, nil
}
port = strconv.FormatUint(uint64(defaultPort), 10)
ok = true
case port != "0":
ok = true
}
hostPort := host + ":" + port
if !ok {
return "", addrErr(hostPort, "invalid port")
}
return hostPort, nil
}
// JoinHostPort is like the standard net.JoinHostPort, but
// it validates the host name and port, and returns it portless
// if the port argument is empty.
func JoinHostPort(host, port string) (string, error) {
ip, _ := ParseAddr(host)
switch {
case ip.IsValid():
if port == "" {
// portless IP ready
return ip.String(), nil
}
host = ipForHostPort(ip)
default:
// not IP address
s, ok := validName(host)
switch {
case !ok:
// bad host name
return "", addrErr(host, "invalid host")
case port == "":
// portless host
return s, nil
default:
// good name
host = s
}
}
return doJoinHostPort(host, port)
}
func doJoinHostPort(host, port string) (string, error) {
hostPort := host + ":" + port
if !validPort(port) {
// bad port
return "", addrErr(hostPort, "invalid port")
}
return hostPort, nil
}
// SplitHostPort is like net.SplitHostPort but doesn't fail if the
// port isn't part of the string and it validates it if present.
// SplitHostPort will also validate the host is a valid IP or name
func SplitHostPort(hostPort string) (host, port string, err error) {
host, port, err = splitHostPortUnsafe(hostPort)
switch {
case err != nil:
// failed split
return "", "", err
case port != "" && !validPort(port):
// bad port
err = addrErr(hostPort, "invalid port")
return "", "", err
default:
if s, ok := validIP(host); ok {
// valid IP
return s, port, nil
}
if s, ok := validName(host); ok {
// valid name
return s, port, nil
}
err = addrErr(hostPort, "invalid address")
return "", "", err
}
}
// SplitAddrPort splits a string containing an IP address and an optional port,
// and validates it.
func SplitAddrPort(addrPort string) (addr netip.Addr, port uint16, err error) {
// split
host, sPort, err := splitHostPortUnsafe(addrPort)
if err != nil {
// failed to split
return netip.Addr{}, 0, err
}
// port
if sPort != "" {
port, err = parsePort(sPort)
if err != nil {
// bad port
err = addrErr(addrPort, "invalid port")
return netip.Addr{}, 0, err
}
}
// addr
addr, err = ParseAddr(host)
if err != nil {
// bad address
err = addrErr(addrPort, "invalid address")
return netip.Addr{}, 0, err
}
// success
return addr, port, nil
}
func splitHostPortUnsafe(hostPort string) (host, port string, err error) {
var ok bool
switch {
case hostPort == "":
// empty
err = addrErr(hostPort, "empty address")
return "", "", err
case hostPort[0] == '[':
// [host]:port [host]
return splitHostPortBracketed(hostPort)
case strings.Count(hostPort, ":") > 1:
// unbracketed IPv6
return hostPort, "", nil
}
host, port, ok = splitLastRune(':', hostPort)
switch {
case !ok:
// host without port
host, port = hostPort, ""
case port == "":
// host:
err = addrErr(hostPort, "missing port after ':'")
case host == "":
// :port
host = "::" // use undetermined host
}
return host, port, err
}
func splitHostPortBracketed(hostPort string) (host, port string, err error) {
host, s, ok := splitLastRune(']', hostPort[1:])
switch {
case !ok:
// [host
host = ""
err = addrErr(hostPort, "missing ']' in address")
case s == "":
// [host]
case s[0] == ':':
// [host]:...
port = s[1:]
if port == "" {
// [host]:
err = addrErr(hostPort, "missing port after ':'")
}
default:
// [host]...
host = ""
err = addrErr(hostPort, "invalid character after ']'")
}
return host, port, err
}
func splitLastRune(r rune, s string) (before, after string, found bool) {
i := strings.LastIndexFunc(s, func(v rune) bool {
return r == v
})
if i < 0 {
return s, "", false
}
return s[:i], s[i+1:], true
}
func parsePort(s string) (uint16, error) {
u64, err := strconv.ParseUint(s, 10, 16)
if err != nil {
return 0, err
}
return uint16(u64), nil
}
func validPort(s string) bool {
_, err := parsePort(s)
return err == nil
}
func validIP(s string) (string, bool) {
addr, err := ParseAddr(s)
if err == nil {
return addr.String(), true
}
return "", false
}
var nameRE = regexp.MustCompile(`^(([\p{L}\p{M}\p{N}_%+-]+\.)+)?[\p{L}\p{M}\p{N}-]+$`)
func validName(s string) (string, bool) {
if nameRE.MatchString(s) {
s, err := idna.Display.ToUnicode(s)
if err == nil {
return s, true
}
}
return "", false
}
func ipForHostPort(ip netip.Addr) string {
if ip.Is6() {
return "[" + ip.String() + "]"
}
return ip.String()
}
func addrErr(addr, why string) error {
return &net.AddrError{Err: why, Addr: addr}
}