#!/bin/bash # Túnel SSH para MCP Outline PostgreSQL (hub.descomplicar.pt) # Cria túnel para o PostgreSQL do Outline no EasyPanel # # Uso: ./start-tunnel.sh [start|stop|status] TUNNEL_PORT=5433 REMOTE_HOST="root@178.63.18.51" CONTAINER_IP="172.18.0.46" CONTAINER_PORT=5432 start_tunnel() { # Verificar se já está activo if lsof -i :$TUNNEL_PORT >/dev/null 2>&1; then echo "Túnel já activo na porta $TUNNEL_PORT" return 0 fi # Criar túnel em background ssh -f -N -L $TUNNEL_PORT:$CONTAINER_IP:$CONTAINER_PORT $REMOTE_HOST if [ $? -eq 0 ]; then echo "Túnel criado: localhost:$TUNNEL_PORT -> Outline PostgreSQL" echo "DATABASE_URL=postgres://postgres:***@localhost:$TUNNEL_PORT/descomplicar" else echo "Erro ao criar túnel" return 1 fi } stop_tunnel() { PID=$(lsof -t -i:$TUNNEL_PORT 2>/dev/null) if [ -n "$PID" ]; then kill $PID echo "Túnel terminado (PID: $PID)" else echo "Nenhum túnel activo na porta $TUNNEL_PORT" fi } status_tunnel() { if lsof -i :$TUNNEL_PORT >/dev/null 2>&1; then echo "Túnel ACTIVO na porta $TUNNEL_PORT" lsof -i :$TUNNEL_PORT | grep ssh else echo "Túnel INACTIVO" fi } case "${1:-start}" in start) start_tunnel ;; stop) stop_tunnel ;; status) status_tunnel ;; *) echo "Uso: $0 [start|stop|status]" exit 1 ;; esac