1

Golang HTTP Client using SOCKS5 proxy and DialContext · GitHub

 1 year ago
source link: https://gist.github.com/ometa/71d23ed48c03c003f6e4910648612859
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Golang HTTP Client using SOCKS5 proxy and DialContext · GitHub

Instantly share code, notes, and snippets.

Golang HTTP Client using SOCKS5 proxy and DialContext

Hm. Why do I need all this DialContext: dialContext when it already uses http.ProxyFromEnvironment?

Hm. Why do I need all this DialContext: dialContext when it already uses http.ProxyFromEnvironment?

this code could use any proxy at all, not just one from the environment, which is quite limiting, if say, for instance, you wanted to use 10 different proxies at the same time, or round robin between them, etc...

I agree in this example, to make it clearer, I'd not use os.Getenv("PROXY_HOST") and not http.ProxyFromEnvironment!

Ah makes sense, thanks

in this example, to make it clearer, I'd not use os.Getenv("PROXY_HOST") and not http.ProxyFromEnvironment!

How to use it then? I can't see how I can pass my SOCKS5 proxy setting to the code.

Would you give another complete demo code to use it please?

resp, err := http.Get("http://example.com/") 
if err != nil { // handle error } 

defer resp.Body.Close() 
body, err := ioutil.ReadAll(resp.Body)

in this example, to make it clearer, I'd not use os.Getenv("PROXY_HOST") and not http.ProxyFromEnvironment!

How to use it then? I can't see how I can pass my SOCKS5 proxy setting to the code.

Would you give another complete demo code to use it please?

resp, err := http.Get("http://example.com/") 
if err != nil { // handle error } 

defer resp.Body.Close() 
body, err := ioutil.ReadAll(resp.Body)
client := NewClientFromEnv()
resp, err := client.Get("http://example.com")

client := NewClientFromEnv()
resp, err := client.Get("http://example.com")

Thanks, and where can I find the definition of such NewClientFromEnv() please?

Thanks, and where can I find the definition of such NewClientFromEnv() please?

@suntong, Here it is. It is a part of snippet

Another way to get the dial context:

    dialer, err := proxy.SOCKS5("tcp", proxyUrl, nil, proxy.Direct)
    dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
        return dialer.Dial(network, address)
    }

Source: https://stackoverflow.com/a/63661992

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK