| #!/usr/bin/env bash |
|
|
| set -euo pipefail |
|
|
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
|
|
| HF_REPO_TYPE="${HF_REPO_TYPE:-dataset}" |
| HF_REPO_NAME="${HF_REPO_NAME:-dsw-web-config}" |
| HF_PRIVATE="${HF_PRIVATE:-1}" |
| HF_REVISION="${HF_REVISION:-main}" |
| HF_DOWNLOAD_BASE_URL="${HF_DOWNLOAD_BASE_URL:-https://hf-mirror.com}" |
| BUNDLE_NAME="${BUNDLE_NAME:-web_mihomo_bundle.tar.gz}" |
| SHA_NAME="${BUNDLE_NAME}.sha256" |
| ENCRYPT_BUNDLE="${ENCRYPT_BUNDLE:-0}" |
| ENCRYPTED_BUNDLE_NAME="${ENCRYPTED_BUNDLE_NAME:-${BUNDLE_NAME}.enc}" |
| ENCRYPTED_SHA_NAME="${ENCRYPTED_BUNDLE_NAME}.sha256" |
| OPENSSL_ITER="${OPENSSL_ITER:-200000}" |
| MANIFEST_NAME="${MANIFEST_NAME:-manifest.json}" |
| REMOTE_SCRIPT_NAME="${REMOTE_SCRIPT_NAME:-process_web_hf.sh}" |
| DEPLOY_SCRIPT_NAME="${DEPLOY_SCRIPT_NAME:-deploy_web_hf_mirror.sh}" |
| 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; export it before running this script" |
| } |
|
|
| need_bundle_key() { |
| [[ -n "${WEB_BUNDLE_KEY:-}" ]] || die "WEB_BUNDLE_KEY is required to encrypt the public 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 |
| } |
|
|
| repo_url() { |
| local repo_id="$1" |
| printf 'https://huggingface.co/%s%s' "$(repo_prefix)" "${repo_id}" |
| } |
|
|
| download_repo_url() { |
| local repo_id="$1" |
| printf '%s/%s%s' "${HF_DOWNLOAD_BASE_URL%/}" "$(repo_prefix)" "${repo_id}" |
| } |
|
|
| resolve_repo_id_with_hub() { |
| if [[ -n "${HF_REPO_ID:-}" ]]; then |
| printf '%s\n' "${HF_REPO_ID}" |
| return |
| fi |
|
|
| need_token |
| python3 - "${HF_REPO_NAME}" <<'PY' |
| import os |
| import sys |
| from huggingface_hub import HfApi |
|
|
| repo_name = sys.argv[1] |
| info = HfApi(token=os.environ["HF_TOKEN"]).whoami() |
| owner = info.get("name") |
| if not owner: |
| raise SystemExit("could not determine Hugging Face username from token; set HF_REPO_ID") |
| print(f"{owner}/{repo_name}") |
| PY |
| } |
|
|
| resolve_repo_id_with_api() { |
| if [[ -n "${HF_REPO_ID:-}" ]]; then |
| printf '%s\n' "${HF_REPO_ID}" |
| return |
| fi |
|
|
| need_token |
| require_cmd python3 |
| python3 - "${HF_REPO_NAME}" <<'PY' |
| import json |
| import os |
| import sys |
| import urllib.request |
|
|
| repo_name = sys.argv[1] |
| req = urllib.request.Request( |
| "https://huggingface.co/api/whoami-v2", |
| headers={"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}, |
| ) |
| with urllib.request.urlopen(req, timeout=30) as resp: |
| info = json.load(resp) |
| owner = info.get("name") |
| if not owner: |
| raise SystemExit("could not determine Hugging Face username from token; set HF_REPO_ID") |
| print(f"{owner}/{repo_name}") |
| PY |
| } |
|
|
| have_huggingface_hub() { |
| require_cmd python3 |
| if python3 - <<'PY' >/dev/null 2>&1 |
| import huggingface_hub |
| PY |
| then |
| return 0 |
| fi |
|
|
| return 1 |
| } |
|
|
| ensure_huggingface_hub() { |
| if have_huggingface_hub; then |
| return 0 |
| fi |
|
|
| if [[ "${HF_SKIP_DEPENDENCY_INSTALL:-0}" == "1" ]]; then |
| return 1 |
| fi |
|
|
| if ! python3 -m pip --version >/dev/null 2>&1; then |
| return 1 |
| fi |
|
|
| log "installing python package: huggingface_hub" |
| python3 -m pip install --user -q huggingface_hub |
| have_huggingface_hub |
| } |
|
|
| validate_local_payload() { |
| [[ -x "${SCRIPT_DIR}/mihomo" ]] || die "missing executable: ${SCRIPT_DIR}/mihomo" |
| [[ -d "${SCRIPT_DIR}/etc/mihomo" ]] || die "missing directory: ${SCRIPT_DIR}/etc/mihomo" |
| [[ -f "${SCRIPT_DIR}/etc/mihomo/config.yaml" ]] || die "missing file: ${SCRIPT_DIR}/etc/mihomo/config.yaml" |
| } |
|
|
| make_bundle() { |
| local out_dir="$1" |
| local bundle="${out_dir}/${BUNDLE_NAME}" |
| local sha_file="${out_dir}/${SHA_NAME}" |
| local manifest="${out_dir}/${MANIFEST_NAME}" |
| local bundle_sha |
|
|
| validate_local_payload |
| require_cmd tar |
| require_cmd sha256sum |
|
|
| tar -C "${SCRIPT_DIR}" -czf "${bundle}" mihomo etc/mihomo |
| bundle_sha="$(sha256sum "${bundle}" | awk '{print $1}')" |
| printf '%s %s\n' "${bundle_sha}" "${BUNDLE_NAME}" > "${sha_file}" |
|
|
| cat > "${manifest}" <<EOF |
| { |
| "created_at_utc": "$(date -u +%Y-%m-%dT%H:%M:%SZ)", |
| "bundle": "${BUNDLE_NAME}", |
| "sha256": "${bundle_sha}", |
| "repo_type": "${HF_REPO_TYPE}", |
| "files": [ |
| "mihomo", |
| "etc/mihomo" |
| ] |
| } |
| EOF |
|
|
| printf '%s\n' "${bundle}" |
| } |
|
|
| encrypt_bundle() { |
| local bundle="$1" |
| local out_dir="$2" |
| local encrypted_bundle="${out_dir}/${ENCRYPTED_BUNDLE_NAME}" |
| local encrypted_sha_file="${out_dir}/${ENCRYPTED_SHA_NAME}" |
|
|
| require_cmd openssl |
| require_cmd sha256sum |
| need_bundle_key |
|
|
| openssl enc -aes-256-cbc -salt -pbkdf2 -iter "${OPENSSL_ITER}" -md sha256 \ |
| -pass env:WEB_BUNDLE_KEY \ |
| -in "${bundle}" \ |
| -out "${encrypted_bundle}" |
| sha256sum "${encrypted_bundle}" | awk -v name="${ENCRYPTED_BUNDLE_NAME}" '{ print $1 " " name }' > "${encrypted_sha_file}" |
| printf '%s\n' "${encrypted_bundle}" |
| } |
|
|
| print_remote_install_command() { |
| local repo_id="$1" |
|
|
| cat <<EOF |
| |
| Run this on the new cluster: |
| |
| export HF_REPO_ID=${repo_id} |
| WEB_BUNDLE_KEY=<your_web_bundle_key> wget -O /tmp/${DEPLOY_SCRIPT_NAME} "$(download_repo_url "${repo_id}")/resolve/${HF_REVISION}/${DEPLOY_SCRIPT_NAME}" && WEB_BUNDLE_KEY=<your_web_bundle_key> bash /tmp/${DEPLOY_SCRIPT_NAME} install |
| |
| EOF |
| } |
|
|
| publish_bundle_with_hub() { |
| local repo_id="$1" |
| local bundle="$2" |
| local sha_file="$3" |
| local manifest="$4" |
| local script_path="$5" |
| local deploy_script_path="$6" |
|
|
| log "publishing through huggingface_hub: ${HF_REPO_TYPE} repo ${repo_id}" |
| python3 - "${repo_id}" "${HF_REPO_TYPE}" "${bundle}" "${sha_file}" "${manifest}" "${script_path}" "${REMOTE_SCRIPT_NAME}" "${deploy_script_path}" "${DEPLOY_SCRIPT_NAME}" <<'PY' |
| import os |
| import sys |
| from huggingface_hub import HfApi |
|
|
| repo_id, repo_type, bundle, sha_file, manifest, script_path, remote_script_name, deploy_script_path, deploy_script_name = sys.argv[1:] |
| token = os.environ["HF_TOKEN"] |
| private = os.environ.get("HF_PRIVATE", "1").lower() not in {"0", "false", "no"} |
| api = HfApi(token=token) |
| api.create_repo(repo_id=repo_id, repo_type=repo_type, private=private, exist_ok=True) |
| uploads = [ |
| (bundle, os.path.basename(bundle)), |
| (sha_file, os.path.basename(sha_file)), |
| (manifest, os.path.basename(manifest)), |
| (script_path, remote_script_name), |
| (deploy_script_path, deploy_script_name), |
| ] |
| for local_path, path_in_repo in uploads: |
| api.upload_file( |
| path_or_fileobj=local_path, |
| path_in_repo=path_in_repo, |
| repo_id=repo_id, |
| repo_type=repo_type, |
| token=token, |
| ) |
| PY |
| } |
|
|
| create_repo_with_api() { |
| local repo_id="$1" |
|
|
| need_token |
| require_cmd python3 |
| python3 - "${repo_id}" "${HF_REPO_TYPE}" <<'PY' |
| import json |
| import os |
| import sys |
| import urllib.error |
| import urllib.request |
|
|
| repo_id, repo_type = sys.argv[1:] |
| token = os.environ["HF_TOKEN"] |
|
|
| whoami_req = urllib.request.Request( |
| "https://huggingface.co/api/whoami-v2", |
| headers={"Authorization": f"Bearer {token}"}, |
| ) |
| with urllib.request.urlopen(whoami_req, timeout=30) as resp: |
| username = json.load(resp).get("name") |
|
|
| if "/" in repo_id: |
| owner, name = repo_id.split("/", 1) |
| else: |
| owner, name = None, repo_id |
|
|
| private = os.environ.get("HF_PRIVATE", "1").lower() not in {"0", "false", "no"} |
| payload = {"type": repo_type, "name": name, "private": private} |
| if owner and owner != username: |
| payload["organization"] = owner |
|
|
| req = urllib.request.Request( |
| "https://huggingface.co/api/repos/create", |
| data=json.dumps(payload).encode("utf-8"), |
| headers={ |
| "Authorization": f"Bearer {token}", |
| "Content-Type": "application/json", |
| }, |
| method="POST", |
| ) |
| try: |
| with urllib.request.urlopen(req, timeout=60) as resp: |
| resp.read() |
| except urllib.error.HTTPError as exc: |
| body = exc.read().decode("utf-8", errors="replace") |
| if exc.code in {400, 409} and ("exist" in body.lower() or "already created" in body.lower()): |
| pass |
| else: |
| raise SystemExit(f"create repo failed: HTTP {exc.code}: {body}") |
| PY |
| } |
|
|
| publish_bundle_with_git() { |
| local repo_id="$1" |
| local bundle="$2" |
| local sha_file="$3" |
| local manifest="$4" |
| local script_path="$5" |
| local deploy_script_path="$6" |
| local tmp_git repo_dir repo_http_url askpass upload_bundle_name upload_sha_name |
|
|
| require_cmd git |
| create_repo_with_api "${repo_id}" |
|
|
| tmp_git="$(mktemp -d)" |
| cleanup_git() { |
| rm -rf "${tmp_git}" |
| } |
| trap cleanup_git RETURN |
|
|
| repo_dir="${tmp_git}/repo" |
| repo_http_url="$(repo_url "${repo_id}")" |
| upload_bundle_name="$(basename "${bundle}")" |
| upload_sha_name="$(basename "${sha_file}")" |
| askpass="${tmp_git}/git-askpass.sh" |
| cat > "${askpass}" <<'EOF' |
| |
| case "$1" in |
| *Username*) printf '%s\n' "hf_user" ;; |
| *Password*) printf '%s\n' "${HF_TOKEN}" ;; |
| *) printf '%s\n' "${HF_TOKEN}" ;; |
| esac |
| EOF |
| chmod 700 "${askpass}" |
|
|
| log "publishing through git: private ${HF_REPO_TYPE} repo ${repo_id}" |
| GIT_ASKPASS="${askpass}" GIT_TERMINAL_PROMPT=0 git clone "${repo_http_url}" "${repo_dir}" >/dev/null 2>&1 || { |
| rm -rf "${repo_dir}" |
| mkdir -p "${repo_dir}" |
| git -C "${repo_dir}" init >/dev/null |
| git -C "${repo_dir}" remote add origin "${repo_http_url}" |
| git -C "${repo_dir}" checkout -b main >/dev/null |
| } |
|
|
| cp "${bundle}" "${repo_dir}/${upload_bundle_name}" |
| cp "${sha_file}" "${repo_dir}/${upload_sha_name}" |
| cp "${manifest}" "${repo_dir}/${MANIFEST_NAME}" |
| cp "${script_path}" "${repo_dir}/${REMOTE_SCRIPT_NAME}" |
| cp "${deploy_script_path}" "${repo_dir}/${DEPLOY_SCRIPT_NAME}" |
|
|
| if command -v git-lfs >/dev/null 2>&1; then |
| git -C "${repo_dir}" lfs install --local >/dev/null |
| git -C "${repo_dir}" lfs track "${upload_bundle_name}" >/dev/null |
| fi |
|
|
| git -C "${repo_dir}" config user.email "${HF_GIT_EMAIL:-web-config-uploader@local}" |
| git -C "${repo_dir}" config user.name "${HF_GIT_NAME:-web-config-uploader}" |
| git -C "${repo_dir}" add "${upload_bundle_name}" "${upload_sha_name}" "${MANIFEST_NAME}" "${REMOTE_SCRIPT_NAME}" "${DEPLOY_SCRIPT_NAME}" |
| if [[ -f "${repo_dir}/.gitattributes" ]]; then |
| git -C "${repo_dir}" add .gitattributes |
| fi |
| if git -C "${repo_dir}" diff --cached --quiet; then |
| log "repo already has the current files" |
| else |
| git -C "${repo_dir}" commit -m "Upload web mihomo bundle" >/dev/null |
| GIT_ASKPASS="${askpass}" GIT_TERMINAL_PROMPT=0 git -C "${repo_dir}" push origin HEAD:main >/dev/null |
| fi |
| } |
|
|
| publish_bundle() { |
| need_token |
|
|
| local tmp_dir tmp_dir_q repo_id bundle sha_file manifest script_path deploy_script_path upload_bundle upload_sha_file |
| tmp_dir="$(mktemp -d)" |
| tmp_dir_q="$(printf '%q' "${tmp_dir}")" |
| trap "rm -rf ${tmp_dir_q}" EXIT |
|
|
| bundle="$(make_bundle "${tmp_dir}")" |
| sha_file="${tmp_dir}/${SHA_NAME}" |
| manifest="${tmp_dir}/${MANIFEST_NAME}" |
| upload_bundle="${bundle}" |
| upload_sha_file="${sha_file}" |
| if [[ "${ENCRYPT_BUNDLE}" == "1" ]]; then |
| upload_bundle="$(encrypt_bundle "${bundle}" "${tmp_dir}")" |
| upload_sha_file="${tmp_dir}/${ENCRYPTED_SHA_NAME}" |
| fi |
| repo_id="$(resolve_repo_id_with_api)" |
| script_path="${SCRIPT_DIR}/$(basename "$0")" |
| deploy_script_path="${SCRIPT_DIR}/${DEPLOY_SCRIPT_NAME}" |
| [[ -f "${deploy_script_path}" ]] || die "missing deploy script: ${deploy_script_path}" |
|
|
| if ensure_huggingface_hub; then |
| publish_bundle_with_hub "${repo_id}" "${upload_bundle}" "${upload_sha_file}" "${manifest}" "${script_path}" "${deploy_script_path}" |
| else |
| log "huggingface_hub is unavailable; falling back to git upload" |
| publish_bundle_with_git "${repo_id}" "${upload_bundle}" "${upload_sha_file}" "${manifest}" "${script_path}" "${deploy_script_path}" |
| fi |
|
|
| log "uploaded bundle sha256: $(awk '{print $1}' "${upload_sha_file}")" |
| log "repo: $(repo_url "${repo_id}")" |
| log "deploy mirror: $(download_repo_url "${repo_id}")" |
| print_remote_install_command "${repo_id}" |
| } |
|
|
| 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}" |
| } |
|
|
| download_with_hf_token() { |
| local url="$1" |
| local out="$2" |
| local cfg headers status location origin |
|
|
| need_token |
| require_cmd curl |
|
|
| 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="$(python3 - "$url" <<'PY' |
| import sys |
| from urllib.parse import urlsplit |
| |
| parts = urlsplit(sys.argv[1]) |
| print(f"{parts.scheme}://{parts.netloc}") |
| PY |
| )" |
| 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_and_install() { |
| need_token |
| require_cmd tar |
| require_cmd sha256sum |
|
|
| local repo_id base_url tmp_dir bundle sha_file extract_dir |
| repo_id="$(resolve_repo_id_with_api)" |
| base_url="$(download_repo_url "${repo_id}")/resolve/${HF_REVISION}" |
| tmp_dir="$(mktemp -d)" |
| cleanup() { |
| rm -rf "${tmp_dir}" |
| } |
| trap cleanup EXIT |
|
|
| bundle="${tmp_dir}/${BUNDLE_NAME}" |
| sha_file="${tmp_dir}/${SHA_NAME}" |
| extract_dir="${tmp_dir}/payload" |
| mkdir -p "${extract_dir}" |
|
|
| log "downloading ${BUNDLE_NAME} from $(download_repo_url "${repo_id}")" |
| download_with_hf_token "${base_url}/${BUNDLE_NAME}" "${bundle}" |
| download_with_hf_token "${base_url}/${SHA_NAME}" "${sha_file}" |
|
|
| (cd "${tmp_dir}" && sha256sum -c "${SHA_NAME}") |
| 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." |
| } |
|
|
| local_install() { |
| validate_local_payload |
| install_payload "${SCRIPT_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") publish Package local mihomo files and upload them to a private Hugging Face repo. |
| $(basename "$0") publish-public-encrypted |
| Package local mihomo files, encrypt them, and upload to a public Hugging Face repo. |
| $(basename "$0") install Download the bundle from Hugging Face and install it on this machine. |
| $(basename "$0") local Install from files already present in this directory. |
| $(basename "$0") package DIR Create a local bundle in DIR without uploading. |
| |
| Environment: |
| HF_TOKEN Required for publish/install. |
| HF_REPO_ID Optional. Example: yourname/dsw-web-config |
| HF_REPO_NAME Default: ${HF_REPO_NAME} |
| HF_REPO_TYPE Default: ${HF_REPO_TYPE} |
| HF_PRIVATE Default: ${HF_PRIVATE} |
| HF_REVISION Default: ${HF_REVISION} |
| HF_DOWNLOAD_BASE_URL |
| Default: ${HF_DOWNLOAD_BASE_URL} |
| WEB_BUNDLE_KEY Required encryption key for publish-public-encrypted. |
| |
| Examples: |
| export HF_TOKEN=<your_huggingface_token> |
| bash $(basename "$0") publish |
| |
| export HF_TOKEN=<your_huggingface_token> |
| export HF_REPO_ID=yourname/dsw-web-config |
| bash $(basename "$0") install |
| EOF |
| } |
|
|
| main() { |
| local cmd="${1:-install}" |
| case "${cmd}" in |
| publish|upload) |
| publish_bundle |
| ;; |
| publish-public-encrypted|publish_encrypted_public|publish-encrypted-public) |
| export HF_PRIVATE=0 |
| export ENCRYPT_BUNDLE=1 |
| if [[ -z "${HF_REPO_ID:-}" ]]; then |
| HF_REPO_NAME="${HF_PUBLIC_REPO_NAME:-dsw-web-config-encrypted}" |
| fi |
| publish_bundle |
| ;; |
| install) |
| download_and_install |
| ;; |
| local|install-local) |
| local_install |
| ;; |
| package) |
| local out_dir="${2:-}" |
| [[ -n "${out_dir}" ]] || die "package requires an output directory" |
| mkdir -p "${out_dir}" |
| make_bundle "${out_dir}" >/dev/null |
| log "created ${out_dir}/${BUNDLE_NAME}" |
| log "created ${out_dir}/${SHA_NAME}" |
| log "created ${out_dir}/${MANIFEST_NAME}" |
| ;; |
| -h|--help|help) |
| print_usage |
| ;; |
| *) |
| print_usage >&2 |
| exit 2 |
| ;; |
| esac |
| } |
|
|
| main "$@" |
|
|