Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ssh/web/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ func (c *Conn) WriteMessage(message *Message) (int, error) {
return wrote, nil
}

func (c *Conn) WriteBinary(data []byte) (int, error) {
socket := c.Socket.(*websocket.Conn)

frame, err := socket.NewFrameWriter(websocket.BinaryFrame)
if err != nil {
return 0, errors.Join(ErrConnWriteMessageFailedFrame, err)
}

wrote, err := frame.Write(data)
if err != nil {
return wrote, errors.Join(ErrConnReadMessageSocketWrite, err)
}

return wrote, nil
}

func (c *Conn) Read(buffer []byte) (int, error) {
return c.Socket.Read(buffer)
}
Expand Down
9 changes: 5 additions & 4 deletions ssh/web/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ var (
)

var (
ErrConnReadMessageSocketRead = errors.New("failed to read the message from socket")
ErrConnReadMessageSocketWrite = errors.New("failed to write the message's data to socket")
ErrConnReadMessageJSONInvalid = errors.New("failed to parse the message from json")
ErrConnReadMessageKindInvalid = errors.New("this kind of message is invalid")
ErrConnReadMessageSocketRead = errors.New("failed to read the message from socket")
ErrConnReadMessageSocketWrite = errors.New("failed to write the message's data to socket")
ErrConnReadMessageJSONInvalid = errors.New("failed to parse the message from json")
ErrConnReadMessageKindInvalid = errors.New("this kind of message is invalid")
ErrConnWriteMessageFailedFrame = errors.New("failed to create frame")
)

var (
Expand Down
3 changes: 3 additions & 0 deletions ssh/web/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const (
// messageKindSignature is the identifier to a signature message. This kind of message contains the data to be
// signed by the user's private key.
messageKindSignature
// messageKindError is the identifier to output an erro rmessage. This kind of message contains data to be show
// in terminal for information propose.
messageKindError
)

type Message struct {
Expand Down
4 changes: 2 additions & 2 deletions ssh/web/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func newSession(ctx context.Context, cache cache.Cache, conn *Conn, creds *Crede
return nil
}

func redirToWs(rd io.Reader, ws io.ReadWriter) error {
func redirToWs(rd io.Reader, ws *Conn) error {
var buf [32 * 1024]byte
var start, end, buflen int

Expand Down Expand Up @@ -292,7 +292,7 @@ func redirToWs(rd io.Reader, ws io.ReadWriter) error {
}
}

if _, err = ws.Write([]byte(string(bytes.Runes(buf[0:end])))); err != nil {
if _, err = ws.WriteBinary([]byte(string(bytes.Runes(buf[0:end])))); err != nil {
return err
}

Expand Down
10 changes: 9 additions & 1 deletion ssh/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/shellhub-io/shellhub/pkg/cache"
"github.com/shellhub-io/shellhub/ssh/pkg/magickey"
"github.com/shellhub-io/shellhub/ssh/web/pkg/token"
log "github.com/sirupsen/logrus"
"golang.org/x/net/websocket"
)

Expand Down Expand Up @@ -69,7 +70,14 @@ func NewSSHServerBridge(router *echo.Echo, cache cache.Cache) {

// exit sends the error's message to the client on the browser.
exit := func(wsconn *websocket.Conn, err error) {
wsconn.Write([]byte(err.Error())) //nolint:errcheck
buffer, err := json.Marshal(Message{
Kind: messageKindError,
Data: err.Error(),
})

log.WithError(err).Error("failed to parsing the error message on web terminal")

wsconn.Write(buffer) //nolint:errcheck
}

token, err := getToken(wsconn.Request())
Expand Down
Loading