146 lines
3.8 KiB
Go
146 lines
3.8 KiB
Go
package netproxy
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ProxyFromEnvOrSystem resolves a proxy for req, preferring standard
|
|
// HTTP_PROXY/HTTPS_PROXY/NO_PROXY environment variables and falling back to
|
|
// the host OS proxy settings (Windows Internet Settings registry).
|
|
func ProxyFromEnvOrSystem(req *http.Request) (*url.URL, error) {
|
|
if u, err := http.ProxyFromEnvironment(req); u != nil || err != nil {
|
|
return u, err
|
|
}
|
|
return systemProxy(req)
|
|
}
|
|
|
|
// NewTransport returns an http.Transport configured with env/OS proxy support
|
|
// and bounded connection/header timeouts (without limiting streaming body
|
|
// download duration).
|
|
func NewTransport() *http.Transport {
|
|
var tr *http.Transport
|
|
if base, ok := http.DefaultTransport.(*http.Transport); ok {
|
|
tr = base.Clone()
|
|
} else {
|
|
tr = &http.Transport{}
|
|
}
|
|
tr.Proxy = ProxyFromEnvOrSystem
|
|
tr.DialContext = (&net.Dialer{
|
|
Timeout: 20 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
}).DialContext
|
|
tr.TLSHandshakeTimeout = 20 * time.Second
|
|
tr.ResponseHeaderTimeout = 30 * time.Second
|
|
return tr
|
|
}
|
|
|
|
// NewClient returns an http.Client using NewTransport.
|
|
func NewClient() *http.Client {
|
|
return &http.Client{Transport: NewTransport()}
|
|
}
|
|
|
|
// parseWindowsProxyServer parses Windows Internet Settings ProxyServer values:
|
|
// either a single proxy ("127.0.0.1:7890", "http://127.0.0.1:7890") or a
|
|
// semicolon-separated per-scheme map ("http=127.0.0.1:7890;https=127.0.0.1:7890;socks=127.0.0.1:7891").
|
|
func parseWindowsProxyServer(server, reqScheme string) (*url.URL, error) {
|
|
server = strings.TrimSpace(server)
|
|
if server == "" {
|
|
return nil, nil
|
|
}
|
|
reqScheme = strings.ToLower(strings.TrimSpace(reqScheme))
|
|
if reqScheme == "" {
|
|
reqScheme = "http"
|
|
}
|
|
|
|
var chosen, chosenKey string
|
|
if !strings.Contains(server, "=") {
|
|
chosen = server
|
|
} else {
|
|
byScheme := map[string]string{}
|
|
for _, entry := range strings.Split(server, ";") {
|
|
entry = strings.TrimSpace(entry)
|
|
if entry == "" {
|
|
continue
|
|
}
|
|
k, v, ok := strings.Cut(entry, "=")
|
|
if !ok {
|
|
continue
|
|
}
|
|
k = strings.ToLower(strings.TrimSpace(k))
|
|
v = strings.TrimSpace(v)
|
|
if k != "" && v != "" {
|
|
byScheme[k] = v
|
|
}
|
|
}
|
|
for _, key := range []string{reqScheme, "http", "https", "socks5", "socks"} {
|
|
if v, ok := byScheme[key]; ok {
|
|
chosen = v
|
|
chosenKey = key
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if chosen == "" {
|
|
return nil, nil
|
|
}
|
|
if !strings.Contains(chosen, "://") {
|
|
if chosenKey == "socks" || chosenKey == "socks5" {
|
|
chosen = "socks5://" + chosen
|
|
} else {
|
|
// In Windows Internet Settings, "https=host:port" denotes an HTTP
|
|
// CONNECT proxy used for HTTPS requests, not a TLS proxy endpoint.
|
|
chosen = "http://" + chosen
|
|
}
|
|
}
|
|
u, err := url.Parse(chosen)
|
|
if err != nil || u.Host == "" {
|
|
return nil, nil
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
// shouldBypassWindowsProxy evaluates Windows ProxyOverride rules (semicolon or
|
|
// whitespace separated patterns, including "<local>" and "*" wildcards).
|
|
func shouldBypassWindowsProxy(override, host string) bool {
|
|
host = strings.ToLower(strings.TrimSpace(host))
|
|
if host == "" {
|
|
return false
|
|
}
|
|
fields := strings.FieldsFunc(override, func(r rune) bool {
|
|
return r == ';' || r == ' ' || r == '\t' || r == ','
|
|
})
|
|
for _, pat := range fields {
|
|
pat = strings.ToLower(strings.TrimSpace(pat))
|
|
if pat == "" {
|
|
continue
|
|
}
|
|
if pat == "<local>" {
|
|
if !strings.Contains(host, ".") || host == "localhost" || host == "127.0.0.1" || host == "::1" {
|
|
return true
|
|
}
|
|
continue
|
|
}
|
|
// Strip optional scheme or port in Windows bypass rules (e.g. "http://*.local").
|
|
if _, after, ok := strings.Cut(pat, "://"); ok {
|
|
pat = after
|
|
}
|
|
if h, _, err := net.SplitHostPort(pat); err == nil && h != "" {
|
|
pat = h
|
|
}
|
|
if pat == host {
|
|
return true
|
|
}
|
|
if strings.ContainsAny(pat, "*?") {
|
|
if matched, err := path.Match(pat, host); err == nil && matched {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|