2022-07-01 02:02:19 +02:00
|
|
|
package scbase
|
|
|
|
|
|
|
|
import (
|
2022-07-07 04:01:00 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-11-02 05:42:56 +01:00
|
|
|
"io"
|
2022-07-07 04:01:00 +02:00
|
|
|
"io/fs"
|
2022-10-31 20:40:45 +01:00
|
|
|
"log"
|
2022-07-01 02:02:19 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-12-29 08:09:37 +01:00
|
|
|
"runtime"
|
2022-09-21 02:37:49 +02:00
|
|
|
"strconv"
|
2022-07-07 04:01:00 +02:00
|
|
|
"sync"
|
2022-07-08 01:29:14 +02:00
|
|
|
|
2022-09-21 02:37:49 +02:00
|
|
|
"github.com/google/uuid"
|
2022-07-08 06:39:25 +02:00
|
|
|
"github.com/scripthaus-dev/mshell/pkg/base"
|
2022-11-02 05:42:56 +01:00
|
|
|
"golang.org/x/mod/semver"
|
2022-07-08 01:29:14 +02:00
|
|
|
"golang.org/x/sys/unix"
|
2022-07-01 02:02:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const HomeVarName = "HOME"
|
2022-12-20 02:36:19 +01:00
|
|
|
const PromptHomeVarName = "PROMPT_HOME"
|
2022-12-28 22:56:19 +01:00
|
|
|
const PromptDevVarName = "PROMPT_DEV"
|
2022-07-07 04:01:00 +02:00
|
|
|
const SessionsDirBaseName = "sessions"
|
2022-12-20 03:52:08 +01:00
|
|
|
const PromptLockFile = "prompt.lock"
|
2022-12-20 02:36:19 +01:00
|
|
|
const PromptDirName = "prompt"
|
2022-12-28 22:56:19 +01:00
|
|
|
const PromptDevDirName = "prompt-dev"
|
2022-12-20 02:36:19 +01:00
|
|
|
const PromptAppPathVarName = "PROMPT_APP_PATH"
|
2023-01-26 07:30:25 +01:00
|
|
|
const PromptVersion = "v0.1.2"
|
2022-12-20 03:52:08 +01:00
|
|
|
const PromptAuthKeyFileName = "prompt.authkey"
|
2022-12-29 08:09:37 +01:00
|
|
|
const MShellVersion = "v0.2.0"
|
2022-07-07 04:01:00 +02:00
|
|
|
|
|
|
|
var SessionDirCache = make(map[string]string)
|
|
|
|
var BaseLock = &sync.Mutex{}
|
2022-07-01 02:02:19 +02:00
|
|
|
|
2022-12-29 02:47:12 +01:00
|
|
|
func IsDevMode() bool {
|
|
|
|
pdev := os.Getenv(PromptDevVarName)
|
|
|
|
return pdev != ""
|
|
|
|
}
|
|
|
|
|
2022-11-02 05:42:56 +01:00
|
|
|
// must match js
|
2022-12-20 02:36:19 +01:00
|
|
|
func GetPromptHomeDir() string {
|
|
|
|
scHome := os.Getenv(PromptHomeVarName)
|
2022-07-01 02:02:19 +02:00
|
|
|
if scHome == "" {
|
|
|
|
homeVar := os.Getenv(HomeVarName)
|
|
|
|
if homeVar == "" {
|
|
|
|
homeVar = "/"
|
|
|
|
}
|
2022-12-28 22:56:19 +01:00
|
|
|
pdev := os.Getenv(PromptDevVarName)
|
|
|
|
if pdev != "" {
|
|
|
|
scHome = path.Join(homeVar, PromptDevDirName)
|
|
|
|
} else {
|
|
|
|
scHome = path.Join(homeVar, PromptDirName)
|
|
|
|
}
|
|
|
|
|
2022-07-01 02:02:19 +02:00
|
|
|
}
|
|
|
|
return scHome
|
|
|
|
}
|
2022-07-07 04:01:00 +02:00
|
|
|
|
2022-12-29 08:09:37 +01:00
|
|
|
func MShellBinaryDir() string {
|
2022-12-20 02:36:19 +01:00
|
|
|
appPath := os.Getenv(PromptAppPathVarName)
|
2022-11-02 05:42:56 +01:00
|
|
|
if appPath == "" {
|
2022-12-29 08:09:37 +01:00
|
|
|
appPath = "."
|
2022-11-02 05:42:56 +01:00
|
|
|
}
|
2022-12-29 08:09:37 +01:00
|
|
|
if IsDevMode() {
|
|
|
|
return path.Join(appPath, "dev-bin")
|
|
|
|
}
|
|
|
|
return path.Join(appPath, "bin", "mshell")
|
|
|
|
}
|
|
|
|
|
|
|
|
func MShellBinaryPath(version string, goos string, goarch string) (string, error) {
|
2022-11-02 05:42:56 +01:00
|
|
|
if !base.ValidGoArch(goos, goarch) {
|
2022-12-29 08:09:37 +01:00
|
|
|
return "", fmt.Errorf("invalid goos/goarch combination: %s/%s", goos, goarch)
|
2022-11-02 05:42:56 +01:00
|
|
|
}
|
2022-12-29 08:09:37 +01:00
|
|
|
binaryDir := MShellBinaryDir()
|
2022-11-02 05:42:56 +01:00
|
|
|
versionStr := semver.MajorMinor(version)
|
|
|
|
if versionStr == "" {
|
2022-12-29 08:09:37 +01:00
|
|
|
return "", fmt.Errorf("invalid mshell version: %q", version)
|
2022-11-02 05:42:56 +01:00
|
|
|
}
|
|
|
|
fileName := fmt.Sprintf("mshell-%s-%s.%s", versionStr, goos, goarch)
|
2022-12-29 08:09:37 +01:00
|
|
|
fullFileName := path.Join(binaryDir, fileName)
|
|
|
|
return fullFileName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LocalMShellBinaryPath() (string, error) {
|
|
|
|
return MShellBinaryPath(MShellVersion, runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MShellBinaryReader(version string, goos string, goarch string) (io.ReadCloser, error) {
|
|
|
|
mshellPath, err := MShellBinaryPath(version, goos, goarch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fd, err := os.Open(mshellPath)
|
2022-11-02 05:42:56 +01:00
|
|
|
if err != nil {
|
2022-12-29 08:09:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot open mshell binary %q: %v", mshellPath, err)
|
2022-11-02 05:42:56 +01:00
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 03:52:08 +01:00
|
|
|
func createPromptAuthKeyFile(fileName string) (string, error) {
|
|
|
|
fd, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
keyStr := GenPromptUUID()
|
|
|
|
_, err = fd.Write([]byte(keyStr))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return keyStr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadPromptAuthKey() (string, error) {
|
|
|
|
homeDir := GetPromptHomeDir()
|
|
|
|
err := ensureDir(homeDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot find/create PROMPT_HOME directory %q", homeDir)
|
|
|
|
}
|
|
|
|
fileName := path.Join(homeDir, PromptAuthKeyFileName)
|
|
|
|
fd, err := os.Open(fileName)
|
|
|
|
if err != nil && errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return createPromptAuthKeyFile(fileName)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error opening prompt authkey:%s: %v", fileName, err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
buf, err := io.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error reading prompt authkey:%s: %v", fileName, err)
|
|
|
|
}
|
|
|
|
keyStr := string(buf)
|
|
|
|
_, err = uuid.Parse(keyStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("invalid authkey:%s format: %v", fileName, err)
|
|
|
|
}
|
|
|
|
return keyStr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func AcquirePromptLock() (*os.File, error) {
|
2022-12-20 02:36:19 +01:00
|
|
|
homeDir := GetPromptHomeDir()
|
2022-10-31 20:40:45 +01:00
|
|
|
err := ensureDir(homeDir)
|
|
|
|
if err != nil {
|
2022-12-20 02:36:19 +01:00
|
|
|
return nil, fmt.Errorf("cannot find/create PROMPT_HOME directory %q", homeDir)
|
2022-10-31 20:40:45 +01:00
|
|
|
}
|
2022-12-20 03:52:08 +01:00
|
|
|
lockFileName := path.Join(homeDir, PromptLockFile)
|
2022-07-08 01:29:14 +02:00
|
|
|
fd, err := os.Create(lockFileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = unix.Flock(int(fd.Fd()), unix.LOCK_EX|unix.LOCK_NB)
|
|
|
|
if err != nil {
|
|
|
|
fd.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
2022-07-07 04:01:00 +02:00
|
|
|
func EnsureSessionDir(sessionId string) (string, error) {
|
2022-08-24 22:21:54 +02:00
|
|
|
if sessionId == "" {
|
|
|
|
return "", fmt.Errorf("cannot get session dir for blank sessionid")
|
|
|
|
}
|
2022-07-07 04:01:00 +02:00
|
|
|
BaseLock.Lock()
|
|
|
|
sdir, ok := SessionDirCache[sessionId]
|
|
|
|
BaseLock.Unlock()
|
|
|
|
if ok {
|
|
|
|
return sdir, nil
|
|
|
|
}
|
2022-12-20 02:36:19 +01:00
|
|
|
scHome := GetPromptHomeDir()
|
2022-07-07 04:01:00 +02:00
|
|
|
sdir = path.Join(scHome, SessionsDirBaseName, sessionId)
|
|
|
|
err := ensureDir(sdir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
BaseLock.Lock()
|
|
|
|
SessionDirCache[sessionId] = sdir
|
|
|
|
BaseLock.Unlock()
|
|
|
|
return sdir, nil
|
|
|
|
}
|
|
|
|
|
2022-12-27 03:49:45 +01:00
|
|
|
func GetSessionsDir() string {
|
|
|
|
promptHome := GetPromptHomeDir()
|
|
|
|
sdir := path.Join(promptHome, SessionsDirBaseName)
|
|
|
|
return sdir
|
|
|
|
}
|
|
|
|
|
2022-07-07 04:01:00 +02:00
|
|
|
func ensureDir(dirName string) error {
|
|
|
|
info, err := os.Stat(dirName)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
err = os.MkdirAll(dirName, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-20 02:36:19 +01:00
|
|
|
log.Printf("[prompt] created directory %q\n", dirName)
|
2022-07-07 04:01:00 +02:00
|
|
|
info, err = os.Stat(dirName)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return fmt.Errorf("'%s' must be a directory", dirName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PtyOutFile(sessionId string, cmdId string) (string, error) {
|
|
|
|
sdir, err := EnsureSessionDir(sessionId)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
if sessionId == "" {
|
|
|
|
return "", fmt.Errorf("cannot get ptyout file for blank sessionid")
|
|
|
|
}
|
|
|
|
if cmdId == "" {
|
|
|
|
return "", fmt.Errorf("cannot get ptyout file for blank cmdid")
|
|
|
|
}
|
2022-08-19 22:23:00 +02:00
|
|
|
return fmt.Sprintf("%s/%s.ptyout.cf", sdir, cmdId), nil
|
2022-07-07 04:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RunOutFile(sessionId string, cmdId string) (string, error) {
|
|
|
|
sdir, err := EnsureSessionDir(sessionId)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-08-24 22:21:54 +02:00
|
|
|
if sessionId == "" {
|
|
|
|
return "", fmt.Errorf("cannot get runout file for blank sessionid")
|
|
|
|
}
|
|
|
|
if cmdId == "" {
|
|
|
|
return "", fmt.Errorf("cannot get runout file for blank cmdid")
|
|
|
|
}
|
2022-07-07 04:01:00 +02:00
|
|
|
return fmt.Sprintf("%s/%s.runout", sdir, cmdId), nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 03:52:08 +01:00
|
|
|
type PromptFileNameGenerator struct {
|
|
|
|
PromptHome string
|
2022-07-08 06:39:25 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 03:52:08 +01:00
|
|
|
func (g PromptFileNameGenerator) PtyOutFile(ck base.CommandKey) string {
|
|
|
|
return path.Join(g.PromptHome, SessionsDirBaseName, ck.GetSessionId(), ck.GetCmdId()+".ptyout")
|
2022-07-08 06:39:25 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 03:52:08 +01:00
|
|
|
func (g PromptFileNameGenerator) RunOutFile(ck base.CommandKey) string {
|
|
|
|
return path.Join(g.PromptHome, SessionsDirBaseName, ck.GetSessionId(), ck.GetCmdId()+".runout")
|
2022-07-08 06:39:25 +02:00
|
|
|
}
|
2022-09-21 02:37:49 +02:00
|
|
|
|
2022-12-20 03:52:08 +01:00
|
|
|
func GenPromptUUID() string {
|
2022-09-21 02:37:49 +02:00
|
|
|
for {
|
|
|
|
rtn := uuid.New().String()
|
|
|
|
_, err := strconv.Atoi(rtn[0:8])
|
|
|
|
if err == nil { // do not allow UUIDs where the initial 8 bytes parse to an integer
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 21:39:55 +02:00
|
|
|
|
|
|
|
func NumFormatDec(num int64) string {
|
|
|
|
var signStr string
|
|
|
|
absNum := num
|
|
|
|
if absNum < 0 {
|
|
|
|
absNum = -absNum
|
|
|
|
signStr = "-"
|
|
|
|
}
|
|
|
|
if absNum < 1000 {
|
|
|
|
// raw num
|
|
|
|
return signStr + strconv.FormatInt(absNum, 10)
|
|
|
|
}
|
|
|
|
if absNum < 1000000 {
|
|
|
|
// k num
|
|
|
|
kVal := float64(absNum) / 1000
|
|
|
|
return signStr + strconv.FormatFloat(kVal, 'f', 2, 64) + "k"
|
|
|
|
}
|
|
|
|
if absNum < 1000000000 {
|
|
|
|
// M num
|
|
|
|
mVal := float64(absNum) / 1000000
|
|
|
|
return signStr + strconv.FormatFloat(mVal, 'f', 2, 64) + "m"
|
|
|
|
} else {
|
|
|
|
// G num
|
|
|
|
gVal := float64(absNum) / 1000000000
|
|
|
|
return signStr + strconv.FormatFloat(gVal, 'f', 2, 64) + "g"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NumFormatB2(num int64) string {
|
|
|
|
var signStr string
|
|
|
|
absNum := num
|
|
|
|
if absNum < 0 {
|
|
|
|
absNum = -absNum
|
|
|
|
signStr = "-"
|
|
|
|
}
|
|
|
|
if absNum < 1024 {
|
|
|
|
// raw num
|
|
|
|
return signStr + strconv.FormatInt(absNum, 10)
|
|
|
|
}
|
|
|
|
if absNum < 1000000 {
|
|
|
|
// k num
|
|
|
|
if absNum%1024 == 0 {
|
|
|
|
return signStr + strconv.FormatInt(absNum/1024, 10) + "K"
|
|
|
|
}
|
|
|
|
kVal := float64(absNum) / 1024
|
|
|
|
return signStr + strconv.FormatFloat(kVal, 'f', 2, 64) + "K"
|
|
|
|
}
|
|
|
|
if absNum < 1000000000 {
|
|
|
|
// M num
|
|
|
|
if absNum%(1024*1024) == 0 {
|
|
|
|
return signStr + strconv.FormatInt(absNum/(1024*1024), 10) + "M"
|
|
|
|
}
|
|
|
|
mVal := float64(absNum) / (1024 * 1024)
|
|
|
|
return signStr + strconv.FormatFloat(mVal, 'f', 2, 64) + "M"
|
|
|
|
} else {
|
|
|
|
// G num
|
|
|
|
if absNum%(1024*1024*1024) == 0 {
|
|
|
|
return signStr + strconv.FormatInt(absNum/(1024*1024*1024), 10) + "G"
|
|
|
|
}
|
|
|
|
gVal := float64(absNum) / (1024 * 1024 * 1024)
|
|
|
|
return signStr + strconv.FormatFloat(gVal, 'f', 2, 64) + "G"
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 08:36:52 +01:00
|
|
|
|
|
|
|
func ClientArch() string {
|
|
|
|
return fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|