feat(device): 工作根支持相对路径,按可执行文件目录解析(不越出该目录)

This commit is contained in:
2026-11-20 09:18:00 +08:00
parent 7212e0dfee
commit 2f0b056a63
2 changed files with 29 additions and 6 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ sh scripts/build.sh all
|---|---| |---|---|
| `-server` | core 的接入地址,`wss://…/dev/ws` | | `-server` | core 的接入地址,`wss://…/dev/ws` |
| `-token` | Bearer token,须与服务端 `DEVICE_TOKEN` 一致 | | `-token` | Bearer token,须与服务端 `DEVICE_TOKEN` 一致 |
| `-root` | 工作根目录,命令的 `cwd` 必须落在它之内(默认 `~/agent-work`,会自动建) | | `-root` | 工作根目录,命令的 `cwd` 必须落在它之内(默认 `~/agent-work`,会自动建)。写相对路径时按**二进制所在目录**解析,如 `workspace` / `.`,不能越出该目录 |
| `-name` | 设备名,服务端用它区分设备(默认 hostname) | | `-name` | 设备名,服务端用它区分设备(默认 hostname) |
| `-shell` | `sh` / `cmd` / `powershell`,默认按平台 | | `-shell` | `sh` / `cmd` / `powershell`,默认按平台 |
| `-config` | 配置文件路径,默认 `<二进制所在目录>/config.json` | | `-config` | 配置文件路径,默认 `<二进制所在目录>/config.json` |
+28 -5
View File
@@ -101,11 +101,8 @@ func main() {
// token 也不会出现在 ps 里。命令行显式给的 -config 优先。 // token 也不会出现在 ps 里。命令行显式给的 -config 优先。
effectiveCfg := *configPath effectiveCfg := *configPath
if effectiveCfg == "" { if effectiveCfg == "" {
if exe, err := os.Executable(); err == nil { if dir := exeDir(); dir != "" {
if real, err := filepath.EvalSymlinks(exe); err == nil { p := filepath.Join(dir, "config.json")
exe = real
}
p := filepath.Join(filepath.Dir(exe), "config.json")
if _, err := os.Stat(p); err == nil { if _, err := os.Stat(p); err == nil {
effectiveCfg = p effectiveCfg = p
} }
@@ -163,6 +160,20 @@ func main() {
r.root = filepath.Join(home, "agent-work") r.root = filepath.Join(home, "agent-work")
} }
r.root = filepath.Clean(r.root) r.root = filepath.Clean(r.root)
// 相对路径按可执行文件所在目录解析:双击、服务、命令行启动都得到同一个工作区,
// 不受进程当前目录影响;同时不允许用 ".." 跑到 exe 目录之外。
if !filepath.IsAbs(r.root) {
dir := exeDir()
if dir == "" {
log.Fatalf("工作根 %q 是相对路径,但取不到可执行文件位置", r.root)
}
abs := filepath.Join(dir, r.root)
rel, err := filepath.Rel(dir, abs)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
log.Fatalf("工作根 %q 超出可执行文件目录:%s", r.root, abs)
}
r.root = abs
}
if err := os.MkdirAll(r.root, 0o755); err != nil { if err := os.MkdirAll(r.root, 0o755); err != nil {
log.Fatalf("工作根不可用:%v", err) log.Fatalf("工作根不可用:%v", err)
} }
@@ -371,6 +382,18 @@ func flatten(env map[string]string) []string {
return out return out
} }
// exeDir 返回可执行文件所在目录(解开软链接)。相对的工作根、默认配置都基于它。
func exeDir() string {
exe, err := os.Executable()
if err != nil {
return ""
}
if real, err := filepath.EvalSymlinks(exe); err == nil {
exe = real
}
return filepath.Dir(exe)
}
// defaultShell 选默认解释器。POSIX 上固定 /bin/sh(不读 $SHELL:用户把 shell 设成 fish // defaultShell 选默认解释器。POSIX 上固定 /bin/sh(不读 $SHELL:用户把 shell 设成 fish
// 之后模型写的 POSIX 语法全废);Windows 默认 cmd,想用 git-bash / busybox 就在 // 之后模型写的 POSIX 语法全废);Windows 默认 cmd,想用 git-bash / busybox 就在
// config.json 里写 "shell": "C:\\Program Files\\Git\\bin\\bash.exe"(或 PATH 里的名字)。 // config.json 里写 "shell": "C:\\Program Files\\Git\\bin\\bash.exe"(或 PATH 里的名字)。