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

50 lines
1.8 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 断连修复Clash TUN + fake-ip 导致 chatgpt.com SSL 握手失败
# 现象stream disconnected / error sending request (chatgpt.com/backend-api/codex/responses)
# 用法bash codex_clash_dns_fix.sh
set -euo pipefail
API_PORT=""
for port in 9097 9090; do
if curl -sf -m 2 "http://127.0.0.1:${port}/version" >/dev/null 2>&1; then
API_PORT="$port"
break
fi
done
if [[ -z "$API_PORT" ]]; then
echo "❌ Mihomo API 不可用Clash Verge 未运行?)" >&2
exit 1
fi
BASE="http://127.0.0.1:${API_PORT}"
MIXED_PORT="${CLASH_MIXED_PORT:-7897}"
echo "== Codex Clash DNS 修复 (API :${API_PORT}) =="
curl -sf -m 8 -X PATCH "${BASE}/configs" \
-H "Content-Type: application/json" \
-d '{"tun":{"enable":false},"mode":"rule"}' >/dev/null
echo "▸ TUN 已关闭"
curl -sf -m 8 -X PATCH "${BASE}/configs" \
-H "Content-Type: application/json" \
-d '{"dns":{"enable":true,"enhanced-mode":"fake-ip","fake-ip-filter":["+.chatgpt.com","+.openai.com","+.oaistatic.com","+.oaiusercontent.com","+.local","+.lan"]}}' >/dev/null
echo "▸ chatgpt/openai 已加入 fake-ip-filter使用真实 DNS"
CHATGPT_IP="$(dig +short chatgpt.com A 2>/dev/null | head -1 || true)"
if [[ "$CHATGPT_IP" == 198.18.* ]]; then
echo "⚠️ chatgpt.com 仍解析为 fake-ip ${CHATGPT_IP},请重启 Clash Verge 后再跑本脚本" >&2
fi
code="$(curl -sS -m 12 -x "http://127.0.0.1:${MIXED_PORT}" -o /dev/null -w '%{http_code}' https://chatgpt.com/ 2>/dev/null || echo 000)"
echo "▸ chatgpt.com 经代理 HTTP ${code}"
if [[ "$code" =~ ^(403|401|200|301|302)$ ]]; then
echo "✅ 连通正常。请 Cmd+Q 重启 Codex模型选 GPT-5.4,长会话失败则新建对话。"
exit 0
fi
echo "❌ chatgpt.com 仍不可达 (HTTP ${code}),检查 Clash 节点或系统代理" >&2
exit 2