diff --git a/pkg/base/base.go b/pkg/base/base.go index bff94cf2..91b22c95 100644 --- a/pkg/base/base.go +++ b/pkg/base/base.go @@ -30,6 +30,7 @@ const MShellPathVarName = "MSHELL_PATH" const MShellHomeVarName = "MSHELL_HOME" const MShellInstallBinVarName = "MSHELL_INSTALLBIN_PATH" const SSHCommandVarName = "SSH_COMMAND" +const MShellDebugVarName = "MSHELL_DEBUG" const SessionsDirBaseName = "sessions" const MShellVersion = "v0.2.0" const RemoteIdFile = "remoteid" @@ -37,6 +38,9 @@ const DefaultMShellInstallBinDir = "/opt/mshell/bin" const LogFileName = "mshell.log" const ForceDebugLog = false +const DebugFlag_LogRcFile = "logrc" +const LogRcFileName = "debug.rcfile" + var sessionDirCache = make(map[string]string) var baseLock = &sync.Mutex{} var DebugLogEnabled = false @@ -146,6 +150,23 @@ func (ckey CommandKey) Validate(typeStr string) error { return nil } +func HasDebugFlag(envMap map[string]string, flagName string) bool { + msDebug := envMap[MShellDebugVarName] + flags := strings.Split(msDebug, ",") + Logf("hasdebugflag[%s]: %s [%#v]\n", flagName, msDebug, flags) + for _, flag := range flags { + if strings.TrimSpace(flag) == flagName { + return true + } + } + return false +} + +func GetDebugRcFileName() string { + msHome := GetMShellHomeDir() + return path.Join(msHome, LogRcFileName) +} + func GetHomeDir() string { homeVar := os.Getenv(HomeVarName) if homeVar == "" { diff --git a/pkg/shexec/shexec.go b/pkg/shexec/shexec.go index 520c333b..7b38e0a7 100644 --- a/pkg/shexec/shexec.go +++ b/pkg/shexec/shexec.go @@ -24,11 +24,11 @@ import ( "time" "github.com/alessio/shellescape" - "github.com/creack/pty" "github.com/commandlinedev/apishell/pkg/base" "github.com/commandlinedev/apishell/pkg/cirfile" "github.com/commandlinedev/apishell/pkg/mpio" "github.com/commandlinedev/apishell/pkg/packet" + "github.com/creack/pty" "golang.org/x/mod/semver" "golang.org/x/sys/unix" ) @@ -1081,6 +1081,14 @@ func RunCommandSimple(pk *packet.RunPacketType, sender *packet.PacketSender, fro trapCmdStr := makeExitTrap(cmd.ReturnState.FdNum) rcFileStr += trapCmdStr } + shellVarMap := ShellVarMapFromState(state) + if base.HasDebugFlag(shellVarMap, base.DebugFlag_LogRcFile) { + debugRcFileName := base.GetDebugRcFileName() + err := os.WriteFile(debugRcFileName, []byte(rcFileStr), 0600) + if err != nil { + base.Logf("error writing %s: %v\n", debugRcFileName, err) + } + } rcFileFdNum, err := AddRunData(pk, rcFileStr, "rcfile") if err != nil { return nil, err