Files
karuo-ai/01_卡资(金)/金仓_存储备份/Codex版本锁定/脚本/codex_block_updates.sh
2026-07-19 00:21:46 +08:00

106 lines
3.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Codex 禁止更新CLI 配置 + Sparkle 关检查 + 文件锁 + hosts 阻断 CDN
# 用法: bash codex_block_updates.sh [on|off] 默认 on
set -euo pipefail
MODE="${1:-on}"
CONFIG="${HOME}/.codex/config.toml"
APP="/Applications/Codex.app"
CLI="${APP}/Contents/Resources/codex"
HOSTS_MARKER="# karuo-codex-no-update"
HOSTS_FILE="/etc/hosts"
HOSTS_BLOCK=(
"0.0.0.0 persistent.oaistatic.com"
"0.0.0.0 downloads.openai.com"
)
ensure_config_off() {
if [[ ! -f "${CONFIG}" ]]; then
mkdir -p "$(dirname "${CONFIG}")"
printf 'check_for_update_on_startup = false\n' > "${CONFIG}"
return
fi
if grep -q '^check_for_update_on_startup' "${CONFIG}"; then
sed -i '' 's/^check_for_update_on_startup.*/check_for_update_on_startup = false/' "${CONFIG}"
else
printf 'check_for_update_on_startup = false\n\n' | cat - "${CONFIG}" > "${CONFIG}.tmp" && mv "${CONFIG}.tmp" "${CONFIG}"
fi
}
sparkle_off() {
defaults write com.openai.codex SUEnableAutomaticChecks -bool false
defaults write com.openai.codex SUAutomaticallyUpdate -bool false
}
sparkle_on() {
defaults write com.openai.codex SUEnableAutomaticChecks -bool true
}
lock_app() {
[[ -d "${APP}" ]] || { echo "未找到 ${APP}" >&2; exit 1; }
chflags -R uchg "${APP}"
if [[ -f "${CLI}" ]]; then
chflags uchg "${CLI}" 2>/dev/null || true
fi
if [[ -f /usr/local/bin/codex ]] && [[ ! -L /usr/local/bin/codex ]]; then
chflags uchg /usr/local/bin/codex 2>/dev/null || true
fi
}
unlock_app() {
chflags -R nouchg "${APP}" 2>/dev/null || true
chflags nouchg /usr/local/bin/codex 2>/dev/null || true
}
hosts_block() {
if grep -q "${HOSTS_MARKER}" "${HOSTS_FILE}" 2>/dev/null; then
echo "hosts 阻断已存在"
return
fi
local tmp
tmp="$(mktemp)"
{
echo ""
echo "${HOSTS_MARKER}"
for line in "${HOSTS_BLOCK[@]}"; do echo "${line}"; done
} | sudo tee -a "${HOSTS_FILE}" >/dev/null
echo "已写入 /etc/hosts 阻断 Codex 更新 CDN"
}
hosts_unblock() {
if ! grep -q "${HOSTS_MARKER}" "${HOSTS_FILE}" 2>/dev/null; then
echo "hosts 无 Codex 阻断条目"
return
fi
sudo sed -i '' "/${HOSTS_MARKER}/,/^0\.0\.0\.0 downloads\.openai\.com/d" "${HOSTS_FILE}"
echo "已移除 hosts 阻断"
}
case "${MODE}" in
on)
ensure_config_off
sparkle_off
lock_app
hosts_block || echo "⚠️ hosts 阻断需 sudo可稍后手动: sudo bash $0 on"
echo "✅ Codex 禁止更新已启用"
;;
off)
unlock_app
sparkle_on
hosts_unblock || true
sed -i '' 's/^check_for_update_on_startup.*/check_for_update_on_startup = true/' "${CONFIG}" 2>/dev/null || true
echo "✅ Codex 禁止更新已关闭(可正常升级)"
;;
*)
echo "用法: $0 [on|off]" >&2
exit 1
;;
esac
echo "--- 状态 ---"
grep '^check_for_update_on_startup' "${CONFIG}" 2>/dev/null || true
defaults read com.openai.codex SUEnableAutomaticChecks SUAutomaticallyUpdate 2>/dev/null || true
ls -lO "${APP}" 2>/dev/null | head -2 || true
grep -A3 "${HOSTS_MARKER}" "${HOSTS_FILE}" 2>/dev/null || echo "hosts: 未阻断"
codex --version 2>/dev/null || true