2022-07-01 21:17:19 +02:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
2022-08-24 11:14:16 +02:00
|
|
|
"bytes"
|
2022-07-02 02:38:36 +02:00
|
|
|
"context"
|
2022-07-07 07:46:22 +02:00
|
|
|
"encoding/base64"
|
2022-07-07 04:01:00 +02:00
|
|
|
"errors"
|
2022-07-01 21:17:19 +02:00
|
|
|
"fmt"
|
2022-08-19 22:23:00 +02:00
|
|
|
"io"
|
2022-11-27 23:12:15 +01:00
|
|
|
"log"
|
2022-08-19 22:23:00 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-08-17 21:24:09 +02:00
|
|
|
"path"
|
2022-09-14 22:01:52 +02:00
|
|
|
"regexp"
|
2022-08-24 06:05:49 +02:00
|
|
|
"strconv"
|
2022-08-17 21:24:09 +02:00
|
|
|
"strings"
|
2022-07-01 21:17:19 +02:00
|
|
|
"sync"
|
2022-08-19 22:23:00 +02:00
|
|
|
"syscall"
|
2022-10-01 02:22:28 +02:00
|
|
|
"time"
|
2022-07-01 21:17:19 +02:00
|
|
|
|
2022-09-15 08:10:35 +02:00
|
|
|
"github.com/armon/circbuf"
|
2022-08-19 22:23:00 +02:00
|
|
|
"github.com/creack/pty"
|
2022-08-24 11:14:16 +02:00
|
|
|
"github.com/google/uuid"
|
2022-07-01 21:17:19 +02:00
|
|
|
"github.com/scripthaus-dev/mshell/pkg/base"
|
|
|
|
"github.com/scripthaus-dev/mshell/pkg/packet"
|
|
|
|
"github.com/scripthaus-dev/mshell/pkg/shexec"
|
2022-11-29 03:03:02 +01:00
|
|
|
"github.com/scripthaus-dev/mshell/pkg/statediff"
|
2022-11-02 05:42:56 +01:00
|
|
|
"github.com/scripthaus-dev/sh2-server/pkg/scbase"
|
2022-09-16 02:09:04 +02:00
|
|
|
"github.com/scripthaus-dev/sh2-server/pkg/scpacket"
|
2022-07-02 02:38:36 +02:00
|
|
|
"github.com/scripthaus-dev/sh2-server/pkg/sstore"
|
2022-09-25 04:54:06 +02:00
|
|
|
"golang.org/x/mod/semver"
|
2022-07-01 21:17:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const RemoteTypeMShell = "mshell"
|
2022-07-07 04:01:00 +02:00
|
|
|
const DefaultTerm = "xterm-256color"
|
2022-08-19 22:23:00 +02:00
|
|
|
const DefaultMaxPtySize = 1024 * 1024
|
2022-09-15 08:10:35 +02:00
|
|
|
const CircBufSize = 64 * 1024
|
2022-09-16 02:09:04 +02:00
|
|
|
const RemoteTermRows = 8
|
2022-09-15 09:17:23 +02:00
|
|
|
const RemoteTermCols = 80
|
|
|
|
const PtyReadBufSize = 100
|
2022-07-01 21:17:19 +02:00
|
|
|
|
2022-09-27 06:09:43 +02:00
|
|
|
const MShellServerCommandFmt = `
|
2022-08-17 21:24:09 +02:00
|
|
|
PATH=$PATH:~/.mshell;
|
2022-09-27 06:09:43 +02:00
|
|
|
which mshell-[%VERSION%] > /dev/null;
|
2022-08-17 21:24:09 +02:00
|
|
|
if [[ "$?" -ne 0 ]]
|
|
|
|
then
|
|
|
|
printf "\n##N{\"type\": \"init\", \"notfound\": true, \"uname\": \"%s | %s\"}\n" "$(uname -s)" "$(uname -m)"
|
|
|
|
else
|
2022-09-27 06:09:43 +02:00
|
|
|
mshell-[%VERSION%] --server
|
2022-08-17 21:24:09 +02:00
|
|
|
fi
|
|
|
|
`
|
|
|
|
|
2022-12-29 09:07:16 +01:00
|
|
|
func MakeLocalMShellCommandStr(isSudo bool) (string, error) {
|
2022-12-29 08:09:37 +01:00
|
|
|
mshellPath, err := scbase.LocalMShellBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-12-29 09:07:16 +01:00
|
|
|
if isSudo {
|
|
|
|
return fmt.Sprintf("sudo %s --server", mshellPath), nil
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("%s --server", mshellPath), nil
|
|
|
|
}
|
2022-12-29 08:09:37 +01:00
|
|
|
}
|
|
|
|
|
2022-09-27 06:09:43 +02:00
|
|
|
func MakeServerCommandStr() string {
|
2022-12-29 08:09:37 +01:00
|
|
|
return strings.ReplaceAll(MShellServerCommandFmt, "[%VERSION%]", semver.MajorMinor(scbase.MShellVersion))
|
2022-09-27 06:09:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 02:38:36 +02:00
|
|
|
const (
|
|
|
|
StatusConnected = "connected"
|
2022-09-16 02:44:39 +02:00
|
|
|
StatusConnecting = "connecting"
|
2022-07-02 02:38:36 +02:00
|
|
|
StatusDisconnected = "disconnected"
|
|
|
|
StatusError = "error"
|
|
|
|
)
|
|
|
|
|
2022-09-25 04:54:06 +02:00
|
|
|
func init() {
|
2022-12-29 08:09:37 +01:00
|
|
|
if scbase.MShellVersion != base.MShellVersion {
|
|
|
|
panic(fmt.Sprintf("sh2-server mshell version must match '%s' vs '%s'", scbase.MShellVersion, base.MShellVersion))
|
2022-09-25 04:54:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:38:36 +02:00
|
|
|
var GlobalStore *Store
|
|
|
|
|
2022-07-01 23:57:42 +02:00
|
|
|
type Store struct {
|
2022-09-05 23:49:23 +02:00
|
|
|
Lock *sync.Mutex
|
|
|
|
Map map[string]*MShellProc // key=remoteid
|
2022-09-06 01:31:22 +02:00
|
|
|
CmdWaitMap map[base.CommandKey][]func()
|
2022-07-01 23:57:42 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 21:47:10 +02:00
|
|
|
type MShellProc struct {
|
|
|
|
Lock *sync.Mutex
|
|
|
|
Remote *sstore.RemoteType
|
|
|
|
|
|
|
|
// runtime
|
2022-10-28 07:22:17 +02:00
|
|
|
RemoteId string // can be read without a lock
|
2022-09-16 21:28:09 +02:00
|
|
|
Status string
|
|
|
|
ServerProc *shexec.ClientProc
|
|
|
|
UName string
|
|
|
|
Err error
|
|
|
|
ControllingPty *os.File
|
|
|
|
PtyBuffer *circbuf.Buffer
|
|
|
|
MakeClientCancelFn context.CancelFunc
|
2022-11-28 09:13:00 +01:00
|
|
|
StateMap map[string]*packet.ShellState // sha1->state
|
|
|
|
CurrentState string // sha1
|
2022-12-28 22:56:19 +01:00
|
|
|
NumTryConnect int
|
2022-09-27 06:09:43 +02:00
|
|
|
|
|
|
|
// install
|
|
|
|
InstallStatus string
|
2022-09-25 04:54:06 +02:00
|
|
|
NeedsMShellUpgrade bool
|
2022-09-27 06:09:43 +02:00
|
|
|
InstallCancelFn context.CancelFunc
|
|
|
|
InstallErr error
|
2022-09-01 21:47:10 +02:00
|
|
|
|
2022-10-28 07:22:17 +02:00
|
|
|
RunningCmds map[base.CommandKey]RunCmdType
|
2022-10-28 07:00:10 +02:00
|
|
|
WaitingCmds []RunCmdType
|
|
|
|
PendingStateCmds map[string]base.CommandKey // key=[remoteinstance name]
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunCmdType struct {
|
2022-10-28 07:22:17 +02:00
|
|
|
SessionId string
|
|
|
|
WindowId string
|
2022-10-28 07:00:10 +02:00
|
|
|
RemotePtr sstore.RemotePtrType
|
|
|
|
RunPacket *packet.RunPacketType
|
2022-09-01 21:47:10 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
type RemoteRuntimeState struct {
|
2022-12-29 09:07:16 +01:00
|
|
|
RemoteType string `json:"remotetype"`
|
|
|
|
RemoteId string `json:"remoteid"`
|
|
|
|
PhysicalId string `json:"physicalremoteid"`
|
|
|
|
RemoteAlias string `json:"remotealias,omitempty"`
|
|
|
|
RemoteCanonicalName string `json:"remotecanonicalname"`
|
|
|
|
RemoteVars map[string]string `json:"remotevars"`
|
|
|
|
DefaultFeState *sstore.FeStateType `json:"defaultfestate"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
ErrorStr string `json:"errorstr,omitempty"`
|
|
|
|
InstallStatus string `json:"installstatus"`
|
|
|
|
InstallErrorStr string `json:"installerrorstr,omitempty"`
|
|
|
|
NeedsMShellUpgrade bool `json:"needsmshellupgrade,omitempty"`
|
|
|
|
ConnectMode string `json:"connectmode"`
|
|
|
|
AutoInstall bool `json:"autoinstall"`
|
|
|
|
Archived bool `json:"archived,omitempty"`
|
|
|
|
RemoteIdx int64 `json:"remoteidx"`
|
|
|
|
UName string `json:"uname"`
|
|
|
|
MShellVersion string `json:"mshellversion"`
|
|
|
|
WaitingForPassword bool `json:"waitingforpassword,omitempty"`
|
|
|
|
Local bool `json:"local,omitempty"`
|
|
|
|
RemoteOpts *sstore.RemoteOptsType `json:"remoteopts,omitempty"`
|
2022-07-05 07:18:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
func (state RemoteRuntimeState) IsConnected() bool {
|
2022-08-24 22:21:54 +02:00
|
|
|
return state.Status == StatusConnected
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:28:09 +02:00
|
|
|
func (msh *MShellProc) GetStatus() string {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.Status
|
|
|
|
}
|
|
|
|
|
2022-11-02 05:42:56 +01:00
|
|
|
func (msh *MShellProc) GetDefaultState() *packet.ShellState {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-11-28 09:13:00 +01:00
|
|
|
return msh.StateMap[msh.CurrentState]
|
|
|
|
}
|
|
|
|
|
2022-11-29 03:03:02 +01:00
|
|
|
func (msh *MShellProc) GetDefaultStatePtr() *sstore.ShellStatePtr {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
if msh.CurrentState == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &sstore.ShellStatePtr{BaseHash: msh.CurrentState}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) GetDefaultFeState() *sstore.FeStateType {
|
|
|
|
state := msh.GetDefaultState()
|
|
|
|
return sstore.FeStateFromShellState(state)
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:13:00 +01:00
|
|
|
func (msh *MShellProc) GetStateByHash(hval string) *packet.ShellState {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.StateMap[hval]
|
2022-11-02 05:42:56 +01:00
|
|
|
}
|
|
|
|
|
2022-10-04 20:45:24 +02:00
|
|
|
func (msh *MShellProc) GetRemoteId() string {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.Remote.RemoteId
|
|
|
|
}
|
|
|
|
|
2022-09-27 06:09:43 +02:00
|
|
|
func (msh *MShellProc) GetInstallStatus() string {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.InstallStatus
|
|
|
|
}
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
func (state RemoteRuntimeState) GetBaseDisplayName() string {
|
|
|
|
if state.RemoteAlias != "" {
|
|
|
|
return state.RemoteAlias
|
|
|
|
}
|
|
|
|
return state.RemoteCanonicalName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state RemoteRuntimeState) GetDisplayName(rptr *sstore.RemotePtrType) string {
|
2022-12-31 02:01:17 +01:00
|
|
|
baseDisplayName := state.GetBaseDisplayName()
|
2022-08-30 01:31:06 +02:00
|
|
|
if rptr == nil {
|
2022-12-31 02:01:17 +01:00
|
|
|
return baseDisplayName
|
2022-08-30 01:31:06 +02:00
|
|
|
}
|
2022-12-31 02:01:17 +01:00
|
|
|
return rptr.GetDisplayName(baseDisplayName)
|
2022-08-30 01:31:06 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 02:38:36 +02:00
|
|
|
func LoadRemotes(ctx context.Context) error {
|
|
|
|
GlobalStore = &Store{
|
2022-09-05 23:49:23 +02:00
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
Map: make(map[string]*MShellProc),
|
2022-09-06 01:31:22 +02:00
|
|
|
CmdWaitMap: make(map[base.CommandKey][]func()),
|
2022-07-02 02:38:36 +02:00
|
|
|
}
|
|
|
|
allRemotes, err := sstore.GetAllRemotes(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
var numLocal int
|
2022-12-29 09:07:16 +01:00
|
|
|
var numSudoLocal int
|
2022-07-02 02:38:36 +02:00
|
|
|
for _, remote := range allRemotes {
|
|
|
|
msh := MakeMShell(remote)
|
2022-07-06 01:54:49 +02:00
|
|
|
GlobalStore.Map[remote.RemoteId] = msh
|
2022-08-21 21:31:29 +02:00
|
|
|
if remote.ConnectMode == sstore.ConnectModeStartup {
|
2022-07-02 02:38:36 +02:00
|
|
|
go msh.Launch()
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
if remote.Local {
|
2022-12-29 09:07:16 +01:00
|
|
|
if remote.RemoteSudo {
|
|
|
|
numSudoLocal++
|
|
|
|
} else {
|
|
|
|
numLocal++
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if numLocal == 0 {
|
|
|
|
return fmt.Errorf("no local remote found")
|
|
|
|
}
|
|
|
|
if numLocal > 1 {
|
|
|
|
return fmt.Errorf("multiple local remotes found")
|
2022-07-02 02:38:36 +02:00
|
|
|
}
|
2022-12-29 09:07:16 +01:00
|
|
|
if numSudoLocal > 1 {
|
|
|
|
return fmt.Errorf("multiple local sudo remotes found")
|
|
|
|
}
|
2022-07-02 02:38:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-01 21:47:10 +02:00
|
|
|
func LoadRemoteById(ctx context.Context, remoteId string) error {
|
|
|
|
r, err := sstore.GetRemoteById(ctx, remoteId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r == nil {
|
|
|
|
return fmt.Errorf("remote %s not found", remoteId)
|
|
|
|
}
|
|
|
|
msh := MakeMShell(r)
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
existingRemote := GlobalStore.Map[remoteId]
|
|
|
|
if existingRemote != nil {
|
2022-12-28 22:56:19 +01:00
|
|
|
return fmt.Errorf("cannot add remote %s, already in global map", remoteId)
|
2022-09-01 21:47:10 +02:00
|
|
|
}
|
|
|
|
GlobalStore.Map[r.RemoteId] = msh
|
|
|
|
if r.ConnectMode == sstore.ConnectModeStartup {
|
|
|
|
go msh.Launch()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:10:35 +02:00
|
|
|
func ReadRemotePty(ctx context.Context, remoteId string) (int64, []byte, error) {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
msh := GlobalStore.Map[remoteId]
|
|
|
|
if msh == nil {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
barr := msh.PtyBuffer.Bytes()
|
|
|
|
offset := msh.PtyBuffer.TotalWritten() - int64(len(barr))
|
|
|
|
return offset, barr, nil
|
|
|
|
}
|
|
|
|
|
2022-09-14 02:11:36 +02:00
|
|
|
func AddRemote(ctx context.Context, r *sstore.RemoteType) error {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
|
|
|
|
existingRemote := getRemoteByCanonicalName_nolock(r.RemoteCanonicalName)
|
|
|
|
if existingRemote != nil {
|
2022-09-14 21:56:05 +02:00
|
|
|
erCopy := existingRemote.GetRemoteCopy()
|
2022-09-14 02:11:36 +02:00
|
|
|
if !erCopy.Archived {
|
|
|
|
return fmt.Errorf("duplicate canonical name %q: cannot create new remote", r.RemoteCanonicalName)
|
|
|
|
}
|
|
|
|
r.RemoteId = erCopy.RemoteId
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
if r.Local {
|
|
|
|
return fmt.Errorf("cannot create another local remote (there can be only one)")
|
|
|
|
}
|
|
|
|
|
2022-09-14 02:11:36 +02:00
|
|
|
err := sstore.UpsertRemote(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create remote %q: %v", r.RemoteCanonicalName, err)
|
|
|
|
}
|
|
|
|
newMsh := MakeMShell(r)
|
|
|
|
GlobalStore.Map[r.RemoteId] = newMsh
|
|
|
|
go newMsh.NotifyRemoteUpdate()
|
|
|
|
if r.ConnectMode == sstore.ConnectModeStartup {
|
|
|
|
go newMsh.Launch()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ArchiveRemote(ctx context.Context, remoteId string) error {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
msh := GlobalStore.Map[remoteId]
|
|
|
|
if msh == nil {
|
|
|
|
return fmt.Errorf("remote not found, cannot archive")
|
|
|
|
}
|
|
|
|
if msh.Status == StatusConnected {
|
|
|
|
return fmt.Errorf("cannot archive connected remote")
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
if msh.Remote.Local {
|
|
|
|
return fmt.Errorf("cannot archive local remote")
|
|
|
|
}
|
2022-09-14 21:56:05 +02:00
|
|
|
rcopy := msh.GetRemoteCopy()
|
2022-09-14 02:11:36 +02:00
|
|
|
archivedRemote := &sstore.RemoteType{
|
|
|
|
RemoteId: rcopy.RemoteId,
|
|
|
|
RemoteType: rcopy.RemoteType,
|
|
|
|
RemoteCanonicalName: rcopy.RemoteCanonicalName,
|
|
|
|
ConnectMode: sstore.ConnectModeManual,
|
|
|
|
Archived: true,
|
|
|
|
}
|
|
|
|
err := sstore.UpsertRemote(ctx, archivedRemote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newMsh := MakeMShell(archivedRemote)
|
|
|
|
GlobalStore.Map[remoteId] = newMsh
|
|
|
|
go newMsh.NotifyRemoteUpdate()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-14 22:01:52 +02:00
|
|
|
var partialUUIDRe = regexp.MustCompile("^[0-9a-f]{8}$")
|
|
|
|
|
|
|
|
func isPartialUUID(s string) bool {
|
|
|
|
return partialUUIDRe.MatchString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRemoteByArg(arg string) *MShellProc {
|
2022-07-02 02:38:36 +02:00
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
2022-09-14 22:01:52 +02:00
|
|
|
isPuid := isPartialUUID(arg)
|
2022-07-06 01:54:49 +02:00
|
|
|
for _, msh := range GlobalStore.Map {
|
2022-09-14 21:56:05 +02:00
|
|
|
rcopy := msh.GetRemoteCopy()
|
2022-09-14 22:01:52 +02:00
|
|
|
if rcopy.RemoteAlias == arg || rcopy.RemoteCanonicalName == arg || rcopy.RemoteId == arg {
|
|
|
|
return msh
|
|
|
|
}
|
|
|
|
if isPuid && strings.HasPrefix(rcopy.RemoteId, arg) {
|
2022-07-06 01:54:49 +02:00
|
|
|
return msh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-14 02:11:36 +02:00
|
|
|
func getRemoteByCanonicalName_nolock(name string) *MShellProc {
|
|
|
|
for _, msh := range GlobalStore.Map {
|
2022-09-14 21:56:05 +02:00
|
|
|
rcopy := msh.GetRemoteCopy()
|
2022-09-14 02:11:36 +02:00
|
|
|
if rcopy.RemoteCanonicalName == name {
|
|
|
|
return msh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-06 01:54:49 +02:00
|
|
|
func GetRemoteById(remoteId string) *MShellProc {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
return GlobalStore.Map[remoteId]
|
2022-07-02 02:38:36 +02:00
|
|
|
}
|
2022-07-01 23:57:42 +02:00
|
|
|
|
2022-12-31 02:01:17 +01:00
|
|
|
func GetRemoteMap() map[string]*MShellProc {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
rtn := make(map[string]*MShellProc)
|
|
|
|
for remoteId, msh := range GlobalStore.Map {
|
|
|
|
rtn[remoteId] = msh
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2022-10-04 20:45:24 +02:00
|
|
|
func GetLocalRemote() *MShellProc {
|
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
for _, msh := range GlobalStore.Map {
|
2022-12-29 09:07:16 +01:00
|
|
|
if msh.IsLocal() && !msh.IsSudo() {
|
2022-10-04 20:45:24 +02:00
|
|
|
return msh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
func ResolveRemoteRef(remoteRef string) *RemoteRuntimeState {
|
2022-08-24 11:14:16 +02:00
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
|
|
|
|
_, err := uuid.Parse(remoteRef)
|
|
|
|
if err == nil {
|
|
|
|
msh := GlobalStore.Map[remoteRef]
|
|
|
|
if msh != nil {
|
2022-08-30 01:31:06 +02:00
|
|
|
state := msh.GetRemoteRuntimeState()
|
2022-08-24 11:14:16 +02:00
|
|
|
return &state
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, msh := range GlobalStore.Map {
|
|
|
|
if msh.Remote.RemoteAlias == remoteRef || msh.Remote.RemoteCanonicalName == remoteRef {
|
2022-08-30 01:31:06 +02:00
|
|
|
state := msh.GetRemoteRuntimeState()
|
2022-08-24 11:14:16 +02:00
|
|
|
return &state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-23 01:00:25 +02:00
|
|
|
func unquoteDQBashString(str string) (string, bool) {
|
|
|
|
if len(str) < 2 {
|
|
|
|
return str, false
|
|
|
|
}
|
|
|
|
if str[0] != '"' || str[len(str)-1] != '"' {
|
|
|
|
return str, false
|
|
|
|
}
|
|
|
|
rtn := make([]byte, 0, len(str)-2)
|
|
|
|
for idx := 1; idx < len(str)-1; idx++ {
|
|
|
|
ch := str[idx]
|
|
|
|
if ch == '"' {
|
|
|
|
return str, false
|
|
|
|
}
|
|
|
|
if ch == '\\' {
|
|
|
|
if idx == len(str)-2 {
|
|
|
|
return str, false
|
|
|
|
}
|
|
|
|
nextCh := str[idx+1]
|
|
|
|
if nextCh == '\n' {
|
|
|
|
idx++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if nextCh == '$' || nextCh == '"' || nextCh == '\\' || nextCh == '`' {
|
|
|
|
idx++
|
|
|
|
rtn = append(rtn, nextCh)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rtn = append(rtn, '\\')
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
rtn = append(rtn, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(rtn), true
|
|
|
|
}
|
|
|
|
|
2022-08-24 06:05:49 +02:00
|
|
|
func makeShortHost(host string) string {
|
|
|
|
dotIdx := strings.Index(host, ".")
|
|
|
|
if dotIdx == -1 {
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
return host[0:dotIdx]
|
|
|
|
}
|
|
|
|
|
2022-10-04 20:45:24 +02:00
|
|
|
func (msh *MShellProc) IsLocal() bool {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.Remote.Local
|
|
|
|
}
|
|
|
|
|
2022-12-29 09:07:16 +01:00
|
|
|
func (msh *MShellProc) IsSudo() bool {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.Remote.RemoteSudo
|
|
|
|
}
|
|
|
|
|
2022-09-04 22:51:49 +02:00
|
|
|
func (msh *MShellProc) GetRemoteRuntimeState() RemoteRuntimeState {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-08-30 01:31:06 +02:00
|
|
|
state := RemoteRuntimeState{
|
2022-09-04 22:51:49 +02:00
|
|
|
RemoteType: msh.Remote.RemoteType,
|
|
|
|
RemoteId: msh.Remote.RemoteId,
|
|
|
|
RemoteAlias: msh.Remote.RemoteAlias,
|
|
|
|
RemoteCanonicalName: msh.Remote.RemoteCanonicalName,
|
|
|
|
PhysicalId: msh.Remote.PhysicalId,
|
|
|
|
Status: msh.Status,
|
|
|
|
ConnectMode: msh.Remote.ConnectMode,
|
2022-09-25 04:54:06 +02:00
|
|
|
AutoInstall: msh.Remote.AutoInstall,
|
2022-09-14 02:11:36 +02:00
|
|
|
Archived: msh.Remote.Archived,
|
2022-09-14 21:06:55 +02:00
|
|
|
RemoteIdx: msh.Remote.RemoteIdx,
|
2022-09-22 08:26:24 +02:00
|
|
|
UName: msh.UName,
|
2022-09-27 08:23:04 +02:00
|
|
|
InstallStatus: msh.InstallStatus,
|
|
|
|
NeedsMShellUpgrade: msh.NeedsMShellUpgrade,
|
2022-10-04 20:45:24 +02:00
|
|
|
Local: msh.Remote.Local,
|
2022-09-04 22:51:49 +02:00
|
|
|
}
|
2022-12-29 09:07:16 +01:00
|
|
|
if msh.Remote.RemoteOpts != nil {
|
|
|
|
optsCopy := *msh.Remote.RemoteOpts
|
|
|
|
state.RemoteOpts = &optsCopy
|
|
|
|
}
|
2022-09-04 22:51:49 +02:00
|
|
|
if msh.Err != nil {
|
|
|
|
state.ErrorStr = msh.Err.Error()
|
|
|
|
}
|
2022-09-27 08:23:04 +02:00
|
|
|
if msh.InstallErr != nil {
|
|
|
|
state.InstallErrorStr = msh.InstallErr.Error()
|
|
|
|
}
|
2022-10-01 02:22:28 +02:00
|
|
|
if msh.Status == StatusConnecting {
|
|
|
|
state.WaitingForPassword = msh.isWaitingForPassword_nolock()
|
|
|
|
}
|
2022-08-23 23:01:52 +02:00
|
|
|
vars := make(map[string]string)
|
2022-09-04 22:51:49 +02:00
|
|
|
vars["user"] = msh.Remote.RemoteUser
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["bestuser"] = vars["user"]
|
2022-09-04 22:51:49 +02:00
|
|
|
vars["host"] = msh.Remote.RemoteHost
|
|
|
|
vars["shorthost"] = makeShortHost(msh.Remote.RemoteHost)
|
|
|
|
vars["alias"] = msh.Remote.RemoteAlias
|
|
|
|
vars["cname"] = msh.Remote.RemoteCanonicalName
|
|
|
|
vars["physicalid"] = msh.Remote.PhysicalId
|
|
|
|
vars["remoteid"] = msh.Remote.RemoteId
|
|
|
|
vars["status"] = msh.Status
|
|
|
|
vars["type"] = msh.Remote.RemoteType
|
|
|
|
if msh.Remote.RemoteSudo {
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["sudo"] = "1"
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
if msh.Remote.Local {
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["local"] = "1"
|
|
|
|
}
|
2022-10-03 21:25:43 +02:00
|
|
|
vars["port"] = "22"
|
2022-10-04 04:04:48 +02:00
|
|
|
if msh.Remote.SSHOpts != nil {
|
|
|
|
if msh.Remote.SSHOpts.SSHPort != 0 {
|
|
|
|
vars["port"] = strconv.Itoa(msh.Remote.SSHOpts.SSHPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msh.Remote.RemoteOpts != nil && msh.Remote.RemoteOpts.Color != "" {
|
|
|
|
vars["color"] = msh.Remote.RemoteOpts.Color
|
2022-10-03 21:25:43 +02:00
|
|
|
}
|
2022-09-04 22:51:49 +02:00
|
|
|
if msh.ServerProc != nil && msh.ServerProc.InitPk != nil {
|
2022-09-22 08:26:24 +02:00
|
|
|
state.MShellVersion = msh.ServerProc.InitPk.Version
|
2022-09-04 22:51:49 +02:00
|
|
|
vars["home"] = msh.ServerProc.InitPk.HomeDir
|
|
|
|
vars["remoteuser"] = msh.ServerProc.InitPk.User
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["bestuser"] = vars["remoteuser"]
|
2022-09-04 22:51:49 +02:00
|
|
|
vars["remotehost"] = msh.ServerProc.InitPk.HostName
|
|
|
|
vars["remoteshorthost"] = makeShortHost(msh.ServerProc.InitPk.HostName)
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["besthost"] = vars["remotehost"]
|
|
|
|
vars["bestshorthost"] = vars["remoteshorthost"]
|
|
|
|
}
|
2022-11-28 09:13:00 +01:00
|
|
|
curState := msh.StateMap[msh.CurrentState]
|
|
|
|
if curState != nil {
|
|
|
|
state.DefaultFeState = sstore.FeStateFromShellState(curState)
|
|
|
|
vars["cwd"] = curState.Cwd
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
if msh.Remote.Local && msh.Remote.RemoteSudo {
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["bestuser"] = "sudo"
|
2022-09-04 22:51:49 +02:00
|
|
|
} else if msh.Remote.RemoteSudo {
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["bestuser"] = "sudo@" + vars["bestuser"]
|
|
|
|
}
|
2022-10-04 20:45:24 +02:00
|
|
|
if msh.Remote.Local {
|
2022-08-24 11:14:16 +02:00
|
|
|
vars["bestname"] = vars["bestuser"] + "@local"
|
|
|
|
vars["bestshortname"] = vars["bestuser"] + "@local"
|
|
|
|
} else {
|
|
|
|
vars["bestname"] = vars["bestuser"] + "@" + vars["besthost"]
|
|
|
|
vars["bestshortname"] = vars["bestuser"] + "@" + vars["bestshorthost"]
|
2022-08-23 23:01:52 +02:00
|
|
|
}
|
|
|
|
state.RemoteVars = vars
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:49:23 +02:00
|
|
|
func (msh *MShellProc) NotifyRemoteUpdate() {
|
2022-08-30 01:31:06 +02:00
|
|
|
rstate := msh.GetRemoteRuntimeState()
|
2022-09-01 08:12:26 +02:00
|
|
|
update := &sstore.ModelUpdate{Remotes: []interface{}{rstate}}
|
2022-08-26 22:12:17 +02:00
|
|
|
sstore.MainBus.SendUpdate("", update)
|
|
|
|
}
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
func GetAllRemoteRuntimeState() []RemoteRuntimeState {
|
2022-07-05 07:18:01 +02:00
|
|
|
GlobalStore.Lock.Lock()
|
|
|
|
defer GlobalStore.Lock.Unlock()
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
var rtn []RemoteRuntimeState
|
2022-07-05 07:18:01 +02:00
|
|
|
for _, proc := range GlobalStore.Map {
|
2022-08-30 01:31:06 +02:00
|
|
|
state := proc.GetRemoteRuntimeState()
|
2022-07-05 07:18:01 +02:00
|
|
|
rtn = append(rtn, state)
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2022-10-17 08:51:04 +02:00
|
|
|
func GetDefaultRemoteStateById(remoteId string) (*packet.ShellState, error) {
|
2022-07-16 02:37:32 +02:00
|
|
|
remote := GetRemoteById(remoteId)
|
|
|
|
if remote == nil {
|
|
|
|
return nil, fmt.Errorf("remote not found")
|
|
|
|
}
|
|
|
|
if !remote.IsConnected() {
|
|
|
|
return nil, fmt.Errorf("remote not connected")
|
|
|
|
}
|
|
|
|
state := remote.GetDefaultState()
|
|
|
|
if state == nil {
|
|
|
|
return nil, fmt.Errorf("could not get default remote state")
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:38:36 +02:00
|
|
|
func MakeMShell(r *sstore.RemoteType) *MShellProc {
|
2022-09-15 08:10:35 +02:00
|
|
|
buf, err := circbuf.NewBuffer(CircBufSize)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // this should never happen (NewBuffer only returns an error if CirBufSize <= 0)
|
|
|
|
}
|
|
|
|
rtn := &MShellProc{
|
2022-10-28 07:00:10 +02:00
|
|
|
Lock: &sync.Mutex{},
|
|
|
|
Remote: r,
|
2022-10-28 07:22:17 +02:00
|
|
|
RemoteId: r.RemoteId,
|
2022-10-28 07:00:10 +02:00
|
|
|
Status: StatusDisconnected,
|
|
|
|
PtyBuffer: buf,
|
|
|
|
InstallStatus: StatusDisconnected,
|
2022-10-28 07:22:17 +02:00
|
|
|
RunningCmds: make(map[base.CommandKey]RunCmdType),
|
2022-10-28 07:00:10 +02:00
|
|
|
PendingStateCmds: make(map[string]base.CommandKey),
|
2022-11-28 09:13:00 +01:00
|
|
|
StateMap: make(map[string]*packet.ShellState),
|
2022-09-15 08:10:35 +02:00
|
|
|
}
|
|
|
|
rtn.WriteToPtyBuffer("console for remote [%s]\n", r.GetName())
|
2022-07-02 02:38:36 +02:00
|
|
|
return rtn
|
2022-07-01 23:57:42 +02:00
|
|
|
}
|
|
|
|
|
2022-09-16 02:09:04 +02:00
|
|
|
func SendRemoteInput(pk *scpacket.RemoteInputPacketType) error {
|
|
|
|
data, err := base64.StdEncoding.DecodeString(pk.InputData64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot decode base64: %v\n", err)
|
|
|
|
}
|
|
|
|
msh := GetRemoteById(pk.RemoteId)
|
|
|
|
if msh == nil {
|
|
|
|
return fmt.Errorf("remote not found")
|
|
|
|
}
|
|
|
|
var cmdPty *os.File
|
|
|
|
msh.WithLock(func() {
|
|
|
|
cmdPty = msh.ControllingPty
|
|
|
|
})
|
|
|
|
if cmdPty == nil {
|
|
|
|
return fmt.Errorf("remote has no attached pty")
|
|
|
|
}
|
|
|
|
_, err = cmdPty.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("writing to pty: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:24:09 +02:00
|
|
|
func convertSSHOpts(opts *sstore.SSHOpts) shexec.SSHOpts {
|
2022-08-24 11:14:16 +02:00
|
|
|
if opts == nil || opts.Local {
|
|
|
|
opts = &sstore.SSHOpts{}
|
2022-08-17 21:24:09 +02:00
|
|
|
}
|
|
|
|
return shexec.SSHOpts{
|
|
|
|
SSHHost: opts.SSHHost,
|
|
|
|
SSHOptsStr: opts.SSHOptsStr,
|
|
|
|
SSHIdentity: opts.SSHIdentity,
|
|
|
|
SSHUser: opts.SSHUser,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 07:57:41 +02:00
|
|
|
func (msh *MShellProc) addControllingTty(ecmd *exec.Cmd) (*os.File, error) {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
|
2022-08-19 22:23:00 +02:00
|
|
|
cmdPty, cmdTty, err := pty.Open()
|
|
|
|
if err != nil {
|
2022-08-25 07:57:41 +02:00
|
|
|
return nil, err
|
2022-08-19 22:23:00 +02:00
|
|
|
}
|
2022-09-15 09:17:23 +02:00
|
|
|
pty.Setsize(cmdPty, &pty.Winsize{Rows: RemoteTermRows, Cols: RemoteTermCols})
|
2022-08-19 22:23:00 +02:00
|
|
|
msh.ControllingPty = cmdPty
|
|
|
|
ecmd.ExtraFiles = append(ecmd.ExtraFiles, cmdTty)
|
|
|
|
if ecmd.SysProcAttr == nil {
|
|
|
|
ecmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
}
|
|
|
|
ecmd.SysProcAttr.Setsid = true
|
|
|
|
ecmd.SysProcAttr.Setctty = true
|
|
|
|
ecmd.SysProcAttr.Ctty = len(ecmd.ExtraFiles) + 3 - 1
|
2022-08-25 07:57:41 +02:00
|
|
|
return cmdPty, nil
|
2022-08-19 22:23:00 +02:00
|
|
|
}
|
|
|
|
|
2022-08-25 07:57:41 +02:00
|
|
|
func (msh *MShellProc) setErrorStatus(err error) {
|
2022-07-02 02:38:36 +02:00
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-08-25 07:57:41 +02:00
|
|
|
msh.Status = StatusError
|
|
|
|
msh.Err = err
|
2022-09-05 23:49:23 +02:00
|
|
|
go msh.NotifyRemoteUpdate()
|
2022-08-25 07:57:41 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 06:09:43 +02:00
|
|
|
func (msh *MShellProc) setInstallErrorStatus(err error) {
|
2022-09-27 08:23:04 +02:00
|
|
|
msh.WriteToPtyBuffer("*error, %s\n", err.Error())
|
2022-09-27 06:09:43 +02:00
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
msh.InstallStatus = StatusError
|
|
|
|
msh.InstallErr = err
|
|
|
|
go msh.NotifyRemoteUpdate()
|
|
|
|
}
|
|
|
|
|
2022-09-14 21:56:05 +02:00
|
|
|
func (msh *MShellProc) GetRemoteCopy() sstore.RemoteType {
|
2022-08-25 07:57:41 +02:00
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return *msh.Remote
|
|
|
|
}
|
2022-07-02 02:38:36 +02:00
|
|
|
|
2022-09-22 08:26:24 +02:00
|
|
|
func (msh *MShellProc) GetUName() string {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.UName
|
|
|
|
}
|
|
|
|
|
2022-09-01 21:47:10 +02:00
|
|
|
func (msh *MShellProc) GetNumRunningCommands() int {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return len(msh.RunningCmds)
|
|
|
|
}
|
|
|
|
|
2022-10-03 03:52:55 +02:00
|
|
|
func (msh *MShellProc) UpdateRemote(ctx context.Context, editMap map[string]interface{}) error {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
updatedRemote, err := sstore.UpdateRemote(ctx, msh.Remote.RemoteId, editMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if updatedRemote == nil {
|
|
|
|
return fmt.Errorf("no remote returned from UpdateRemote")
|
|
|
|
}
|
|
|
|
msh.Remote = updatedRemote
|
|
|
|
go msh.NotifyRemoteUpdate()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 08:23:04 +02:00
|
|
|
func (msh *MShellProc) Disconnect(force bool) {
|
|
|
|
status := msh.GetStatus()
|
|
|
|
if status != StatusConnected && status != StatusConnecting {
|
|
|
|
msh.WriteToPtyBuffer("remote already disconnected (no action taken)\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
numCommands := msh.GetNumRunningCommands()
|
|
|
|
if numCommands > 0 && !force {
|
|
|
|
msh.WriteToPtyBuffer("remote not disconnected, has %d running commands. use force=1 to force disconnection\n", numCommands)
|
|
|
|
return
|
|
|
|
}
|
2022-09-01 21:47:10 +02:00
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-09-14 02:11:36 +02:00
|
|
|
if msh.ServerProc != nil {
|
|
|
|
msh.ServerProc.Close()
|
|
|
|
}
|
2022-09-16 21:28:09 +02:00
|
|
|
if msh.MakeClientCancelFn != nil {
|
|
|
|
msh.MakeClientCancelFn()
|
|
|
|
msh.MakeClientCancelFn = nil
|
|
|
|
}
|
2022-09-01 21:47:10 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 08:23:04 +02:00
|
|
|
func (msh *MShellProc) CancelInstall() {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
if msh.InstallCancelFn != nil {
|
|
|
|
msh.InstallCancelFn()
|
|
|
|
msh.InstallCancelFn = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 21:58:16 +02:00
|
|
|
func (msh *MShellProc) GetRemoteName() string {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.Remote.GetName()
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:10:35 +02:00
|
|
|
func (msh *MShellProc) WriteToPtyBuffer(strFmt string, args ...interface{}) {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
msh.writeToPtyBuffer_nolock(strFmt, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) writeToPtyBuffer_nolock(strFmt string, args ...interface{}) {
|
|
|
|
// inefficient string manipulation here and read of PtyBuffer, but these messages are rare, nbd
|
|
|
|
realStr := fmt.Sprintf(strFmt, args...)
|
|
|
|
if !strings.HasPrefix(realStr, "~") {
|
|
|
|
realStr = strings.ReplaceAll(realStr, "\n", "\r\n")
|
|
|
|
if !strings.HasSuffix(realStr, "\r\n") {
|
|
|
|
realStr = realStr + "\r\n"
|
|
|
|
}
|
2022-09-15 09:37:17 +02:00
|
|
|
if strings.HasPrefix(realStr, "*") {
|
2022-12-20 02:36:19 +01:00
|
|
|
realStr = "\033[0m\033[31mprompt>\033[0m " + realStr[1:]
|
2022-09-15 09:37:17 +02:00
|
|
|
} else {
|
2022-12-20 02:36:19 +01:00
|
|
|
realStr = "\033[0m\033[32mprompt>\033[0m " + realStr
|
2022-09-15 09:37:17 +02:00
|
|
|
}
|
2022-09-15 08:10:35 +02:00
|
|
|
barr := msh.PtyBuffer.Bytes()
|
|
|
|
if len(barr) > 0 && barr[len(barr)-1] != '\n' {
|
|
|
|
realStr = "\r\n" + realStr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
realStr = realStr[1:]
|
|
|
|
}
|
2022-09-15 09:17:23 +02:00
|
|
|
curOffset := msh.PtyBuffer.TotalWritten()
|
|
|
|
data := []byte(realStr)
|
|
|
|
msh.PtyBuffer.Write(data)
|
|
|
|
sendRemotePtyUpdate(msh.Remote.RemoteId, curOffset, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendRemotePtyUpdate(remoteId string, dataOffset int64, data []byte) {
|
|
|
|
data64 := base64.StdEncoding.EncodeToString(data)
|
|
|
|
update := &sstore.PtyDataUpdate{
|
|
|
|
RemoteId: remoteId,
|
|
|
|
PtyPos: dataOffset,
|
|
|
|
PtyData64: data64,
|
|
|
|
PtyDataLen: int64(len(data)),
|
|
|
|
}
|
|
|
|
sstore.MainBus.SendUpdate("", update)
|
2022-09-15 08:10:35 +02:00
|
|
|
}
|
|
|
|
|
2022-10-01 02:22:28 +02:00
|
|
|
func (msh *MShellProc) isWaitingForPassword_nolock() bool {
|
|
|
|
barr := msh.PtyBuffer.Bytes()
|
|
|
|
if len(barr) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nlIdx := bytes.LastIndex(barr, []byte{'\n'})
|
|
|
|
var lastLine string
|
|
|
|
if nlIdx == -1 {
|
|
|
|
lastLine = string(barr)
|
|
|
|
} else {
|
|
|
|
lastLine = string(barr[nlIdx+1:])
|
|
|
|
}
|
|
|
|
pwIdx := strings.Index(lastLine, "assword")
|
|
|
|
return pwIdx != -1
|
|
|
|
}
|
|
|
|
|
2022-09-16 02:44:39 +02:00
|
|
|
func (msh *MShellProc) RunPtyReadLoop(cmdPty *os.File) {
|
|
|
|
buf := make([]byte, PtyReadBufSize)
|
2022-10-01 02:22:28 +02:00
|
|
|
var isWaiting bool
|
2022-09-16 02:44:39 +02:00
|
|
|
for {
|
|
|
|
n, readErr := cmdPty.Read(buf)
|
|
|
|
if readErr == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if readErr != nil {
|
|
|
|
msh.WriteToPtyBuffer("*error reading from controlling-pty: %v\n", readErr)
|
|
|
|
break
|
|
|
|
}
|
2022-10-01 02:22:28 +02:00
|
|
|
var newIsWaiting bool
|
2022-09-16 02:44:39 +02:00
|
|
|
msh.WithLock(func() {
|
|
|
|
curOffset := msh.PtyBuffer.TotalWritten()
|
|
|
|
msh.PtyBuffer.Write(buf[0:n])
|
|
|
|
sendRemotePtyUpdate(msh.Remote.RemoteId, curOffset, buf[0:n])
|
2022-10-01 02:22:28 +02:00
|
|
|
newIsWaiting = msh.isWaitingForPassword_nolock()
|
2022-09-16 02:44:39 +02:00
|
|
|
})
|
2022-10-01 02:22:28 +02:00
|
|
|
if newIsWaiting != isWaiting {
|
|
|
|
isWaiting = newIsWaiting
|
|
|
|
go msh.NotifyRemoteUpdate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) WaitAndSendPassword(pw string) {
|
|
|
|
var numWaits int
|
|
|
|
for {
|
|
|
|
var isWaiting bool
|
|
|
|
var isConnecting bool
|
|
|
|
msh.WithLock(func() {
|
|
|
|
isWaiting = msh.isWaitingForPassword_nolock()
|
|
|
|
isConnecting = msh.Status == StatusConnecting
|
|
|
|
})
|
|
|
|
if !isConnecting {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !isWaiting {
|
|
|
|
numWaits = 0
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numWaits++
|
|
|
|
if numWaits < 10 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
} else {
|
|
|
|
// send password
|
|
|
|
msh.WithLock(func() {
|
|
|
|
if msh.ControllingPty == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pwBytes := []byte(pw + "\r")
|
|
|
|
msh.writeToPtyBuffer_nolock("~[sent password]\r\n")
|
|
|
|
_, err := msh.ControllingPty.Write(pwBytes)
|
|
|
|
if err != nil {
|
|
|
|
msh.writeToPtyBuffer_nolock("*cannot write password to controlling pty: %v\n", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
2022-09-16 02:44:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 06:09:43 +02:00
|
|
|
func (msh *MShellProc) RunInstall() {
|
|
|
|
remoteCopy := msh.GetRemoteCopy()
|
|
|
|
if remoteCopy.Archived {
|
2022-09-27 08:23:04 +02:00
|
|
|
msh.WriteToPtyBuffer("*error: cannot install on archived remote\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
baseStatus := msh.GetStatus()
|
|
|
|
if baseStatus == StatusConnecting || baseStatus == StatusConnected {
|
|
|
|
msh.WriteToPtyBuffer("*error: cannot install on remote that is connected/connecting, disconnect to install\n")
|
2022-09-27 06:09:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
curStatus := msh.GetInstallStatus()
|
|
|
|
if curStatus == StatusConnecting {
|
2022-09-27 08:23:04 +02:00
|
|
|
msh.WriteToPtyBuffer("*error: cannot install on remote that is already trying to install, cancel current install to try again\n")
|
2022-09-27 06:09:43 +02:00
|
|
|
return
|
|
|
|
}
|
2022-12-29 08:09:37 +01:00
|
|
|
msh.WriteToPtyBuffer("installing mshell %s to %s...\n", scbase.MShellVersion, remoteCopy.RemoteCanonicalName)
|
2022-09-27 06:09:43 +02:00
|
|
|
sshOpts := convertSSHOpts(remoteCopy.SSHOpts)
|
|
|
|
sshOpts.SSHErrorsToTty = true
|
|
|
|
cmdStr := shexec.MakeInstallCommandStr()
|
|
|
|
ecmd := sshOpts.MakeSSHExecCmd(cmdStr)
|
|
|
|
cmdPty, err := msh.addControllingTty(ecmd)
|
|
|
|
if err != nil {
|
|
|
|
statusErr := fmt.Errorf("cannot attach controlling tty to mshell install command: %w", err)
|
|
|
|
msh.setInstallErrorStatus(statusErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if len(ecmd.ExtraFiles) > 0 {
|
|
|
|
ecmd.ExtraFiles[len(ecmd.ExtraFiles)-1].Close()
|
|
|
|
}
|
2022-09-27 08:23:04 +02:00
|
|
|
cmdPty.Close()
|
2022-09-27 06:09:43 +02:00
|
|
|
}()
|
|
|
|
go msh.RunPtyReadLoop(cmdPty)
|
|
|
|
clientCtx, clientCancelFn := context.WithCancel(context.Background())
|
|
|
|
defer clientCancelFn()
|
|
|
|
msh.WithLock(func() {
|
2022-09-27 08:23:04 +02:00
|
|
|
msh.InstallErr = nil
|
2022-09-27 06:09:43 +02:00
|
|
|
msh.InstallStatus = StatusConnecting
|
|
|
|
msh.InstallCancelFn = clientCancelFn
|
|
|
|
go msh.NotifyRemoteUpdate()
|
|
|
|
})
|
|
|
|
msgFn := func(msg string) {
|
|
|
|
msh.WriteToPtyBuffer("%s", msg)
|
|
|
|
}
|
2022-12-29 08:09:37 +01:00
|
|
|
err = shexec.RunInstallFromCmd(clientCtx, ecmd, true, nil, scbase.MShellBinaryReader, msgFn)
|
2022-09-27 08:23:04 +02:00
|
|
|
if err == context.Canceled {
|
|
|
|
msh.WriteToPtyBuffer("*install canceled\n")
|
|
|
|
msh.WithLock(func() {
|
|
|
|
msh.InstallStatus = StatusDisconnected
|
|
|
|
go msh.NotifyRemoteUpdate()
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 06:09:43 +02:00
|
|
|
if err != nil {
|
|
|
|
statusErr := fmt.Errorf("install failed: %w", err)
|
|
|
|
msh.setInstallErrorStatus(statusErr)
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 08:23:04 +02:00
|
|
|
msh.WithLock(func() {
|
|
|
|
msh.InstallStatus = StatusDisconnected
|
|
|
|
msh.InstallCancelFn = nil
|
|
|
|
msh.NeedsMShellUpgrade = false
|
|
|
|
})
|
2022-12-29 08:09:37 +01:00
|
|
|
msh.WriteToPtyBuffer("successfully installed mshell %s\n", scbase.MShellVersion)
|
2022-09-27 08:23:04 +02:00
|
|
|
go msh.NotifyRemoteUpdate()
|
2022-09-27 06:09:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 02:10:36 +02:00
|
|
|
func (msh *MShellProc) ReInit(ctx context.Context) (*packet.InitPacketType, error) {
|
|
|
|
reinitPk := packet.MakeReInitPacket()
|
|
|
|
reinitPk.ReqId = uuid.New().String()
|
|
|
|
resp, err := msh.PacketRpcRaw(ctx, reinitPk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
return nil, fmt.Errorf("no response")
|
|
|
|
}
|
|
|
|
initPk, ok := resp.(*packet.InitPacketType)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid reinit response (not an initpacket): %T", resp)
|
|
|
|
}
|
2022-10-28 07:52:38 +02:00
|
|
|
if initPk.State == nil {
|
|
|
|
return nil, fmt.Errorf("invalid reinit response initpk does not contain remote state")
|
|
|
|
}
|
2022-11-28 09:13:00 +01:00
|
|
|
hval := initPk.State.GetHashVal(false)
|
2022-11-29 03:03:02 +01:00
|
|
|
sstore.StoreStateBase(ctx, initPk.State)
|
2022-10-28 02:10:36 +02:00
|
|
|
msh.WithLock(func() {
|
2022-11-28 09:13:00 +01:00
|
|
|
msh.CurrentState = hval
|
|
|
|
msh.StateMap[hval] = initPk.State
|
2022-10-28 02:10:36 +02:00
|
|
|
})
|
|
|
|
return initPk, nil
|
|
|
|
}
|
|
|
|
|
2022-11-02 05:42:56 +01:00
|
|
|
func addScVarsToState(state *packet.ShellState) *packet.ShellState {
|
|
|
|
if state == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rtn := *state
|
|
|
|
envMap := shexec.DeclMapFromState(&rtn)
|
2022-12-20 02:36:19 +01:00
|
|
|
envMap["PROMPT"] = &shexec.DeclareDeclType{Name: "PROMPT", Value: "1"}
|
|
|
|
envMap["PROMPT_VERSION"] = &shexec.DeclareDeclType{Name: "PROMPT_VERSION", Value: scbase.PromptVersion}
|
2022-11-02 05:42:56 +01:00
|
|
|
rtn.ShellVars = shexec.SerializeDeclMap(envMap)
|
|
|
|
return &rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func stripScVarsFromState(state *packet.ShellState) *packet.ShellState {
|
|
|
|
if state == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rtn := *state
|
2022-11-29 03:03:02 +01:00
|
|
|
rtn.HashVal = ""
|
2022-11-02 05:42:56 +01:00
|
|
|
envMap := shexec.DeclMapFromState(&rtn)
|
2022-12-20 02:36:19 +01:00
|
|
|
delete(envMap, "PROMPT")
|
|
|
|
delete(envMap, "PROMPT_VERSION")
|
2022-11-02 05:42:56 +01:00
|
|
|
rtn.ShellVars = shexec.SerializeDeclMap(envMap)
|
|
|
|
return &rtn
|
|
|
|
}
|
|
|
|
|
2022-11-29 03:03:02 +01:00
|
|
|
func stripScVarsFromStateDiff(stateDiff *packet.ShellStateDiff) *packet.ShellStateDiff {
|
|
|
|
if stateDiff == nil || len(stateDiff.VarsDiff) == 0 {
|
|
|
|
return stateDiff
|
|
|
|
}
|
|
|
|
rtn := *stateDiff
|
|
|
|
rtn.HashVal = ""
|
|
|
|
var mapDiff statediff.MapDiffType
|
|
|
|
err := mapDiff.Decode(stateDiff.VarsDiff)
|
|
|
|
if err != nil {
|
|
|
|
return stateDiff
|
|
|
|
}
|
2022-12-20 02:36:19 +01:00
|
|
|
delete(mapDiff.ToAdd, "PROMPT")
|
|
|
|
delete(mapDiff.ToAdd, "PROMPT_VERSION")
|
2022-11-29 03:03:02 +01:00
|
|
|
rtn.VarsDiff = mapDiff.Encode()
|
|
|
|
return &rtn
|
|
|
|
}
|
|
|
|
|
2022-08-25 07:57:41 +02:00
|
|
|
func (msh *MShellProc) Launch() {
|
2022-09-14 21:56:05 +02:00
|
|
|
remoteCopy := msh.GetRemoteCopy()
|
2022-09-14 02:11:36 +02:00
|
|
|
if remoteCopy.Archived {
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.WriteToPtyBuffer("cannot launch archived remote\n")
|
2022-09-13 21:06:12 +02:00
|
|
|
return
|
|
|
|
}
|
2022-09-16 21:28:09 +02:00
|
|
|
curStatus := msh.GetStatus()
|
2022-09-27 08:23:04 +02:00
|
|
|
if curStatus == StatusConnected {
|
|
|
|
msh.WriteToPtyBuffer("remote is already connected (no action taken)\n")
|
|
|
|
return
|
|
|
|
}
|
2022-09-16 21:28:09 +02:00
|
|
|
if curStatus == StatusConnecting {
|
|
|
|
msh.WriteToPtyBuffer("remote is already connecting, disconnect before trying to connect again\n")
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 08:23:04 +02:00
|
|
|
istatus := msh.GetInstallStatus()
|
|
|
|
if istatus == StatusConnecting {
|
|
|
|
msh.WriteToPtyBuffer("remote is trying to install, cancel install before trying to connect again\n")
|
|
|
|
return
|
|
|
|
}
|
2022-09-15 09:37:17 +02:00
|
|
|
msh.WriteToPtyBuffer("connecting to %s...\n", remoteCopy.RemoteCanonicalName)
|
2022-09-14 02:11:36 +02:00
|
|
|
sshOpts := convertSSHOpts(remoteCopy.SSHOpts)
|
|
|
|
sshOpts.SSHErrorsToTty = true
|
2022-10-01 02:22:28 +02:00
|
|
|
if remoteCopy.ConnectMode != sstore.ConnectModeManual && remoteCopy.SSHOpts.SSHPassword == "" {
|
2022-10-01 01:05:48 +02:00
|
|
|
sshOpts.BatchMode = true
|
|
|
|
}
|
2022-12-29 08:09:37 +01:00
|
|
|
var cmdStr string
|
2022-12-29 09:07:16 +01:00
|
|
|
if sshOpts.SSHHost == "" && remoteCopy.Local {
|
2022-12-29 08:09:37 +01:00
|
|
|
var err error
|
2022-12-29 09:07:16 +01:00
|
|
|
cmdStr, err = MakeLocalMShellCommandStr(remoteCopy.RemoteSudo)
|
2022-12-29 08:09:37 +01:00
|
|
|
if err != nil {
|
|
|
|
msh.WriteToPtyBuffer("*error, cannot find local mshell binary: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("local mshell binary: %s\n", cmdStr)
|
|
|
|
} else {
|
|
|
|
cmdStr = MakeServerCommandStr()
|
|
|
|
}
|
2022-09-27 06:09:43 +02:00
|
|
|
ecmd := sshOpts.MakeSSHExecCmd(cmdStr)
|
2022-08-25 07:57:41 +02:00
|
|
|
cmdPty, err := msh.addControllingTty(ecmd)
|
2022-08-19 22:23:00 +02:00
|
|
|
if err != nil {
|
2022-09-04 08:36:15 +02:00
|
|
|
statusErr := fmt.Errorf("cannot attach controlling tty to mshell command: %w", err)
|
2022-09-15 09:37:17 +02:00
|
|
|
msh.WriteToPtyBuffer("*error, %s\n", statusErr.Error())
|
2022-09-04 08:36:15 +02:00
|
|
|
msh.setErrorStatus(statusErr)
|
2022-08-19 22:23:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if len(ecmd.ExtraFiles) > 0 {
|
|
|
|
ecmd.ExtraFiles[len(ecmd.ExtraFiles)-1].Close()
|
|
|
|
}
|
|
|
|
}()
|
2022-09-16 02:44:39 +02:00
|
|
|
go msh.RunPtyReadLoop(cmdPty)
|
2022-10-01 02:22:28 +02:00
|
|
|
if remoteCopy.SSHOpts.SSHPassword != "" {
|
|
|
|
go msh.WaitAndSendPassword(remoteCopy.SSHOpts.SSHPassword)
|
|
|
|
}
|
2022-09-16 21:28:09 +02:00
|
|
|
makeClientCtx, makeClientCancelFn := context.WithCancel(context.Background())
|
|
|
|
defer makeClientCancelFn()
|
2022-09-16 02:44:39 +02:00
|
|
|
msh.WithLock(func() {
|
2022-09-27 08:23:04 +02:00
|
|
|
msh.Err = nil
|
2022-09-16 02:44:39 +02:00
|
|
|
msh.Status = StatusConnecting
|
2022-09-16 21:28:09 +02:00
|
|
|
msh.MakeClientCancelFn = makeClientCancelFn
|
2022-09-16 02:44:39 +02:00
|
|
|
go msh.NotifyRemoteUpdate()
|
|
|
|
})
|
2022-10-19 03:03:02 +02:00
|
|
|
cproc, initPk, err := shexec.MakeClientProc(makeClientCtx, ecmd)
|
2022-10-28 07:52:38 +02:00
|
|
|
// TODO check if initPk.State is not nil
|
2022-09-25 04:54:06 +02:00
|
|
|
var mshellVersion string
|
2022-11-28 09:13:00 +01:00
|
|
|
var stateBaseHash string
|
2022-08-25 07:57:41 +02:00
|
|
|
msh.WithLock(func() {
|
2022-09-16 21:28:09 +02:00
|
|
|
msh.MakeClientCancelFn = nil
|
2022-10-19 03:03:02 +02:00
|
|
|
if initPk != nil {
|
|
|
|
msh.UName = initPk.UName
|
|
|
|
mshellVersion = initPk.Version
|
2022-12-29 08:09:37 +01:00
|
|
|
if semver.Compare(mshellVersion, scbase.MShellVersion) < 0 {
|
2022-10-04 20:15:59 +02:00
|
|
|
// only set NeedsMShellUpgrade if we got an InitPk
|
|
|
|
msh.NeedsMShellUpgrade = true
|
|
|
|
}
|
2022-09-22 08:26:24 +02:00
|
|
|
}
|
2022-11-28 09:13:00 +01:00
|
|
|
if initPk != nil && initPk.State != nil {
|
|
|
|
hval := initPk.State.GetHashVal(false)
|
|
|
|
msh.CurrentState = hval
|
|
|
|
msh.StateMap[hval] = initPk.State
|
|
|
|
sstore.StoreStateBase(context.Background(), initPk.State)
|
|
|
|
stateBaseHash = hval
|
|
|
|
} else {
|
|
|
|
msh.CurrentState = ""
|
|
|
|
}
|
2022-09-04 22:51:49 +02:00
|
|
|
// no notify here, because we'll call notify in either case below
|
2022-08-25 07:57:41 +02:00
|
|
|
})
|
2022-09-16 21:28:09 +02:00
|
|
|
if err == context.Canceled {
|
2022-09-27 08:23:04 +02:00
|
|
|
msh.WriteToPtyBuffer("*forced disconnection\n")
|
|
|
|
msh.WithLock(func() {
|
|
|
|
msh.Status = StatusDisconnected
|
|
|
|
go msh.NotifyRemoteUpdate()
|
|
|
|
})
|
|
|
|
return
|
2022-09-16 21:28:09 +02:00
|
|
|
}
|
2022-12-29 08:09:37 +01:00
|
|
|
if err == nil && semver.MajorMinor(mshellVersion) != semver.MajorMinor(scbase.MShellVersion) {
|
|
|
|
err = fmt.Errorf("mshell version is not compatible current=%s remote=%s", scbase.MShellVersion, mshellVersion)
|
2022-09-25 04:54:06 +02:00
|
|
|
}
|
2022-07-02 02:38:36 +02:00
|
|
|
if err != nil {
|
2022-08-25 07:57:41 +02:00
|
|
|
msh.setErrorStatus(err)
|
2022-10-01 01:05:48 +02:00
|
|
|
msh.WriteToPtyBuffer("*error connecting to remote: %v\n", err)
|
2022-07-02 02:38:36 +02:00
|
|
|
return
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
2022-11-28 09:13:00 +01:00
|
|
|
msh.WriteToPtyBuffer("connected state:%s\n", stateBaseHash)
|
2022-08-25 07:57:41 +02:00
|
|
|
msh.WithLock(func() {
|
|
|
|
msh.ServerProc = cproc
|
|
|
|
msh.Status = StatusConnected
|
2022-09-05 23:49:23 +02:00
|
|
|
go msh.NotifyRemoteUpdate()
|
2022-08-25 07:57:41 +02:00
|
|
|
})
|
2022-07-01 21:17:19 +02:00
|
|
|
go func() {
|
2022-07-07 04:01:00 +02:00
|
|
|
exitErr := cproc.Cmd.Wait()
|
2022-07-01 21:17:19 +02:00
|
|
|
exitCode := shexec.GetExitCode(exitErr)
|
2022-07-02 02:38:36 +02:00
|
|
|
msh.WithLock(func() {
|
2022-09-16 02:44:39 +02:00
|
|
|
if msh.Status == StatusConnected || msh.Status == StatusConnecting {
|
2022-07-02 02:38:36 +02:00
|
|
|
msh.Status = StatusDisconnected
|
2022-09-05 23:49:23 +02:00
|
|
|
go msh.NotifyRemoteUpdate()
|
2022-07-02 02:38:36 +02:00
|
|
|
}
|
|
|
|
})
|
2022-09-15 09:37:17 +02:00
|
|
|
msh.WriteToPtyBuffer("*disconnected exitcode=%d\n", exitCode)
|
2022-07-01 21:17:19 +02:00
|
|
|
}()
|
2022-07-02 02:38:36 +02:00
|
|
|
go msh.ProcessPackets()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) IsConnected() bool {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
return msh.Status == StatusConnected
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 11:14:16 +02:00
|
|
|
func replaceHomePath(pathStr string, homeDir string) string {
|
|
|
|
if homeDir == "" {
|
|
|
|
return pathStr
|
|
|
|
}
|
|
|
|
if pathStr == homeDir {
|
|
|
|
return "~"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(pathStr, homeDir+"/") {
|
|
|
|
return "~" + pathStr[len(homeDir):]
|
|
|
|
}
|
|
|
|
return pathStr
|
|
|
|
}
|
|
|
|
|
2022-08-30 01:31:06 +02:00
|
|
|
func (state RemoteRuntimeState) ExpandHomeDir(pathStr string) (string, error) {
|
2022-08-17 21:24:09 +02:00
|
|
|
if pathStr != "~" && !strings.HasPrefix(pathStr, "~/") {
|
|
|
|
return pathStr, nil
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
homeDir := state.RemoteVars["home"]
|
2022-08-17 21:24:09 +02:00
|
|
|
if homeDir == "" {
|
|
|
|
return "", fmt.Errorf("remote does not have HOME set, cannot do ~ expansion")
|
|
|
|
}
|
|
|
|
if pathStr == "~" {
|
|
|
|
return homeDir, nil
|
|
|
|
}
|
|
|
|
return path.Join(homeDir, pathStr[2:]), nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 07:46:28 +02:00
|
|
|
func (msh *MShellProc) IsCmdRunning(ck base.CommandKey) bool {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-09-27 08:23:04 +02:00
|
|
|
for runningCk, _ := range msh.RunningCmds {
|
2022-07-08 07:46:28 +02:00
|
|
|
if runningCk == ck {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
func (msh *MShellProc) SendInput(dataPk *packet.DataPacketType) error {
|
2022-07-08 07:46:28 +02:00
|
|
|
if !msh.IsConnected() {
|
|
|
|
return fmt.Errorf("remote is not connected, cannot send input")
|
|
|
|
}
|
2022-09-06 01:31:22 +02:00
|
|
|
if !msh.IsCmdRunning(dataPk.CK) {
|
2022-07-08 07:46:28 +02:00
|
|
|
return fmt.Errorf("cannot send input, cmd is not running")
|
|
|
|
}
|
|
|
|
return msh.ServerProc.Input.SendPacket(dataPk)
|
|
|
|
}
|
|
|
|
|
2022-09-06 05:08:59 +02:00
|
|
|
func (msh *MShellProc) SendSpecialInput(siPk *packet.SpecialInputPacketType) error {
|
|
|
|
if !msh.IsConnected() {
|
|
|
|
return fmt.Errorf("remote is not connected, cannot send input")
|
|
|
|
}
|
|
|
|
if !msh.IsCmdRunning(siPk.CK) {
|
|
|
|
return fmt.Errorf("cannot send input, cmd is not running")
|
|
|
|
}
|
|
|
|
return msh.ServerProc.Input.SendPacket(siPk)
|
|
|
|
}
|
|
|
|
|
2022-09-04 08:36:15 +02:00
|
|
|
func makeTermOpts(runPk *packet.RunPacketType) sstore.TermOpts {
|
|
|
|
return sstore.TermOpts{Rows: int64(runPk.TermOpts.Rows), Cols: int64(runPk.TermOpts.Cols), FlexRows: true, MaxPtySize: DefaultMaxPtySize}
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 07:00:10 +02:00
|
|
|
// returns (ok, currentPSC)
|
2022-10-28 07:22:17 +02:00
|
|
|
func (msh *MShellProc) testAndSetPendingStateCmd(riName string, newCK *base.CommandKey) (bool, *base.CommandKey) {
|
2022-10-28 07:00:10 +02:00
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-10-28 07:22:17 +02:00
|
|
|
ck, found := msh.PendingStateCmds[riName]
|
2022-10-28 07:00:10 +02:00
|
|
|
if found {
|
|
|
|
return false, &ck
|
|
|
|
}
|
|
|
|
if newCK != nil {
|
2022-10-28 07:22:17 +02:00
|
|
|
msh.PendingStateCmds[riName] = *newCK
|
2022-10-28 07:00:10 +02:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-10-28 07:22:17 +02:00
|
|
|
func (msh *MShellProc) removePendingStateCmd(riName string, ck base.CommandKey) {
|
2022-10-28 07:00:10 +02:00
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-10-28 07:22:17 +02:00
|
|
|
existingCK, found := msh.PendingStateCmds[riName]
|
2022-10-28 07:00:10 +02:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if existingCK == ck {
|
2022-10-28 07:22:17 +02:00
|
|
|
delete(msh.PendingStateCmds, riName)
|
2022-10-28 07:00:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:49:23 +02:00
|
|
|
// returns (cmdtype, allow-updates-callback, err)
|
2022-10-28 07:00:10 +02:00
|
|
|
func RunCommand(ctx context.Context, sessionId string, windowId string, remotePtr sstore.RemotePtrType, runPacket *packet.RunPacketType) (rtnCmd *sstore.CmdType, rtnCallback func(), rtnErr error) {
|
2022-10-28 07:22:17 +02:00
|
|
|
rct := RunCmdType{
|
|
|
|
SessionId: sessionId,
|
|
|
|
WindowId: windowId,
|
|
|
|
RemotePtr: remotePtr,
|
|
|
|
RunPacket: runPacket,
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
if remotePtr.OwnerId != "" {
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, fmt.Errorf("cannot run command against another user's remote '%s'", remotePtr.MakeFullRemoteRef())
|
2022-08-24 22:21:54 +02:00
|
|
|
}
|
2022-10-28 07:00:10 +02:00
|
|
|
if sessionId != runPacket.CK.GetSessionId() {
|
|
|
|
return nil, nil, fmt.Errorf("run commands sessionids do not match")
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
msh := GetRemoteById(remotePtr.RemoteId)
|
2022-07-06 01:54:49 +02:00
|
|
|
if msh == nil {
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, fmt.Errorf("no remote id=%s found", remotePtr.RemoteId)
|
2022-07-06 01:54:49 +02:00
|
|
|
}
|
|
|
|
if !msh.IsConnected() {
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, fmt.Errorf("remote '%s' is not connected", remotePtr.RemoteId)
|
2022-07-16 02:37:32 +02:00
|
|
|
}
|
2022-10-28 07:52:38 +02:00
|
|
|
if runPacket.State != nil {
|
|
|
|
return nil, nil, fmt.Errorf("runPacket.State should not be set, it is set in RunCommand")
|
2022-07-06 01:54:49 +02:00
|
|
|
}
|
2022-10-28 07:00:10 +02:00
|
|
|
var newPSC *base.CommandKey
|
|
|
|
if runPacket.ReturnState {
|
|
|
|
newPSC = &runPacket.CK
|
|
|
|
}
|
|
|
|
ok, existingPSC := msh.testAndSetPendingStateCmd(remotePtr.Name, newPSC)
|
|
|
|
if !ok {
|
|
|
|
line, _, err := sstore.GetLineCmdByCmdId(ctx, sessionId, windowId, existingPSC.GetCmdId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("cannot run command while a stateful command is still running: %v", err)
|
|
|
|
}
|
|
|
|
if line == nil {
|
2022-12-28 22:56:19 +01:00
|
|
|
return nil, nil, fmt.Errorf("cannot run command while a stateful command is still running %s", *existingPSC)
|
2022-10-28 07:00:10 +02:00
|
|
|
}
|
|
|
|
return nil, nil, fmt.Errorf("cannot run command while a stateful command (linenum=%d) is still running", line.LineNum)
|
|
|
|
}
|
2022-10-28 02:10:36 +02:00
|
|
|
startCmdWait(runPacket.CK)
|
|
|
|
defer func() {
|
|
|
|
if rtnErr != nil {
|
2022-10-28 07:22:17 +02:00
|
|
|
removeCmdWait(runPacket.CK)
|
2022-10-28 07:00:10 +02:00
|
|
|
if newPSC != nil {
|
|
|
|
msh.removePendingStateCmd(remotePtr.Name, *newPSC)
|
|
|
|
}
|
2022-10-28 02:10:36 +02:00
|
|
|
}
|
|
|
|
}()
|
2022-10-28 07:52:38 +02:00
|
|
|
// get current remote-instance state
|
2022-11-29 03:03:02 +01:00
|
|
|
statePtr, err := sstore.GetRemoteStatePtr(ctx, sessionId, windowId, remotePtr)
|
2022-10-28 07:52:38 +02:00
|
|
|
if err != nil {
|
2022-11-29 03:03:02 +01:00
|
|
|
return nil, nil, fmt.Errorf("cannot get current remote stateptr: %w", err)
|
2022-10-28 07:52:38 +02:00
|
|
|
}
|
2022-11-29 03:03:02 +01:00
|
|
|
if statePtr == nil {
|
|
|
|
statePtr = msh.GetDefaultStatePtr()
|
2022-10-28 07:52:38 +02:00
|
|
|
}
|
2022-11-29 03:03:02 +01:00
|
|
|
if statePtr == nil {
|
|
|
|
return nil, nil, fmt.Errorf("cannot run command, no valid remote stateptr")
|
|
|
|
}
|
|
|
|
currentState, err := sstore.GetFullState(ctx, *statePtr)
|
|
|
|
if err != nil || currentState == nil {
|
|
|
|
return nil, nil, fmt.Errorf("cannot get current remote state: %w", err)
|
2022-10-28 07:52:38 +02:00
|
|
|
}
|
2022-11-02 05:42:56 +01:00
|
|
|
runPacket.State = addScVarsToState(currentState)
|
2022-10-28 07:52:38 +02:00
|
|
|
runPacket.StateComplete = true
|
2022-07-07 04:01:00 +02:00
|
|
|
msh.ServerProc.Output.RegisterRpc(runPacket.ReqId)
|
2022-10-28 07:52:38 +02:00
|
|
|
err = shexec.SendRunPacketAndRunData(ctx, msh.ServerProc.Input, runPacket)
|
2022-07-07 04:01:00 +02:00
|
|
|
if err != nil {
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, fmt.Errorf("sending run packet to remote: %w", err)
|
2022-07-07 04:01:00 +02:00
|
|
|
}
|
|
|
|
rtnPk := msh.ServerProc.Output.WaitForResponse(ctx, runPacket.ReqId)
|
2022-08-31 21:01:16 +02:00
|
|
|
if rtnPk == nil {
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, ctx.Err()
|
2022-08-31 21:01:16 +02:00
|
|
|
}
|
2022-07-07 09:10:37 +02:00
|
|
|
startPk, ok := rtnPk.(*packet.CmdStartPacketType)
|
|
|
|
if !ok {
|
|
|
|
respPk, ok := rtnPk.(*packet.ResponsePacketType)
|
|
|
|
if !ok {
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, fmt.Errorf("invalid response received from server for run packet: %s", packet.AsString(rtnPk))
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
2022-07-07 04:01:00 +02:00
|
|
|
if respPk.Error != "" {
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, errors.New(respPk.Error)
|
2022-07-07 04:01:00 +02:00
|
|
|
}
|
2022-09-05 23:49:23 +02:00
|
|
|
return nil, nil, fmt.Errorf("invalid response received from server for run packet: %s", packet.AsString(rtnPk))
|
2022-07-07 09:10:37 +02:00
|
|
|
}
|
2022-07-08 01:29:14 +02:00
|
|
|
status := sstore.CmdStatusRunning
|
|
|
|
if runPacket.Detached {
|
|
|
|
status = sstore.CmdStatusDetached
|
|
|
|
}
|
2022-07-07 09:10:37 +02:00
|
|
|
cmd := &sstore.CmdType{
|
2022-11-29 03:03:02 +01:00
|
|
|
SessionId: runPacket.CK.GetSessionId(),
|
|
|
|
CmdId: runPacket.CK.GetCmdId(),
|
|
|
|
CmdStr: runPacket.Command,
|
|
|
|
Remote: remotePtr,
|
|
|
|
FeState: *sstore.FeStateFromShellState(currentState),
|
|
|
|
StatePtr: *statePtr,
|
|
|
|
TermOpts: makeTermOpts(runPacket),
|
|
|
|
Status: status,
|
|
|
|
StartPk: startPk,
|
|
|
|
DoneInfo: nil,
|
|
|
|
RunOut: nil,
|
|
|
|
RtnState: runPacket.ReturnState,
|
2022-07-07 04:01:00 +02:00
|
|
|
}
|
2022-08-19 22:23:00 +02:00
|
|
|
err = sstore.CreateCmdPtyFile(ctx, cmd.SessionId, cmd.CmdId, cmd.TermOpts.MaxPtySize)
|
2022-07-08 06:39:25 +02:00
|
|
|
if err != nil {
|
2022-09-05 23:49:23 +02:00
|
|
|
// TODO the cmd is running, so this is a tricky error to handle
|
|
|
|
return nil, nil, fmt.Errorf("cannot create local ptyout file for running command: %v", err)
|
2022-07-08 06:39:25 +02:00
|
|
|
}
|
2022-10-28 07:22:17 +02:00
|
|
|
msh.AddRunningCmd(rct)
|
|
|
|
return cmd, func() { removeCmdWait(runPacket.CK) }, nil
|
|
|
|
}
|
|
|
|
|
2022-10-28 07:52:38 +02:00
|
|
|
func (msh *MShellProc) AddWaitingCmd(rct RunCmdType) {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
msh.WaitingCmds = append(msh.WaitingCmds, rct)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) reExecSingle(rct RunCmdType) {
|
|
|
|
// TODO fixme
|
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
_, callback, _ := RunCommand(ctx, rct.SessionId, rct.WindowId, rct.RemotePtr, rct.RunPacket)
|
|
|
|
if callback != nil {
|
|
|
|
defer callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) ReExecWaitingCmds() {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
for len(msh.WaitingCmds) > 0 {
|
|
|
|
rct := msh.WaitingCmds[0]
|
|
|
|
go msh.reExecSingle(rct)
|
|
|
|
if rct.RunPacket.ReturnState {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(msh.WaitingCmds) == 0 {
|
|
|
|
msh.WaitingCmds = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 07:22:17 +02:00
|
|
|
func (msh *MShellProc) AddRunningCmd(rct RunCmdType) {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
msh.RunningCmds[rct.RunPacket.CK] = rct
|
2022-07-06 01:54:49 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 07:22:17 +02:00
|
|
|
func (msh *MShellProc) GetRunningCmd(ck base.CommandKey) *RunCmdType {
|
2022-07-08 07:13:45 +02:00
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-10-28 07:22:17 +02:00
|
|
|
rct, found := msh.RunningCmds[ck]
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &rct
|
2022-09-27 08:23:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) RemoveRunningCmd(ck base.CommandKey) {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
|
|
|
delete(msh.RunningCmds, ck)
|
2022-10-28 07:00:10 +02:00
|
|
|
for name, pendingCk := range msh.PendingStateCmds {
|
|
|
|
if pendingCk == ck {
|
|
|
|
delete(msh.PendingStateCmds, name)
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 02:10:36 +02:00
|
|
|
func (msh *MShellProc) PacketRpcRaw(ctx context.Context, pk packet.RpcPacketType) (packet.RpcResponsePacketType, error) {
|
2022-07-07 04:01:00 +02:00
|
|
|
if !msh.IsConnected() {
|
2022-07-02 02:38:36 +02:00
|
|
|
return nil, fmt.Errorf("runner is not connected")
|
|
|
|
}
|
2022-07-01 21:17:19 +02:00
|
|
|
if pk == nil {
|
|
|
|
return nil, fmt.Errorf("PacketRpc passed nil packet")
|
|
|
|
}
|
2022-07-07 04:01:00 +02:00
|
|
|
reqId := pk.GetReqId()
|
|
|
|
msh.ServerProc.Output.RegisterRpc(reqId)
|
|
|
|
defer msh.ServerProc.Output.UnRegisterRpc(reqId)
|
|
|
|
err := msh.ServerProc.Input.SendPacketCtx(ctx, pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rtnPk := msh.ServerProc.Output.WaitForResponse(ctx, reqId)
|
|
|
|
if rtnPk == nil {
|
|
|
|
return nil, ctx.Err()
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
2022-10-28 02:10:36 +02:00
|
|
|
return rtnPk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) PacketRpc(ctx context.Context, pk packet.RpcPacketType) (*packet.ResponsePacketType, error) {
|
|
|
|
rtnPk, err := msh.PacketRpcRaw(ctx, pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-07 04:01:00 +02:00
|
|
|
if respPk, ok := rtnPk.(*packet.ResponsePacketType); ok {
|
|
|
|
return respPk, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid response packet received: %s", packet.AsString(rtnPk))
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 22:51:49 +02:00
|
|
|
func (msh *MShellProc) WithLock(fn func()) {
|
|
|
|
msh.Lock.Lock()
|
|
|
|
defer msh.Lock.Unlock()
|
2022-07-02 02:38:36 +02:00
|
|
|
fn()
|
|
|
|
}
|
|
|
|
|
2022-07-07 07:46:22 +02:00
|
|
|
func makeDataAckPacket(ck base.CommandKey, fdNum int, ackLen int, err error) *packet.DataAckPacketType {
|
|
|
|
ack := packet.MakeDataAckPacket()
|
|
|
|
ack.CK = ck
|
|
|
|
ack.FdNum = fdNum
|
|
|
|
ack.AckLen = ackLen
|
|
|
|
if err != nil {
|
|
|
|
ack.Error = err.Error()
|
|
|
|
}
|
|
|
|
return ack
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:49:23 +02:00
|
|
|
func (msh *MShellProc) notifyHangups_nolock() {
|
2022-09-27 08:23:04 +02:00
|
|
|
for ck, _ := range msh.RunningCmds {
|
2022-09-05 23:49:23 +02:00
|
|
|
cmd, err := sstore.GetCmdById(context.Background(), ck.GetSessionId(), ck.GetCmdId())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
update := sstore.ModelUpdate{Cmd: cmd}
|
|
|
|
sstore.MainBus.SendUpdate(ck.GetSessionId(), update)
|
|
|
|
}
|
2022-10-28 07:22:17 +02:00
|
|
|
msh.RunningCmds = make(map[base.CommandKey]RunCmdType)
|
2022-09-05 23:49:23 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 01:29:14 +02:00
|
|
|
func (msh *MShellProc) handleCmdDonePacket(donePk *packet.CmdDonePacketType) {
|
2022-10-28 07:22:17 +02:00
|
|
|
// this will remove from RunningCmds and from PendingStateCmds
|
|
|
|
defer msh.RemoveRunningCmd(donePk.CK)
|
2022-11-02 05:42:56 +01:00
|
|
|
if donePk.FinalState != nil {
|
|
|
|
donePk.FinalState = stripScVarsFromState(donePk.FinalState)
|
|
|
|
}
|
2022-11-29 03:03:02 +01:00
|
|
|
if donePk.FinalStateDiff != nil {
|
|
|
|
donePk.FinalStateDiff = stripScVarsFromStateDiff(donePk.FinalStateDiff)
|
|
|
|
}
|
|
|
|
doneInfo := &sstore.CmdDoneInfo{
|
|
|
|
Ts: donePk.Ts,
|
|
|
|
ExitCode: int64(donePk.ExitCode),
|
|
|
|
DurationMs: donePk.DurationMs,
|
|
|
|
}
|
|
|
|
update, err := sstore.UpdateCmdDoneInfo(context.Background(), donePk.CK, doneInfo)
|
2022-07-08 01:29:14 +02:00
|
|
|
if err != nil {
|
2022-10-28 07:22:17 +02:00
|
|
|
msh.WriteToPtyBuffer("*error updating cmddone: %v\n", err)
|
2022-07-08 01:29:14 +02:00
|
|
|
return
|
|
|
|
}
|
2022-10-12 08:11:43 +02:00
|
|
|
sws, err := sstore.UpdateSWsWithCmdFg(context.Background(), donePk.CK.GetSessionId(), donePk.CK.GetCmdId())
|
|
|
|
if err != nil {
|
2022-10-28 07:22:17 +02:00
|
|
|
msh.WriteToPtyBuffer("*error trying to update cmd-fg screen windows: %v\n", err)
|
2022-10-12 08:11:43 +02:00
|
|
|
// fall-through (nothing to do)
|
|
|
|
}
|
|
|
|
update.ScreenWindows = sws
|
2022-11-28 09:13:00 +01:00
|
|
|
rct := msh.GetRunningCmd(donePk.CK)
|
2022-11-29 03:03:02 +01:00
|
|
|
var statePtr *sstore.ShellStatePtr
|
2022-11-28 09:13:00 +01:00
|
|
|
if donePk.FinalState != nil && rct != nil {
|
|
|
|
feState := sstore.FeStateFromShellState(donePk.FinalState)
|
|
|
|
remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.WindowId, rct.RemotePtr, *feState, donePk.FinalState, nil)
|
|
|
|
if err != nil {
|
|
|
|
msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err)
|
|
|
|
// fall-through (nothing to do)
|
|
|
|
}
|
|
|
|
if remoteInst != nil {
|
|
|
|
update.Sessions = sstore.MakeSessionsUpdateForRemote(rct.SessionId, remoteInst)
|
|
|
|
}
|
2022-11-29 03:03:02 +01:00
|
|
|
statePtr = &sstore.ShellStatePtr{BaseHash: donePk.FinalState.GetHashVal(false)}
|
2022-11-28 09:13:00 +01:00
|
|
|
} else if donePk.FinalStateDiff != nil && rct != nil {
|
|
|
|
feState, err := msh.getFeStateFromDiff(donePk.FinalStateDiff)
|
|
|
|
if err != nil {
|
|
|
|
msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err)
|
|
|
|
// fall-through (nothing to do)
|
|
|
|
} else {
|
|
|
|
remoteInst, err := sstore.UpdateRemoteState(context.Background(), rct.SessionId, rct.WindowId, rct.RemotePtr, *feState, nil, donePk.FinalStateDiff)
|
2022-10-28 07:22:17 +02:00
|
|
|
if err != nil {
|
|
|
|
msh.WriteToPtyBuffer("*error trying to update remotestate: %v\n", err)
|
|
|
|
// fall-through (nothing to do)
|
|
|
|
}
|
|
|
|
if remoteInst != nil {
|
|
|
|
update.Sessions = sstore.MakeSessionsUpdateForRemote(rct.SessionId, remoteInst)
|
|
|
|
}
|
2022-11-29 03:03:02 +01:00
|
|
|
diffHashArr := append(([]string)(nil), donePk.FinalStateDiff.DiffHashArr...)
|
|
|
|
diffHashArr = append(diffHashArr, donePk.FinalStateDiff.GetHashVal(false))
|
|
|
|
statePtr = &sstore.ShellStatePtr{BaseHash: donePk.FinalStateDiff.BaseHash, DiffHashArr: diffHashArr}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if statePtr != nil {
|
|
|
|
err = sstore.UpdateCmdRtnState(context.Background(), donePk.CK, *statePtr)
|
|
|
|
if err != nil {
|
|
|
|
msh.WriteToPtyBuffer("*error trying to update cmd rtnstate: %v\n", err)
|
|
|
|
// fall-through (nothing to do)
|
2022-10-28 07:22:17 +02:00
|
|
|
}
|
2022-07-08 07:13:45 +02:00
|
|
|
}
|
2022-10-28 07:22:17 +02:00
|
|
|
sstore.MainBus.SendUpdate(donePk.CK.GetSessionId(), update)
|
2022-07-08 01:29:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-27 23:12:15 +01:00
|
|
|
func (msh *MShellProc) handleCmdFinalPacket(finalPk *packet.CmdFinalPacketType) {
|
|
|
|
defer msh.RemoveRunningCmd(finalPk.CK)
|
|
|
|
rtnCmd, err := sstore.GetCmdById(context.Background(), finalPk.CK.GetSessionId(), finalPk.CK.GetCmdId())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error calling GetCmdById in handleCmdFinalPacket: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-29 03:03:02 +01:00
|
|
|
if rtnCmd == nil || rtnCmd.DoneInfo != nil {
|
2022-11-27 23:12:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("finalpk %s (hangup): %s\n", finalPk.CK, finalPk.Error)
|
|
|
|
sstore.HangupCmd(context.Background(), finalPk.CK)
|
|
|
|
rtnCmd, err = sstore.GetCmdById(context.Background(), finalPk.CK.GetSessionId(), finalPk.CK.GetCmdId())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting cmd(2) in handleCmdFinalPacket: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if rtnCmd == nil {
|
|
|
|
log.Printf("error getting cmd(2) in handleCmdFinalPacket (not found)\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
update := &sstore.ModelUpdate{Cmd: rtnCmd}
|
|
|
|
sstore.MainBus.SendUpdate(finalPk.CK.GetSessionId(), update)
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:49:23 +02:00
|
|
|
// TODO notify FE about cmd errors
|
2022-07-08 01:29:14 +02:00
|
|
|
func (msh *MShellProc) handleCmdErrorPacket(errPk *packet.CmdErrorPacketType) {
|
|
|
|
err := sstore.AppendCmdErrorPk(context.Background(), errPk)
|
|
|
|
if err != nil {
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.WriteToPtyBuffer("cmderr> [remote %s] [error] adding cmderr: %v\n", msh.GetRemoteName(), err)
|
2022-07-08 01:29:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:49:23 +02:00
|
|
|
func (msh *MShellProc) handleDataPacket(dataPk *packet.DataPacketType, dataPosMap map[base.CommandKey]int64) {
|
|
|
|
realData, err := base64.StdEncoding.DecodeString(dataPk.Data64)
|
|
|
|
if err != nil {
|
|
|
|
ack := makeDataAckPacket(dataPk.CK, dataPk.FdNum, 0, err)
|
|
|
|
msh.ServerProc.Input.SendPacket(ack)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var ack *packet.DataAckPacketType
|
|
|
|
if len(realData) > 0 {
|
|
|
|
dataPos := dataPosMap[dataPk.CK]
|
|
|
|
update, err := sstore.AppendToCmdPtyBlob(context.Background(), dataPk.CK.GetSessionId(), dataPk.CK.GetCmdId(), realData, dataPos)
|
2022-08-20 02:14:53 +02:00
|
|
|
if err != nil {
|
2022-09-05 23:49:23 +02:00
|
|
|
ack = makeDataAckPacket(dataPk.CK, dataPk.FdNum, 0, err)
|
|
|
|
} else {
|
|
|
|
ack = makeDataAckPacket(dataPk.CK, dataPk.FdNum, len(realData), nil)
|
|
|
|
}
|
|
|
|
dataPosMap[dataPk.CK] += int64(len(realData))
|
|
|
|
if update != nil {
|
2022-09-06 01:31:22 +02:00
|
|
|
sstore.MainBus.SendUpdate(dataPk.CK.GetSessionId(), update)
|
2022-07-08 07:13:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-05 23:49:23 +02:00
|
|
|
if ack != nil {
|
|
|
|
msh.ServerProc.Input.SendPacket(ack)
|
|
|
|
}
|
2022-10-31 20:40:45 +01:00
|
|
|
// log.Printf("data %s fd=%d len=%d eof=%v err=%v\n", dataPk.CK, dataPk.FdNum, len(realData), dataPk.Eof, dataPk.Error)
|
2022-07-08 07:13:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 01:31:22 +02:00
|
|
|
func (msh *MShellProc) makeHandleDataPacketClosure(dataPk *packet.DataPacketType, dataPosMap map[base.CommandKey]int64) func() {
|
|
|
|
return func() {
|
|
|
|
msh.handleDataPacket(dataPk, dataPosMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msh *MShellProc) makeHandleCmdDonePacketClosure(donePk *packet.CmdDonePacketType) func() {
|
|
|
|
return func() {
|
|
|
|
msh.handleCmdDonePacket(donePk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 23:12:15 +01:00
|
|
|
func (msh *MShellProc) makeHandleCmdFinalPacketClosure(finalPk *packet.CmdFinalPacketType) func() {
|
|
|
|
return func() {
|
|
|
|
msh.handleCmdFinalPacket(finalPk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 22:51:49 +02:00
|
|
|
func (msh *MShellProc) ProcessPackets() {
|
|
|
|
defer msh.WithLock(func() {
|
|
|
|
if msh.Status == StatusConnected {
|
|
|
|
msh.Status = StatusDisconnected
|
2022-07-02 02:38:36 +02:00
|
|
|
}
|
2022-09-04 22:51:49 +02:00
|
|
|
err := sstore.HangupRunningCmdsByRemoteId(context.Background(), msh.Remote.RemoteId)
|
2022-07-08 01:29:14 +02:00
|
|
|
if err != nil {
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.writeToPtyBuffer_nolock("error calling HUP on cmds %v\n", err)
|
2022-07-08 01:29:14 +02:00
|
|
|
}
|
2022-09-04 22:51:49 +02:00
|
|
|
msh.notifyHangups_nolock()
|
2022-09-05 23:49:23 +02:00
|
|
|
go msh.NotifyRemoteUpdate()
|
2022-07-02 02:38:36 +02:00
|
|
|
})
|
2022-08-13 19:27:22 +02:00
|
|
|
dataPosMap := make(map[base.CommandKey]int64)
|
2022-09-04 22:51:49 +02:00
|
|
|
for pk := range msh.ServerProc.Output.MainCh {
|
2022-07-07 04:01:00 +02:00
|
|
|
if pk.GetType() == packet.DataPacketStr {
|
2022-07-07 07:46:22 +02:00
|
|
|
dataPk := pk.(*packet.DataPacketType)
|
2022-09-06 01:31:22 +02:00
|
|
|
runCmdUpdateFn(dataPk.CK, msh.makeHandleDataPacketClosure(dataPk, dataPosMap))
|
2022-07-07 04:01:00 +02:00
|
|
|
continue
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
2022-09-04 22:51:49 +02:00
|
|
|
if pk.GetType() == packet.DataAckPacketStr {
|
|
|
|
// TODO process ack (need to keep track of buffer size for sending)
|
|
|
|
// this is low priority though since most input is coming from keyboard and won't overflow this buffer
|
|
|
|
continue
|
|
|
|
}
|
2022-07-01 21:17:19 +02:00
|
|
|
if pk.GetType() == packet.CmdDataPacketStr {
|
|
|
|
dataPacket := pk.(*packet.CmdDataPacketType)
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.WriteToPtyBuffer("cmd-data> [remote %s] [%s] pty=%d run=%d\n", msh.GetRemoteName(), dataPacket.CK, dataPacket.PtyDataLen, dataPacket.RunDataLen)
|
2022-07-01 21:17:19 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-07-07 04:01:00 +02:00
|
|
|
if pk.GetType() == packet.CmdDonePacketStr {
|
2022-09-06 01:31:22 +02:00
|
|
|
donePk := pk.(*packet.CmdDonePacketType)
|
|
|
|
runCmdUpdateFn(donePk.CK, msh.makeHandleCmdDonePacketClosure(donePk))
|
2022-07-08 01:29:14 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-11-27 23:12:15 +01:00
|
|
|
if pk.GetType() == packet.CmdFinalPacketStr {
|
|
|
|
finalPk := pk.(*packet.CmdFinalPacketType)
|
|
|
|
runCmdUpdateFn(finalPk.CK, msh.makeHandleCmdFinalPacketClosure(finalPk))
|
|
|
|
continue
|
|
|
|
}
|
2022-07-08 01:29:14 +02:00
|
|
|
if pk.GetType() == packet.CmdErrorPacketStr {
|
2022-09-04 22:51:49 +02:00
|
|
|
msh.handleCmdErrorPacket(pk.(*packet.CmdErrorPacketType))
|
2022-07-01 21:17:19 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pk.GetType() == packet.MessagePacketStr {
|
|
|
|
msgPacket := pk.(*packet.MessagePacketType)
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.WriteToPtyBuffer("msg> [remote %s] [%s] %s\n", msh.GetRemoteName(), msgPacket.CK, msgPacket.Message)
|
2022-07-01 21:17:19 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pk.GetType() == packet.RawPacketStr {
|
|
|
|
rawPacket := pk.(*packet.RawPacketType)
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.WriteToPtyBuffer("stderr> [remote %s] %s\n", msh.GetRemoteName(), rawPacket.Data)
|
2022-07-01 21:17:19 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-07-07 07:46:22 +02:00
|
|
|
if pk.GetType() == packet.CmdStartPacketStr {
|
|
|
|
startPk := pk.(*packet.CmdStartPacketType)
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.WriteToPtyBuffer("start> [remote %s] reqid=%s (%p)\n", msh.GetRemoteName(), startPk.RespId, msh.ServerProc.Output)
|
2022-07-07 07:46:22 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-09-15 08:10:35 +02:00
|
|
|
msh.WriteToPtyBuffer("MSH> [remote %s] unhandled packet %s\n", msh.GetRemoteName(), packet.AsString(pk))
|
2022-07-01 21:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-24 06:05:49 +02:00
|
|
|
|
2022-08-24 11:14:16 +02:00
|
|
|
// returns number of chars (including braces) for brace-expr
|
|
|
|
func getBracedStr(runeStr []rune) int {
|
|
|
|
if len(runeStr) < 3 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if runeStr[0] != '{' {
|
|
|
|
return 0
|
2022-08-24 06:05:49 +02:00
|
|
|
}
|
2022-08-24 11:14:16 +02:00
|
|
|
for i := 1; i < len(runeStr); i++ {
|
|
|
|
if runeStr[i] == '}' {
|
|
|
|
if i == 1 { // cannot have {}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDigit(r rune) bool {
|
|
|
|
return r >= '0' && r <= '9' // just check ascii digits (not unicode)
|
|
|
|
}
|
|
|
|
|
2022-10-17 08:51:04 +02:00
|
|
|
func EvalPrompt(promptFmt string, vars map[string]string, state *packet.ShellState) string {
|
2022-08-24 11:14:16 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
promptRunes := []rune(promptFmt)
|
|
|
|
for i := 0; i < len(promptRunes); i++ {
|
|
|
|
ch := promptRunes[i]
|
|
|
|
if ch == '\\' && i != len(promptRunes)-1 {
|
|
|
|
nextCh := promptRunes[i+1]
|
|
|
|
if nextCh == 'x' || nextCh == 'y' {
|
|
|
|
nr := getBracedStr(promptRunes[i+2:])
|
|
|
|
if nr > 0 {
|
|
|
|
escCode := string(promptRunes[i+1 : i+1+nr+1]) // start at "x" or "y", extend nr+1 runes
|
|
|
|
escStr := evalPromptEsc(escCode, vars, state)
|
|
|
|
buf.WriteString(escStr)
|
|
|
|
i += nr + 1
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
buf.WriteRune(ch) // invalid escape, so just write ch and move on
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if isDigit(nextCh) {
|
|
|
|
if len(promptRunes) >= i+4 && isDigit(promptRunes[i+2]) && isDigit(promptRunes[i+3]) {
|
|
|
|
i += 3
|
|
|
|
escStr := evalPromptEsc(string(promptRunes[i+1:i+4]), vars, state)
|
|
|
|
buf.WriteString(escStr)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
buf.WriteRune(ch) // invalid escape, so just write ch and move on
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i += 1
|
|
|
|
escStr := evalPromptEsc(string(nextCh), vars, state)
|
|
|
|
buf.WriteString(escStr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.WriteRune(ch)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2022-10-17 08:51:04 +02:00
|
|
|
func evalPromptEsc(escCode string, vars map[string]string, state *packet.ShellState) string {
|
2022-08-24 06:05:49 +02:00
|
|
|
if strings.HasPrefix(escCode, "x{") && strings.HasSuffix(escCode, "}") {
|
|
|
|
varName := escCode[2 : len(escCode)-1]
|
|
|
|
return vars[varName]
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(escCode, "y{") && strings.HasSuffix(escCode, "}") {
|
2022-08-24 11:14:16 +02:00
|
|
|
if state == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-08-24 06:05:49 +02:00
|
|
|
varName := escCode[2 : len(escCode)-1]
|
2022-10-25 21:31:29 +02:00
|
|
|
varMap := shexec.ShellVarMapFromState(state)
|
2022-08-24 06:05:49 +02:00
|
|
|
return varMap[varName]
|
|
|
|
}
|
|
|
|
if escCode == "h" {
|
|
|
|
return vars["remoteshorthost"]
|
|
|
|
}
|
|
|
|
if escCode == "H" {
|
|
|
|
return vars["remotehost"]
|
|
|
|
}
|
|
|
|
if escCode == "s" {
|
|
|
|
return "mshell"
|
|
|
|
}
|
|
|
|
if escCode == "u" {
|
|
|
|
return vars["remoteuser"]
|
|
|
|
}
|
|
|
|
if escCode == "w" {
|
2022-08-24 11:14:16 +02:00
|
|
|
if state == nil {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
return replaceHomePath(state.Cwd, vars["home"])
|
2022-08-24 06:05:49 +02:00
|
|
|
}
|
|
|
|
if escCode == "W" {
|
2022-08-24 11:14:16 +02:00
|
|
|
if state == nil {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
return path.Base(replaceHomePath(state.Cwd, vars["home"]))
|
2022-08-24 06:05:49 +02:00
|
|
|
}
|
|
|
|
if escCode == "$" {
|
2022-08-24 11:14:16 +02:00
|
|
|
if vars["remoteuser"] == "root" || vars["sudo"] == "1" {
|
2022-08-24 06:05:49 +02:00
|
|
|
return "#"
|
|
|
|
} else {
|
|
|
|
return "$"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(escCode) == 3 {
|
|
|
|
// \nnn escape
|
|
|
|
ival, err := strconv.ParseInt(escCode, 8, 32)
|
|
|
|
if err != nil {
|
|
|
|
return escCode
|
|
|
|
}
|
|
|
|
return string([]byte{byte(ival)})
|
|
|
|
}
|
|
|
|
if escCode == "e" {
|
|
|
|
return "\033"
|
|
|
|
}
|
|
|
|
if escCode == "n" {
|
|
|
|
return "\n"
|
|
|
|
}
|
|
|
|
if escCode == "r" {
|
|
|
|
return "\r"
|
|
|
|
}
|
|
|
|
if escCode == "a" {
|
|
|
|
return "\007"
|
|
|
|
}
|
|
|
|
if escCode == "\\" {
|
|
|
|
return "\\"
|
|
|
|
}
|
|
|
|
if escCode == "[" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if escCode == "]" {
|
|
|
|
return ""
|
|
|
|
}
|
2022-08-24 11:14:16 +02:00
|
|
|
|
|
|
|
// we don't support date/time escapes (d, t, T, @), version escapes (v, V), cmd number (#, !), terminal device (l), jobs (j)
|
2022-08-24 06:05:49 +02:00
|
|
|
return "(" + escCode + ")"
|
|
|
|
}
|
2022-11-28 09:13:00 +01:00
|
|
|
|
|
|
|
func (msh *MShellProc) getFullState(stateDiff *packet.ShellStateDiff) (*packet.ShellState, error) {
|
|
|
|
baseState := msh.GetStateByHash(stateDiff.BaseHash)
|
|
|
|
if baseState != nil && len(stateDiff.DiffHashArr) == 0 {
|
|
|
|
newState, err := shexec.ApplyShellStateDiff(*baseState, *stateDiff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &newState, nil
|
|
|
|
} else {
|
2022-11-29 03:03:02 +01:00
|
|
|
fullState, err := sstore.GetFullState(context.Background(), sstore.ShellStatePtr{stateDiff.BaseHash, stateDiff.DiffHashArr})
|
2022-11-28 09:13:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newState, err := shexec.ApplyShellStateDiff(*fullState, *stateDiff)
|
|
|
|
return &newState, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal func, first tries the StateMap, otherwise will fallback on sstore.GetFullState
|
|
|
|
func (msh *MShellProc) getFeStateFromDiff(stateDiff *packet.ShellStateDiff) (*sstore.FeStateType, error) {
|
|
|
|
baseState := msh.GetStateByHash(stateDiff.BaseHash)
|
|
|
|
if baseState != nil && len(stateDiff.DiffHashArr) == 0 {
|
|
|
|
newState, err := shexec.ApplyShellStateDiff(*baseState, *stateDiff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sstore.FeStateFromShellState(&newState), nil
|
|
|
|
} else {
|
2022-11-29 03:03:02 +01:00
|
|
|
fullState, err := sstore.GetFullState(context.Background(), sstore.ShellStatePtr{stateDiff.BaseHash, stateDiff.DiffHashArr})
|
2022-11-28 09:13:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newState, err := shexec.ApplyShellStateDiff(*fullState, *stateDiff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sstore.FeStateFromShellState(&newState), nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-28 22:56:19 +01:00
|
|
|
|
|
|
|
func (msh *MShellProc) TryAutoConnect() error {
|
|
|
|
if msh.IsConnected() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rcopy := msh.GetRemoteCopy()
|
2022-12-29 01:59:54 +01:00
|
|
|
if rcopy.ConnectMode == sstore.ConnectModeManual {
|
2022-12-28 22:56:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
msh.WithLock(func() {
|
|
|
|
if msh.NumTryConnect > 5 {
|
2022-12-29 01:59:54 +01:00
|
|
|
err = fmt.Errorf("too many unsuccessful tries")
|
2022-12-28 22:56:19 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
msh.NumTryConnect++
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msh.Launch()
|
2022-12-29 01:59:54 +01:00
|
|
|
if !msh.IsConnected() {
|
|
|
|
return fmt.Errorf("error connecting")
|
|
|
|
}
|
2022-12-28 22:56:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-31 02:01:17 +01:00
|
|
|
|
|
|
|
func (msh *MShellProc) GetDisplayName() string {
|
|
|
|
rcopy := msh.GetRemoteCopy()
|
|
|
|
return rcopy.GetName()
|
|
|
|
}
|