-
-
Save JAremko/69392f63347b8406ed38 to your computer and use it in GitHub Desktop.
golang websocket over https proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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