2022-06-10 09:35:24 +02:00
|
|
|
// Copyright 2022 Dashborg Inc
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-07-02 02:37:37 +02:00
|
|
|
"io"
|
2022-06-10 09:35:24 +02:00
|
|
|
"io/fs"
|
2022-11-29 03:05:54 +01:00
|
|
|
"log"
|
2022-06-10 09:35:24 +02:00
|
|
|
"os"
|
2022-06-23 19:16:54 +02:00
|
|
|
"os/exec"
|
2022-06-10 09:35:24 +02:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2022-06-25 08:42:00 +02:00
|
|
|
"strings"
|
2022-08-08 18:52:50 +02:00
|
|
|
"sync"
|
2022-06-27 21:03:47 +02:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-09-26 22:02:34 +02:00
|
|
|
"golang.org/x/mod/semver"
|
2022-06-10 09:35:24 +02:00
|
|
|
)
|
|
|
|
|
2022-07-02 02:37:37 +02:00
|
|
|
const HomeVarName = "HOME"
|
|
|
|
const DefaultMShellHome = "~/.mshell"
|
|
|
|
const DefaultMShellName = "mshell"
|
2022-06-23 19:16:54 +02:00
|
|
|
const MShellPathVarName = "MSHELL_PATH"
|
2022-07-02 02:37:37 +02:00
|
|
|
const MShellHomeVarName = "MSHELL_HOME"
|
2022-09-26 22:02:34 +02:00
|
|
|
const MShellInstallBinVarName = "MSHELL_INSTALLBIN_PATH"
|
2022-06-23 19:16:54 +02:00
|
|
|
const SSHCommandVarName = "SSH_COMMAND"
|
2022-07-02 02:37:37 +02:00
|
|
|
const SessionsDirBaseName = "sessions"
|
2022-10-17 08:46:59 +02:00
|
|
|
const MShellVersion = "v0.2.0"
|
2022-07-02 02:37:37 +02:00
|
|
|
const RemoteIdFile = "remoteid"
|
2022-09-26 22:02:34 +02:00
|
|
|
const DefaultMShellInstallBinDir = "/opt/mshell/bin"
|
2022-11-29 03:05:54 +01:00
|
|
|
const LogFileName = "mshell.log"
|
|
|
|
const ForceDebugLog = false
|
2022-06-10 09:35:24 +02:00
|
|
|
|
2022-08-08 18:52:50 +02:00
|
|
|
var sessionDirCache = make(map[string]string)
|
|
|
|
var baseLock = &sync.Mutex{}
|
2022-11-29 03:05:54 +01:00
|
|
|
var DebugLogEnabled = false
|
|
|
|
var DebugLogger *log.Logger
|
2023-04-13 06:45:45 +02:00
|
|
|
var BuildTime string = "0"
|
2022-08-08 18:52:50 +02:00
|
|
|
|
2022-06-10 09:35:24 +02:00
|
|
|
type CommandFileNames struct {
|
2022-06-11 06:37:21 +02:00
|
|
|
PtyOutFile string
|
|
|
|
StdinFifo string
|
|
|
|
RunnerOutFile string
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:03:47 +02:00
|
|
|
type CommandKey string
|
|
|
|
|
2023-04-13 06:45:45 +02:00
|
|
|
func SetBuildTime(build string) {
|
|
|
|
BuildTime = build
|
|
|
|
}
|
|
|
|
|
2022-06-27 21:03:47 +02:00
|
|
|
func MakeCommandKey(sessionId string, cmdId string) CommandKey {
|
|
|
|
if sessionId == "" && cmdId == "" {
|
|
|
|
return CommandKey("")
|
|
|
|
}
|
|
|
|
return CommandKey(fmt.Sprintf("%s/%s", sessionId, cmdId))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ckey CommandKey) IsEmpty() bool {
|
|
|
|
return string(ckey) == ""
|
|
|
|
}
|
|
|
|
|
2022-11-29 03:05:54 +01:00
|
|
|
func Logf(fmtStr string, args ...interface{}) {
|
|
|
|
if (!DebugLogEnabled && !ForceDebugLog) || DebugLogger == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DebugLogger.Printf(fmtStr, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitDebugLog(prefix string) {
|
|
|
|
homeDir := GetMShellHomeDir()
|
|
|
|
err := os.MkdirAll(homeDir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logFile := path.Join(homeDir, LogFileName)
|
|
|
|
fd, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DebugLogger = log.New(fd, prefix+" ", log.LstdFlags)
|
2022-12-06 07:26:13 +01:00
|
|
|
Logf("logger initialized\n")
|
2022-11-29 03:05:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetEnableDebugLog(enable bool) {
|
|
|
|
DebugLogEnabled = enable
|
|
|
|
}
|
|
|
|
|
2023-03-21 03:21:23 +01:00
|
|
|
// deprecated (use GetGroupId instead)
|
2022-06-27 21:03:47 +02:00
|
|
|
func (ckey CommandKey) GetSessionId() string {
|
2023-03-21 03:21:23 +01:00
|
|
|
return ckey.GetGroupId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ckey CommandKey) GetGroupId() string {
|
2022-06-27 21:03:47 +02:00
|
|
|
slashIdx := strings.Index(string(ckey), "/")
|
|
|
|
if slashIdx == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(ckey[0:slashIdx])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ckey CommandKey) GetCmdId() string {
|
|
|
|
slashIdx := strings.Index(string(ckey), "/")
|
|
|
|
if slashIdx == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(ckey[slashIdx+1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ckey CommandKey) Split() (string, string) {
|
|
|
|
fields := strings.SplitN(string(ckey), "/", 2)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
return fields[0], fields[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ckey CommandKey) Validate(typeStr string) error {
|
|
|
|
if typeStr == "" {
|
|
|
|
typeStr = "ck"
|
|
|
|
}
|
|
|
|
if ckey == "" {
|
|
|
|
return fmt.Errorf("%s has empty commandkey", typeStr)
|
|
|
|
}
|
|
|
|
sessionId, cmdId := ckey.Split()
|
|
|
|
if sessionId == "" {
|
|
|
|
return fmt.Errorf("%s does not have sessionid", typeStr)
|
|
|
|
}
|
|
|
|
_, err := uuid.Parse(sessionId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s has invalid sessionid '%s'", typeStr, sessionId)
|
|
|
|
}
|
|
|
|
if cmdId == "" {
|
|
|
|
return fmt.Errorf("%s does not have cmdid", typeStr)
|
|
|
|
}
|
|
|
|
_, err = uuid.Parse(cmdId)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s has invalid cmdid '%s'", typeStr, cmdId)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:16:58 +02:00
|
|
|
func GetHomeDir() string {
|
|
|
|
homeVar := os.Getenv(HomeVarName)
|
|
|
|
if homeVar == "" {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
return homeVar
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:37:37 +02:00
|
|
|
func GetMShellHomeDir() string {
|
|
|
|
homeVar := os.Getenv(MShellHomeVarName)
|
|
|
|
if homeVar != "" {
|
|
|
|
return homeVar
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
2022-07-02 02:37:37 +02:00
|
|
|
return ExpandHomeDir(DefaultMShellHome)
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:03:47 +02:00
|
|
|
func GetCommandFileNames(ck CommandKey) (*CommandFileNames, error) {
|
|
|
|
if err := ck.Validate("ck"); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get command files: %w", err)
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
2022-06-27 21:03:47 +02:00
|
|
|
sessionId, cmdId := ck.Split()
|
2022-06-10 09:35:24 +02:00
|
|
|
sdir, err := EnsureSessionDir(sessionId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
base := path.Join(sdir, cmdId)
|
|
|
|
return &CommandFileNames{
|
2022-06-11 06:37:21 +02:00
|
|
|
PtyOutFile: base + ".ptyout",
|
|
|
|
StdinFifo: base + ".stdin",
|
|
|
|
RunnerOutFile: base + ".runout",
|
2022-06-10 09:35:24 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CleanUpCmdFiles(sessionId string, cmdId string) error {
|
|
|
|
if cmdId == "" {
|
|
|
|
return fmt.Errorf("bad cmdid, cannot clean up")
|
|
|
|
}
|
|
|
|
sdir, err := EnsureSessionDir(sessionId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmdFileGlob := path.Join(sdir, cmdId+".*")
|
|
|
|
matches, err := filepath.Glob(cmdFileGlob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, file := range matches {
|
|
|
|
rmErr := os.Remove(file)
|
|
|
|
if err == nil && rmErr != nil {
|
|
|
|
err = rmErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-20 23:15:39 +02:00
|
|
|
func GetSessionsDir() string {
|
|
|
|
mhome := GetMShellHomeDir()
|
|
|
|
sdir := path.Join(mhome, SessionsDirBaseName)
|
|
|
|
return sdir
|
|
|
|
}
|
|
|
|
|
2022-06-10 09:35:24 +02:00
|
|
|
func EnsureSessionDir(sessionId string) (string, error) {
|
|
|
|
if sessionId == "" {
|
|
|
|
return "", fmt.Errorf("Bad sessionid, cannot be empty")
|
|
|
|
}
|
2022-08-08 18:52:50 +02:00
|
|
|
baseLock.Lock()
|
|
|
|
sdir, ok := sessionDirCache[sessionId]
|
|
|
|
baseLock.Unlock()
|
|
|
|
if ok {
|
|
|
|
return sdir, nil
|
|
|
|
}
|
2022-07-02 02:37:37 +02:00
|
|
|
mhome := GetMShellHomeDir()
|
2022-08-08 18:52:50 +02:00
|
|
|
sdir = path.Join(mhome, SessionsDirBaseName, sessionId)
|
2022-06-10 09:35:24 +02:00
|
|
|
info, err := os.Stat(sdir)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
err = os.MkdirAll(sdir, 0777)
|
|
|
|
if err != nil {
|
2022-08-17 01:26:06 +02:00
|
|
|
return "", fmt.Errorf("cannot make mshell session directory[%s]: %w", sdir, err)
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
|
|
|
info, err = os.Stat(sdir)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return "", fmt.Errorf("session dir '%s' must be a directory", sdir)
|
|
|
|
}
|
2022-08-08 18:52:50 +02:00
|
|
|
baseLock.Lock()
|
|
|
|
sessionDirCache[sessionId] = sdir
|
|
|
|
baseLock.Unlock()
|
2022-06-10 09:35:24 +02:00
|
|
|
return sdir, nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 21:48:45 +02:00
|
|
|
func GetMShellPath() (string, error) {
|
2022-07-02 02:37:37 +02:00
|
|
|
msPath := os.Getenv(MShellPathVarName) // use MSHELL_PATH
|
2022-06-23 19:16:54 +02:00
|
|
|
if msPath != "" {
|
2022-06-23 21:48:45 +02:00
|
|
|
return exec.LookPath(msPath)
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
2022-07-02 02:37:37 +02:00
|
|
|
mhome := GetMShellHomeDir()
|
|
|
|
userMShellPath := path.Join(mhome, DefaultMShellName) // look in ~/.mshell
|
2022-06-23 21:48:45 +02:00
|
|
|
msPath, err := exec.LookPath(userMShellPath)
|
2022-07-02 02:37:37 +02:00
|
|
|
if err == nil {
|
2022-06-23 21:48:45 +02:00
|
|
|
return msPath, nil
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
2022-07-02 02:37:37 +02:00
|
|
|
return exec.LookPath(DefaultMShellName) // standard path lookup for 'mshell'
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 02:37:37 +02:00
|
|
|
func GetMShellSessionsDir() (string, error) {
|
|
|
|
mhome := GetMShellHomeDir()
|
|
|
|
return path.Join(mhome, SessionsDirBaseName), nil
|
2022-06-10 09:35:24 +02:00
|
|
|
}
|
2022-06-25 08:42:00 +02:00
|
|
|
|
|
|
|
func ExpandHomeDir(pathStr string) string {
|
|
|
|
if pathStr != "~" && !strings.HasPrefix(pathStr, "~/") {
|
|
|
|
return pathStr
|
|
|
|
}
|
|
|
|
homeDir := GetHomeDir()
|
|
|
|
if pathStr == "~" {
|
|
|
|
return homeDir
|
|
|
|
}
|
|
|
|
return path.Join(homeDir, pathStr[2:])
|
|
|
|
}
|
2022-06-28 07:39:16 +02:00
|
|
|
|
|
|
|
func ValidGoArch(goos string, goarch string) bool {
|
|
|
|
return (goos == "darwin" || goos == "linux") && (goarch == "amd64" || goarch == "arm64")
|
|
|
|
}
|
|
|
|
|
2022-09-26 22:02:34 +02:00
|
|
|
func GoArchOptFile(version string, goos string, goarch string) string {
|
|
|
|
installBinDir := os.Getenv(MShellInstallBinVarName)
|
|
|
|
if installBinDir == "" {
|
|
|
|
installBinDir = DefaultMShellInstallBinDir
|
|
|
|
}
|
|
|
|
versionStr := semver.MajorMinor(version)
|
|
|
|
if versionStr == "" {
|
|
|
|
versionStr = "unknown"
|
|
|
|
}
|
|
|
|
binBaseName := fmt.Sprintf("mshell-%s-%s.%s", versionStr, goos, goarch)
|
|
|
|
return fmt.Sprintf(path.Join(installBinDir, binBaseName))
|
2022-06-28 07:39:16 +02:00
|
|
|
}
|
2022-07-02 02:37:37 +02:00
|
|
|
|
2022-11-02 05:19:42 +01:00
|
|
|
func MShellBinaryFromOptDir(version string, goos string, goarch string) (io.ReadCloser, error) {
|
|
|
|
if !ValidGoArch(goos, goarch) {
|
|
|
|
return nil, fmt.Errorf("invalid goos/goarch combination: %s/%s", goos, goarch)
|
|
|
|
}
|
|
|
|
versionStr := semver.MajorMinor(version)
|
|
|
|
if versionStr == "" {
|
|
|
|
return nil, fmt.Errorf("invalid mshell version: %q", version)
|
|
|
|
}
|
|
|
|
fileName := GoArchOptFile(version, goos, goarch)
|
|
|
|
fd, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot open mshell binary %q: %v", fileName, err)
|
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:37:37 +02:00
|
|
|
func GetRemoteId() (string, error) {
|
|
|
|
mhome := GetMShellHomeDir()
|
2022-08-17 01:26:06 +02:00
|
|
|
homeInfo, err := os.Stat(mhome)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
err = os.MkdirAll(mhome, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot make mshell home directory[%s]: %w", mhome, err)
|
|
|
|
}
|
|
|
|
homeInfo, err = os.Stat(mhome)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot stat mshell home directory[%s]: %w", mhome, err)
|
|
|
|
}
|
|
|
|
if !homeInfo.IsDir() {
|
|
|
|
return "", fmt.Errorf("mshell home directory[%s] is not a directory", mhome)
|
|
|
|
}
|
2022-07-02 02:37:37 +02:00
|
|
|
remoteIdFile := path.Join(mhome, RemoteIdFile)
|
|
|
|
fd, err := os.Open(remoteIdFile)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
// write the file
|
|
|
|
remoteId := uuid.New().String()
|
|
|
|
err = os.WriteFile(remoteIdFile, []byte(remoteId), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot write remoteid to '%s': %w", remoteIdFile, err)
|
|
|
|
}
|
|
|
|
return remoteId, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot read remoteid file '%s': %w", remoteIdFile, err)
|
|
|
|
} else {
|
|
|
|
defer fd.Close()
|
|
|
|
contents, err := io.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot read remoteid file '%s': %w", remoteIdFile, err)
|
|
|
|
}
|
|
|
|
uuidStr := string(contents)
|
|
|
|
_, err = uuid.Parse(uuidStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("invalid uuid read from '%s': %w", remoteIdFile, err)
|
|
|
|
}
|
|
|
|
return uuidStr, nil
|
|
|
|
}
|
|
|
|
}
|
2022-09-04 08:26:57 +02:00
|
|
|
|
|
|
|
func BoundInt(ival int, minVal int, maxVal int) int {
|
|
|
|
if ival < minVal {
|
|
|
|
return minVal
|
|
|
|
}
|
|
|
|
if ival > maxVal {
|
|
|
|
return maxVal
|
|
|
|
}
|
|
|
|
return ival
|
|
|
|
}
|
2022-09-04 08:38:35 +02:00
|
|
|
|
|
|
|
func BoundInt64(ival int64, minVal int64, maxVal int64) int64 {
|
|
|
|
if ival < minVal {
|
|
|
|
return minVal
|
|
|
|
}
|
|
|
|
if ival > maxVal {
|
|
|
|
return maxVal
|
|
|
|
}
|
|
|
|
return ival
|
|
|
|
}
|