fix: harden CLI parsing, MCP startup and release integrity

This commit is contained in:
gitea committed 2026-10-17 20:32:36 +08:00
1 parent f0902f5f20
commit 103ead0fe5
53 files changed
+3713 -1945

No files matched your search

+119 -78
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -e
set -euo pipefail
REPO="yunyingk/hose-use"
@@ -34,9 +34,10 @@ case "$ARCH" in
esac
# Resolve best writable bin directory without requiring sudo
TARGET_DIR=""
TARGET_DIR="${HOSE_USE_INSTALL_DIR:-}"
if [ -n "$TARGET_DIR" ]; then mkdir -p "$TARGET_DIR"; fi
for candidate in "/opt/homebrew/bin" "/usr/local/bin" "$HOME/.local/bin" "$HOME/bin"; do
if [ -d "$candidate" ] && [ -w "$candidate" ]; then
if [ -z "$TARGET_DIR" ] && [ -d "$candidate" ] && [ -w "$candidate" ]; then
TARGET_DIR="$candidate"
break
fi
@@ -49,6 +50,10 @@ fi
ASSET_NAME="hose-use-${PLATFORM}-${ARCH_NAME}"
TARGET_PATH="${TARGET_DIR}/hose-use"
if [ -d "$TARGET_PATH" ] || { [ -d "${TARGET_DIR}/hose-aipro" ] && [ ! -L "${TARGET_DIR}/hose-aipro" ]; }; then
echo "Error: an install target is a directory; refusing to replace it." >&2
exit 1
fi
get_file_size() {
local file="$1"
@@ -102,7 +107,7 @@ download_candidate() {
if curl -fL --progress-bar "$url" -o "$dest"; then
local size_bytes
size_bytes="$(get_file_size "$dest")"
if [ "$size_bytes" -gt 1000000 ]; then
if [ "$size_bytes" -gt 0 ]; then
echo "✓ Download complete! Total size: $(format_bytes "$size_bytes")"
echo "✓ Successfully fetched from host: ${host}"
return 0
@@ -114,103 +119,139 @@ download_candidate() {
}
echo "==> Target install location: ${TARGET_PATH}"
TMP_FILE="$(mktemp)"
TMP_DIR="$(mktemp -d)"
TMP_FILE="${TMP_DIR}/download"
CHECKSUM_FILE="${TMP_DIR}/checksums.txt"
INSTALL_STAGE=""
trap 'rm -rf "$TMP_DIR"; if [ -n "$INSTALL_STAGE" ]; then rm -f "$INSTALL_STAGE"; fi' EXIT
DOWNLOAD_SUCCESS=false
DOWNLOADED_FORMAT="archive"
# Resolve latest Gitea tag if available
GITEA_API_URL="https://git.ying-qing.cn/api/v1/repos/gitea/hose-use/releases/latest"
LATEST_TAG=""
if command -v curl >/dev/null 2>&1; then
LATEST_TAG="$(curl -sL --connect-timeout 3 "$GITEA_API_URL" | sed -n 's/.*"tag_name":"\([^"]*\)".*/\1/p' || true)"
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required." >&2
exit 1
fi
[ -z "$LATEST_TAG" ] && LATEST_TAG="v0.8.9"
if command -v sha256sum >/dev/null 2>&1; then
hash_file() { sha256sum "$1" | awk '{ print tolower($1) }'; }
elif command -v shasum >/dev/null 2>&1; then
hash_file() { shasum -a 256 "$1" | awk '{ print tolower($1) }'; }
else
echo "Error: SHA-256 verification requires sha256sum or shasum." >&2
exit 1
fi
MIRROR="${HOSE_USE_MIRROR:-auto}"
case "$MIRROR" in auto|github|gitea) ;; *) echo "Invalid HOSE_USE_MIRROR: $MIRROR" >&2; exit 1 ;; esac
# Resolve latest Gitea tag if available. Resolve once, then pin every mirror and
# checksum request to that exact version instead of guessing an old release.
GITEA_API_URL="https://git.ying-qing.cn/api/v1/repos/gitea/hose-use/releases/latest"
GITHUB_API_URL="https://api.github.com/repos/${REPO}/releases/latest"
LATEST_TAG="${HOSE_USE_VERSION:-}"
if [ -n "$LATEST_TAG" ]; then
LATEST_TAG="v${LATEST_TAG#v}"
else
api_urls=()
if [ "$MIRROR" != "github" ]; then api_urls+=("$GITEA_API_URL"); fi
if [ "$MIRROR" != "gitea" ]; then api_urls+=("$GITHUB_API_URL"); fi
for api_url in "${api_urls[@]}"; do
if release_json=$(curl -fsSL --connect-timeout 4 --max-time 15 "$api_url"); then
LATEST_TAG=$(printf '%s' "$release_json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^" ]*\)".*/\1/p' | head -n 1)
if [ -n "$LATEST_TAG" ]; then break; fi
fi
done
fi
if ! [[ "$LATEST_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.-]+)?$ ]]; then
echo "Error: could not resolve a release version; set HOSE_USE_VERSION explicitly." >&2
exit 1
fi
release_bases=()
if [ "$MIRROR" != "github" ]; then release_bases+=("https://git.ying-qing.cn/gitea/hose-use/releases/download/${LATEST_TAG}"); fi
if [ "$MIRROR" != "gitea" ]; then
release_bases+=("https://github.com/${REPO}/releases/download/${LATEST_TAG}")
release_bases+=("https://ghfast.top/https://github.com/${REPO}/releases/download/${LATEST_TAG}")
fi
verified_download() {
local base="$1" asset="$2" expected actual
if ! download_candidate "${base}/checksums.txt" "$CHECKSUM_FILE" "Release checksums"; then
echo " ↳ Missing checksums.txt for ${LATEST_TAG}; refusing an unverified install." >&2
return 1
fi
expected=$(awk -v name="$asset" '{ sub(/\r$/, "") } $2 == name || $2 == "*" name { count++; if (length($1) == 64 && $1 ~ /^[0-9a-fA-F]+$/) value=tolower($1); else invalid=1 } END { if (count == 1 && !invalid) print value; else exit 1 }' "$CHECKSUM_FILE") || {
echo " ↳ Missing or ambiguous checksum for ${asset}." >&2
return 1
}
if ! download_candidate "${base}/${asset}" "$TMP_FILE" "Release ${LATEST_TAG}"; then return 1; fi
actual=$(hash_file "$TMP_FILE")
if [ "$actual" != "$expected" ]; then
echo " ↳ SHA-256 mismatch for ${asset}; refusing this download." >&2
return 1
fi
echo "✓ SHA-256 verified: ${asset}"
}
# 1. First priority: Compressed Archives (.tar.gz) - fast download & includes aliases
GITEA_ARCHIVE_URL="https://git.ying-qing.cn/gitea/hose-use/releases/download/${LATEST_TAG}/${ASSET_NAME}.tar.gz"
GITHUB_ARCHIVE_URL="https://github.com/${REPO}/releases/latest/download/${ASSET_NAME}.tar.gz"
GHPROXY_ARCHIVE_URL="https://ghfast.top/https://github.com/${REPO}/releases/latest/download/${ASSET_NAME}.tar.gz"
if [ "${HOSE_USE_MIRROR:-auto}" = "gitea" ] || [ "${HOSE_USE_MIRROR:-auto}" = "auto" ]; then
if download_candidate "$GITEA_ARCHIVE_URL" "$TMP_FILE" "Gitea High-Speed Mirror (Archive)"; then
DOWNLOAD_SUCCESS=true
DOWNLOADED_FORMAT="archive"
fi
fi
if [ "$DOWNLOAD_SUCCESS" = "false" ] && [ "${HOSE_USE_MIRROR:-auto}" != "gitea" ]; then
if download_candidate "$GITHUB_ARCHIVE_URL" "$TMP_FILE" "GitHub Releases (Archive Direct)"; then
DOWNLOAD_SUCCESS=true
DOWNLOADED_FORMAT="archive"
fi
fi
if [ "$DOWNLOAD_SUCCESS" = "false" ] && [ "${HOSE_USE_MIRROR:-auto}" != "gitea" ]; then
if download_candidate "$GHPROXY_ARCHIVE_URL" "$TMP_FILE" "Fast CDN Mirror (Archive Fallback)"; then
DOWNLOAD_SUCCESS=true
DOWNLOADED_FORMAT="archive"
fi
fi
# 2. Second priority: Raw Executable (Fallback)
if [ "$DOWNLOAD_SUCCESS" = "false" ]; then
GITEA_RAW_URL="https://git.ying-qing.cn/gitea/hose-use/releases/download/${LATEST_TAG}/${ASSET_NAME}"
GITHUB_RAW_URL="https://github.com/${REPO}/releases/latest/download/${ASSET_NAME}"
GHPROXY_RAW_URL="https://ghfast.top/https://github.com/${REPO}/releases/latest/download/${ASSET_NAME}"
if [ "${HOSE_USE_MIRROR:-auto}" = "gitea" ] || [ "${HOSE_USE_MIRROR:-auto}" = "auto" ]; then
if download_candidate "$GITEA_RAW_URL" "$TMP_FILE" "Gitea High-Speed Mirror (Raw Binary)"; then
# 2. Second priority: Raw Executable (Fallback), with the same checksum requirement.
for format in archive raw; do
asset="$ASSET_NAME"
if [ "$format" = "archive" ]; then asset="${asset}.tar.gz"; fi
for base in "${release_bases[@]}"; do
if verified_download "$base" "$asset"; then
DOWNLOAD_SUCCESS=true
DOWNLOADED_FORMAT="raw"
DOWNLOADED_FORMAT="$format"
break
fi
fi
if [ "$DOWNLOAD_SUCCESS" = "false" ] && [ "${HOSE_USE_MIRROR:-auto}" != "gitea" ]; then
if download_candidate "$GITHUB_RAW_URL" "$TMP_FILE" "GitHub Releases (Raw Direct)"; then
DOWNLOAD_SUCCESS=true
DOWNLOADED_FORMAT="raw"
fi
fi
if [ "$DOWNLOAD_SUCCESS" = "false" ] && [ "${HOSE_USE_MIRROR:-auto}" != "gitea" ]; then
if download_candidate "$GHPROXY_RAW_URL" "$TMP_FILE" "Fast CDN Mirror (Raw Fallback)"; then
DOWNLOAD_SUCCESS=true
DOWNLOADED_FORMAT="raw"
fi
fi
fi
done
if [ "$DOWNLOAD_SUCCESS" = "true" ]; then break; fi
done
if [ "$DOWNLOAD_SUCCESS" = "false" ]; then
echo "Error: Failed to download ${ASSET_NAME} from all candidate mirrors." >&2
rm -f "$TMP_FILE"
echo "Error: Failed to obtain a verified ${ASSET_NAME} for ${LATEST_TAG} from all candidate mirrors." >&2
exit 1
fi
if [ "$DOWNLOADED_FORMAT" = "archive" ]; then
echo "==> Extracting archive to ${TARGET_DIR}..."
tar -xzf "$TMP_FILE" -C "$TARGET_DIR"
rm -f "$TMP_FILE"
chmod +x "$TARGET_PATH"
[ -f "${TARGET_DIR}/hose-aipro" ] && chmod +x "${TARGET_DIR}/hose-aipro"
echo "==> Extracting selected binary in temporary staging..."
member=""
listing=$(tar -tzf "$TMP_FILE")
for candidate in "$ASSET_NAME" "./$ASSET_NAME" hose-use ./hose-use; do
if printf '%s\n' "$listing" | awk -v name="$candidate" '$0 == name { count++ } END { exit count != 1 }'; then
member="$candidate"
break
fi
done
if [ -z "$member" ]; then echo "Error: release archive has no expected platform binary." >&2; exit 1; fi
# Stream only the exact member; archive paths and aliases never get written to bin.
tar -xOzf "$TMP_FILE" "$member" > "${TMP_DIR}/hose-use"
if [ ! -s "${TMP_DIR}/hose-use" ]; then echo "Error: release binary is empty." >&2; exit 1; fi
else
chmod +x "$TMP_FILE"
mv "$TMP_FILE" "$TARGET_PATH"
# Create hose-aipro multicall alias pointing at the single binary.
if [ -w "$TARGET_DIR" ]; then
ln -sf "$TARGET_PATH" "${TARGET_DIR}/hose-aipro" 2>/dev/null || true
fi
mv "$TMP_FILE" "${TMP_DIR}/hose-use"
fi
# Stage on the target filesystem so the final rename is atomic.
INSTALL_STAGE=$(mktemp "${TARGET_DIR}/.hose-use-install.XXXXXX")
cp "${TMP_DIR}/hose-use" "$INSTALL_STAGE"
chmod 755 "$INSTALL_STAGE"
# Ad-hoc sign on macOS
if [ "$PLATFORM" = "darwin" ] && command -v codesign >/dev/null 2>&1; then
echo "==> Applying macOS code signature..."
codesign --force --sign - "$TARGET_PATH" 2>/dev/null || true
[ -f "${TARGET_DIR}/hose-aipro" ] && codesign --force --sign - "${TARGET_DIR}/hose-aipro" 2>/dev/null || true
codesign --force --sign - "$INSTALL_STAGE" 2>/dev/null || true
fi
mv -f "$INSTALL_STAGE" "$TARGET_PATH"
INSTALL_STAGE=""
# Create hose-aipro multicall alias pointing at the single binary.
ln -sfn hose-use "${TARGET_DIR}/hose-aipro"
echo "==> Initializing agent skill to ~/.agents/skills/hose-use..."
"$TARGET_PATH" skill install >/dev/null 2>&1 || true
echo "✓ Agent skill installed to ~/.agents/skills/hose-use"
if "$TARGET_PATH" skill install --yes; then
echo "✓ Agent skill installed to ~/.agents/skills/hose-use"
else
echo "Error: binary installed to ${TARGET_PATH}, but Agent Skill installation failed. Retry: hose-use skill install --yes" >&2
exit 1
fi
echo ""
echo "✨ hose-use successfully installed to ${TARGET_PATH}!"