Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

feat(agent): GITHUB_TOKEN for GitHub API calls, download timeouts and retries (#77)

luolei6c4fac700157
diff --git a/apps/agent/install.sh b/apps/agent/install.sh
index 2cef064..076d7f2 100755
--- a/apps/agent/install.sh
+++ b/apps/agent/install.sh
@@ -55,6 +55,24 @@ detect_existing_install() {
return 1
}
+wget_with_retries() (
+ # BusyBox wget may not support --tries; keep its fallback bounded too.
+ if wget --help 2>&1 | grep -q -- --tries; then
+ wget --timeout=120 --tries=3 "$@"
+ return $?
+ fi
+ attempt=0
+ while [ "$attempt" -lt 3 ]; do
+ if wget --timeout=120 "$@"; then
+ return 0
+ else
+ download_status=$?
+ fi
+ attempt=$((attempt + 1))
+ done
+ return "$download_status"
+)
+
download_file() {
url="$1"
output="$2"
@@ -65,7 +83,7 @@ download_file() {
return $?
fi
if command -v wget >/dev/null 2>&1; then
- wget -qO "$output" --timeout=120 --tries=3 "$url"
+ wget_with_retries -qO "$output" "$url"
return $?
fi
echo "[neko-agent] error: curl or wget is required" >&2
@@ -101,23 +119,61 @@ normalize_arch() {
}
# Query GitHub releases API for the latest agent tag (e.g. "agent-v0.2.0").
-# Returns empty string on failure.
+# Reports HTTP/download errors on stderr and returns non-zero on failure.
#
# The repo publishes BOTH main-app (v*) and agent (agent-v*) releases into the
# same namespace, and either may be marked "latest". So we must NOT use the
# releases/latest endpoint — it can resolve to a main-app release that carries
# no agent binaries. Instead list recent releases and pick the newest agent-v*.
-get_latest_remote_tag() {
+get_latest_remote_tag() (
repo="${1:-foru17/neko-master}"
api_url="https://api.github.com/repos/${repo}/releases?per_page=100"
- tag=""
+ token="${GITHUB_TOKEN:-${GITHUB_PAT:-}}"
+ # Isolate temporary files, credentials and traps from the install/upgrade flow.
+ umask 077
+ api_tmp_dir="$(mktemp -d)" || return 1
+ trap 'rm -rf "$api_tmp_dir"' 0
+ trap 'exit 1' 1 2 15
+ request_status=0
if command -v curl >/dev/null 2>&1; then
- tag="$(curl -fsSL "$api_url" 2>/dev/null | awk -F'"' '/"tag_name"/{print $4}' | grep '^agent-v' | head -1)"
+ set -- -fsSL --connect-timeout 10 --max-time 120 --retry 3 --retry-delay 2
+ if [ -n "$token" ]; then
+ set -- "$@" -H "Authorization: Bearer $token"
+ fi
+ http_code="$(curl "$@" -o "$api_tmp_dir/body" -w '%{http_code}' "$api_url" 2>/dev/null)" || request_status=$?
elif command -v wget >/dev/null 2>&1; then
- tag="$(wget -qO- "$api_url" 2>/dev/null | awk -F'"' '/"tag_name"/{print $4}' | grep '^agent-v' | head -1)"
+ set -- -S -O "$api_tmp_dir/body"
+ if [ -n "$token" ]; then
+ set -- "$@" --header="Authorization: Bearer $token"
+ fi
+ wget_with_retries "$@" "$api_url" 2>"$api_tmp_dir/headers" || request_status=$?
+ http_code="$(awk '$1 ~ /^HTTP\// && $2 ~ /^[0-9][0-9][0-9]$/ {code=$2} END {print code}' "$api_tmp_dir/headers")"
+ else
+ echo "[neko-agent] error: curl or wget is required" >&2
+ return 1
+ fi
+
+ case "$http_code" in
+ 401) api_error="GitHub token rejected (401)" ;;
+ 403|429) api_error="GitHub API rate limit or forbidden (HTTP $http_code); set GITHUB_TOKEN" ;;
+ 2??) api_error="" ;;
+ *) api_error="GitHub API request failed (HTTP ${http_code:-unknown})" ;;
+ esac
+ if [ -n "$api_error" ]; then
+ echo "[neko-agent] error: $api_error" >&2
+ return 1
+ fi
+ if [ "$request_status" -ne 0 ]; then
+ echo "[neko-agent] error: GitHub API download failed (HTTP $http_code; downloader exit $request_status)" >&2
+ return 1
+ fi
+ tag="$(awk -F'"' '/"tag_name"/{print $4}' "$api_tmp_dir/body" | grep '^agent-v' | head -1)"
+ if [ -z "$tag" ]; then
+ echo "[neko-agent] error: no agent-v* release found in GitHub API response" >&2
+ return 1
fi
printf '%s\n' "$tag"
-}
+)
compute_sha256() {
file="$1"
diff --git a/apps/agent/nekoagent b/apps/agent/nekoagent
index a700ac0..7a49def 100755
--- a/apps/agent/nekoagent
+++ b/apps/agent/nekoagent
@@ -79,16 +79,34 @@ is_running() {
# ── Download helpers ────────────────────────────────────────────────────────
+wget_with_retries() (
+ # BusyBox wget may not support --tries; keep its fallback bounded too.
+ if wget --help 2>&1 | grep -q -- --tries; then
+ wget --timeout=120 --tries=3 "$@"
+ return $?
+ fi
+ attempt=0
+ while [ "$attempt" -lt 3 ]; do
+ if wget --timeout=120 "$@"; then
+ return 0
+ else
+ download_status=$?
+ fi
+ attempt=$((attempt + 1))
+ done
+ return "$download_status"
+)
+
download_file() {
url="$1"
output="$2"
if command -v curl >/dev/null 2>&1; then
- curl -fsSL "$url" -o "$output"
- return 0
+ curl -fsSL --connect-timeout 10 --max-time 120 --retry 3 --retry-delay 2 "$url" -o "$output"
+ return $?
fi
if command -v wget >/dev/null 2>&1; then
- wget -qO "$output" "$url"
- return 0
+ wget_with_retries -qO "$output" "$url"
+ return $?
fi
err "curl or wget is required"
exit 1
@@ -269,14 +287,14 @@ write_systemd_unit() {
Description=Neko Agent (${name})
After=network-online.target
Wants=network-online.target
+StartLimitIntervalSec=300
+StartLimitBurst=10
[Service]
Type=simple
ExecStart=${runner}
Restart=always
RestartSec=5
-StartLimitIntervalSec=300
-StartLimitBurst=10
LimitNOFILE=65536
NoNewPrivileges=true
PrivateTmp=true
@@ -485,23 +503,61 @@ cmd_disable_autostart() {
}
# Query GitHub releases API for the latest agent tag (e.g. "agent-v0.2.0").
-# Returns empty string on failure — callers must handle this gracefully.
+# Reports HTTP/download errors on stderr and returns non-zero on failure.
#
# The repo publishes BOTH main-app (v*) and agent (agent-v*) releases into the
# same namespace, and either may be marked "latest". So we must NOT use the
# releases/latest endpoint — it can resolve to a main-app release that carries
# no agent binaries. Instead list recent releases and pick the newest agent-v*.
-get_latest_remote_tag() {
+get_latest_remote_tag() (
repo="${1:-$AGENT_REPO}"
api_url="https://api.github.com/repos/${repo}/releases?per_page=100"
- tag=""
+ token="${GITHUB_TOKEN:-${GITHUB_PAT:-}}"
+ # Isolate temporary files, credentials and traps from the install/upgrade flow.
+ umask 077
+ api_tmp_dir="$(mktemp -d)" || return 1
+ trap 'rm -rf "$api_tmp_dir"' 0
+ trap 'exit 1' 1 2 15
+ request_status=0
if command -v curl >/dev/null 2>&1; then
- tag="$(curl -fsSL "$api_url" 2>/dev/null | awk -F'"' '/"tag_name"/{print $4}' | grep '^agent-v' | head -1)"
+ set -- -fsSL --connect-timeout 10 --max-time 120 --retry 3 --retry-delay 2
+ if [ -n "$token" ]; then
+ set -- "$@" -H "Authorization: Bearer $token"
+ fi
+ http_code="$(curl "$@" -o "$api_tmp_dir/body" -w '%{http_code}' "$api_url" 2>/dev/null)" || request_status=$?
elif command -v wget >/dev/null 2>&1; then
- tag="$(wget -qO- "$api_url" 2>/dev/null | awk -F'"' '/"tag_name"/{print $4}' | grep '^agent-v' | head -1)"
+ set -- -S -O "$api_tmp_dir/body"
+ if [ -n "$token" ]; then
+ set -- "$@" --header="Authorization: Bearer $token"
+ fi
+ wget_with_retries "$@" "$api_url" 2>"$api_tmp_dir/headers" || request_status=$?
+ http_code="$(awk '$1 ~ /^HTTP\// && $2 ~ /^[0-9][0-9][0-9]$/ {code=$2} END {print code}' "$api_tmp_dir/headers")"
+ else
+ err "curl or wget is required"
+ return 1
+ fi
+
+ case "$http_code" in
+ 401) api_error="GitHub token rejected (401)" ;;
+ 403|429) api_error="GitHub API rate limit or forbidden (HTTP $http_code); set GITHUB_TOKEN" ;;
+ 2??) api_error="" ;;
+ *) api_error="GitHub API request failed (HTTP ${http_code:-unknown})" ;;
+ esac
+ if [ -n "$api_error" ]; then
+ err "$api_error"
+ return 1
+ fi
+ if [ "$request_status" -ne 0 ]; then
+ err "GitHub API download failed (HTTP $http_code; downloader exit $request_status)"
+ return 1
+ fi
+ tag="$(awk -F'"' '/"tag_name"/{print $4}' "$api_tmp_dir/body" | grep '^agent-v' | head -1)"
+ if [ -z "$tag" ]; then
+ err "no agent-v* release found in GitHub API response"
+ return 1
fi
printf '%s\n' "$tag"
-}
+)
# Return the version string reported by the local neko-agent binary, or "".
get_local_binary_version() {
diff --git a/docs/agent/install.en.md b/docs/agent/install.en.md
index c53c523..8e5d64b 100644
--- a/docs/agent/install.en.md
+++ b/docs/agent/install.en.md
@@ -39,6 +39,7 @@ Optional env:
- `NEKO_INSTANCE_NAME`: instance name for `nekoagent` manager (default `backend-<id>`)
- `NEKO_BIN_LINK_MODE`: `auto|true|false` for symlink into global bin dir (default `auto`)
- `NEKO_LINK_DIR`: global bin dir for symlink (default `/usr/local/bin`)
+- `GITHUB_TOKEN`: token for GitHub API queries; falls back to `GITHUB_PAT` when empty
Set `AGENT_HEARTBEAT_TIMEOUT_MS` in the collector process environment to adjust how long an agent may stay silent before it is shown offline (backend list, automatic health checks and manual test) (milliseconds, default `90000`, minimum `15000`). The default allows 3 × the agent's default 30-second heartbeat interval to tolerate delayed reports; this is a collector setting, not an agent install option.
@@ -59,6 +60,31 @@ Uninstall binaries:
nekoagent uninstall
```
+### GitHub API rate limit / private mirrors
+
+If fetching the latest version hits GitHub's anonymous API rate limit, set `GITHUB_TOKEN` and retry:
+
+```sh
+GITHUB_TOKEN='<github-token>' nekoagent upgrade
+```
+
+For installation, set the same variable before running a downloaded `install.sh`, preserving your existing `NEKO_*` installation settings:
+
+```sh
+GITHUB_TOKEN='<github-token>' \
+NEKO_SERVER='http://your-panel:3000' \
+NEKO_BACKEND_ID='13' \
+NEKO_BACKEND_TOKEN='ag_xxx' \
+NEKO_GATEWAY_TYPE='clash' \
+NEKO_GATEWAY_URL='http://127.0.0.1:9090' \
+sh ./install.sh
+```
+
+A non-empty `GITHUB_TOKEN` takes precedence; otherwise, `GITHUB_PAT` is used. The token is sent only to `https://api.github.com`, never to release binaries, raw files, or mirror download URLs, and is not saved in instance configs. If installation with a private mirror still queries GitHub for the latest version, this variable authenticates that API query; access to the mirror download itself must be handled separately.
+
+- `GitHub token rejected (401)`: the token is invalid or expired; replace it and retry.
+- `GitHub API rate limit or forbidden (HTTP 403); set GITHUB_TOKEN` (or `HTTP 429`): the API is rate-limited or access is forbidden; set a valid token and check permissions or wait for the limit to reset.
+
## Manual install
1. Download the correct tarball from GitHub Releases
diff --git a/docs/agent/install.md b/docs/agent/install.md
index a9eb8e8..0c4ff89 100644
--- a/docs/agent/install.md
+++ b/docs/agent/install.md
@@ -39,6 +39,7 @@ curl -fsSL https://raw.githubusercontent.com/foru17/neko-master/main/apps/agent/
- `NEKO_INSTANCE_NAME`:`nekoagent` 管理器中的实例名(默认 `backend-<id>`)
- `NEKO_BIN_LINK_MODE`:全局 bin 目录软链模式(`auto|true|false`,默认 `auto`)
- `NEKO_LINK_DIR`:软链目标目录(默认 `/usr/local/bin`)
+- `GITHUB_TOKEN`:查询 GitHub API 时使用的 token;为空时回退到 `GITHUB_PAT`
采集器端可设置 `AGENT_HEARTBEAT_TIMEOUT_MS` 调整 Agent 判定离线的心跳超时(后端列表、自动健康检查与手动测试共用)(毫秒,默认 `90000`,最小 `15000`)。默认值为 Agent 默认 30 秒心跳间隔的 3 倍,用于容忍上报延迟;此变量应配置在采集器进程的环境中,而非 Agent 安装命令中。
@@ -59,6 +60,31 @@ nekoagent remove <instance>
nekoagent uninstall
```
+### GitHub API rate limit / 私有镜像
+
+查询最新版本遇到 GitHub API 匿名限流时,设置 `GITHUB_TOKEN` 后重试:
+
+```sh
+GITHUB_TOKEN='<github-token>' nekoagent upgrade
+```
+
+安装时,在已下载的 `install.sh` 命令前设置同一变量,并保留原有的 `NEKO_*` 安装参数:
+
+```sh
+GITHUB_TOKEN='<github-token>' \
+NEKO_SERVER='http://your-panel:3000' \
+NEKO_BACKEND_ID='13' \
+NEKO_BACKEND_TOKEN='ag_xxx' \
+NEKO_GATEWAY_TYPE='clash' \
+NEKO_GATEWAY_URL='http://127.0.0.1:9090' \
+sh ./install.sh
+```
+
+优先使用非空的 `GITHUB_TOKEN`,否则读取 `GITHUB_PAT`。token 仅用于 `https://api.github.com` 请求,不会发送到 Release 二进制、原始文件或镜像下载地址,也不会写入实例配置。使用私有镜像时,若安装仍需查询 GitHub 最新版本,可用此变量为 API 查询鉴权;镜像下载本身的访问权限须单独解决。
+
+- `GitHub token rejected (401)`:token 无效或已失效,请更换后重试。
+- `GitHub API rate limit or forbidden (HTTP 403); set GITHUB_TOKEN`(或 `HTTP 429`):API 限流或禁止访问;设置有效 token,并检查权限或等待限额恢复。
+
## 手动安装
1. 从 GitHub Releases 下载对应平台的压缩包
diff --git a/docs/agent/troubleshooting.en.md b/docs/agent/troubleshooting.en.md
index de4a973..37e21f8 100644
--- a/docs/agent/troubleshooting.en.md
+++ b/docs/agent/troubleshooting.en.md
@@ -2,6 +2,19 @@
[中文](./troubleshooting.md) | **English**
+## GitHub API rate limit / private mirrors
+
+When installation or upgrade queries the latest version:
+
+- `GitHub token rejected (401)`: the token is invalid or expired; replace it.
+- `GitHub API rate limit or forbidden (HTTP 403); set GITHUB_TOKEN` (or `HTTP 429`): the API is rate-limited or access is forbidden; set a valid token and check permissions or wait for the limit to reset.
+
+```sh
+GITHUB_TOKEN='<github-token>' nekoagent upgrade
+```
+
+You can also set `GITHUB_TOKEN` and rerun a downloaded `install.sh`, preserving your existing `NEKO_*` settings; see the [install guide](./install.en.md#github-api-rate-limit--private-mirrors). A non-empty `GITHUB_TOKEN` takes precedence; otherwise, `GITHUB_PAT` is used. The token is sent only to `https://api.github.com`, is not saved in instance configs, and is never sent to release binaries, raw files, or private mirror download URLs. Private mirror download authentication must be handled separately.
+
## `exec format error`
Cause: binary architecture mismatch.
diff --git a/docs/agent/troubleshooting.md b/docs/agent/troubleshooting.md
index 46d72bd..9d68f05 100644
--- a/docs/agent/troubleshooting.md
+++ b/docs/agent/troubleshooting.md
@@ -2,6 +2,19 @@
**中文 | [English](./troubleshooting.en.md)**
+## GitHub API rate limit / 私有镜像
+
+安装或升级查询最新版本时:
+
+- `GitHub token rejected (401)`:token 无效或已失效,请更换。
+- `GitHub API rate limit or forbidden (HTTP 403); set GITHUB_TOKEN`(或 `HTTP 429`):API 限流或禁止访问,请设置有效 token,并检查权限或等待限额恢复。
+
+```sh
+GITHUB_TOKEN='<github-token>' nekoagent upgrade
+```
+
+也可设置 `GITHUB_TOKEN` 后重新运行已下载的 `install.sh`,保留原有 `NEKO_*` 参数,见[安装指南](./install.md#github-api-rate-limit--私有镜像)。非空的 `GITHUB_TOKEN` 优先,否则读取 `GITHUB_PAT`。token 仅发送给 `https://api.github.com`,不会写入实例配置,也不会发送给 Release 二进制、原始文件或私有镜像下载地址;私有镜像的下载鉴权须单独解决。
+
## `exec format error`
原因:二进制架构与主机不匹配。