update where shell starts and add override -home setting

This commit is contained in:
2026-05-24 09:17:21 +00:00
parent ade413bac7
commit 6318643c25
7 changed files with 39 additions and 23 deletions
+1
View File
@@ -60,6 +60,7 @@ type Session struct {
var (
initialCwd string
shellHome string // starting directory for new shell sessions
nopwMode bool
appCreds storedCreds
authSecret []byte
+2 -2
View File
@@ -374,7 +374,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
destDir := strings.TrimSpace(r.FormValue("dest"))
if destDir == "" {
destDir = initialCwd
destDir = shellHome
}
destPath := filepath.Join(filepath.Clean(destDir), filepath.Base(header.Filename))
@@ -400,7 +400,7 @@ func handleDownload(w http.ResponseWriter, r *http.Request) {
}
full := filepath.Clean(path)
if !filepath.IsAbs(full) {
full = filepath.Join(initialCwd, full)
full = filepath.Join(shellHome, full)
}
if _, err := os.Stat(full); err != nil {
http.Error(w, "file not found", http.StatusNotFound)
+32
View File
@@ -15,6 +15,24 @@ import (
qrcode "github.com/skip2/go-qrcode"
)
// expandHome resolves a leading ~ to the current user's home directory.
func expandHome(p string) (string, error) {
if p == "" {
return os.UserHomeDir()
}
if p == "~" {
return os.UserHomeDir()
}
if strings.HasPrefix(p, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, p[2:]), nil
}
return p, nil
}
// Run is the application entry point, called from main().
func Run() {
addr := flag.String("addr", "127.0.0.1:5000", "listen address")
@@ -25,10 +43,24 @@ func Run() {
certreset := flag.Bool("certreset", false, "remove stored custom certificate, revert to self-signed")
logFlag := flag.String("log", "", "auth log file path; 'off' disables file logging (default: gotermix.log next to binary)")
mfaFlag := flag.String("mfa", "", "manage MFA for a user: -mfa <username> on|off")
homeFlag := flag.String("home", "", "starting directory for new shell sessions (default: user home ~)")
flag.Parse()
initialCwd, _ = os.Getwd()
// Resolve shell starting directory.
home, err := expandHome(*homeFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "error resolving home directory: %v\n", err)
os.Exit(1)
}
info, err := os.Stat(home)
if err != nil || !info.IsDir() {
fmt.Fprintf(os.Stderr, "error: -home %q is not a valid directory\n", home)
os.Exit(1)
}
shellHome = home
// Credentials file lives next to the executable.
exe, err := os.Executable()
if err != nil {
+1 -1
View File
@@ -67,7 +67,7 @@ func getOrCreate(id string) *Session {
} else {
cmd = exec.Command("/bin/bash", "-i")
}
cmd.Dir = initialCwd
cmd.Dir = shellHome
// Build environment: inherit parent env but force TERM so that bash readline
// correctly decodes modifier+cursor sequences (Shift+Arrow etc.).