Package httpproxy provides support for HTTP proxy determination based on environment variables, as provided by net/http's ProxyFromEnvironment function.
The API is not subject to the Go 1 compatibility promise and may change at any time.
import "golang.org/x/net/http/httpproxy"// Config holds configuration for HTTP proxy settings
type Config struct {
// HTTPProxy represents the value of the HTTP_PROXY or http_proxy environment variable
HTTPProxy string
// HTTPSProxy represents the HTTPS_PROXY or https_proxy environment variable
HTTPSProxy string
// NoProxy represents the NO_PROXY or no_proxy environment variable
NoProxy string
// CGI holds whether the current process is running as a CGI handler
CGI bool
}// FromEnvironment returns a Config instance populated from environment variables
func FromEnvironment() *Config
// ProxyFunc returns a function that determines the proxy URL to use for a given request URL
func (cfg *Config) ProxyFunc() func(reqURL *url.URL) (*url.URL, error)FromEnvironment returns a Config instance populated from the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof).
The environment values may be either a complete URL or a "host[:port]", in which case the "http" scheme is assumed. An error is returned if the value is a different form.
ProxyFunc returns a function that determines the proxy URL to use for a given request URL. Changing the contents of cfg will not affect proxy functions created earlier.
A nil URL and nil error are returned if no proxy is defined in the environment, or a proxy should not be used for the given request, as defined by NO_PROXY.
As a special case, if req.URL.Host is "localhost" or a loopback address (with or without a port number), then a nil URL and nil error will be returned.
import (
"fmt"
"golang.org/x/net/http/httpproxy"
"net/url"
)
func getProxyFromEnv() {
// Read proxy configuration from environment
config := httpproxy.FromEnvironment()
fmt.Printf("HTTP Proxy: %s\n", config.HTTPProxy)
fmt.Printf("HTTPS Proxy: %s\n", config.HTTPSProxy)
fmt.Printf("No Proxy: %s\n", config.NoProxy)
// Create proxy function
proxyFunc := config.ProxyFunc()
// Determine proxy for a specific URL
targetURL, _ := url.Parse("https://example.com/api")
proxyURL, err := proxyFunc(targetURL)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
if proxyURL != nil {
fmt.Printf("Use proxy: %s\n", proxyURL)
} else {
fmt.Println("No proxy needed")
}
}func customProxyConfig() {
config := &httpproxy.Config{
HTTPProxy: "http://proxy.example.com:8080",
HTTPSProxy: "http://proxy.example.com:8443",
NoProxy: "localhost,127.0.0.1,.local,internal.example.com",
CGI: false,
}
proxyFunc := config.ProxyFunc()
// Test different URLs
testURLs := []string{
"http://example.com",
"https://secure.example.com",
"http://localhost:8080",
"http://internal.example.com",
}
for _, urlStr := range testURLs {
targetURL, _ := url.Parse(urlStr)
proxyURL, _ := proxyFunc(targetURL)
if proxyURL != nil {
fmt.Printf("%s -> proxy: %s\n", urlStr, proxyURL)
} else {
fmt.Printf("%s -> direct connection\n", urlStr)
}
}
}import (
"golang.org/x/net/http/httpproxy"
"net/http"
"net/url"
)
func createClientWithProxy() *http.Client {
config := httpproxy.FromEnvironment()
proxyFunc := config.ProxyFunc()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return proxyFunc(req.URL)
},
}
return &http.Client{
Transport: transport,
}
}