Gorilla WebSocket: Configure Dialer.Proxy for HTTP CONNECT
Configure Dialer.Proxy for an HTTP forward proxy instead of treating an alternate TCP address as a completed proxy tunnel.
For an HTTP forward proxy, configure Gorilla’s Dialer.Proxy. The WebSocket URL remains the destination:
proxyURL, err := url.Parse("http://proxy.example:8080")
if err != nil {
return err
}
dialer := websocket.Dialer{
Proxy: http.ProxyURL(proxyURL),
HandshakeTimeout: 15 * time.Second,
}
conn, response, err := dialer.DialContext(
ctx, "wss://events.example/stream", nil)
This fragment belongs in the application’s connection function with net/url, net/http, time and github.com/gorilla/websocket available. Handle err and any failed-handshake response, then manage conn through the normal message and close lifecycle. For a permitted public WSS endpoint, the HTTP proxy list supplies candidates for proxyURL.
The HTTP proxy dialer opens the intermediary connection and sends CONNECT for the destination. For wss, destination TLS follows through the established tunnel before the WebSocket handshake.
NetDialContext customizes the lower-level TCP connection. Pointing it at a proxy port alone does not implement CONNECT. When Proxy is also set, the custom network callback dials the intermediary, while the proxy layer handles negotiation.
Keep the destination hostname in the wss URL so the normal TLS identity check applies to the intended server.