1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-28 11:50:34 +00:00
norman/pkg/remotedialer/client.go

58 lines
1.7 KiB
Go
Raw Normal View History

package remotedialer
import (
"context"
2019-08-28 23:35:50 +00:00
"io/ioutil"
"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, dialer *websocket.Dialer, auth ConnectAuthorizer, onConnect func(context.Context) error) {
if err := connectToProxy(wsURL, headers, auth, dialer, onConnect); 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, onConnect func(context.Context) error) error {
logrus.WithField("url", proxyURL).Info("Connecting to proxy")
if dialer == nil {
dialer = &websocket.Dialer{HandshakeTimeout: 10 * time.Second}
}
2019-08-28 23:35:50 +00:00
ws, resp, err := dialer.Dial(proxyURL, headers)
if err != nil {
2019-08-29 21:24:11 +00:00
if resp == nil {
logrus.WithError(err).Errorf("Failed to connect to proxy. Empty dialer response")
2019-08-28 23:35:50 +00:00
} else {
2019-08-29 21:24:11 +00:00
rb, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
logrus.WithError(err).Errorf("Failed to connect to proxy. Response status: %v - %v. Couldn't read response body (err: %v)", resp.StatusCode, resp.Status, err2)
} else {
logrus.WithError(err).Errorf("Failed to connect to proxy. Response status: %v - %v. Response body: %s", resp.StatusCode, resp.Status, rb)
}
2019-08-28 23:35:50 +00:00
}
return err
}
defer ws.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if onConnect != nil {
if err := onConnect(ctx); err != nil {
return err
}
}
2018-10-31 20:14:22 +00:00
session := NewClientSession(auth, ws)
_, err = session.Serve()
session.Close()
return err
}