Proxy configuration

更新时间:
复制 MD 格式

The V2.0 Go SDK supports HTTP, HTTPS, and SOCKS5 proxies. You can configure proxies through environment variables, Config, or RuntimeOptions.

Proxy types

The SDK supports HTTP, HTTPS, and SOCKS5 proxies. Use only one proxy type at a time.

Proxy type

Description

HTTP proxy

Set httpProxy to the proxy address in the format http://<IP address>:<port>. Applies to HTTP requests only.

HTTPS proxy

Set httpsProxy to the proxy address in the format http://<IP address>:<port>. Applies to HTTPS requests only.

SOCKS5 proxy

Set socks5Proxy to the proxy address in the format socks5://<IP address>:<port>, and set Socks5NetWork to the transport protocol (TCP or UDP).

Note

If the proxy requires authentication, include the username and password in the URL: http://<user>:<password>@<IP address>:<port> or socks5://<user>:<password>@<IP address>:<port>.

For HTTP or HTTPS proxies, use noProxy to specify comma-separated addresses (domain names or IPs) that bypass the proxy.

Configuration methods

Note

Priority order (highest to lowest): RuntimeOptions > Config > environment variables.

  • Configure the proxy with environment variables.

    Note

    SOCKS5 proxy does not support environment variable configuration.

    • HTTP_PROXY or http_proxy: the HTTP proxy address.

    • HTTPS_PROXY or https_proxy: the HTTPS proxy address.

    • NO_PROXY or no_proxy: addresses that bypass the proxy.

  • Configure the proxy in RuntimeOptions. This applies only to requests that use these runtime parameters.

    import (
        util "github.com/alibabacloud-go/tea-utils/v2/service"
        openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
        "github.com/alibabacloud-go/tea/tea"
    )
    
    config := &openapi.Config{
        Protocol: tea.String("https"),
    }
    
    runtime := &util.RuntimeOptions{
        // Modify the request protocol (Protocol) as needed. httpsProxy is effective only for the HTTPS protocol, and httpProxy is effective only for the HTTP protocol.
        HttpProxy:  tea.String("http://127.0.0.1:9898"),
        HttpsProxy: tea.String("http://127.0.0.1:8989"),
        
        NoProxy:    tea.String("127.0.0.1,localhost"),
    
        // SOCKS5 proxy
        // Socks5Proxy:   tea.String("socks5://127.0.0.1:8989"),
        // Socks5NetWork: tea.String("tcp"), 
    }
    
  • Configure the proxy in Config when initializing the client. This applies to all requests.

    Example:

    import (
        openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
        "github.com/alibabacloud-go/tea/tea"
    )
    
    config := &openapi.Config{
        // httpsProxy is effective only for the HTTPS protocol, and httpProxy is effective only for the HTTP protocol.
        Protocol: tea.String("https"),
    
        // HTTP proxy
        HttpProxy:  tea.String("http://127.0.0.1:9898"),
        // HTTPS proxy
        HttpsProxy: tea.String("http://127.0.0.1:8989"),
        
        NoProxy:    tea.String("127.0.0.1,localhost"),
    
        // SOCKS5 proxy
        // Socks5Proxy:   tea.String("socks5://127.0.0.1:8989"),
        // Socks5NetWork: tea.String("tcp"),
    }
    

References

HTTP proxy configuration practice