#!/bin/bash # 查 api.cloudflare.com 到底该用哪个 IP。 # # 现在钉的 104.16.132.229 返回的是 Cloudflare 错误页(error 1034), # 说明这个 IP 不对 —— 我当初是随便挑的一个 Cloudflare 网段地址。 set -u echo "===== 1. 当前响应内容到底是什么 =====" curl -4 -s -D /tmp/h.txt -o /tmp/b.txt https://api.cloudflare.com/client/v4/user 2>/dev/null echo " 响应头前几行:" head -8 /tmp/h.txt | sed 's/^/ /' echo echo " 响应体前 300 字节:" head -c 300 /tmp/b.txt | sed 's/^/ /' echo echo echo "===== 2. 用公共 DNS 查真实 IP(绕开本地解析)=====" for dns in 1.1.1.1 8.8.8.8 223.5.5.5; do echo " --- 经 $dns ---" timeout 8 getent ahostsv4 api.cloudflare.com >/dev/null 2>&1 # 用 python 发 DNS 查询(设备上不一定有 dig/nslookup) python3 - "$dns" <<'PY' 2>/dev/null || echo " (查不了)" import socket, struct, sys dns = sys.argv[1] name = "api.cloudflare.com" # 手搓一个 DNS A 查询 tid = 0x1234 flags = 0x0100 q = struct.pack(">HHHHHH", tid, flags, 1, 0, 0, 0) for part in name.split("."): q += bytes([len(part)]) + part.encode() q += b"\x00" + struct.pack(">HH", 1, 1) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(5) try: s.sendto(q, (dns, 53)) data, _ = s.recvfrom(2048) except Exception as exc: print(f" 查询失败: {exc}") raise SystemExit # 跳过 header + question idx = 12 while data[idx] != 0: idx += data[idx] + 1 idx += 5 ancount = struct.unpack(">H", data[6:8])[0] found = [] for _ in range(ancount): if data[idx] & 0xC0: idx += 2 else: while data[idx] != 0: idx += data[idx] + 1 idx += 1 rtype, _cls, _ttl, rdlen = struct.unpack(">HHIH", data[idx:idx+10]) idx += 10 if rtype == 1 and rdlen == 4: found.append(".".join(str(b) for b in data[idx:idx+4])) idx += rdlen print(" " + (", ".join(found) if found else "没有 A 记录")) PY done echo echo "===== 3. 挨个试候选 IP,看哪个返回正常的 API JSON =====" for ip in 104.16.132.229 104.16.133.229 104.17.0.1 104.18.0.1 172.64.0.1 162.159.0.1; do OUT=$(timeout 12 curl -4 -s -w '\n%{http_code}' \ --resolve "api.cloudflare.com:443:$ip" \ https://api.cloudflare.com/client/v4/user 2>/dev/null) CODE=$(echo "$OUT" | tail -1) FIRST=$(echo "$OUT" | head -c 90 | tr -d '\n') printf " %-16s HTTP %-4s %s\n" "$ip" "${CODE:-超时}" "$FIRST" done echo echo "===== 4. 去掉 hosts 钉的 IP,用系统解析试试 =====" # 临时备份并移除 cp /etc/hosts /tmp/hosts.bak grep -v 'api.cloudflare.com' /etc/hosts > /tmp/hosts.new cat /tmp/hosts.new > /etc/hosts echo " 现在的解析: $(getent ahostsv4 api.cloudflare.com | head -1 | awk '{print $1}')" OUT=$(timeout 12 curl -4 -s -w '\n%{http_code}' https://api.cloudflare.com/client/v4/user 2>/dev/null) echo " HTTP $(echo "$OUT" | tail -1) $(echo "$OUT" | head -c 90)" echo echo " 把 hosts 恢复回去(钉 IP 还是需要的,不然全乱)" cp /tmp/hosts.bak /etc/hosts getent ahostsv4 api.cloudflare.com | head -1 | sed 's/^/ /'