get input working

This commit is contained in:
sawka
2022-07-07 22:46:28 -07:00
parent 5fcbe209bb
commit 368c16eb60
2 changed files with 37 additions and 10 deletions
+12 -10
View File
@@ -16,7 +16,6 @@ import (
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/scripthaus-dev/mshell/pkg/base"
"github.com/scripthaus-dev/mshell/pkg/cmdtail"
"github.com/scripthaus-dev/mshell/pkg/packet"
"github.com/scripthaus-dev/sh2-server/pkg/remote"
@@ -254,18 +253,21 @@ func sendCmdInput(pk *packet.InputPacketType) error {
if err != nil {
return err
}
if len(pk.InputData) > MaxInputDataSize {
return fmt.Errorf("input data size too large, len=%d (max=%d)", len(pk.InputData), MaxInputDataSize)
if pk.RemoteId == "" {
return fmt.Errorf("input must set remoteid")
}
fileNames, err := base.GetCommandFileNames(pk.CK)
if err != nil {
return err
if len(pk.InputData64) == 0 && pk.SigNum == 0 {
return fmt.Errorf("empty input packet")
}
err = writeToFifo(fileNames.StdinFifo, []byte(pk.InputData))
if err != nil {
return err
inputLen := packet.B64DecodedLen(pk.InputData64)
if inputLen > MaxInputDataSize {
return fmt.Errorf("input data size too large, len=%d (max=%d)", inputLen, MaxInputDataSize)
}
return nil
msh := remote.GetRemoteById(pk.RemoteId)
if msh == nil {
return fmt.Errorf("cannot connect to remote")
}
return msh.SendInput(pk)
}
// params: name
+25
View File
@@ -168,6 +168,31 @@ func (msh *MShellProc) IsConnected() bool {
return msh.Status == StatusConnected
}
func (msh *MShellProc) IsCmdRunning(ck base.CommandKey) bool {
msh.Lock.Lock()
defer msh.Lock.Unlock()
for _, runningCk := range msh.RunningCmds {
if runningCk == ck {
return true
}
}
return false
}
func (msh *MShellProc) SendInput(pk *packet.InputPacketType) error {
if !msh.IsConnected() {
return fmt.Errorf("remote is not connected, cannot send input")
}
if !msh.IsCmdRunning(pk.CK) {
return fmt.Errorf("cannot send input, cmd is not running")
}
dataPk := packet.MakeDataPacket()
dataPk.CK = pk.CK
dataPk.FdNum = 0 // stdin
dataPk.Data64 = pk.InputData64
return msh.ServerProc.Input.SendPacket(dataPk)
}
func convertRemoteState(rs scpacket.RemoteState) sstore.RemoteState {
return sstore.RemoteState{Cwd: rs.Cwd}
}