-
Notifications
You must be signed in to change notification settings - Fork 13
/
host.go
180 lines (146 loc) · 3.6 KB
/
host.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
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package forwarder
import (
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strconv"
"strings"
)
type HostPort struct {
Host string
Port string
}
func (hp HostPort) Validate() error {
if hp.Host != "" && hp.Host != "*" {
if !isDomainName(hp.Host) {
if ip := net.ParseIP(hp.Host); ip == nil {
return fmt.Errorf("invalid host %q", hp.Host)
}
}
}
if hp.Port != "" {
if _, err := strconv.ParseUint(hp.Port, 10, 16); err != nil {
return fmt.Errorf("invalid port %q", hp.Port)
}
}
return nil
}
type HostPortUser struct {
HostPort
*url.Userinfo
}
// ParseHostPortUser parses a user:password@host:port string into HostUser.
func ParseHostPortUser(val string) (*HostPortUser, error) {
if val == "" || !strings.Contains(val, "@") {
return nil, errors.New("expected user[:password]@host:port")
}
idx := strings.LastIndex(val, "@")
up := val[:idx]
hp := val[idx:]
ui, err := ParseUserinfo(up)
if err != nil {
return nil, err
}
u, err := url.Parse("http://" + wildcardPortTo0(hp))
if err != nil {
return nil, err
}
hpi := &HostPortUser{
HostPort: HostPort{
Host: u.Hostname(),
Port: u.Port(),
},
Userinfo: ui,
}
if err := hpi.Validate(); err != nil {
return nil, err
}
return hpi, nil
}
func (hpu *HostPortUser) Validate() error {
if hpu.Host == "" {
return errors.New("missing host")
}
if hpu.Port == "" {
return errors.New("missing port")
}
if err := hpu.HostPort.Validate(); err != nil {
return err
}
if hpu.Userinfo == nil {
return errors.New("missing user")
}
if err := validatedUserInfo(hpu.Userinfo); err != nil {
return err
}
return nil
}
func (hpu *HostPortUser) String() string {
if hpu == nil {
return ""
}
port := hpu.Port
if port == "0" {
port = "*"
}
p, ok := hpu.Password()
if !ok {
return fmt.Sprintf("%s@%s:%s", hpu.Username(), hpu.Host, port)
}
return fmt.Sprintf("%s:%s@%s:%s", hpu.Username(), p, hpu.Host, port)
}
func RedactHostPortUser(hpu *HostPortUser) string {
if hpu == nil {
return ""
}
port := hpu.Port
if port == "0" {
port = "*"
}
if _, ok := hpu.Password(); !ok {
return fmt.Sprintf("%s@%s:%s", hpu.Username(), hpu.Host, port)
}
return fmt.Sprintf("%s:xxxxx@%s:%s", hpu.Username(), hpu.Host, port)
}
type HostPortPair struct {
Src, Dst HostPort
}
func (p HostPortPair) String() string {
return fmt.Sprintf("%s:%s:%s:%s", p.Src.Host, p.Src.Port, p.Dst.Host, p.Dst.Port)
}
func (p HostPortPair) Validate() error {
if err := p.Src.Validate(); err != nil {
return fmt.Errorf("src: %w", err)
}
if err := p.Dst.Validate(); err != nil {
return fmt.Errorf("dst: %w", err)
}
return nil
}
// ParseHostPortPair parses HOST1:PORT1:HOST2:PORT2 string into HostPortPair.
// HOST1:PORT1 is the source, HOST2:PORT2 is the destination.
func ParseHostPortPair(val string) (HostPortPair, error) {
const (
dns = `[.\w\-]+`
ip4 = `[.0-9]+`
ip6 = `\[?[:0-9a-fA-F]+\]?`
)
hostPortPairRe := regexp.MustCompile(`^(` + dns + `|` + ip4 + `|` + ip6 + `)?:(\d+)?:(` + dns + `|` + ip4 + `|` + ip6 + `)?:(\d+)?$`)
m := hostPortPairRe.FindStringSubmatch(val)
if m == nil {
return HostPortPair{}, errors.New("expected src_host:src_port:dst_host:dst_port")
}
r := strings.NewReplacer("[", "", "]", "")
hpp := HostPortPair{
Src: HostPort{Host: r.Replace(m[1]), Port: m[2]},
Dst: HostPort{Host: r.Replace(m[3]), Port: m[4]},
}
return hpp, hpp.Validate()
}