-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas_ips.go
64 lines (62 loc) · 1.7 KB
/
as_ips.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
package netlib
import (
"errors"
"fmt"
"net"
"regexp"
"strings"
)
// AsIps resolves hostname:port pairs to IPv4-address:port pairs.
func AsIps(hostnamePortPairs ...string) ([]string, error) {
var (
hasPortExpr = regexp.MustCompile("[^:]:[0-9]+$")
n = len(hostnamePortPairs)
hostnames = make([]string, n)
portMappings = make([]string, n)
final = []string{}
err error
)
for i := 0; i < n; i++ {
if hasPortExpr.MatchString(hostnamePortPairs[i]) {
if hostnames[i], portMappings[i], err = net.SplitHostPort(hostnamePortPairs[i]); err != nil {
return nil, fmt.Errorf("AsIps: splitting hostname/port from %q: %s", hostnamePortPairs[i], err)
}
} else {
hostnames[i] = hostnamePortPairs[i]
}
hostnames[i] = strings.Trim(hostnames[i], "[]")
if ip := net.ParseIP(hostnames[i]); ip != nil {
portMappings[i] = hostnamePortPairs[i]
hostnames[i] = ""
}
}
var (
resolutions = BulkResolver4(hostnames...)
maybeColon string
reconstructed string
)
for i, r := range resolutions.Results {
if r.DomainName == "" {
// If DomainName is empty the entry is an ip, backfill the value from
// dual-purposed portMappings.
final = append(final, portMappings[i])
} else {
if r.Error == nil && len(r.Ips) > 0 {
if len(portMappings[i]) > 0 {
maybeColon = ":"
} else {
maybeColon = ""
}
reconstructed = fmt.Sprintf("%v%v%v", r.Ips[0], maybeColon, portMappings[i])
final = append(final, reconstructed)
}
}
}
if n != 0 && len(final) == 0 {
return nil, errors.New("AsIps: 0 ips were found in the bulk-dns-resolution result")
}
if err := resolutions.AnyErrors(); err != nil {
return nil, err
}
return final, nil
}