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
package networkdial
import (
"crypto/tls"
"errors"
"fmt"
"maps"
"net"
"sort"
"strings"
)
var (
ErrNoResolver = errors.New("networkdial: no host resolver was given")
ErrUnknownHost = errors.New("networkdial: the host is not in the network host map")
ErrInvalidHost = errors.New("networkdial: invalid host map entry")
ErrNoCABundle = errors.New("networkdial: no network CA bundle was given")
ErrInvalidCABundle = errors.New("networkdial: the network CA bundle has no certificates")
ErrSystemDNSBlocked = errors.New("networkdial: system DNS is not allowed")
)
type Host struct {
Name string
Address string
}
type Resolver interface {
Address(hostport string) (string, bool)
}
type HostMap struct {
hosts map[string]string
}
func NewHostMap(hosts []Host) (*HostMap, error) {
m := &HostMap{hosts: map[string]string{}}
for _, host := range hosts {
if err := m.add(host.Name, host.Address); err != nil {
return nil, err
}
}
return m, nil
}
func HostMapFromPairs(pairs map[string]string) (*HostMap, error) {
m := &HostMap{hosts: map[string]string{}}
for name, address := range pairs {
if err := m.add(name, address); err != nil {
return nil, err
}
}
return m, nil
}
func ParseHostMap(value string) (*HostMap, error) {
m := &HostMap{hosts: map[string]string{}}
for pair := range strings.SplitSeq(value, ",") {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
i := strings.IndexByte(pair, '=')
if i <= 0 {
return nil, fmt.Errorf("%w: %q is not name=address", ErrInvalidHost, pair)
}
if err := m.add(pair[:i], pair[i+1:]); err != nil {
return nil, err
}
}
return m, nil
}
func (m *HostMap) add(name, address string) error {
host := normalizeHost(name)
if host == "" {
return fmt.Errorf("%w: %q has no host name", ErrInvalidHost, name)
}
address = strings.TrimSpace(address)
if address == "" {
return fmt.Errorf("%w: %q has no address", ErrInvalidHost, name)
}
_, port, err := net.SplitHostPort(address)
if err != nil {
return fmt.Errorf("%w: %q address %q is not host:port: %v", ErrInvalidHost, name, address, err)
}
if port == "" {
return fmt.Errorf("%w: %q address %q has no port", ErrInvalidHost, name, address)
}
if _, ok := m.hosts[host]; ok {
return fmt.Errorf("%w: %q appears more than once", ErrInvalidHost, host)
}
m.hosts[host] = address
return nil
}
func (m *HostMap) Lookup(host string) (string, bool) {
if m == nil {
return "", false
}
name := normalizeHost(host)
if address, ok := m.hosts[name]; ok {
return address, true
}
for base, address := range m.hosts {
if strings.HasSuffix(name, "."+base) {
return address, true
}
}
return "", false
}
func (m *HostMap) Address(hostport string) (string, bool) {
return m.Lookup(hostport)
}
func (m *HostMap) Hosts() map[string]string {
if m == nil {
return nil
}
out := make(map[string]string, len(m.hosts))
maps.Copy(out, m.hosts)
return out
}
func (m *HostMap) Names() []string {
if m == nil {
return nil
}
out := make([]string, 0, len(m.hosts))
for name := range m.hosts {
out = append(out, name)
}
sort.Strings(out)
return out
}
func (m *HostMap) Len() int {
if m == nil {
return 0
}
return len(m.hosts)
}
func (m *HostMap) TLSConfig(caBundlePEM []byte, host string) (*tls.Config, error) {
name := normalizeHost(host)
if name == "" {
return nil, fmt.Errorf("%w: %q", ErrInvalidHost, host)
}
if _, ok := m.Lookup(name); !ok {
return nil, fmt.Errorf("%w: %s", ErrUnknownHost, name)
}
return TLSConfigFromCA(caBundlePEM, name)
}
func normalizeHost(value string) string {
host := strings.TrimSpace(value)
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
host = strings.Trim(host, "[]")
host = strings.TrimSuffix(host, ".")
host = strings.ToLower(host)
if strings.ContainsAny(host, "/?#@") {
return ""
}
return host
}