File size: 7,104 Bytes
3718c84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
#!/bin/bash
# Script test ngrok tunnel và kiểm tra PostgreSQL connection
# Usage: ./test_ngrok.sh [ngrok_host] [ngrok_port]
set -e
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'
log() {
echo -e "${BLUE}[TEST]${NC} $1"
}
success() {
echo -e "${GREEN}✅${NC} $1"
}
warn() {
echo -e "${YELLOW}⚠️${NC} $1"
}
error() {
echo -e "${RED}❌${NC} $1"
}
# Default values
NGROK_API="http://192.168.1.212:4040"
DB_USER="${POSTGRES_USER:-hue_remote}"
DB_PASSWORD="${POSTGRES_PASSWORD:-huepass123}"
DB_NAME="${POSTGRES_DB:-hue_portal}"
echo -e "${BLUE}============================================================${NC}"
echo -e "${BLUE}Test Ngrok Tunnel và PostgreSQL Connection${NC}"
echo -e "${BLUE}============================================================${NC}"
echo ""
# Step 1: Check ngrok API
log "Bước 1: Kiểm tra ngrok API ($NGROK_API)..."
if command -v curl &> /dev/null; then
if curl -s "$NGROK_API/api/tunnels" &> /dev/null; then
success "Ngrok API đang chạy"
else
error "Không thể kết nối ngrok API"
echo ""
echo "💡 Kiểm tra:"
echo " 1. Ngrok container có đang chạy không?"
echo " 2. Port 4040 có được expose không?"
echo " 3. Thử truy cập: http://192.168.1.212:4040"
exit 1
fi
else
warn "Không có curl, bỏ qua kiểm tra API"
fi
# Step 2: Get ngrok tunnel URL
log "Bước 2: Lấy ngrok tunnel URL..."
if command -v curl &> /dev/null && command -v python3 &> /dev/null; then
response=$(curl -s "$NGROK_API/api/tunnels" 2>/dev/null || echo "")
if [ -n "$response" ]; then
ngrok_url=$(echo "$response" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
tunnels = data.get('tunnels', [])
if not tunnels:
print('NO_TUNNELS')
sys.exit(0)
for tunnel in tunnels:
if tunnel.get('proto') == 'tcp':
public_url = tunnel.get('public_url', '')
if public_url.startswith('tcp://'):
url = public_url.replace('tcp://', '')
print(url)
sys.exit(0)
print('NO_TCP_TUNNEL')
except Exception as e:
print('ERROR: ' + str(e))
" 2>/dev/null)
if [ "$ngrok_url" = "NO_TUNNELS" ]; then
error "Không tìm thấy tunnel nào!"
echo ""
echo "💡 Kiểm tra:"
echo " 1. Ngrok có đang chạy command 'tcp' không?"
echo " 2. Toggle switch có được BẬT (ON) không?"
echo " 3. Xem logs: docker logs ngrok"
exit 1
elif [ "$ngrok_url" = "NO_TCP_TUNNEL" ]; then
error "Không tìm thấy TCP tunnel!"
echo ""
echo "💡 Ngrok có thể đang chạy HTTP tunnel, cần TCP tunnel cho PostgreSQL"
exit 1
elif [[ "$ngrok_url" == ERROR* ]]; then
error "Lỗi parse JSON: $ngrok_url"
exit 1
else
ngrok_host=$(echo "$ngrok_url" | cut -d':' -f1)
ngrok_port=$(echo "$ngrok_url" | cut -d':' -f2)
success "Tìm thấy ngrok URL: tcp://$ngrok_host:$ngrok_port"
fi
else
error "Không thể lấy thông tin từ ngrok API"
exit 1
fi
else
# Manual input
if [ -n "$1" ] && [ -n "$2" ]; then
ngrok_host="$1"
ngrok_port="$2"
warn "Dùng ngrok URL từ tham số: tcp://$ngrok_host:$ngrok_port"
else
error "Cần cung cấp ngrok host và port"
echo ""
echo "Usage: $0 [ngrok_host] [ngrok_port]"
echo "Ví dụ: $0 0.tcp.ap.ngrok.io 13432"
exit 1
fi
fi
echo ""
log "Thông tin kết nối:"
echo " Ngrok Host: $ngrok_host"
echo " Ngrok Port: $ngrok_port"
echo " Database User: $DB_USER"
echo " Database Name: $DB_NAME"
echo ""
# Step 3: Test PostgreSQL connection
log "Bước 3: Test kết nối PostgreSQL qua ngrok..."
export PGPASSWORD="$DB_PASSWORD"
if command -v psql &> /dev/null; then
if psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1;" &>/dev/null; then
success "Kết nối PostgreSQL thành công!"
echo ""
# Get PostgreSQL version
VERSION=$(psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT version();" 2>/dev/null | head -1 | xargs)
log "PostgreSQL version: ${VERSION:0:60}..."
# Count tables
TABLE_COUNT=$(psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';" 2>/dev/null | xargs)
success "Số lượng tables: $TABLE_COUNT"
echo ""
# Check data
echo -e "${BLUE}📊 Kiểm tra dữ liệu:${NC}"
echo "============================================================"
TOTAL_DOCS=0
for table in core_legaldocument core_fine core_procedure core_office core_advisory; do
COUNT=$(psql -h "$ngrok_host" -p "$ngrok_port" -U "$DB_USER" -d "$DB_NAME" -t -c "SELECT COUNT(*) FROM $table;" 2>/dev/null | xargs || echo "0")
if [ "$COUNT" != "0" ] && [ -n "$COUNT" ]; then
TOTAL_DOCS=$((TOTAL_DOCS + COUNT))
success "$table: $(printf "%'d" $COUNT) records"
else
warn "$table: 0 records (trống)"
fi
done
echo "============================================================"
success "Tổng số documents: $(printf "%'d" $TOTAL_DOCS)"
echo ""
# Show DATABASE_URL
echo -e "${BLUE}============================================================${NC}"
success "✅ NGROK TUNNEL HOẠT ĐỘNG TỐT!"
if [ "$TOTAL_DOCS" -gt 0 ]; then
success "Có $(printf "%'d" $TOTAL_DOCS) documents trong database"
else
warn "Database trống, chưa có dữ liệu"
fi
echo ""
echo -e "${BLUE}📝 DATABASE_URL để dùng:${NC}"
echo "postgres://$DB_USER:$DB_PASSWORD@$ngrok_host:$ngrok_port/$DB_NAME"
echo -e "${BLUE}============================================================${NC}"
else
error "KHÔNG KẾT NỐI ĐƯỢC PostgreSQL!"
echo ""
echo -e "${BLUE}💡 Kiểm tra:${NC}"
echo " 1. PostgreSQL có đang chạy trên 192.168.1.212:5432 không?"
echo " 2. Ngrok tunnel có đang chạy đúng command 'tcp 192.168.1.212:5432' không?"
echo " 3. Toggle switch có được BẬT (ON) không?"
echo " 4. Firewall có block không?"
echo ""
echo "Thử test trực tiếp:"
echo " psql -h $ngrok_host -p $ngrok_port -U $DB_USER -d $DB_NAME"
exit 1
fi
else
warn "Không có psql, bỏ qua test PostgreSQL"
echo ""
echo -e "${BLUE}📝 DATABASE_URL để dùng:${NC}"
echo "postgres://$DB_USER:$DB_PASSWORD@$ngrok_host:$ngrok_port/$DB_NAME"
fi
|