Skip to content

Instantly share code, notes, and snippets.

@JAremko
Forked from madmo/gist:8548738
Last active February 20, 2020 00:46
Show Gist options
  • Save JAremko/69392f63347b8406ed38 to your computer and use it in GitHub Desktop.
Save JAremko/69392f63347b8406ed38 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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment