forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
42 lines (35 loc) · 1019 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package remotedialer
import (
"crypto/tls"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
)
type ConnectAuthorizer func(proto, address string) bool
func ClientConnect(wsURL string, headers http.Header, tlsConfig *tls.Config, auth ConnectAuthorizer) {
dialer := &websocket.Dialer{
TLSClientConfig: tlsConfig,
}
for {
if err := connectToProxy(wsURL, headers, auth, dialer); err != nil {
logrus.WithError(err).Error("Failed to connect to proxy")
time.Sleep(time.Duration(5) * time.Second)
}
}
}
func connectToProxy(proxyURL string, headers http.Header, auth ConnectAuthorizer, dialer *websocket.Dialer) error {
logrus.WithField("url", proxyURL).Info("Connecting to proxy")
if dialer == nil {
dialer = &websocket.Dialer{}
}
ws, _, err := dialer.Dial(proxyURL, headers)
if err != nil {
logrus.WithError(err).Error("Failed to connect to proxy")
return err
}
session := newClientSession(auth, ws)
_, err = session.serve()
session.Close()
return err
}