| 1 | |
| 2 | |
| 3 | package hostproxy |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "os" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "time" |
| 14 | |
| 15 | "golang.org/x/net/http/httpproxy" |
| 16 | ) |
| 17 | |
| 18 | var environment struct { |
| 19 | sync.Mutex |
| 20 | config *httpproxy.Config |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | func CaptureEnvironment() { |
| 26 | environment.Lock() |
| 27 | defer environment.Unlock() |
| 28 | if environment.config == nil { |
| 29 | environment.config = readEnvironment() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func readEnvironment() *httpproxy.Config { |
| 34 | cfg := httpproxy.FromEnvironment() |
| 35 | all := os.Getenv("ALL_PROXY") |
| 36 | if all == "" { |
| 37 | all = os.Getenv("all_proxy") |
| 38 | } |
| 39 | if cfg.HTTPProxy == "" { |
| 40 | cfg.HTTPProxy = all |
| 41 | } |
| 42 | if cfg.HTTPSProxy == "" { |
| 43 | cfg.HTTPSProxy = all |
| 44 | } |
| 45 | return cfg |
| 46 | } |
| 47 | |
| 48 | func NewTransport() *http.Transport { |
| 49 | environment.Lock() |
| 50 | cfg := environment.config |
| 51 | if cfg == nil { |
| 52 | cfg = readEnvironment() |
| 53 | } |
| 54 | copy := *cfg |
| 55 | environment.Unlock() |
| 56 | return transport(©, systemProxy) |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | func DownloadEnvironment() []string { |
| 62 | environment.Lock() |
| 63 | defer environment.Unlock() |
| 64 | cfg := environment.config |
| 65 | if cfg == nil { |
| 66 | cfg = readEnvironment() |
| 67 | } |
| 68 | return []string{ |
| 69 | "HTTP_PROXY=" + cfg.HTTPProxy, "http_proxy=" + cfg.HTTPProxy, |
| 70 | "HTTPS_PROXY=" + cfg.HTTPSProxy, "https_proxy=" + cfg.HTTPSProxy, |
| 71 | "NO_PROXY=" + cfg.NoProxy, "no_proxy=" + cfg.NoProxy, |
| 72 | "ALL_PROXY=", "all_proxy=", |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func transport(env *httpproxy.Config, system func(*http.Request) (*url.URL, error)) *http.Transport { |
| 77 | envProxy := env.ProxyFunc() |
| 78 | return &http.Transport{ |
| 79 | Proxy: func(req *http.Request) (*url.URL, error) { |
| 80 | value := env.HTTPProxy |
| 81 | if req.URL.Scheme == "https" { |
| 82 | value = env.HTTPSProxy |
| 83 | } |
| 84 | if value != "" { |
| 85 | if _, err := proxyURL(value); err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | return envProxy(req.URL) |
| 89 | } |
| 90 | return system(req) |
| 91 | }, |
| 92 | |
| 93 | DialContext: (&net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}).DialContext, |
| 94 | ForceAttemptHTTP2: true, TLSHandshakeTimeout: 15 * time.Second, |
| 95 | ResponseHeaderTimeout: 30 * time.Second, IdleConnTimeout: 90 * time.Second, |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func proxyURL(value string) (*url.URL, error) { |
| 100 | value = strings.TrimSpace(value) |
| 101 | if !strings.Contains(value, "://") { |
| 102 | value = "http://" + value |
| 103 | } |
| 104 | u, err := url.Parse(value) |
| 105 | if err != nil || u.Hostname() == "" { |
| 106 | return nil, errors.New("主机代理地址无效,请检查系统代理设置") |
| 107 | } |
| 108 | switch u.Scheme { |
| 109 | case "http", "https", "socks5", "socks5h": |
| 110 | return u, nil |
| 111 | default: |
| 112 | return nil, errors.New("主机代理协议不受支持,请使用 HTTP 或 SOCKS5 系统代理") |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func staticProxy(req *http.Request, httpProxy, httpsProxy, bypass string) (*url.URL, error) { |
| 117 | selected := httpProxy |
| 118 | if req.URL.Scheme == "https" { |
| 119 | selected = httpsProxy |
| 120 | } |
| 121 | if selected != "" { |
| 122 | if _, err := proxyURL(selected); err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | } |
| 126 | cfg := httpproxy.Config{HTTPProxy: httpProxy, HTTPSProxy: httpsProxy, NoProxy: strings.ReplaceAll(bypass, ";", ",")} |
| 127 | u, err := cfg.ProxyFunc()(req.URL) |
| 128 | if err != nil { |
| 129 | return nil, errors.New("主机代理设置无效,请检查系统代理设置") |
| 130 | } |
| 131 | if u != nil { |
| 132 | return proxyURL(u.String()) |
| 133 | } |
| 134 | return nil, nil |
| 135 | } |