#!/bin/bash
# Dify服务器网络诊断脚本
# 请在Dify服务器(47.112.29.0)上运行此脚本

echo "=== Dify服务器网络诊断 ==="
echo ""

# 1. DNS解析测试
echo "1. DNS解析测试："
echo "   hl.ireborn.com.cn: $(nslookup hl.ireborn.com.cn 2>/dev/null | grep Address | tail -1 || echo '解析失败')"
echo "   yy.ireborn.com.cn: $(nslookup yy.ireborn.com.cn 2>/dev/null | grep Address | tail -1 || echo '解析失败')"
echo ""

# 2. 网络连通性测试
echo "2. 网络连通性测试 (ping)："
ping -c 3 120.79.247.16 2>&1 | tail -3
echo ""

# 3. 端口连通性测试
echo "3. 端口连通性测试："
echo "   HTTP (80): $(timeout 5 bash -c 'echo >/dev/tcp/120.79.247.16/80' 2>&1 && echo '可连接' || echo '不可连接')"
echo "   HTTPS (443): $(timeout 5 bash -c 'echo >/dev/tcp/120.79.247.16/443' 2>&1 && echo '可连接' || echo '不可连接')"
echo "   API (8000): $(timeout 5 bash -c 'echo >/dev/tcp/120.79.247.16/8000' 2>&1 && echo '可连接' || echo '不可连接')"
echo ""

# 4. HTTPS请求测试
echo "4. HTTPS请求测试："
echo "   使用域名："
curl -s -o /dev/null -w "HTTP状态码: %{http_code}, 连接时间: %{time_connect}s\n" \
  --connect-timeout 10 \
  https://hl.ireborn.com.cn/health 2>&1 || echo "   请求失败"

echo "   使用IP地址："
curl -s -o /dev/null -w "HTTP状态码: %{http_code}, 连接时间: %{time_connect}s\n" \
  --connect-timeout 10 \
  -H "Host: hl.ireborn.com.cn" \
  https://120.79.247.16/health 2>&1 || echo "   请求失败"
echo ""

# 5. SQL执行器API测试
echo "5. SQL执行器API测试："
curl -s -X POST https://hl.ireborn.com.cn/api/v1/sql/execute-simple \
  -H "Content-Type: application/json" \
  -H "X-API-Key: dify-2025-kaopeilian" \
  -d '{"sql":"SELECT 1 as test"}' \
  --connect-timeout 10 2>&1 | head -1 || echo "   请求失败"
echo ""

echo "=== 诊断完成 ==="
echo ""
echo "如果以上测试有失败项，请检查："
echo "1. 阿里云安全组是否允许来自47.112.29.0的入站流量"
echo "2. 服务器防火墙规则"
echo "3. VPC网络配置"





