🧟 W-ZOMBI: Implementar Relay Switch (A01.P010)

- Soporte para comando `relay` en el CLI (start/stop/status/ps1)
- Redirige tráfico HTTPS a través de puerto `socat` en host relay (ej: srv-dasu)
- Permite a las PCs de LAN interna que no corren VPN conectar con NS8 W-ZOMBI en Tailscale.
- Se integra de forma robusta con la tool de `wzombi`
This commit is contained in:
Ricardo Monla
2026-04-09 22:10:30 -03:00
parent 6ebf1c3010
commit 16cb7975ce
2 changed files with 246 additions and 10 deletions
+104 -4
View File
@@ -9,12 +9,18 @@ config = WZombi::Config.new(base_path: base)
def help
puts <<~HELP
Uso:
w-zombi start
w-zombi cmd "<comando>"
w-zombi start Inicia servidor WEBrick
w-zombi cmd "<comando>" Envía comando al zombi
w-zombi relay start <host> [port] Inicia relay socat en host remoto
w-zombi relay stop <host> Detiene relay en host remoto
w-zombi relay status <host> Estado del relay en host remoto
w-zombi relay ps1 <host_lan_ip> Genera PS1 para conectar vía relay
w-zombi ayuda | -h | -help
Ejemplo ADN:
adn tools w-zombi cmd "ipconfig"
Ejemplo relay:
w-zombi relay start srv-dasu 8001 # Relay en srv-dasu:8001 → NS8:8000
w-zombi relay ps1 192.168.1.13 # PS1 que apunta al relay
w-zombi relay stop srv-dasu # Detener relay
HELP
end
@@ -34,6 +40,100 @@ when "cmd"
store.update(command)
puts "Comando actualizado: #{command}"
when "relay"
subcmd = ARGV[1]
case subcmd
when "start"
host = ARGV[2]
port = ARGV[3] || "8001"
unless host
puts "Falta host. Uso: w-zombi relay start <host> [port]"
exit 1
end
ns8_ip = ENV.fetch("WZ_NS8_IP", "100.111.195.4")
ns8_port = config.puerto
puts "🧟 Relay W-Zombi → #{host}:#{port} → NS8(#{ns8_ip}:#{ns8_port})"
# Matar relay anterior si existe
system("ssh #{host} 'pkill -f \"socat.*TCP-LISTEN:#{port}\"' 2>/dev/null")
sleep 0.5
# Lanzar socat relay
socat_cmd = "socat TCP-LISTEN:#{port},fork,reuseaddr,bind=0.0.0.0 TCP:#{ns8_ip}:#{ns8_port}"
result = system("ssh #{host} 'nohup #{socat_cmd} > /tmp/wzombi-relay.log 2>&1 & echo PID:$!'")
if result
# Verificar
sleep 1
test = `ssh #{host} "curl -s --connect-timeout 3 http://localhost:#{port}/cmd.json" 2>/dev/null`.strip
if test.include?('"cmd"')
puts "✅ Relay activo — #{host}:#{port} → NS8:#{ns8_port}"
puts " Test: #{test}"
puts "\n Para generar PS1: w-zombi relay ps1 <IP_LAN_de_#{host}>"
else
puts "⚠️ Relay lanzado pero no responde. Revisar logs en #{host}:/tmp/wzombi-relay.log"
end
else
puts "❌ Error lanzando relay en #{host}"
exit 1
end
when "stop"
host = ARGV[2]
port = ARGV[3] || "8001"
unless host
puts "Falta host. Uso: w-zombi relay stop <host> [port]"
exit 1
end
system("ssh #{host} 'pkill -f \"socat.*TCP-LISTEN:#{port}\"' 2>/dev/null")
puts "🛑 Relay detenido en #{host}:#{port}"
when "status"
host = ARGV[2]
port = ARGV[3] || "8001"
unless host
puts "Falta host. Uso: w-zombi relay status <host> [port]"
exit 1
end
output = `ssh #{host} "ss -tlnp 2>/dev/null | grep :#{port}; echo '---'; curl -s --connect-timeout 3 http://localhost:#{port}/cmd.json 2>/dev/null || echo 'NO_RESPONSE'" 2>&1`.strip
lines = output.split("---")
listening = lines[0]&.include?(port.to_s)
response = lines[1]&.strip
if listening && response&.include?('"cmd"')
puts "✅ Relay ACTIVO en #{host}:#{port}"
puts " Respuesta: #{response}"
elsif listening
puts "⚠️ Relay escuchando en #{host}:#{port} pero sin respuesta del backend"
else
puts "❌ Relay NO activo en #{host}:#{port}"
end
when "ps1"
relay_ip = ARGV[2]
relay_port = ARGV[3] || "8001"
unless relay_ip
puts "Falta IP del relay. Uso: w-zombi relay ps1 <ip_lan_relay> [port]"
exit 1
end
# Generar PS1 que apunta al relay en vez de NS8 directo
require_relative '../lib/wzombi/engine/ps1_builder'
host = "#{relay_ip}:#{relay_port}"
ps1 = WZombi::Engine::PS1Builder.build(host: host)
puts ps1
else
puts "Subcomando relay desconocido: #{subcmd}"
puts "Uso: w-zombi relay start|stop|status|ps1 <host>"
exit 1
end
when "ayuda", "-h", "-help", nil
help