From 19ac7cab0bf1fdc8fafe6a3723bad5e30aac5ee7 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Tue, 16 Dec 2025 10:27:20 -0500 Subject: [PATCH] Switch to coder/websocket and make library context aware Related PR https://github.com/rancher/remotedialer/pull/147 Note: This contains a replace directive in go.mod due to remoteDialer PR not yet being merged. It should be removed after PR approval, but prior to merging. --- go.mod | 5 ++++- go.sum | 6 ++++-- proxy/server_test.go | 3 +-- proxyclient/client.go | 48 +++++++++++++++++++++++-------------------- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 4e0bf46..beb9ef5 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.24.0 toolchain go1.24.6 require ( + github.com/coder/websocket v1.8.14 github.com/gorilla/mux v1.8.1 - github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/rancher/dynamiclistener v0.7.3-rc.1 github.com/rancher/remotedialer v0.6.0-rc.1 github.com/rancher/wrangler/v3 v3.3.1 @@ -32,6 +32,7 @@ require ( github.com/google/gnostic-models v0.7.0 // indirect github.com/google/go-cmp v0.7.0 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/mailru/easyjson v0.7.7 // indirect @@ -71,3 +72,5 @@ require ( sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect sigs.k8s.io/yaml v1.6.0 // indirect ) + +replace github.com/rancher/remotedialer => github.com/commitgo/remotedialer v0.0.0-20251206042109-bfe21d19229c diff --git a/go.sum b/go.sum index d39a0a6..6b2ed6a 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,10 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g= +github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg= +github.com/commitgo/remotedialer v0.0.0-20251206042109-bfe21d19229c h1:nfeiY7eT0Yku/9WxU3FsmlwtYLyk8c3hhpUCtxVDQFU= +github.com/commitgo/remotedialer v0.0.0-20251206042109-bfe21d19229c/go.mod h1:moyd+I/6xt+z3MX74aXomNRvYZc9iCdsVKkaPTKxRDk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -86,8 +90,6 @@ github.com/rancher/dynamiclistener v0.7.3-rc.1 h1:F4Et+TaL8RcXTdTvJKuj6OL5Gg1I1I github.com/rancher/dynamiclistener v0.7.3-rc.1/go.mod h1:++39p1G3pz0N7yRzvZsjc+5x4l0cMUi2jo49Fy+IbCE= github.com/rancher/lasso v0.2.5 h1:K++lWDDdfeN98Ixc1kCfUq0/q6tLjoHN++Np6QntXw0= github.com/rancher/lasso v0.2.5/go.mod h1:71rWfv+KkdSmSxZ9Ly5QYhxAu0nEUcaq9N2ByjcHqAM= -github.com/rancher/remotedialer v0.6.0-rc.1 h1:HMwcJjjT4irqM+d++jPcpjoNfhPCaxoHIyPzdpghZhE= -github.com/rancher/remotedialer v0.6.0-rc.1/go.mod h1:CW6Q8F8IESN05/yl48OSwhVi54nDwVQQriV16zAiGkg= github.com/rancher/wrangler/v3 v3.3.1 h1:YFqRfhxjuLNudUrvWrn+64wUPZ8pnn2KWbTsha75JLg= github.com/rancher/wrangler/v3 v3.3.1/go.mod h1:0D4kZDaOUkP5W2Zfww/75tQwF9w7kaZgzpZG+4XQDAI= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= diff --git a/proxy/server_test.go b/proxy/server_test.go index 62d477a..edf4c86 100644 --- a/proxy/server_test.go +++ b/proxy/server_test.go @@ -11,7 +11,6 @@ import ( "testing" "time" - "github.com/gorilla/websocket" "github.com/rancher/remotedialer" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -62,7 +61,7 @@ func TestRunProxyListener(t *testing.T) { // ClientConnect will exit when the context is cancelled or the connection is otherwise lost. headers := http.Header{} headers.Set("X-API-Tunnel-Secret", "test-secret") - err := remotedialer.ClientConnect(ctx, wsURL, headers, websocket.DefaultDialer, connectAuthorizer, onConnect) + err := remotedialer.ClientConnect(ctx, wsURL, headers, nil, connectAuthorizer, onConnect) // No error on clean context cancellation if ctx.Err() == nil && err != nil { t.Errorf("remotedialer client connect error: %v", err) diff --git a/proxyclient/client.go b/proxyclient/client.go index 788ae3f..4cc23d5 100644 --- a/proxyclient/client.go +++ b/proxyclient/client.go @@ -9,7 +9,7 @@ import ( "sync" "time" - "github.com/gorilla/websocket" + "github.com/coder/websocket" "github.com/rancher/remotedialer" v1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1" @@ -38,8 +38,8 @@ type ProxyClient struct { serverUrl string serverConnectSecret string - dialer *websocket.Dialer - dialerMtx sync.Mutex + dialOpts *websocket.DialOptions + dialOptsMtx sync.Mutex secretController v1.SecretController namespace string @@ -103,14 +103,18 @@ func (c *ProxyClient) setUpBuildDialerCallback(ctx context.Context, certSecretNa return nil, err } - c.dialerMtx.Lock() - c.dialer = &websocket.Dialer{ - TLSClientConfig: &tls.Config{ - RootCAs: rootCAs, - ServerName: c.certServerName, + c.dialOptsMtx.Lock() + c.dialOpts = &websocket.DialOptions{ + HTTPClient: &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + RootCAs: rootCAs, + ServerName: c.certServerName, + }, + }, }, } - c.dialerMtx.Unlock() + c.dialOptsMtx.Unlock() logrus.Infof("RDPClient: certificate updated successfully") } @@ -142,18 +146,18 @@ func (c *ProxyClient) Run(ctx context.Context) { return default: - logrus.Info("RDPClient: Checking if dialer is built...") + logrus.Info("RDPClient: Checking if dialOpts is built...") - c.dialerMtx.Lock() - dialer := c.dialer - c.dialerMtx.Unlock() + c.dialOptsMtx.Lock() + dialOpts := c.dialOpts + c.dialOptsMtx.Unlock() - if dialer != nil { - logrus.Info("RDPClient: Dialer is built. Ready to start.") + if dialOpts != nil { + logrus.Info("RDPClient: DialOpts is built. Ready to start.") break LookForDialer } - logrus.Infof("RDPClient: Dialer is not built yet, waiting %d secs to re-check.", getSecretRetryTimeout/time.Second) + logrus.Infof("RDPClient: DialOpts is not built yet, waiting %d secs to re-check.", getSecretRetryTimeout/time.Second) time.Sleep(getSecretRetryTimeout) } } @@ -185,11 +189,11 @@ func (c *ProxyClient) Run(ctx context.Context) { return nil } - c.dialerMtx.Lock() - dialer := c.dialer - c.dialerMtx.Unlock() + c.dialOptsMtx.Lock() + dialOpts := c.dialOpts + c.dialOptsMtx.Unlock() - if err := remotedialer.ClientConnect(ctx, c.serverUrl, headers, dialer, onConnectAuth, onConnect); err != nil { + if err := remotedialer.ClientConnect(ctx, c.serverUrl, headers, dialOpts, onConnectAuth, onConnect); err != nil { logrus.Errorf("RDPClient: remotedialer.ClientConnect error: %s", err.Error()) c.forwarder.Stop() time.Sleep(retryTimeout) @@ -218,8 +222,8 @@ func WithOnConnectCallback(onConnect func(ctx context.Context, session *remotedi } } -func WithCustomDialer(dialer *websocket.Dialer) ProxyClientOpt { +func WithCustomDialOptions(dialOpts *websocket.DialOptions) ProxyClientOpt { return func(pc *ProxyClient) { - pc.dialer = dialer + pc.dialOpts = dialOpts } }