refactor(device): 二进制改名 agent-device,默认读同目录 config.json

- 输出名 agent-device-<os>-<arch>,避免和仓库名/agent-core 在 ps 里混淆
- 不带 -config 时自动加载可执行文件同目录的 config.json(存在才读)
- 启动日志打印实际加载的配置文件路径
This commit is contained in:
2026-11-19 17:31:21 +08:00
parent 4333a53dd3
commit af2e4a1c20
3 changed files with 36 additions and 17 deletions
+11 -7
View File
@@ -20,7 +20,7 @@ WebSocket 端点,接收 shell 命令、流式回传输出与退出码、上报
```sh ```sh
git clone <本仓库> && cd device 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。 `chmod +x` 即可,静态链接、无运行时依赖,一个文件约 5.5 MB。
## 运行 ## 运行
```sh ```sh
# 直接给参数 # 推荐:二进制旁边放一个 config.json,直接启动(token 不出现在 ps 里)
./agent -server wss://your-host.example/dev/ws -token <TOKEN> -root /var/agent-work -name box-01 ./agent-device
# 或走配置文件(token 不出现在 ps 里) # 或者全走命令行
./agent -config agent.json ./agent-device -server wss://your-host.example/dev/ws -token <TOKEN> -root /var/agent-work -name box-01
# 配置文件不在默认位置时显式指定
./agent-device -config /etc/agent-device.json
``` ```
`agent.json`: **默认会自动读可执行文件同目录的 `config.json`**(存在才读,不存在就当没配),所以部署时把二进制和配置放一个目录、直接跑就行:
```json ```json
{ {
@@ -67,6 +70,7 @@ sh scripts/build.sh all
| `-root` | 工作根目录,命令的 `cwd` 必须落在它之内(默认 `~/agent-work`,会自动建) | | `-root` | 工作根目录,命令的 `cwd` 必须落在它之内(默认 `~/agent-work`,会自动建) |
| `-name` | 设备名,服务端用它区分设备(默认 hostname) | | `-name` | 设备名,服务端用它区分设备(默认 hostname) |
| `-shell` | `sh` / `cmd` / `powershell`,默认按平台 | | `-shell` | `sh` / `cmd` / `powershell`,默认按平台 |
| `-config` | 配置文件路径,默认 `<二进制所在目录>/config.json` |
| `-insecure` | 跳过证书校验(自签证书调试用) | | `-insecure` | 跳过证书校验(自签证书调试用) |
| `-fix-clock` | 证书报错时用服务器 `Date` 头校准本机时间(小设备没有 RTC 时用,Linux 需 root) | | `-fix-clock` | 证书报错时用服务器 `Date` 头校准本机时间(小设备没有 RTC 时用,Linux 需 root) |
+23 -8
View File
@@ -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 <TOKEN> -root /var/agent/work // agent-device -server wss://your-host.example/dev/ws -token <TOKEN> -root /var/agent/work
// node-agent -config agent.json # 同上,但 token 不落在命令行(ps 看不见) // agent-device # 自动读同目录下的 config.json(token 不进 ps)
// //
// 设计约束(对齐 files/print/agent): // 设计约束(对齐 files/print/agent):
// - 只出站,服务器不需要任何入站端口 // - 只出站,服务器不需要任何入站端口
@@ -86,7 +86,7 @@ func main() {
root := flag.String("root", "", "工作根目录(默认 ~/agent-work),命令的 cwd 必须落在它之内") root := flag.String("root", "", "工作根目录(默认 ~/agent-work),命令的 cwd 必须落在它之内")
shellName := flag.String("shell", "", "sh | cmd | powershell(默认按平台)") shellName := flag.String("shell", "", "sh | cmd | powershell(默认按平台)")
name := flag.String("name", "", "设备名(默认 hostname)") 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, "跳过证书校验") insecure := flag.Bool("insecure", false, "跳过证书校验")
fixClock := flag.Bool("fix-clock", false, "证书失败时用服务器时间校准本机(Linux 需 root)") fixClock := flag.Bool("fix-clock", false, "证书失败时用服务器时间校准本机(Linux 需 root)")
flag.Parse() flag.Parse()
@@ -95,15 +95,30 @@ func main() {
// 配置文件的意义是把 token 从命令行拿掉——ps aux 能看见所有参数。 // 配置文件的意义是把 token 从命令行拿掉——ps aux 能看见所有参数。
set := map[string]bool{} set := map[string]bool{}
flag.Visit(func(f *flag.Flag) { set[f.Name] = true }) 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 var fc fileConfig
b, err := os.ReadFile(*configPath) b, err := os.ReadFile(effectiveCfg)
if err != nil { if err != nil {
log.Fatalf("读配置失败:%v", err) log.Fatalf("读配置失败:%v", err)
} }
if err := json.Unmarshal(b, &fc); err != nil { 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 != "" { if !set["server"] && fc.Server != "" {
*server = fc.Server *server = fc.Server
} }
@@ -156,7 +171,7 @@ func main() {
host = *name 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) shPath, shVer := shellInfo(r.shell)
log.Printf("shell:%s(%s %s)", r.shell, shPath, shVer) log.Printf("shell:%s(%s %s)", r.shell, shPath, shVer)
cfg := wsnode.Config{URL: *server, Token: *token, Insecure: *insecure, FixClock: *fixClock} cfg := wsnode.Config{URL: *server, Token: *token, Insecure: *insecure, FixClock: *fixClock}
+2 -2
View File
@@ -18,7 +18,7 @@ build_with_go() {
[ "$os" = windows ] && ext=.exe [ "$os" = windows ] && ext=.exe
echo "build $os/$arch" echo "build $os/$arch"
GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" \ 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 done
} }
@@ -35,7 +35,7 @@ elif command -v docker >/dev/null 2>&1; then
[ "$os" = windows ] && ext=.exe [ "$os" = windows ] && ext=.exe
echo "build $os/$arch" echo "build $os/$arch"
GOOS=$os GOARCH=$arch go build -trimpath -ldflags "-s -w" \ 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' done'
else else
echo "需要 Go 或 docker 才能编译" >&2 echo "需要 Go 或 docker 才能编译" >&2