Files
cellular-proxy/scripts/fetch-binaries.sh
T
Hermes 6f47e4f9eb feat: sing-box 下载默认经 https://git.86482425.xyz 代理
- GITHUB_PROXY 可配置;失败自动回退直连 GitHub
2026-07-21 11:18:47 +00:00

93 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# 仅下载 sing-box(轻量核心)
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=lib.sh
source "$ROOT_DIR/scripts/lib.sh"
load_config
need_root
ensure_dirs
mkdir -p /var/lib/cellular-proxy
ARCH="$(arch_go)"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
# GitHub 下载代理:默认 https://git.86482425.xyz
# 空字符串 = 直连 GitHub;也可设为其它镜像前缀(末尾可带或不带 /)
GITHUB_PROXY="${GITHUB_PROXY:-https://git.86482425.xyz}"
github_url() {
# 把 https://github.com/... 加上代理前缀
local raw="$1"
local proxy="${GITHUB_PROXY:-}"
if [[ -z "$proxy" ]]; then
echo "$raw"
return
fi
proxy="${proxy%/}"
# 已是代理地址则不重复加
case "$raw" in
"${proxy}"/*) echo "$raw"; return ;;
esac
# 常见 ghproxy 风格:https://proxy/https://github.com/...
echo "${proxy}/${raw}"
}
download() {
local url="$1" dest="$2"
info "下载: $url"
if command -v curl >/dev/null 2>&1; then
curl -fL --retry 3 --connect-timeout 20 -o "$dest" "$url"
elif command -v wget >/dev/null 2>&1; then
wget -O "$dest" "$url"
else
die "需要 curl 或 wget"
fi
}
download_github() {
# 先走代理,失败再直连 GitHub
local raw="$1" dest="$2"
local proxied
proxied="$(github_url "$raw")"
if [[ "$proxied" != "$raw" ]]; then
info "经 GitHub 代理下载: $proxied"
if download "$proxied" "$dest"; then
return 0
fi
warn "代理下载失败,尝试直连 GitHub: $raw"
fi
download "$raw" "$dest"
}
if [[ "$SING_BOX_SOURCE" == "local" ]]; then
[[ -n "$SING_BOX_BIN" && -x "$SING_BOX_BIN" ]] || die "SING_BOX_SOURCE=local 但 SING_BOX_BIN 无效"
install -m 0755 "$SING_BOX_BIN" "$INSTALL_DIR/bin/sing-box"
info "已安装本地 sing-box"
elif command -v sing-box >/dev/null 2>&1 && [[ -z "${FORCE_DOWNLOAD:-}" ]]; then
install -m 0755 "$(command -v sing-box)" "$INSTALL_DIR/bin/sing-box"
info "复用系统 sing-box: $(command -v sing-box)"
else
ver="$SING_BOX_VERSION"
name="sing-box-${ver}-linux-${ARCH}"
raw_url="https://github.com/SagerNet/sing-box/releases/download/v${ver}/${name}.tar.gz"
download_github "$raw_url" "$TMP/sb.tgz"
tar -xzf "$TMP/sb.tgz" -C "$TMP"
install -m 0755 "$TMP/${name}/sing-box" "$INSTALL_DIR/bin/sing-box"
info "已安装 sing-box v${ver}"
fi
# 安装中文静态 UI
mkdir -p "$INSTALL_DIR/ui"
if [[ -f "$ROOT_DIR/ui/index.html" ]]; then
cp -a "$ROOT_DIR/ui/." "$INSTALL_DIR/ui/"
info "已安装内置中文面板 -> $INSTALL_DIR/ui"
else
warn "未找到 ui/index.html"
fi
info "二进制就绪:"
ls -la "$INSTALL_DIR/bin"
du -sh "$INSTALL_DIR/bin" "$INSTALL_DIR/ui" 2>/dev/null || true