#!/bin/bash
INST="friend-0f02c9c6"
TUN=$(curl -sS -X POST -H "Authorization: Bearer dev-admin-token" -H "Content-Type: application/json" \
  -d '{"instance_id":"'$INST'","ttl_seconds":600}' \
  "http://127.0.0.1:19088/api/v1/instances/$INST/tunnels")
TK=$(echo "$TUN" | python3 -c "import sys,json,re;u=json.load(sys.stdin).get('url','');m=re.search(r'fctun_[a-f0-9]+',u);print(m.group(0) if m else '')")
BASE="http://127.0.0.1:19088/panel/$INST"
echo "=== 当前插件配置 ==="
curl -sS -m 10 -H "Cookie: friend_cluster_tunnel=$TK" "$BASE/api/plugins/friend-cloud-connector/config" | head -c 400
echo
echo "=== PUT 回连配置（包裹格式）==="
curl -sS -m 10 -X PUT -H "Cookie: friend_cluster_tunnel=$TK" -H "Content-Type: application/json" \
  -d '{"config":{"host_ws_url":"ws://10.168.3.36:8081/connector/ws","reconnect_min_seconds":2,"reconnect_max_seconds":30}}' \
  "$BASE/api/plugins/friend-cloud-connector/config" | head -c 300
echo
sleep 15
echo "=== 8081 ESTAB ==="
ss -tn | grep ":8081" | grep ESTAB | head -3 || echo "(none)"
echo "=== run.log connector ==="
grep -E "friendbridge|connector online" /root/knowledge/run.log | tail -4
