Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Last active June 23, 2025 04:52
Show Gist options
  • Save wjkoh/17a8c53301a3fb82c3e83eb8756e6868 to your computer and use it in GitHub Desktop.
Save wjkoh/17a8c53301a3fb82c3e83eb8756e6868 to your computer and use it in GitHub Desktop.
Go: How to use a proxy server with `net/http` (ft. Bright Data)
// Please refer to the following code if you encounter "tls: failed to verify certificate: x509: “*.example.com” certificate is not standards compliant"
package main
import (
"crypto/tls"
"crypto/x509"
"errors"
"net"
"net/http"
"net/url"
)
//go:embed proxy.crt
var proxyCert []byte
// NewProxyClient creates an HTTP client configured to use a proxy server
// with a custom CA certificate. This helps resolve "tls: failed to verify certificate:
// x509: “*.example.com” certificate is not standards compliant" errors.
func NewProxyClient() (*http.Client, error) {
const (
proxyUser = "user"
proxyPassword = "password"
proxyHost = "proxy.example.com"
proxyPort = "33335"
)
proxyURL := &url.URL{
Scheme: "http",
User: url.UserPassword(proxyUser, proxyPassword),
Host: net.JoinHostPort(proxyHost, proxyPort),
}
// Download and use the proxy server's CA certificate to avoid the
// `tls: failed to verify certificate: x509: “*.example.com” certificate is not standards compliant` error.
// Check out https://docs.brightdata.com/general/account/ssl-certificate.
pool := x509.NewCertPool()
ok := pool.AppendCertsFromPEM(proxyCert)
if !ok {
return nil, errors.New("AppendCertsFromPEM failed")
}
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{RootCAs: pool},
DisableKeepAlives: true,
},
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment