diff --git a/scripts/install.sh b/scripts/install.sh index e9649d3..b9b8cae 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -48,13 +48,49 @@ if [ -z "$TARGET_DIR" ]; then fi ASSET_NAME="seal-home-${PLATFORM}-${ARCH_NAME}" -DOWNLOAD_URL="https://github.com/${REPO}/releases/latest/download/${ASSET_NAME}" TARGET_PATH="${TARGET_DIR}/seal-home" echo "==> Downloading seal-home (${PLATFORM}-${ARCH_NAME}) into ${TARGET_DIR}..." TMP_FILE="$(mktemp)" -if ! curl -fL "$DOWNLOAD_URL" -o "$TMP_FILE"; then - echo "Error: Failed to download $DOWNLOAD_URL" >&2 +DOWNLOAD_SUCCESS=false + +# Multi-source candidate mirrors +GITEA_BASE="https://git.ying-qing.cn/gitea/seal-home" +GITHUB_BASE="https://github.com/${REPO}" +GHPROXY_BASE="https://ghfast.top/https://github.com/${REPO}" + +# 1. Check Gitea mirror if responsive +if [ "${SEAL_HOME_MIRROR:-auto}" = "gitea" ] || [ "${SEAL_HOME_MIRROR:-auto}" = "auto" ]; then + if curl -s --connect-timeout 2 -I "https://git.ying-qing.cn" >/dev/null 2>&1; then + GITEA_URL="${GITEA_BASE}/releases/download/latest/${ASSET_NAME}" + if curl -fL --connect-timeout 4 "$GITEA_URL" -o "$TMP_FILE" 2>/dev/null; then + echo "✓ Downloaded from high-speed mirror (git.ying-qing.cn)" + DOWNLOAD_SUCCESS=true + fi + fi +fi + +# 2. Try direct GitHub +if [ "$DOWNLOAD_SUCCESS" = "false" ]; then + GITHUB_URL="${GITHUB_BASE}/releases/latest/download/${ASSET_NAME}" + if curl -fL --connect-timeout 6 "$GITHUB_URL" -o "$TMP_FILE" 2>/dev/null; then + echo "✓ Downloaded from GitHub Releases" + DOWNLOAD_SUCCESS=true + fi +fi + +# 3. Fallback to GitHub CDN proxy +if [ "$DOWNLOAD_SUCCESS" = "false" ]; then + GHPROXY_URL="${GHPROXY_BASE}/releases/latest/download/${ASSET_NAME}" + echo "==> Trying CDN fallback mirror..." + if curl -fL "$GHPROXY_URL" -o "$TMP_FILE" 2>/dev/null; then + echo "✓ Downloaded from CDN mirror" + DOWNLOAD_SUCCESS=true + fi +fi + +if [ "$DOWNLOAD_SUCCESS" = "false" ]; then + echo "Error: Failed to download ${ASSET_NAME} from any available mirror." >&2 rm -f "$TMP_FILE" exit 1 fi diff --git a/src/cli/commands/update.ts b/src/cli/commands/update.ts index e6145da..e5f3537 100644 --- a/src/cli/commands/update.ts +++ b/src/cli/commands/update.ts @@ -302,110 +302,123 @@ type GitHubRelease = { }; async function fetchGitHubReleases(repo: string, limit = 20): Promise { - const url = `https://api.github.com/repos/${repo}/releases?per_page=${limit}`; + const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN; const headers: Record = { "User-Agent": `seal-home/${VERSION}`, - Accept: "application/vnd.github.v3+json" + Accept: "application/vnd.github.v3+json", + ...(token ? { Authorization: `Bearer ${token}` } : {}) }; - const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN; - if (token) { - headers.Authorization = `Bearer ${token}`; - } + const urls = [ + `https://api.github.com/repos/${repo}/releases?per_page=${limit}`, + `https://git.ying-qing.cn/api/v1/repos/gitea/seal-home/releases?per_page=${limit}` + ]; - try { - const res = await fetch(url, { headers }); - if (!res.ok) return []; - const json = (await res.json()) as Array<{ - tag_name?: string; - name?: string; - body?: string; - html_url?: string; - published_at?: string; - author?: { login?: string }; - assets?: Array<{ name: string; browser_download_url: string; size?: number }>; - }>; + for (const url of urls) { + try { + const res = await fetch(url, { headers, signal: AbortSignal.timeout(5000) }); + if (!res.ok) continue; + const json = (await res.json()) as Array<{ + tag_name?: string; + name?: string; + body?: string; + html_url?: string; + published_at?: string; + author?: { login?: string }; + assets?: Array<{ name: string; browser_download_url: string; size?: number }>; + }>; - if (!Array.isArray(json)) return []; + if (!Array.isArray(json) || json.length === 0) continue; - const list: GitHubRelease[] = []; - for (const item of json) { - const tagName = item.tag_name ?? ""; - const version = tagName.replace(/^v/, ""); - if (!version) continue; - const assets: GitHubAsset[] = (item.assets ?? []).map((a) => ({ - name: a.name, - downloadUrl: a.browser_download_url, - size: a.size ?? 0 - })); - list.push({ - version, - tagName, - name: item.name ?? tagName, - publishedAt: item.published_at, - author: item.author?.login, - notes: item.body ?? "", - htmlUrl: item.html_url ?? "", - assets - }); + const list: GitHubRelease[] = []; + for (const item of json) { + const tagName = item.tag_name ?? ""; + const version = tagName.replace(/^v/, ""); + if (!version) continue; + const assets: GitHubAsset[] = (item.assets ?? []).map((a) => ({ + name: a.name, + downloadUrl: a.browser_download_url, + size: a.size ?? 0 + })); + list.push({ + version, + tagName, + name: item.name ?? tagName, + publishedAt: item.published_at, + author: item.author?.login, + notes: item.body ?? "", + htmlUrl: item.html_url ?? "", + assets + }); + } + if (list.length > 0) return list; + } catch { + // try next source } - return list; - } catch { - return []; } + + return []; } async function fetchGitHubRelease(repo: string, targetVersion?: string): Promise { - const url = targetVersion - ? `https://api.github.com/repos/${repo}/releases/tags/${targetVersion.startsWith("v") ? targetVersion : `v${targetVersion}`}` - : `https://api.github.com/repos/${repo}/releases/latest`; - + const tag = targetVersion ? (targetVersion.startsWith("v") ? targetVersion : `v${targetVersion}`) : undefined; + const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN; const headers: Record = { "User-Agent": `seal-home/${VERSION}`, - Accept: "application/vnd.github.v3+json" + Accept: "application/vnd.github.v3+json", + ...(token ? { Authorization: `Bearer ${token}` } : {}) }; - const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN; - if (token) { - headers.Authorization = `Bearer ${token}`; + const urls = tag + ? [ + `https://api.github.com/repos/${repo}/releases/tags/${tag}`, + `https://git.ying-qing.cn/api/v1/repos/gitea/seal-home/releases/tags/${tag}` + ] + : [ + `https://api.github.com/repos/${repo}/releases/latest`, + `https://git.ying-qing.cn/api/v1/repos/gitea/seal-home/releases/latest` + ]; + + for (const url of urls) { + try { + const res = await fetch(url, { headers, signal: AbortSignal.timeout(5000) }); + if (!res.ok) continue; + const json = (await res.json()) as { + tag_name?: string; + name?: string; + body?: string; + html_url?: string; + published_at?: string; + author?: { login?: string }; + assets?: Array<{ name: string; browser_download_url: string; size?: number }>; + }; + + const tagName = json.tag_name ?? ""; + const version = tagName.replace(/^v/, ""); + if (!version) continue; + + const assets: GitHubAsset[] = (json.assets ?? []).map((item) => ({ + name: item.name, + downloadUrl: item.browser_download_url, + size: item.size ?? 0 + })); + + return { + version, + tagName, + name: json.name ?? tagName, + publishedAt: json.published_at, + author: json.author?.login, + notes: json.body ?? "", + htmlUrl: json.html_url ?? "", + assets + }; + } catch { + // try next source + } } - try { - const res = await fetch(url, { headers }); - if (!res.ok) return undefined; - const json = (await res.json()) as { - tag_name?: string; - name?: string; - body?: string; - html_url?: string; - published_at?: string; - author?: { login?: string }; - assets?: Array<{ name: string; browser_download_url: string; size?: number }>; - }; - - const tagName = json.tag_name ?? ""; - const version = tagName.replace(/^v/, ""); - if (!version) return undefined; - - const assets: GitHubAsset[] = (json.assets ?? []).map((item) => ({ - name: item.name, - downloadUrl: item.browser_download_url, - size: item.size ?? 0 - })); - - return { - version, - tagName, - name: json.name ?? tagName, - publishedAt: json.published_at, - author: json.author?.login, - notes: json.body ?? "", - htmlUrl: json.html_url ?? "", - assets - }; - } catch { - return undefined; - } + return undefined; } function matchAssetForPlatform( @@ -447,12 +460,27 @@ function matchAssetForPlatform( } async function downloadFile(url: string, destPath: string) { - const res = await fetch(url); - if (!res.ok) { - throw new Error(`Failed to download asset from ${url}: ${res.status} ${res.statusText}`); + const candidates = [url]; + if (url.includes("github.com")) { + candidates.push(`https://ghfast.top/${url}`); } - const buffer = Buffer.from(await res.arrayBuffer()); - writeFileSync(destPath, buffer); + + let lastError: Error | undefined; + for (const candidate of candidates) { + try { + const res = await fetch(candidate, { signal: AbortSignal.timeout(15000) }); + if (!res.ok) { + throw new Error(`HTTP ${res.status} ${res.statusText}`); + } + const buffer = Buffer.from(await res.arrayBuffer()); + writeFileSync(destPath, buffer); + return; + } catch (err) { + lastError = err instanceof Error ? err : new Error(String(err)); + } + } + + throw new Error(`Failed to download asset from ${url}: ${lastError?.message}`); } function extractTarGz(archivePath: string, targetDir: string, platform: string): string {