-
Notifications
You must be signed in to change notification settings - Fork 256
/
netextended.go
104 lines (96 loc) · 3.39 KB
/
netextended.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
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package netextended
import (
"errors"
"fmt"
"net"
)
const (
domainNameMinLength = 2
domainNameMaxLength = 254
maxSegmentLength = 63
)
// ValidateHostname verifies the given hostname is a well-formed IP address
// or domain name, optionally including a port, and returns the hostname part.
func ValidateHostname(hostname string) (string, error) {
if len(hostname) == 0 {
return "", errors.New("must not be empty")
}
if len(hostname) < domainNameMinLength || len(hostname) > domainNameMaxLength {
return "", fmt.Errorf("must be at least %d and at most %d characters", domainNameMinLength, domainNameMaxLength)
}
parsedHost := hostname
if host, _, err := net.SplitHostPort(hostname); err == nil {
parsedHost = host
}
if net.ParseIP(parsedHost) != nil {
// hostname is a valid IP address
return hostname, nil
}
if err := isValidDomainName(parsedHost); err != nil {
return "", fmt.Errorf("must either be a valid IP address or domain name: invalid domain name %q, %w", hostname, err)
}
return hostname, nil
}
// isValidDomainName validates a hostname according to the requirements set for
// domain names internally in the Go standard library's net package, see
// golang.org/issue/12421.
//
// Adapted from https://github.com/golang/go/blob/f4e7a6b905ce60448e506a3f6578d01b60602cdd/src/net/dnsclient.go#L73-L128
// See https://github.com/golang/go/blob/f4e7a6b905ce60448e506a3f6578d01b60602cdd/LICENSE for the license.
func isValidDomainName(hostname string) error {
previous := rune('.')
nonNumeric := false
segmentLen := 0
for _, char := range hostname {
switch {
case '0' <= char && char <= '9':
segmentLen++
case 'a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' || char == '_':
nonNumeric = true
segmentLen++
case char == '-':
if previous == '.' {
return fmt.Errorf("cannot begin a segment after a period (.) with a hyphen (-)")
}
nonNumeric = true
segmentLen++
case char == '.':
if previous == '.' {
return fmt.Errorf("cannot contain two periods (.) in a row")
}
if previous == '-' {
return fmt.Errorf("cannot have a hyphen (-) immediately before a period (.)")
}
if segmentLen > maxSegmentLength {
return fmt.Errorf("cannot have segments greater than %v characters between periods (.)", maxSegmentLength)
}
segmentLen = 0
default:
return fmt.Errorf("included invalid character %q, must only contain letters, digits, periods (.), hyphens (-), or underscores (_)", char)
}
previous = char
}
if previous == '-' {
return fmt.Errorf("cannot have a hyphen (-) as the final character")
}
if segmentLen > maxSegmentLength {
return fmt.Errorf("cannot have segments greater than %v characters between periods (.)", maxSegmentLength)
}
if !nonNumeric {
return errors.New("must have at least one non-numeric character")
}
return nil
}