summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nlopes/slack/websocket_proxy.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-09-05 16:34:37 +0200
committerWim <wim@42.be>2016-09-05 16:34:37 +0200
commitb30e85836e575974bac1b2ea91e9b6c2dd39fe44 (patch)
treeb3aa5d148e436cacd6c737ad56ba3fc0b9369e26 /vendor/github.com/nlopes/slack/websocket_proxy.go
parente449a97bd0114e55c2a73aa79b061b55d755aa16 (diff)
downloadmatterbridge-msglm-b30e85836e575974bac1b2ea91e9b6c2dd39fe44.tar.gz
matterbridge-msglm-b30e85836e575974bac1b2ea91e9b6c2dd39fe44.tar.bz2
matterbridge-msglm-b30e85836e575974bac1b2ea91e9b6c2dd39fe44.zip
Add Slack support
Diffstat (limited to 'vendor/github.com/nlopes/slack/websocket_proxy.go')
-rw-r--r--vendor/github.com/nlopes/slack/websocket_proxy.go83
1 files changed, 83 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..440015d6
--- /dev/null
+++ b/vendor/github.com/nlopes/slack/websocket_proxy.go
@@ -0,0 +1,83 @@
+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)
+ cc.Do(&req)
+ if err != nil && err != httputil.ErrPersistEOF {
+ 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)
+}