diff --git a/device/README.md b/device/README.md index b64e5e3..594d2cd 100644 --- a/device/README.md +++ b/device/README.md @@ -20,7 +20,7 @@ WebSocket 端点,接收 shell 命令、流式回传输出与退出码、上报 ```sh git clone <本仓库> && cd device -go build -trimpath -ldflags "-s -w" -o bin/agent ./cmd/agent +go build -trimpath -ldflags "-s -w" -o bin/agent-device ./cmd/agent ``` 交叉编译(一次出五个平台): @@ -34,20 +34,23 @@ sh scripts/build.sh all ### 下载预编译 -从本仓库的 Releases 页面取对应平台的文件(`agent-linux-arm64`、`agent-windows-amd64.exe` 等), +从本仓库的 Releases 页面取对应平台的文件(`agent-device-linux-arm64`、`agent-device-windows-amd64.exe` 等), `chmod +x` 即可,静态链接、无运行时依赖,一个文件约 5.5 MB。 ## 运行 ```sh -# 直接给参数 -./agent -server wss://your-host.example/dev/ws -token -root /var/agent-work -name box-01 +# 推荐:二进制旁边放一个 config.json,直接启动(token 不出现在 ps 里) +./agent-device -# 或走配置文件(token 不出现在 ps 里) -./agent -config agent.json +# 或者全走命令行 +./agent-device -server wss://your-host.example/dev/ws -token -root /var/agent-work -name box-01 + +# 配置文件不在默认位置时显式指定 +./agent-device -config /etc/agent-device.json ``` -`agent.json`: +**默认会自动读可执行文件同目录的 `config.json`**(存在才读,不存在就当没配),所以部署时把二进制和配置放一个目录、直接跑就行: ```json { @@ -67,6 +70,7 @@ sh scripts/build.sh all | `-root` | 工作根目录,命令的 `cwd` 必须落在它之内(默认 `~/agent-work`,会自动建) | | `-name` | 设备名,服务端用它区分设备(默认 hostname) | | `-shell` | `sh` / `cmd` / `powershell`,默认按平台 | +| `-config` | 配置文件路径,默认 `<二进制所在目录>/config.json` | | `-insecure` | 跳过证书校验(自签证书调试用) | | `-fix-clock` | 证书报错时用服务器 `Date` 头校准本机时间(小设备没有 RTC 时用,Linux 需 root) | diff --git a/device/cmd/agent/main.go b/device/cmd/agent/main.go index 8b43849..6441021 100644 --- a/device/cmd/agent/main.go +++ b/device/cmd/agent/main.go @@ -1,7 +1,7 @@ -// node-agent:跑在盒子 / Windows / 任意 Linux 上,主动出站连 core 的 wss,接收并执行命令。 +// agent-device:跑在盒子 / Windows / 任意 Linux 上,主动出站连 core 的 wss,接收并执行命令。 // -// node-agent -server wss://your-host.example/dev/ws -token -root /var/agent/work -// node-agent -config agent.json # 同上,但 token 不落在命令行(ps 看不见) +// agent-device -server wss://your-host.example/dev/ws -token -root /var/agent/work +// agent-device # 自动读同目录下的 config.json(token 不进 ps) // // 设计约束(对齐 files/print/agent): // - 只出站,服务器不需要任何入站端口 @@ -86,7 +86,7 @@ func main() { root := flag.String("root", "", "工作根目录(默认 ~/agent-work),命令的 cwd 必须落在它之内") shellName := flag.String("shell", "", "sh | cmd | powershell(默认按平台)") name := flag.String("name", "", "设备名(默认 hostname)") - configPath := flag.String("config", "", "JSON 配置文件:server/token/root/name/shell/insecure;命令行参数优先于它") + configPath := flag.String("config", "", "JSON 配置文件;不给时自动找可执行文件同目录的 config.json") insecure := flag.Bool("insecure", false, "跳过证书校验") fixClock := flag.Bool("fix-clock", false, "证书失败时用服务器时间校准本机(Linux 需 root)") flag.Parse() @@ -95,15 +95,30 @@ func main() { // 配置文件的意义是把 token 从命令行拿掉——ps aux 能看见所有参数。 set := map[string]bool{} flag.Visit(func(f *flag.Flag) { set[f.Name] = true }) - if *configPath != "" { + // 配置文件默认位置:可执行文件同目录的 config.json。放二进制旁边就不用带参数, + // token 也不会出现在 ps 里。命令行显式给的 -config 优先。 + effectiveCfg := *configPath + if effectiveCfg == "" { + if exe, err := os.Executable(); err == nil { + if real, err := filepath.EvalSymlinks(exe); err == nil { + exe = real + } + p := filepath.Join(filepath.Dir(exe), "config.json") + if _, err := os.Stat(p); err == nil { + effectiveCfg = p + } + } + } + if effectiveCfg != "" { var fc fileConfig - b, err := os.ReadFile(*configPath) + b, err := os.ReadFile(effectiveCfg) if err != nil { log.Fatalf("读配置失败:%v", err) } if err := json.Unmarshal(b, &fc); err != nil { - log.Fatalf("配置 %s 不是合法 JSON:%v", *configPath, err) + log.Fatalf("配置 %s 不是合法 JSON:%v", effectiveCfg, err) } + log.Printf("已加载配置 %s", effectiveCfg) if !set["server"] && fc.Server != "" { *server = fc.Server } @@ -156,7 +171,7 @@ func main() { host = *name } - log.Printf("node-agent %s 启动:root=%s shell=%s → %s", version, r.root, r.shell, *server) + log.Printf("agent-device %s 启动:root=%s shell=%s → %s", version, r.root, r.shell, *server) shPath, shVer := shellInfo(r.shell) log.Printf("shell:%s(%s %s)", r.shell, shPath, shVer) cfg := wsnode.Config{URL: *server, Token: *token, Insecure: *insecure, FixClock: *fixClock} diff --git a/device/scripts/build.sh b/device/scripts/build.sh index 96aabae..227d568 100644 --- a/device/scripts/build.sh +++ b/device/scripts/build.sh @@ -18,7 +18,7 @@ build_with_go() { [ "$os" = windows ] && ext=.exe echo "build $os/$arch" GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" \ - -o "bin/agent-$os-$arch$ext" ./cmd/agent + -o "bin/agent-device-$os-$arch$ext" ./cmd/agent done } @@ -35,7 +35,7 @@ elif command -v docker >/dev/null 2>&1; then [ "$os" = windows ] && ext=.exe echo "build $os/$arch" GOOS=$os GOARCH=$arch go build -trimpath -ldflags "-s -w" \ - -o "bin/agent-$os-$arch$ext" ./cmd/agent + -o "bin/agent-device-$os-$arch$ext" ./cmd/agent done' else echo "需要 Go 或 docker 才能编译" >&2