Gorilla Websocket - Proxy with URL, Username and Password

783 views
Skip to first unread message

Subramanian Sridharan

unread,
Mar 7, 2019, 6:21:47 AM3/7/19
to golang-nuts
Hello Gophers,

I've been using Gorilla Websocket for a persistent connection and it has been working as expected.
Now there is a need to achieve the same functionality over a Proxy.
After researching a bit, I came across
http.ProxyFromEnvironment

But I need to connect to the Proxy using an URL, a proxy username and password.

It looks like I should use this function 
func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)

Can someone explain how I should set the Username and Password for the hpd, if possible, with an example?

Subramanian Sridharan

unread,
Mar 8, 2019, 8:25:56 AM3/8/19
to golang-nuts
Here's the solution.

You generate a proxyUrl and then a dialer like so:

proxyURL, _ := url.Parse("http://proxy-ip:proxy-port")
proxyURL.User = url.UserPassword("proxy-username", "proxy-password")

dialer := websocket.Dialer{
    Proxy: http.ProxyURL(proxyURL),
}

conn, _, _ := dialer.Dial(WEBSOCKET_URL, nil)
Reply all
Reply to author
Forward
0 new messages