-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
62 lines (57 loc) · 1.44 KB
/
validate.go
File metadata and controls
62 lines (57 loc) · 1.44 KB
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
package sysproxy
import (
"fmt"
"net/url"
"strings"
)
// proxy holds a parsed proxy URL for internal use by platform backends.
type proxy struct {
rawURL string
host string
port string
user string
pass string
}
func parse(rawURL string) (*proxy, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("sysproxy: invalid URL: %w", err)
}
p := &proxy{
rawURL: rawURL,
host: u.Hostname(),
port: u.Port(),
}
if u.User != nil {
p.user = u.User.Username()
p.pass, _ = u.User.Password()
}
return p, nil
}
func validateProxyURL(rawURL string) error {
u, err := url.Parse(rawURL)
if err != nil {
return fmt.Errorf("sysproxy: cannot parse proxy URL %q: %w", rawURL, err)
}
if u.Scheme == "" {
return fmt.Errorf("sysproxy: proxy URL %q missing scheme (e.g. http://host:port)", rawURL)
}
if u.Hostname() == "" {
return fmt.Errorf("sysproxy: proxy URL %q missing host", rawURL)
}
if port := u.Port(); port != "" {
var n int
if _, err := fmt.Sscanf(port, "%d", &n); err != nil || n < 1 || n > 65535 {
return fmt.Errorf("sysproxy: port %q out of range 1–65535 in proxy URL %q", port, rawURL)
}
}
return nil
}
func validatePACURL(pacURL string) error {
if !strings.HasPrefix(pacURL, "http://") &&
!strings.HasPrefix(pacURL, "https://") &&
!strings.HasPrefix(pacURL, "file://") {
return fmt.Errorf("sysproxy: PAC URL must use http, https, or file scheme (got %q)", pacURL)
}
return nil
}