diff options
author | Wim <wim@42.be> | 2018-01-08 22:41:38 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-01-09 00:07:55 +0100 |
commit | be898b44c3c057c7b70efb83f66b7bbbdd5e9276 (patch) | |
tree | 01c8983913bcae177b5e0e015de9a45abe8369a7 /vendor/github.com/nlopes/slack/websocket_proxy.go | |
parent | 4828c434439949f62a6e219ab9947c221a06b111 (diff) | |
download | matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.gz matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.bz2 matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.zip |
Update vendor (slack)
Diffstat (limited to 'vendor/github.com/nlopes/slack/websocket_proxy.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/websocket_proxy.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/vendor/github.com/nlopes/slack/websocket_proxy.go b/vendor/github.com/nlopes/slack/websocket_proxy.go new file mode 100644 index 00000000..27f1cf99 --- /dev/null +++ b/vendor/github.com/nlopes/slack/websocket_proxy.go @@ -0,0 +1,82 @@ +package slack + +import ( + "crypto/tls" + "errors" + "net" + "net/http" + "net/http/httputil" + "net/url" + "os" + "strings" + + "golang.org/x/net/websocket" +) + +// Taken and reworked from: https://gist.github.com/madmo/8548738 +func websocketHTTPConnect(proxy, urlString string) (net.Conn, error) { + p, err := net.Dial("tcp", proxy) + if err != nil { + return nil, err + } + + turl, err := url.Parse(urlString) + if err != nil { + return nil, err + } + + req := http.Request{ + Method: "CONNECT", + URL: &url.URL{}, + Host: turl.Host, + } + + cc := httputil.NewProxyClientConn(p, nil) + if _, err := cc.Do(&req); err != nil { + return nil, err + } + + rwc, _ := cc.Hijack() + + return rwc, nil +} + +func websocketProxyDial(urlString, origin string) (ws *websocket.Conn, err error) { + if os.Getenv("HTTP_PROXY") == "" { + return websocket.Dial(urlString, "", origin) + } + + purl, err := url.Parse(os.Getenv("HTTP_PROXY")) + if err != nil { + return nil, err + } + + config, err := websocket.NewConfig(urlString, origin) + if err != nil { + return nil, err + } + + client, err := websocketHTTPConnect(purl.Host, urlString) + if err != nil { + return nil, err + } + + switch config.Location.Scheme { + case "ws": + case "wss": + tlsClient := tls.Client(client, &tls.Config{ + ServerName: strings.Split(config.Location.Host, ":")[0], + }) + err := tlsClient.Handshake() + if err != nil { + tlsClient.Close() + return nil, err + } + client = tlsClient + + default: + return nil, errors.New("invalid websocket schema") + } + + return websocket.NewClient(config, client) +} |