Switch from golang.org/x/net/websocket to nhooyr.io/websocket and handle NotFound errors (#105)

* Switch from golang.org/x/net/websocket to nhooyr.io/websocket

* Do not attach errors that we can handle to the Gin's context

* Add missing newline to "no credentials specified or found, ..." message

* Fix potential NPE in ChooseUsernameAndPassword()

* Fix type in PortForward() error message in "orchard ssh vm"

* Fix potential NPE in Connections()

* Use header.Set() for consistency's sake for Authorization header
This commit is contained in:
Nikolay Edigaryev
2023-07-04 18:10:41 +04:00
committed by GitHub
parent 3c3b8e8180
commit 6a325daf74
9 changed files with 88 additions and 42 deletions
+21 -6
View File
@@ -9,8 +9,8 @@ import (
"github.com/cirruslabs/orchard/rpc"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"golang.org/x/net/websocket"
"net/http"
"nhooyr.io/websocket"
"strconv"
"time"
)
@@ -95,11 +95,26 @@ func (controller *Controller) portForwardVM(ctx *gin.Context) responder.Responde
// worker will asynchronously start port-forwarding so we wait
select {
case fromWorkerConnection := <-boomerangConnCh:
websocket.Handler(func(wsConn *websocket.Conn) {
if err := proxy.Connections(wsConn, fromWorkerConnection); err != nil {
controller.logger.Warnf("failed to port-forward: %v", err)
}
}).ServeHTTP(ctx.Writer, ctx.Request)
wsConn, err := websocket.Accept(ctx.Writer, ctx.Request, &websocket.AcceptOptions{
OriginPatterns: []string{"*"},
})
if err != nil {
return responder.Error(err)
}
expectedMsgType := websocket.MessageBinary
// Backwards compatibility with older Orchard clients
// using "golang.org/x/net/websocket" package
if ctx.Request.Header.Get("User-Agent") == "" {
expectedMsgType = websocket.MessageText
}
wsConnAsNetConn := websocket.NetConn(ctx, wsConn, expectedMsgType)
if err := proxy.Connections(wsConnAsNetConn, fromWorkerConnection); err != nil {
controller.logger.Warnf("failed to port-forward: %v", err)
}
return responder.Empty()
case <-ctx.Done():