2023-10-17 06:31:13 +02:00
|
|
|
// Copyright 2023, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
package scws
|
|
|
|
|
|
|
|
import (
|
2022-09-05 21:42:09 +02:00
|
|
|
"context"
|
2022-07-13 23:16:08 +02:00
|
|
|
"fmt"
|
2022-10-31 20:40:45 +01:00
|
|
|
"log"
|
2023-12-26 21:59:25 +01:00
|
|
|
"runtime/debug"
|
2022-07-13 23:16:08 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-10-16 22:30:10 +02:00
|
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
|
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/mapqueue"
|
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/remote"
|
2024-02-16 01:45:47 +01:00
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbus"
|
2023-10-16 22:30:10 +02:00
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/scpacket"
|
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/sstore"
|
2024-02-16 01:45:47 +01:00
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/userinput"
|
2023-10-16 22:30:10 +02:00
|
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/wsshell"
|
2022-07-13 23:16:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const WSStatePacketChSize = 20
|
|
|
|
const MaxInputDataSize = 1000
|
2023-03-13 09:52:30 +01:00
|
|
|
const RemoteInputQueueSize = 100
|
|
|
|
|
|
|
|
var RemoteInputMapQueue *mapqueue.MapQueue
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RemoteInputMapQueue = mapqueue.MakeMapQueue(RemoteInputQueueSize)
|
|
|
|
}
|
2022-07-13 23:16:08 +02:00
|
|
|
|
|
|
|
type WSState struct {
|
2022-12-21 01:16:46 +01:00
|
|
|
Lock *sync.Mutex
|
|
|
|
ClientId string
|
|
|
|
ConnectTime time.Time
|
|
|
|
Shell *wsshell.WSShell
|
2024-02-16 01:45:47 +01:00
|
|
|
UpdateCh chan scbus.UpdatePacket
|
|
|
|
UpdateQueue []any
|
2022-12-21 01:16:46 +01:00
|
|
|
Authenticated bool
|
|
|
|
AuthKey string
|
2022-07-13 23:16:08 +02:00
|
|
|
|
|
|
|
SessionId string
|
|
|
|
ScreenId string
|
|
|
|
}
|
|
|
|
|
2022-12-21 01:16:46 +01:00
|
|
|
func MakeWSState(clientId string, authKey string) *WSState {
|
2022-07-13 23:16:08 +02:00
|
|
|
rtn := &WSState{}
|
|
|
|
rtn.Lock = &sync.Mutex{}
|
|
|
|
rtn.ClientId = clientId
|
|
|
|
rtn.ConnectTime = time.Now()
|
2022-12-21 01:16:46 +01:00
|
|
|
rtn.AuthKey = authKey
|
2022-07-13 23:16:08 +02:00
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2022-12-21 01:16:46 +01:00
|
|
|
func (ws *WSState) SetAuthenticated(authVal bool) {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
|
|
|
ws.Authenticated = authVal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WSState) IsAuthenticated() bool {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
|
|
|
return ws.Authenticated
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
func (ws *WSState) GetShell() *wsshell.WSShell {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
|
|
|
return ws.Shell
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:45:47 +01:00
|
|
|
func (ws *WSState) WriteUpdate(update any) error {
|
2022-07-13 23:16:08 +02:00
|
|
|
shell := ws.GetShell()
|
|
|
|
if shell == nil {
|
|
|
|
return fmt.Errorf("cannot write update, empty shell")
|
|
|
|
}
|
|
|
|
err := shell.WriteJson(update)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WSState) UpdateConnectTime() {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
|
|
|
ws.ConnectTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WSState) GetConnectTime() time.Time {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
|
|
|
return ws.ConnectTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WSState) WatchScreen(sessionId string, screenId string) {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
|
|
|
if ws.SessionId == sessionId && ws.ScreenId == screenId {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.SessionId = sessionId
|
|
|
|
ws.ScreenId = screenId
|
2024-02-16 01:45:47 +01:00
|
|
|
ws.UpdateCh = scbus.MainUpdateBus.RegisterChannel(ws.ClientId, &scbus.UpdateChannel{ScreenId: ws.ScreenId})
|
|
|
|
log.Printf("[ws] watch screen clientid=%s sessionid=%s screenid=%s, updateCh=%v\n", ws.ClientId, sessionId, screenId, ws.UpdateCh)
|
2022-09-05 21:42:09 +02:00
|
|
|
go ws.RunUpdates(ws.UpdateCh)
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WSState) UnWatchScreen() {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
2024-02-16 01:45:47 +01:00
|
|
|
scbus.MainUpdateBus.UnregisterChannel(ws.ClientId)
|
2022-07-13 23:16:08 +02:00
|
|
|
ws.SessionId = ""
|
|
|
|
ws.ScreenId = ""
|
2022-10-31 20:40:45 +01:00
|
|
|
log.Printf("[ws] unwatch screen clientid=%s\n", ws.ClientId)
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 01:45:47 +01:00
|
|
|
func (ws *WSState) RunUpdates(updateCh chan scbus.UpdatePacket) {
|
2022-07-13 23:16:08 +02:00
|
|
|
if updateCh == nil {
|
2022-09-05 21:42:09 +02:00
|
|
|
panic("invalid nil updateCh passed to RunUpdates")
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
for update := range updateCh {
|
|
|
|
shell := ws.GetShell()
|
|
|
|
if shell != nil {
|
2023-11-10 22:36:37 +01:00
|
|
|
writeJsonProtected(shell, update)
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 22:36:37 +01:00
|
|
|
func writeJsonProtected(shell *wsshell.WSShell, update any) {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("[error] in scws RunUpdates WriteJson: %v\n", r)
|
|
|
|
}()
|
|
|
|
shell.WriteJson(update)
|
|
|
|
}
|
|
|
|
|
2022-07-13 23:16:08 +02:00
|
|
|
func (ws *WSState) ReplaceShell(shell *wsshell.WSShell) {
|
|
|
|
ws.Lock.Lock()
|
|
|
|
defer ws.Lock.Unlock()
|
|
|
|
if ws.Shell == nil {
|
|
|
|
ws.Shell = shell
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.Shell.Conn.Close()
|
|
|
|
ws.Shell = shell
|
|
|
|
}
|
|
|
|
|
2024-01-30 08:51:01 +01:00
|
|
|
// returns all state required to display current UI
|
2022-09-05 21:42:09 +02:00
|
|
|
func (ws *WSState) handleConnection() error {
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancelFn()
|
2024-02-10 02:19:44 +01:00
|
|
|
connectUpdate, err := sstore.GetConnectUpdate(ctx)
|
2022-09-05 21:42:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting sessions: %w", err)
|
|
|
|
}
|
|
|
|
remotes := remote.GetAllRemoteRuntimeState()
|
2024-02-10 02:19:44 +01:00
|
|
|
connectUpdate.Remotes = remotes
|
2024-01-30 08:51:01 +01:00
|
|
|
// restore status indicators
|
2024-02-10 02:19:44 +01:00
|
|
|
connectUpdate.ScreenStatusIndicators, connectUpdate.ScreenNumRunningCommands = sstore.GetCurrentIndicatorState()
|
2024-02-16 01:45:47 +01:00
|
|
|
mu := scbus.MakeUpdatePacket()
|
|
|
|
mu.AddUpdate(*connectUpdate)
|
2024-02-10 02:19:44 +01:00
|
|
|
err = ws.Shell.WriteJson(mu)
|
2022-09-05 21:42:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WSState) handleWatchScreen(wsPk *scpacket.WatchScreenPacketType) error {
|
|
|
|
if wsPk.SessionId != "" {
|
|
|
|
if _, err := uuid.Parse(wsPk.SessionId); err != nil {
|
|
|
|
return fmt.Errorf("invalid watchscreen sessionid: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if wsPk.ScreenId != "" {
|
|
|
|
if _, err := uuid.Parse(wsPk.ScreenId); err != nil {
|
|
|
|
return fmt.Errorf("invalid watchscreen screenid: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 01:16:46 +01:00
|
|
|
if wsPk.AuthKey == "" {
|
|
|
|
ws.SetAuthenticated(false)
|
|
|
|
return fmt.Errorf("invalid watchscreen, no authkey")
|
|
|
|
}
|
|
|
|
if wsPk.AuthKey != ws.AuthKey {
|
|
|
|
ws.SetAuthenticated(false)
|
|
|
|
return fmt.Errorf("invalid watchscreen, invalid authkey")
|
|
|
|
}
|
|
|
|
ws.SetAuthenticated(true)
|
2022-09-05 21:42:09 +02:00
|
|
|
if wsPk.SessionId == "" || wsPk.ScreenId == "" {
|
|
|
|
ws.UnWatchScreen()
|
|
|
|
} else {
|
|
|
|
ws.WatchScreen(wsPk.SessionId, wsPk.ScreenId)
|
2022-10-31 20:40:45 +01:00
|
|
|
log.Printf("[ws %s] watchscreen %s/%s\n", ws.ClientId, wsPk.SessionId, wsPk.ScreenId)
|
2022-09-05 21:42:09 +02:00
|
|
|
}
|
|
|
|
if wsPk.Connect {
|
2023-04-05 08:44:47 +02:00
|
|
|
// log.Printf("[ws %s] watchscreen connect\n", ws.ClientId)
|
2022-09-05 21:42:09 +02:00
|
|
|
err := ws.handleConnection()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connect: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-26 21:59:25 +01:00
|
|
|
func (ws *WSState) processMessage(msgBytes []byte) error {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("[scws] panic in processMessage: %v\n", r)
|
|
|
|
debug.PrintStack()
|
|
|
|
}()
|
|
|
|
|
|
|
|
pk, err := packet.ParseJsonPacket(msgBytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error unmarshalling ws message: %w", err)
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
if pk.GetType() == scpacket.WatchScreenPacketStr {
|
|
|
|
wsPk := pk.(*scpacket.WatchScreenPacketType)
|
|
|
|
err := ws.handleWatchScreen(wsPk)
|
2022-07-13 23:16:08 +02:00
|
|
|
if err != nil {
|
2023-12-26 21:59:25 +01:00
|
|
|
return fmt.Errorf("client:%s error %w", ws.ClientId, err)
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
isAuth := ws.IsAuthenticated()
|
|
|
|
if !isAuth {
|
|
|
|
return fmt.Errorf("cannot process ws-packet[%s], not authenticated", pk.GetType())
|
|
|
|
}
|
|
|
|
if pk.GetType() == scpacket.FeInputPacketStr {
|
|
|
|
feInputPk := pk.(*scpacket.FeInputPacketType)
|
|
|
|
if feInputPk.Remote.OwnerId != "" {
|
|
|
|
return fmt.Errorf("error cannot send input to remote with ownerid")
|
2022-12-21 01:16:46 +01:00
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
if feInputPk.Remote.RemoteId == "" {
|
|
|
|
return fmt.Errorf("error invalid input packet, remoteid is not set")
|
2022-12-21 01:16:46 +01:00
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
err := RemoteInputMapQueue.Enqueue(feInputPk.Remote.RemoteId, func() {
|
|
|
|
sendErr := sendCmdInput(feInputPk)
|
|
|
|
if sendErr != nil {
|
|
|
|
log.Printf("[scws] sending command input: %v\n", err)
|
2022-08-24 22:21:54 +02:00
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[error] could not queue sendCmdInput: %w", err)
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if pk.GetType() == scpacket.RemoteInputPacketStr {
|
|
|
|
inputPk := pk.(*scpacket.RemoteInputPacketType)
|
|
|
|
if inputPk.RemoteId == "" {
|
|
|
|
return fmt.Errorf("error invalid remoteinput packet, remoteid is not set")
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
sendErr := remote.SendRemoteInput(inputPk)
|
|
|
|
if sendErr != nil {
|
|
|
|
log.Printf("[scws] error processing remote input: %v\n", err)
|
2022-09-16 02:09:04 +02:00
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if pk.GetType() == scpacket.CmdInputTextPacketStr {
|
|
|
|
cmdInputPk := pk.(*scpacket.CmdInputTextPacketType)
|
|
|
|
if cmdInputPk.ScreenId == "" {
|
|
|
|
return fmt.Errorf("error invalid cmdinput packet, screenid is not set")
|
|
|
|
}
|
|
|
|
// no need for goroutine for memory ops
|
|
|
|
sstore.ScreenMemSetCmdInputText(cmdInputPk.ScreenId, cmdInputPk.Text, cmdInputPk.SeqNum)
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-16 01:45:47 +01:00
|
|
|
if pk.GetType() == userinput.UserInputResponsePacketStr {
|
|
|
|
userInputRespPk := pk.(*userinput.UserInputResponsePacketType)
|
|
|
|
uich, ok := scbus.MainRpcBus.GetRpcChannel(userInputRespPk.RequestId)
|
Use ssh library: add user input (#281)
* feat: create backend for user input requests
This is the first part of a change that allows the backend to request
user input from the frontend. Essentially, the backend will send a
request for the user to answer some query, and the frontend will send
that answer back. It is blocking, so it needs to be used within a
goroutine.
There is some placeholder code in the frontend that will be updated in
future commits. Similarly, there is some debug code in the backend
remote.go file.
* feat: create frontend for user input requests
This is part of a change to allow the backend to request user input from
the frontend. This adds a component specifically for handling this
logic. It is only a starting point, and does not work perfectly yet.
* refactor: update user input backend/interface
This updates the user input backend to fix a few potential bugs. It also
refactors the user input request and response types to better handle
markdown and errors while making it more convenient to work with.
A couple frontend changes were made to keep everything compatible.
* fix: add props to user input request modal
There was a second place that the modals were created that I previously
missed. This fixes that second casel
* feat: complete user input modal
This rounds out the most immediate concerns for the new user input
modal. The frontend now includes a timer to show how much time is left
and will close itself once it reaches zero. Css
formatting has been cleaned up to be more reasonable.
There is still some test code present on the back end. This will be
removed once actuall examples of the new modal are in place.
* feat: create first pass known_hosts detection
Manually integrating with golang's ssh library means that the code must
authenticate known_hosts on its own. This is a first pass at creating a
system that parses the known hosts files and denys a connection if there
is a mismatch. This needs to be updated with a means to add keys to the
known-hosts file if the user requests it.
* feat: allow writing to known_hosts first pass
As a follow-up to the previous change, we now allow the user to respond
to interactive queries in order to determine if an unknown known hosts
key can be added to a known_hosts file if it is missing. This needs to
be refined further, but it gets the basic functionality there.
* feat: add user input for kbd-interactive auth
This adds a modal so the user can respond to prompts provided using the
keyboard interactive authentication method.
* feat: add interactive password authentication
This makes the ssh password authentication interactive with its own user
input modal. Unfortunately, this method does not allow trying a default
first. This will need to be expanded in the future to accomodate that.
* fix: allow automatic and interactive auth together
Previously, it was impossible to use to separate methods of the same
type to try ssh authentication. This made it impossible to make an auto
attempt before a manual one. This change restricts that by combining
them into one method where the auto attempt is tried once first and
cannot be tried again. Following that, interactive authentication can be
tried separately.
It also lowers the time limit on kbd interactive authentication to 15
seconds due to limitations on the library we are using.
* fix: set number of retries to one in ssh
Number of retries means number of attempts after the fact, not number of
total attempts. It has been adjusted from 2 to 1 to reflect this.
* refactor: change argument order in GetUserInput
This is a simple change to move the context to the first argument of
GetUserInput to match the convention used elsewhere in the code.
* fix: set number of retries to two again
I was wrong in my previous analysis. The number given is the total
number of tries. This is confusing when keyboard authentication and
password authentication are both available which usually doesn't happen.
* feat: create naive ui for ssh key passphrases
This isn't quite as reactive as the other methods, but it does attempt
to use publickey without a passphrase, then attempt to use the password
as the passphrase, and finally prompting the user for a passphrase. The
problem with this approach is that if multiple keys are used and they
all have passphrases, they need to all be checked up front. In practice,
this will not happen often, but it is something to be aware of.
* fix: add the userinput.tsx changes
These were missed in the previous commit. Adding them now.
2024-02-09 04:16:56 +01:00
|
|
|
if !ok {
|
2024-02-16 01:45:47 +01:00
|
|
|
return fmt.Errorf("received User Input Response with invalid Id (%s): %v", userInputRespPk.RequestId, err)
|
Use ssh library: add user input (#281)
* feat: create backend for user input requests
This is the first part of a change that allows the backend to request
user input from the frontend. Essentially, the backend will send a
request for the user to answer some query, and the frontend will send
that answer back. It is blocking, so it needs to be used within a
goroutine.
There is some placeholder code in the frontend that will be updated in
future commits. Similarly, there is some debug code in the backend
remote.go file.
* feat: create frontend for user input requests
This is part of a change to allow the backend to request user input from
the frontend. This adds a component specifically for handling this
logic. It is only a starting point, and does not work perfectly yet.
* refactor: update user input backend/interface
This updates the user input backend to fix a few potential bugs. It also
refactors the user input request and response types to better handle
markdown and errors while making it more convenient to work with.
A couple frontend changes were made to keep everything compatible.
* fix: add props to user input request modal
There was a second place that the modals were created that I previously
missed. This fixes that second casel
* feat: complete user input modal
This rounds out the most immediate concerns for the new user input
modal. The frontend now includes a timer to show how much time is left
and will close itself once it reaches zero. Css
formatting has been cleaned up to be more reasonable.
There is still some test code present on the back end. This will be
removed once actuall examples of the new modal are in place.
* feat: create first pass known_hosts detection
Manually integrating with golang's ssh library means that the code must
authenticate known_hosts on its own. This is a first pass at creating a
system that parses the known hosts files and denys a connection if there
is a mismatch. This needs to be updated with a means to add keys to the
known-hosts file if the user requests it.
* feat: allow writing to known_hosts first pass
As a follow-up to the previous change, we now allow the user to respond
to interactive queries in order to determine if an unknown known hosts
key can be added to a known_hosts file if it is missing. This needs to
be refined further, but it gets the basic functionality there.
* feat: add user input for kbd-interactive auth
This adds a modal so the user can respond to prompts provided using the
keyboard interactive authentication method.
* feat: add interactive password authentication
This makes the ssh password authentication interactive with its own user
input modal. Unfortunately, this method does not allow trying a default
first. This will need to be expanded in the future to accomodate that.
* fix: allow automatic and interactive auth together
Previously, it was impossible to use to separate methods of the same
type to try ssh authentication. This made it impossible to make an auto
attempt before a manual one. This change restricts that by combining
them into one method where the auto attempt is tried once first and
cannot be tried again. Following that, interactive authentication can be
tried separately.
It also lowers the time limit on kbd interactive authentication to 15
seconds due to limitations on the library we are using.
* fix: set number of retries to one in ssh
Number of retries means number of attempts after the fact, not number of
total attempts. It has been adjusted from 2 to 1 to reflect this.
* refactor: change argument order in GetUserInput
This is a simple change to move the context to the first argument of
GetUserInput to match the convention used elsewhere in the code.
* fix: set number of retries to two again
I was wrong in my previous analysis. The number given is the total
number of tries. This is confusing when keyboard authentication and
password authentication are both available which usually doesn't happen.
* feat: create naive ui for ssh key passphrases
This isn't quite as reactive as the other methods, but it does attempt
to use publickey without a passphrase, then attempt to use the password
as the passphrase, and finally prompting the user for a passphrase. The
problem with this approach is that if multiple keys are used and they
all have passphrases, they need to all be checked up front. In practice,
this will not happen often, but it is something to be aware of.
* fix: add the userinput.tsx changes
These were missed in the previous commit. Adding them now.
2024-02-09 04:16:56 +01:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case uich <- userInputRespPk:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-26 21:59:25 +01:00
|
|
|
return fmt.Errorf("got ws bad message: %v", pk.GetType())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WSState) RunWSRead() {
|
|
|
|
shell := ws.GetShell()
|
|
|
|
if shell == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-02-16 01:45:47 +01:00
|
|
|
shell.WriteJson(map[string]any{"type": "hello"}) // let client know we accepted this connection, ignore error
|
2023-12-26 21:59:25 +01:00
|
|
|
for msgBytes := range shell.ReadChan {
|
|
|
|
err := ws.processMessage(msgBytes)
|
|
|
|
if err != nil {
|
|
|
|
// TODO send errors back to client? likely unrecoverable
|
|
|
|
log.Printf("[scws] %v\n", err)
|
2022-09-16 02:09:04 +02:00
|
|
|
}
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
func sendCmdInput(pk *scpacket.FeInputPacketType) error {
|
2022-07-13 23:16:08 +02:00
|
|
|
err := pk.CK.Validate("input packet")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
if pk.Remote.RemoteId == "" {
|
2022-07-13 23:16:08 +02:00
|
|
|
return fmt.Errorf("input must set remoteid")
|
|
|
|
}
|
2022-09-06 05:08:59 +02:00
|
|
|
msh := remote.GetRemoteById(pk.Remote.RemoteId)
|
|
|
|
if msh == nil {
|
2022-12-28 22:56:19 +01:00
|
|
|
return fmt.Errorf("remote %s not found", pk.Remote.RemoteId)
|
2022-09-06 05:08:59 +02:00
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
if len(pk.InputData64) > 0 {
|
|
|
|
inputLen := packet.B64DecodedLen(pk.InputData64)
|
|
|
|
if inputLen > MaxInputDataSize {
|
|
|
|
return fmt.Errorf("input data size too large, len=%d (max=%d)", inputLen, MaxInputDataSize)
|
|
|
|
}
|
|
|
|
dataPk := packet.MakeDataPacket()
|
|
|
|
dataPk.CK = pk.CK
|
|
|
|
dataPk.FdNum = 0 // stdin
|
|
|
|
dataPk.Data64 = pk.InputData64
|
2022-09-06 05:08:59 +02:00
|
|
|
err = msh.SendInput(dataPk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
2022-09-07 01:41:05 +02:00
|
|
|
if pk.SigName != "" || pk.WinSize != nil {
|
2022-09-06 05:08:59 +02:00
|
|
|
siPk := packet.MakeSpecialInputPacket()
|
|
|
|
siPk.CK = pk.CK
|
2022-09-07 01:41:05 +02:00
|
|
|
siPk.SigName = pk.SigName
|
2022-09-06 05:08:59 +02:00
|
|
|
siPk.WinSize = pk.WinSize
|
|
|
|
err = msh.SendSpecialInput(siPk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
return nil
|
2022-07-13 23:16:08 +02:00
|
|
|
}
|