feat(update): detect installed impl; Go path downloads release asset and replaces binary, flush-usage via binary; Node path unchanged except unit ExecStart rewrite; default REPO now aaaamoooo/NexusGate

This commit is contained in:
aaaamoooo
2026-10-20 06:56:07 +00:00
parent 545c0668ba
commit d29b4f7ef1
+84 -21
View File
@@ -1,8 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -Eeuo pipefail set -Eeuo pipefail
REPO="${NG_REPO:-a2899882/NexusGate}" REPO="${NG_REPO:-aaaamoooo/NexusGate}"
BRANCH="${NG_BRANCH:-main}" BRANCH="${NG_BRANCH:-main}"
# Go agent 二进制所在的 GitHub Release tag(与 agent-install.sh 保持一致)
AGENT_RELEASE="${NG_AGENT_RELEASE:-agent-v0.6.8}"
die() { printf '错误:%s\n' "$*" >&2; exit 1; } die() { printf '错误:%s\n' "$*" >&2; exit 1; }
info() { printf '\033[1;36m[NexusGate Agent]\033[0m %s\n' "$*"; } info() { printf '\033[1;36m[NexusGate Agent]\033[0m %s\n' "$*"; }
[[ "${EUID}" -eq 0 ]] || die "请使用 root 运行" [[ "${EUID}" -eq 0 ]] || die "请使用 root 运行"
@@ -10,25 +12,75 @@ info() { printf '\033[1;36m[NexusGate Agent]\033[0m %s\n' "$*"; }
command -v curl >/dev/null || die "缺少 curl" command -v curl >/dev/null || die "缺少 curl"
tmp_dir="$(mktemp -d /tmp/nexusgate-agent-update.XXXXXX)" tmp_dir="$(mktemp -d /tmp/nexusgate-agent-update.XXXXXX)"
trap 'rm -rf -- "$tmp_dir"' EXIT trap 'rm -rf -- "$tmp_dir"' EXIT
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/agent/agent.js" -o "$tmp_dir/agent.js"
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/agent/run.sh" -o "$tmp_dir/run.sh" # 按已安装的程序判断实现:Go 二进制优先,其次是旧版 Node agent.js
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/scripts/agent-uninstall.sh" -o "$tmp_dir/uninstall.sh" if [[ -x /opt/nexusgate-agent/nexusgate-agent ]]; then
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/scripts/agent-doctor.sh" -o "$tmp_dir/doctor.sh" AGENT_IMPL="go"
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/scripts/agent-cert.sh" -o "$tmp_dir/cert.sh" elif [[ -f /opt/nexusgate-agent/agent.js ]]; then
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/scripts/agent-update.sh" -o "$tmp_dir/update.sh" AGENT_IMPL="node"
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/scripts/agent-singbox.sh" -o "$tmp_dir/singbox.sh" else
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/scripts/agent-logrotate.conf" -o "$tmp_dir/logrotate.conf" die "未找到 Agent 程序(/opt/nexusgate-agent/nexusgate-agent 或 agent.js)"
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/scripts/agent-logrotate-setup.sh" -o "$tmp_dir/logrotate-setup.sh" fi
info "当前 Agent 实现:${AGENT_IMPL}"
raw_fetch() {
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/$1" -o "$2"
}
agent_arch() {
case "$(uname -m)" in
x86_64|amd64) printf 'amd64' ;;
aarch64|arm64) printf 'arm64' ;;
*) return 1 ;;
esac
}
# 下载 Go agent 二进制到 $1(NG_LOCAL_SRC 时从 agent-go/ 取已构建产物)
fetch_agent_binary() {
local arch out="$1"
arch="$(agent_arch)" || die "不支持的架构:$(uname -m)"
if [[ -n "${NG_LOCAL_SRC:-}" && -f "${NG_LOCAL_SRC}/agent-go/nexusgate-agent-linux-${arch}" ]]; then
cp -a "${NG_LOCAL_SRC}/agent-go/nexusgate-agent-linux-${arch}" "$out"
else
curl -fL --retry 3 "https://github.com/${REPO}/releases/download/${AGENT_RELEASE}/nexusgate-agent-linux-${arch}" -o "$out"
fi
[[ -s "$out" ]] || die 'Agent 二进制下载不完整'
}
raw_fetch scripts/agent-uninstall.sh "$tmp_dir/uninstall.sh"
raw_fetch scripts/agent-doctor.sh "$tmp_dir/doctor.sh"
raw_fetch scripts/agent-cert.sh "$tmp_dir/cert.sh"
raw_fetch scripts/agent-update.sh "$tmp_dir/update.sh"
raw_fetch scripts/agent-singbox.sh "$tmp_dir/singbox.sh"
raw_fetch scripts/agent-logrotate.conf "$tmp_dir/logrotate.conf"
raw_fetch scripts/agent-logrotate-setup.sh "$tmp_dir/logrotate-setup.sh"
[[ -s "$tmp_dir/update.sh" ]] || die 'Agent 升级脚本下载不完整' [[ -s "$tmp_dir/update.sh" ]] || die 'Agent 升级脚本下载不完整'
node --check "$tmp_dir/agent.js"
if [[ "$AGENT_IMPL" == "go" ]]; then
fetch_agent_binary "$tmp_dir/nexusgate-agent"
else
raw_fetch agent/agent.js "$tmp_dir/agent.js"
raw_fetch agent/run.sh "$tmp_dir/run.sh"
node --check "$tmp_dir/agent.js"
fi
# Updating restarts sing-box. Persist its current bidirectional counters # Updating restarts sing-box. Persist its current bidirectional counters
# before the running process is replaced. # before the running process is replaced.
if [[ -f /opt/nexusgate-agent/agent.js ]]; then (
( set -a; source /etc/nexusgate/agent.env; set +a; node /opt/nexusgate-agent/agent.js flush-usage ) \ set -a; source /etc/nexusgate/agent.env; set +a
|| die '更新前流量统计上报失败,保留原 Agent;检查控制面连接和 ng-agent doctor 后重试' if [[ "$AGENT_IMPL" == "go" ]]; then
/opt/nexusgate-agent/nexusgate-agent flush-usage
else
node /opt/nexusgate-agent/agent.js flush-usage
fi
) || die '更新前流量统计上报失败,保留原 Agent;检查控制面连接和 ng-agent doctor 后重试'
if [[ "$AGENT_IMPL" == "go" ]]; then
install -m 0755 "$tmp_dir/nexusgate-agent" /opt/nexusgate-agent/nexusgate-agent
else
install -m 0644 "$tmp_dir/agent.js" /opt/nexusgate-agent/agent.js
install -m 0755 "$tmp_dir/run.sh" /opt/nexusgate-agent/run.sh
fi fi
install -m 0644 "$tmp_dir/agent.js" /opt/nexusgate-agent/agent.js
install -m 0755 "$tmp_dir/run.sh" /opt/nexusgate-agent/run.sh
install -m 0755 "$tmp_dir/uninstall.sh" /usr/local/sbin/ng-agent-uninstall install -m 0755 "$tmp_dir/uninstall.sh" /usr/local/sbin/ng-agent-uninstall
install -m 0755 "$tmp_dir/doctor.sh" /usr/local/sbin/ng-agent-doctor install -m 0755 "$tmp_dir/doctor.sh" /usr/local/sbin/ng-agent-doctor
install -m 0755 "$tmp_dir/cert.sh" /usr/local/sbin/ng-agent-cert install -m 0755 "$tmp_dir/cert.sh" /usr/local/sbin/ng-agent-cert
@@ -56,16 +108,27 @@ if [[ ! -x /usr/local/bin/nexusgate-sing-box || ! -x /usr/local/bin/nexusgate-si
|| die 'sing-box 引擎安装失败;修复后运行 ng-agent engine install 重试' || die 'sing-box 引擎安装失败;修复后运行 ng-agent engine install 重试'
fi fi
if command -v systemctl >/dev/null && [[ -d /run/systemd/system ]]; then if command -v systemctl >/dev/null && [[ -d /run/systemd/system ]]; then
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/systemd/nexusgate-agent.service" -o /etc/systemd/system/nexusgate-agent.service raw_fetch systemd/nexusgate-agent.service "$tmp_dir/nexusgate-agent.service"
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/systemd/nexusgate-sing-box.service" -o /etc/systemd/system/nexusgate-sing-box.service raw_fetch systemd/nexusgate-sing-box.service "$tmp_dir/nexusgate-sing-box.service"
install -m 0644 "$tmp_dir/nexusgate-agent.service" /etc/systemd/system/nexusgate-agent.service
install -m 0644 "$tmp_dir/nexusgate-sing-box.service" /etc/systemd/system/nexusgate-sing-box.service
if [[ "$AGENT_IMPL" == "node" ]]; then
# 仓库里的 unit 默认指向 Go 二进制;Node 实现改为经 run.sh 启动
sed -i 's|^ExecStart=.*|ExecStart=/opt/nexusgate-agent/run.sh|' /etc/systemd/system/nexusgate-agent.service
fi
systemctl daemon-reload systemctl daemon-reload
rm -f -- /etc/nexusgate/last-heartbeat.json rm -f -- /etc/nexusgate/last-heartbeat.json
systemctl restart nexusgate-agent.service systemctl restart nexusgate-agent.service
systemctl is-active --quiet nexusgate-agent.service || die "Agent 重启失败" systemctl is-active --quiet nexusgate-agent.service || die "Agent 重启失败"
elif command -v rc-service >/dev/null; then elif command -v rc-service >/dev/null; then
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/openrc/nexusgate-agent" -o /etc/init.d/nexusgate-agent raw_fetch openrc/nexusgate-agent "$tmp_dir/openrc-agent"
curl -fL --retry 3 "https://raw.githubusercontent.com/${REPO}/${BRANCH}/openrc/nexusgate-sing-box" -o /etc/init.d/nexusgate-sing-box raw_fetch openrc/nexusgate-sing-box "$tmp_dir/openrc-sing-box"
chmod 0755 /etc/init.d/nexusgate-agent /etc/init.d/nexusgate-sing-box install -m 0755 "$tmp_dir/openrc-agent" /etc/init.d/nexusgate-agent
install -m 0755 "$tmp_dir/openrc-sing-box" /etc/init.d/nexusgate-sing-box
if [[ "$AGENT_IMPL" == "node" ]]; then
# 仓库里的 OpenRC 服务默认指向 Go 二进制;Node 实现改为经 run.sh 启动
sed -i 's|^command=.*|command="/opt/nexusgate-agent/run.sh"|' /etc/init.d/nexusgate-agent
fi
if [[ -e /etc/init.d/crond ]]; then if [[ -e /etc/init.d/crond ]]; then
rc-update add crond default >/dev/null 2>&1 || true rc-update add crond default >/dev/null 2>&1 || true
rc-service crond start >/dev/null 2>&1 || true rc-service crond start >/dev/null 2>&1 || true