Skip to content

Instantly share code, notes, and snippets.

@madmo
Created January 21, 2014 21:27
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save madmo/8548738 to your computer and use it in GitHub Desktop.
Save madmo/8548738 to your computer and use it in GitHub Desktop.
golang websocket over https proxy
func HttpConnect(proxy, url_ string) (io.ReadWriteCloser, error) {
p, err := net.Dial("tcp", proxy)
if err != nil {
return nil, err
}
turl, err := url.Parse(url_)
if err != nil {
return nil, err
}
req := http.Request{
Method: "CONNECT",
URL: &url.URL{},
Host: turl.Host,
}
cc := httputil.NewProxyClientConn(p, nil)
cc.Do(&req)
if err != nil && err != httputil.ErrPersistEOF {
return nil, err
}
rwc, _ := cc.Hijack()
return rwc, nil
}
func ProxyDial(url_, protocol, origin string) (ws *websocket.Conn, err error) {
if os.Getenv("HTTP_PROXY") == "" {
return websocket.Dial(url_, protocol, origin)
}
purl, err := url.Parse(os.Getenv("HTTP_PROXY"))
if err != nil {
return nil, err
}
config, err := websocket.NewConfig(url_, origin)
if err != nil {
return nil, err
}
if protocol != "" {
config.Protocol = []string{protocol}
}
client, err := HttpConnect(purl.Host, url_)
if err != nil {
return nil, err
}
return websocket.NewClient(config, client)
}
@jasonolmstead33
Copy link

Thank you for this! saved me a ton of time looking for the answer!

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