diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2c2e70d..ddd3f00 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -46,8 +46,8 @@ jobs: | 平台 | 文件 | | --- | --- | - | Linux amd64 / arm64 | `agent-device-linux-*` | + | Linux amd64 / arm64 / mips | `agent-device-linux-*` | | macOS amd64 / arm64 | `agent-device-darwin-*` | | Windows amd64 | `agent-device-windows-amd64.exe` | - 不含 mips / mipsle(OpenWrt 等自行交叉编译:`GOARCH=mips GOMIPS=softfloat`)。 + `linux-mips` 是软浮点(`GOMIPS=softfloat`),给 AR9331 这类没 FPU 的 OpenWrt 设备用。 diff --git a/device/README.md b/device/README.md index 05f7c2b..229c4e5 100644 --- a/device/README.md +++ b/device/README.md @@ -23,7 +23,7 @@ git clone <本仓库> && cd device go build -trimpath -ldflags "-s -w" -o bin/agent-device ./cmd/agent ``` -交叉编译(一次出五个平台): +交叉编译(一次出六个平台): ```sh sh scripts/build.sh all @@ -137,4 +137,4 @@ core 通过同一条连接搬文件,**设备不需要任何额外端口**: | 起始 cwd 越界 | 拒绝 | | 断线重连 | 1s→2s→4s 退避,恢复后自动注册 | -在 Linux / macOS / Windows 三平台编译验证(amd64 + arm64)。 +在 Linux / macOS / Windows 三平台编译验证(amd64 + arm64);另出 `linux-mips` 软浮点版给 OpenWrt 设备。 diff --git a/device/cmd/agent/main.go b/device/cmd/agent/main.go index 765b6a5..03401a5 100644 --- a/device/cmd/agent/main.go +++ b/device/cmd/agent/main.go @@ -24,12 +24,14 @@ import ( "sync" "sync/atomic" "time" + "unicode/utf8" "agent-device/pkg/proto" "agent-device/pkg/wsnode" + "golang.org/x/text/encoding/simplifiedchinese" ) -const version = "0.1.0" +const version = "0.1.1" var ( maxOutBytes = int64(4 << 20) // 单条命令回传上限,超出截断并标记 @@ -464,6 +466,19 @@ func shellCmd(name string) (string, []string) { return "sh", []string{"-c"} } +// toUTF8 把 Windows 中文控制台吐出的 GBK 字节转成 UTF-8。 +// 先过 utf8.Valid:纯 ASCII / 真 UTF-8(英文版 Windows、开了 UTF-8 的系统)一律原样返回, +// 不会被误转;只有不是合法 UTF-8 时才按 GBK 解,解不出来就保留原样。 +func toUTF8(b []byte) []byte { + if utf8.Valid(b) { + return b + } + if d, err := simplifiedchinese.GBK.NewDecoder().Bytes(b); err == nil { + return d + } + return b +} + // shellInfo 探测实际解释器与版本。这是模型写命令的唯一入口,所以路径要解开符号链接 // (/bin/sh 可能是 dash / bash / busybox),版本拿不到就留空(dash、busybox 不支持 --version)。 func shellInfo(name string) (string, string) { @@ -490,6 +505,7 @@ func shellInfo(name string) (string, string) { c = newCmd(ctx, p, "--version") } out, _ := c.Output() + out = toUTF8(out) // 中文 Windows 的 ver 是 GBK;本来就是 UTF-8 的原样返回 ver := "" if s := strings.TrimSpace(string(out)); s != "" { diff --git a/device/go.mod b/device/go.mod index 10b17bb..d6ce5f0 100644 --- a/device/go.mod +++ b/device/go.mod @@ -3,3 +3,5 @@ module agent-device go 1.22 require github.com/gorilla/websocket v1.5.3 + +require golang.org/x/text v0.21.0 diff --git a/device/go.sum b/device/go.sum index 25a9fc4..13d1539 100644 --- a/device/go.sum +++ b/device/go.sum @@ -1,2 +1,4 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= diff --git a/device/scripts/build.sh b/device/scripts/build.sh index 227d568..b7b5ddb 100644 --- a/device/scripts/build.sh +++ b/device/scripts/build.sh @@ -2,22 +2,24 @@ # 交叉编译设备端二进制到 ./bin(纯本地动作:不联网、不推送、不依赖任何特定服务器)。 # # sh scripts/build.sh # 编当前平台 -# sh scripts/build.sh all # 编 linux-arm64 / linux-amd64 / windows-amd64 / darwin-amd64 / darwin-arm64 +# sh scripts/build.sh all # 编 linux-arm64 / linux-amd64 / linux-mips / windows-amd64 / darwin-amd64 / darwin-arm64 # # 有本机 Go 就用本机;没有但有 docker 就用 golang 镜像;都没有就报错。 set -e cd "$(dirname "$0")/.." TARGETS="$(uname -s | tr 'A-Z' 'a-z')/$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')" -[ "${1:-}" = "all" ] && TARGETS="linux/arm64 linux/amd64 windows/amd64 darwin/amd64 darwin/arm64" +[ "${1:-}" = "all" ] && TARGETS="linux/arm64 linux/amd64 linux/mips windows/amd64 darwin/amd64 darwin/arm64" build_with_go() { mkdir -p bin for t in $TARGETS; do - os=${t%/*}; arch=${t#*/}; ext="" + os=${t%/*}; arch=${t#*/}; ext=""; gomips="" [ "$os" = windows ] && ext=.exe + # mips 设备(OpenWrt / 老路由)基本都没有 FPU,硬浮点二进制一跑就是非法指令 + [ "$arch" = mips ] && gomips=softfloat echo "build $os/$arch" - GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" \ + GOOS=$os GOARCH=$arch GOMIPS=$gomips CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" \ -o "bin/agent-device-$os-$arch$ext" ./cmd/agent done } @@ -31,10 +33,11 @@ elif command -v docker >/dev/null 2>&1; then golang:1.23-alpine sh -c ' mkdir -p bin for t in $TARGETS; do - os=${t%/*}; arch=${t#*/}; ext="" + os=${t%/*}; arch=${t#*/}; ext=""; gomips="" [ "$os" = windows ] && ext=.exe + [ "$arch" = mips ] && gomips=softfloat echo "build $os/$arch" - GOOS=$os GOARCH=$arch go build -trimpath -ldflags "-s -w" \ + GOOS=$os GOARCH=$arch GOMIPS=$gomips go build -trimpath -ldflags "-s -w" \ -o "bin/agent-device-$os-$arch$ext" ./cmd/agent done' else