Can gorilla establish websocket connections through a socks5 proxy?
If not, is there a plan to do so?
Thank you!
You can connect through a socks5 proxy by setting the Dialer.NetDial field to the Dial method of a SOCKS5 Dialer.
netDialer, err := proxy.SOCKS5( .... )
if err != nil {
handle error
}
dialer := websocket.Dialer{NetDial: netDialer.Dial}
c, r, err := dialer.Dial(url, nil)
if err != nil {
handle error
}
We use the http.ProxyFromEnvironment function to setup the Dialer.Proxy field.
if proxy == nil {
proxy = http.ProxyFromEnvironment
}
...
dialer: websocket.Dialer{
Proxy: proxy,
...
},
Would it be possible to support this out of the box since golang recently added support for SOCKS5 from the environment? They have a similar implementation for the transport package here.
@vitreuz I don't want to add a dependency outside the standard library, nor do I want to vendor the x/net package as was done here.
I am open to suggestions on how to modify the Dialer to enable easy use of golang.org/x/net/proxy by applications.
How about adding this field to Dialer?
// ProxyDialerFromURL returns a dialer given a Proxy URL specification and an underlying
// dialer for it to make network requests. The Dialer.Dial method defaults to using an HTTP
// proxy if ProxyDialerFromURL is nil or the function returns an unknown scheme error.
//
// The ProxyFromURL function in the package golang.org/x/net/proxy can be used as
// a ProxyDialerFromURL function.
ProxyDialerFromURL func(u *url.URL, forward Dialer) (Dialer, error)
@garyburd Your solution looks great. It will solve the problem of needing to support SOCKS5 in our particular library NOAA. @melnikalex What do you think?
@garyburd I like it. Thank you!
Another approach is to bundle the x/net/proxy package. This avoids adding yet another knob to Dialer and is a much better option than vendoring all of x/net.
Most helpful comment
How about adding this field to Dialer?