Files
grok-keygen/scripts/setup_gmail_worker.sh
chaos 55f56d27c9 Add Gmail+Email Worker hybrid mail path and private runtime config
Prefer Cloudflare Worker KV for verification codes with optional short IMAP
fallback disabled for private deploy; track config.json/.env for private repo.
Also add worker project, protocol mint backoff, and fail-fast Turnstile/mail
timeouts from batch runs.
2026-07-11 23:21:23 +08:00

53 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Helper: create KV + print next steps for Email Worker
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT/cf-email-worker"
if ! command -v npx >/dev/null 2>&1; then
echo "需要 Node.js / npx。先安装: https://nodejs.org/"
exit 1
fi
echo "[1/4] wrangler login (如已登录可跳过)"
npx wrangler login || true
echo "[2/4] 创建 KV namespace GROK_MAIL_CODES"
OUT=$(npx wrangler kv namespace create GROK_MAIL_CODES 2>&1 || true)
echo "$OUT"
ID=$(echo "$OUT" | sed -n 's/.*id *= *"\([^"]*\)".*/\1/p' | head -1)
if [[ -z "${ID}" ]]; then
ID=$(echo "$OUT" | grep -oE '[a-f0-9]{32}' | head -1 || true)
fi
if [[ -n "${ID}" ]]; then
echo "检测到 KV id=$ID — 写入 wrangler.toml"
if grep -q 'REPLACE_WITH_KV_NAMESPACE_ID' wrangler.toml; then
sed -i "s/REPLACE_WITH_KV_NAMESPACE_ID/${ID}/g" wrangler.toml
fi
else
echo "未能自动解析 KV id,请手动填 wrangler.toml"
fi
echo "[3/4] 设置 CODE_API_KEY secret"
echo "将提示你输入密钥(与 config gmail_worker_api_key 一致)"
npx wrangler secret put CODE_API_KEY
if [[ -n "${GMAIL_FORWARD_TO:-}" ]]; then
echo "[3b] 设置 GMAIL_FORWARD_TO=$GMAIL_FORWARD_TO"
printf '%s' "$GMAIL_FORWARD_TO" | npx wrangler secret put GMAIL_FORWARD_TO
else
echo "[3b] 跳过 GMAIL_FORWARD_TO(可 export GMAIL_FORWARD_TO=you@gmail.com 后重跑)"
fi
echo "[4/4] deploy"
npx wrangler deploy
echo
echo "完成。接下来:"
echo " 1) Email Routing catch-all → 绑定本 Worker"
echo " 2) config.json 设置:"
echo " gmail_worker_enabled=true"
echo " gmail_worker_url=https://<worker>.workers.dev"
echo " gmail_worker_api_key=<你的 CODE_API_KEY>"
echo " 3) 测试: python scripts/test_gmail_worker.py --to test@yourdomain.com"