dsw-web-config-encrypted / deploy_web_hf_mirror.sh
cfli's picture
Upload deploy_web_hf_mirror.sh with huggingface_hub
569d099 verified
Raw
History Blame Contribute Delete
8.28 kB
#!/usr/bin/env bash
set -euo pipefail
HF_REPO_ID="${HF_REPO_ID:-cfli/dsw-web-config-encrypted}"
HF_REPO_TYPE="${HF_REPO_TYPE:-dataset}"
HF_REVISION="${HF_REVISION:-main}"
HF_MIRROR_URL="${HF_MIRROR_URL:-https://hf-mirror.com}"
BUNDLE_NAME="${BUNDLE_NAME:-web_mihomo_bundle.tar.gz}"
SHA_NAME="${BUNDLE_NAME}.sha256"
ENCRYPT_BUNDLE="${ENCRYPT_BUNDLE:-1}"
ENCRYPTED_BUNDLE_NAME="${ENCRYPTED_BUNDLE_NAME:-${BUNDLE_NAME}.enc}"
ENCRYPTED_SHA_NAME="${ENCRYPTED_BUNDLE_NAME}.sha256"
HF_AUTH_REQUIRED="${HF_AUTH_REQUIRED:-0}"
OPENSSL_ITER="${OPENSSL_ITER:-200000}"
MIHOMO_PORT="${MIHOMO_PORT:-15777}"
MIHOMO_LOG="${MIHOMO_LOG:-/tmp/mihomo_web.log}"
RESTART_MIHOMO="${RESTART_MIHOMO:-1}"
log() {
printf '[%s] %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*"
}
die() {
printf 'error: %s\n' "$*" >&2
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"
}
need_token() {
[[ -n "${HF_TOKEN:-}" ]] || die "HF_TOKEN is required for this private Hugging Face repo"
}
need_bundle_key() {
[[ -n "${WEB_BUNDLE_KEY:-}" ]] || die "WEB_BUNDLE_KEY is required to decrypt the web bundle"
export WEB_BUNDLE_KEY
}
repo_prefix() {
case "${HF_REPO_TYPE}" in
dataset) printf 'datasets/' ;;
model) printf '' ;;
space) printf 'spaces/' ;;
*) die "unsupported HF_REPO_TYPE: ${HF_REPO_TYPE}; use dataset, model, or space" ;;
esac
}
trim_trailing_slash() {
printf '%s' "${1%/}"
}
repo_file_url() {
local filename="$1"
printf '%s/%s%s/resolve/%s/%s' \
"$(trim_trailing_slash "${HF_MIRROR_URL}")" \
"$(repo_prefix)" \
"${HF_REPO_ID}" \
"${HF_REVISION}" \
"${filename}"
}
url_origin() {
python3 - "$1" <<'PY'
import sys
from urllib.parse import urlsplit
parts = urlsplit(sys.argv[1])
print(f"{parts.scheme}://{parts.netloc}")
PY
}
download_with_hf_token() {
local url="$1"
local out="$2"
local cfg headers status location origin
need_token
require_cmd curl
require_cmd python3
cfg="$(mktemp)"
headers="$(mktemp)"
chmod 600 "${cfg}" "${headers}"
{
printf 'fail\n'
printf 'silent\n'
printf 'show-error\n'
printf 'header = "Authorization: Bearer %s"\n' "${HF_TOKEN}"
} > "${cfg}"
status="$(curl --config "${cfg}" -I -o /dev/null -D "${headers}" -w '%{http_code}' "${url}")"
if [[ "${status}" =~ ^30 ]]; then
location="$(awk 'tolower($1) == "location:" { sub(/\r$/, ""); sub(/^Location:[[:space:]]*/, ""); print; exit }' "${headers}")"
[[ -n "${location}" ]] || die "redirect without Location header for ${url}"
if [[ "${location}" == /* ]]; then
origin="$(url_origin "${url}")"
location="${origin}${location}"
fi
curl -fsSL -o "${out}" "${location}" || curl --config "${cfg}" -L -o "${out}" "${location}"
elif [[ "${status}" == "200" ]]; then
curl --config "${cfg}" -o "${out}" "${url}"
else
rm -f "${cfg}" "${headers}"
die "download failed for ${url}; HTTP ${status}"
fi
rm -f "${cfg}" "${headers}"
}
download_file() {
local url="$1"
local out="$2"
if [[ "${HF_AUTH_REQUIRED}" == "1" ]]; then
require_cmd curl
download_with_hf_token "${url}" "${out}"
elif command -v wget >/dev/null 2>&1; then
if wget --help 2>&1 | grep -q -- '--show-progress'; then
wget --timeout=30 --tries=3 --show-progress -O "${out}" "${url}"
else
wget -O "${out}" "${url}"
fi
else
require_cmd curl
curl -fL --connect-timeout 30 --retry 3 --progress-bar "${url}" -o "${out}"
fi
}
run_as_root() {
if [[ "${EUID}" -eq 0 ]]; then
"$@"
else
require_cmd sudo
sudo "$@"
fi
}
install_payload() {
local payload_dir="$1"
local backup_dir
[[ -x "${payload_dir}/mihomo" ]] || die "payload missing executable: ${payload_dir}/mihomo"
[[ -d "${payload_dir}/etc/mihomo" ]] || die "payload missing directory: ${payload_dir}/etc/mihomo"
if [[ -d /etc/mihomo ]]; then
backup_dir="/etc/mihomo.bak.$(date -u +%Y%m%dT%H%M%SZ)"
log "backing up /etc/mihomo to ${backup_dir}"
run_as_root cp -a /etc/mihomo "${backup_dir}"
fi
log "installing mihomo config to /etc/mihomo"
run_as_root rm -rf /etc/mihomo
run_as_root mkdir -p /etc
run_as_root cp -a "${payload_dir}/etc/mihomo" /etc/mihomo
log "installing mihomo binary to /usr/local/bin/mihomo"
run_as_root install -m 0755 "${payload_dir}/mihomo" /usr/local/bin/mihomo
}
update_shell_profile() {
local bashrc="${HOME}/.bashrc"
local tmp_file
mkdir -p "$(dirname "${bashrc}")"
touch "${bashrc}"
tmp_file="$(mktemp)"
awk '
/^# >>> Mihomo Config Start >>>$/ { skip = 1; next }
/^# <<< Mihomo Config End <<<$/ { skip = 0; next }
skip != 1 { print }
' "${bashrc}" > "${tmp_file}"
cat >> "${tmp_file}" <<EOF
# >>> Mihomo Config Start >>>
# Start mihomo automatically for interactive shells.
if ! pgrep -f "/usr/local/bin/mihomo -d /etc/mihomo" > /dev/null 2>&1; then
nohup /usr/local/bin/mihomo -d /etc/mihomo > ${MIHOMO_LOG} 2>&1 &
fi
prox() {
export https_proxy=http://127.0.0.1:${MIHOMO_PORT}
export http_proxy=http://127.0.0.1:${MIHOMO_PORT}
git config --global https.proxy http://127.0.0.1:${MIHOMO_PORT}
git config --global http.proxy http://127.0.0.1:${MIHOMO_PORT}
}
unprox() {
unset https_proxy http_proxy
git config --global --unset-all http.proxy >/dev/null 2>&1 || true
git config --global --unset-all https.proxy >/dev/null 2>&1 || true
}
# <<< Mihomo Config End <<<
EOF
mv "${tmp_file}" "${bashrc}"
log "updated ${bashrc}"
}
start_mihomo() {
if pgrep -f "/usr/local/bin/mihomo -d /etc/mihomo" >/dev/null 2>&1; then
if [[ "${RESTART_MIHOMO}" == "1" ]]; then
log "restarting mihomo"
pkill -f "/usr/local/bin/mihomo -d /etc/mihomo" >/dev/null 2>&1 || true
sleep 1
else
log "mihomo is already running"
return
fi
else
log "starting mihomo"
fi
nohup /usr/local/bin/mihomo -d /etc/mihomo > "${MIHOMO_LOG}" 2>&1 &
sleep 1
pgrep -f "/usr/local/bin/mihomo -d /etc/mihomo" >/dev/null 2>&1 || die "mihomo did not start; check ${MIHOMO_LOG}"
log "mihomo is running; log: ${MIHOMO_LOG}"
}
install_from_mirror() {
require_cmd tar
require_cmd sha256sum
require_cmd awk
local tmp_dir tmp_dir_q bundle sha_file download_name download_sha_name extract_dir
tmp_dir="$(mktemp -d)"
tmp_dir_q="$(printf '%q' "${tmp_dir}")"
trap "rm -rf ${tmp_dir_q}" EXIT
bundle="${tmp_dir}/${BUNDLE_NAME}"
extract_dir="${tmp_dir}/payload"
mkdir -p "${extract_dir}"
if [[ "${ENCRYPT_BUNDLE}" == "1" ]]; then
require_cmd openssl
need_bundle_key
download_name="${ENCRYPTED_BUNDLE_NAME}"
download_sha_name="${ENCRYPTED_SHA_NAME}"
sha_file="${tmp_dir}/${ENCRYPTED_SHA_NAME}"
else
download_name="${BUNDLE_NAME}"
download_sha_name="${SHA_NAME}"
sha_file="${tmp_dir}/${SHA_NAME}"
fi
log "downloading ${download_name} from ${HF_MIRROR_URL}/${HF_REPO_ID}"
download_file "$(repo_file_url "${download_name}")" "${tmp_dir}/${download_name}"
log "downloading ${download_sha_name}"
download_file "$(repo_file_url "${download_sha_name}")" "${sha_file}"
log "checking sha256"
(cd "${tmp_dir}" && sha256sum -c "${download_sha_name}")
if [[ "${ENCRYPT_BUNDLE}" == "1" ]]; then
log "decrypting ${download_name}"
openssl enc -d -aes-256-cbc -pbkdf2 -iter "${OPENSSL_ITER}" -md sha256 \
-pass env:WEB_BUNDLE_KEY \
-in "${tmp_dir}/${ENCRYPTED_BUNDLE_NAME}" \
-out "${bundle}"
fi
log "extracting bundle"
tar -xzf "${bundle}" -C "${extract_dir}"
install_payload "${extract_dir}"
update_shell_profile
start_mihomo
log "done. Run 'source ~/.bashrc' or open a new shell, then use 'prox' to enable shell/git proxy."
}
print_usage() {
cat <<EOF
Usage:
$(basename "$0") [install]
Environment:
HF_TOKEN Required only when HF_AUTH_REQUIRED=1.
WEB_BUNDLE_KEY Required for encrypted bundle decrypt.
HF_REPO_ID Default: ${HF_REPO_ID}
HF_MIRROR_URL Default: ${HF_MIRROR_URL}
HF_REVISION Default: ${HF_REVISION}
ENCRYPT_BUNDLE Default: ${ENCRYPT_BUNDLE}
HF_AUTH_REQUIRED
Default: ${HF_AUTH_REQUIRED}
EOF
}
case "${1:-install}" in
install)
install_from_mirror
;;
-h|--help|help)
print_usage
;;
*)
print_usage >&2
exit 2
;;
esac