NewUtlsHTTPClient creates an HTTP client using utls Chrome TLS fingerprint. Use this for provider requests that need a Chrome-like TLS fingerprint. Falls back to standard transport for non-HTTPS requests.
(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration)
| 156 | // Use this for provider requests that need a Chrome-like TLS fingerprint. |
| 157 | // Falls back to standard transport for non-HTTPS requests. |
| 158 | func NewUtlsHTTPClient(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, timeout time.Duration) *http.Client { |
| 159 | var proxyURL string |
| 160 | if auth != nil { |
| 161 | proxyURL = strings.TrimSpace(auth.ProxyURL) |
| 162 | } |
| 163 | if proxyURL == "" && cfg != nil { |
| 164 | proxyURL = strings.TrimSpace(cfg.ProxyURL) |
| 165 | } |
| 166 | |
| 167 | var ctxRoundTripper http.RoundTripper |
| 168 | if ctx != nil { |
| 169 | ctxRoundTripper, _ = ctx.Value("cliproxy.roundtripper").(http.RoundTripper) |
| 170 | } |
| 171 | |
| 172 | var utlsRT http.RoundTripper = newUtlsRoundTripper(proxyURL) |
| 173 | var standardTransport http.RoundTripper = http.DefaultTransport |
| 174 | if proxyURL != "" { |
| 175 | if transport := buildProxyTransport(proxyURL); transport != nil { |
| 176 | standardTransport = transport |
| 177 | } |
| 178 | } else if ctxRoundTripper != nil { |
| 179 | utlsRT = ctxRoundTripper |
| 180 | standardTransport = ctxRoundTripper |
| 181 | } |
| 182 | |
| 183 | client := &http.Client{ |
| 184 | Transport: &fallbackRoundTripper{ |
| 185 | utls: utlsRT, |
| 186 | fallback: standardTransport, |
| 187 | }, |
| 188 | } |
| 189 | if timeout > 0 { |
| 190 | client.Timeout = timeout |
| 191 | } |
| 192 | return client |
| 193 | } |