172 lines
4.3 KiB
Bash
Executable File
172 lines
4.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Script para gestionar el proxy TCP de forma dinámica
|
|
# Uso: ./manage_proxy.sh [comando] [argumentos]
|
|
|
|
PROXY_URL="http://localhost:8080"
|
|
|
|
show_help() {
|
|
echo "Uso: $0 [comando] [argumentos]"
|
|
echo ""
|
|
echo "Comandos disponibles:"
|
|
echo " status - Mostrar estado de todos los proxies"
|
|
echo " add <puerto_local> <puerto_remoto> - Añadir nuevo proxy"
|
|
echo " remove <puerto_local> - Eliminar proxy existente"
|
|
echo " logs - Mostrar logs del contenedor"
|
|
echo " restart - Reiniciar el contenedor"
|
|
echo " build - Construir la imagen del contenedor"
|
|
echo " start - Iniciar el contenedor"
|
|
echo " stop - Detener el contenedor"
|
|
echo ""
|
|
echo "Ejemplos:"
|
|
echo " $0 status"
|
|
echo " $0 add 9000 9000"
|
|
echo " $0 remove 9000"
|
|
}
|
|
|
|
check_container() {
|
|
if ! docker ps | grep -q "tcp-proxy-container"; then
|
|
echo "Error: El contenedor tcp-proxy no está ejecutándose"
|
|
echo "Ejecuta: $0 start"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
get_status() {
|
|
check_container
|
|
echo "Estado de los proxies TCP:"
|
|
curl -s "$PROXY_URL/status" | python3 -m json.tool 2>/dev/null || echo "Error obteniendo estado"
|
|
}
|
|
|
|
add_proxy() {
|
|
local local_port=$1
|
|
local remote_port=$2
|
|
|
|
if [[ -z "$local_port" || -z "$remote_port" ]]; then
|
|
echo "Error: Debes especificar puerto local y remoto"
|
|
echo "Uso: $0 add <puerto_local> <puerto_remoto>"
|
|
exit 1
|
|
fi
|
|
|
|
check_container
|
|
|
|
echo "Añadiendo proxy: localhost:$local_port -> 91.99.210.72:$remote_port"
|
|
|
|
response=$(curl -s -X POST "$PROXY_URL/add" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"local_port\": $local_port, \"remote_port\": $remote_port}")
|
|
|
|
echo "$response" | python3 -m json.tool 2>/dev/null || echo "Respuesta: $response"
|
|
|
|
# Verificar si necesitamos exponer el puerto en docker-compose
|
|
if ! grep -q "$local_port:$local_port" docker-compose.yml; then
|
|
echo ""
|
|
echo "⚠️ IMPORTANTE: Agrega la siguiente línea en docker-compose.yml bajo 'ports':"
|
|
echo " - \"$local_port:$local_port\""
|
|
echo " Luego ejecuta: $0 restart"
|
|
fi
|
|
}
|
|
|
|
remove_proxy() {
|
|
local local_port=$1
|
|
|
|
if [[ -z "$local_port" ]]; then
|
|
echo "Error: Debes especificar el puerto local"
|
|
echo "Uso: $0 remove <puerto_local>"
|
|
exit 1
|
|
fi
|
|
|
|
check_container
|
|
|
|
echo "Eliminando proxy del puerto $local_port"
|
|
|
|
response=$(curl -s -X POST "$PROXY_URL/remove" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"local_port\": $local_port}")
|
|
|
|
echo "$response" | python3 -m json.tool 2>/dev/null || echo "Respuesta: $response"
|
|
}
|
|
|
|
show_logs() {
|
|
echo "Logs del contenedor tcp-proxy:"
|
|
docker logs -f tcp-proxy-container
|
|
}
|
|
|
|
restart_container() {
|
|
echo "Reiniciando contenedor..."
|
|
docker-compose down
|
|
docker-compose up -d
|
|
echo "Contenedor reiniciado"
|
|
}
|
|
|
|
build_container() {
|
|
echo "Construyendo imagen del contenedor..."
|
|
docker-compose build
|
|
echo "Imagen construida"
|
|
}
|
|
|
|
start_container() {
|
|
echo "Iniciando contenedor..."
|
|
docker-compose up -d
|
|
echo "Contenedor iniciado"
|
|
|
|
# Esperar a que el servicio esté listo
|
|
echo "Esperando a que el servicio esté listo..."
|
|
for i in {1..30}; do
|
|
if curl -s "$PROXY_URL/status" >/dev/null 2>&1; then
|
|
echo "✅ Servicio listo"
|
|
break
|
|
fi
|
|
sleep 1
|
|
echo -n "."
|
|
done
|
|
echo ""
|
|
}
|
|
|
|
stop_container() {
|
|
echo "Deteniendo contenedor..."
|
|
docker-compose down
|
|
echo "Contenedor detenido"
|
|
}
|
|
|
|
# Verificar argumentos
|
|
if [[ $# -eq 0 ]]; then
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
# Procesar comandos
|
|
case "$1" in
|
|
"status")
|
|
get_status
|
|
;;
|
|
"add")
|
|
add_proxy "$2" "$3"
|
|
;;
|
|
"remove")
|
|
remove_proxy "$2"
|
|
;;
|
|
"logs")
|
|
show_logs
|
|
;;
|
|
"restart")
|
|
restart_container
|
|
;;
|
|
"build")
|
|
build_container
|
|
;;
|
|
"start")
|
|
start_container
|
|
;;
|
|
"stop")
|
|
stop_container
|
|
;;
|
|
"help"|"-h"|"--help")
|
|
show_help
|
|
;;
|
|
*)
|
|
echo "Comando desconocido: $1"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac |