Files
crm-ai/scripts/rpc-tunnel.sh
T

76 lines
2.2 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
# 仅本地开发:测试服 JSON-RPC 端口外网被封时,在本机建 SSH 隧道。
# 测试/生产不需要此隧道:把 RPC URL 直接配成内网可达地址。
#
# 默认只启动隧道并打印 .env 需要的 4 行配置;
# 加 --apply 会自动更新 .env 里的 RPC URL 并重建 bridge。
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
APPLY=0
[[ "${1:-}" == "--apply" ]] && APPLY=1
NET=$(docker network ls --format '{{.Name}}' | grep -E '^crm-new.*_default$' | head -1 || true)
if [[ -z "$NET" ]]; then
echo "找不到 crm-new 的 compose 网络,请先启动 stack(docker compose up -d)" >&2
exit 1
fi
GW=$(docker network inspect "$NET" -f '{{range .IPAM.Config}}{{.Gateway}}{{end}}')
PORTS=(24503 24505 24506 24509)
ARGS=()
for p in "${PORTS[@]}"; do
if ss -ltn "sport = :$p" 2>/dev/null | grep -q "$GW"; then
echo "端口 $p 已在 $GW 监听,跳过"
else
ARGS+=("-L" "$GW:$p:127.0.0.1:$p")
fi
done
if [[ ${#ARGS[@]} -gt 0 ]]; then
ssh -F ~/.ssh/config -fN \
-o ExitOnForwardFailure=yes \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
"${ARGS[@]}" yw-dev
echo "隧道已建立:$GW -> yw-dev 127.0.0.1"
fi
cat <<EOF
bridge 需要以下配置(当前网关 $GW):
RECOMMEND_URL=jsonrpc://$GW:24503
ORDER_API_URL=jsonrpc://$GW:24505
MEMBER_RPC_URL=jsonrpc://$GW:24506
INVENTORY_API_URL=jsonrpc://$GW:24509
EOF
if [[ $APPLY -eq 1 ]]; then
python3 - "$GW" <<'PY'
import pathlib, sys
gw = sys.argv[1]
p = pathlib.Path('.env')
lines = p.read_text().split('\n')
mapping = {
'RECOMMEND_URL': f'jsonrpc://{gw}:24503',
'ORDER_API_URL': f'jsonrpc://{gw}:24505',
'MEMBER_RPC_URL': f'jsonrpc://{gw}:24506',
'INVENTORY_API_URL': f'jsonrpc://{gw}:24509',
}
for k, v in mapping.items():
for i, line in enumerate(lines):
if line.startswith(k + '='):
lines[i] = f'{k}={v}'
break
else:
lines.append(f'{k}={v}')
p.write_text('\n'.join(lines))
print('已更新 .env')
PY
docker compose up -d --force-recreate bridge
echo "已重建 bridge"
fi
echo
echo "注意:网络重建/机器重启后网关 IP 可能变化,重跑本脚本即可。"