229 lines
5.7 KiB
Markdown
229 lines
5.7 KiB
Markdown
# Migración de Servicios al VPS
|
|
|
|
**Fecha:** 29 Sep 2026
|
|
**Servicios migrados:**
|
|
- Telegram Watcher
|
|
- Shutdown Listener
|
|
|
|
---
|
|
|
|
## ✅ Estado Actual
|
|
|
|
### En el PC Local (sobremesa)
|
|
- ❌ **Recordatorios Msgbox** → ELIMINADO (no se quiere más)
|
|
- ❌ **Telegram Watcher** → MIGRADO AL VPS
|
|
- ❌ **Shutdown Listener** → MIGRADO AL VPS
|
|
- ✅ **OmniRoute Watchdog** → MANTIENE LOCAL (necesario para OpenCode)
|
|
|
|
### En el VPS (185.187.169.109)
|
|
- ✅ **Telegram Watcher** → Servicio systemd
|
|
- ✅ **Shutdown Listener** → Servicio systemd
|
|
|
|
---
|
|
|
|
## 📋 Instalación en el VPS
|
|
|
|
### 1. Conectarse al VPS
|
|
|
|
```bash
|
|
ssh -i C:\Users\juanm\Documents\GitHub\contabo root@185.187.169.109
|
|
```
|
|
|
|
### 2. Ejecutar script de configuración
|
|
|
|
```bash
|
|
/root/configurar_servicios.sh
|
|
```
|
|
|
|
Te pedirá:
|
|
- **TELEGRAM_API_ID**: API ID de Telegram (obtener de https://my.telegram.org)
|
|
- **TELEGRAM_API_HASH**: API Hash de Telegram
|
|
- **TELEGRAM_BOT_TOKEN**: Token del bot de Telegram (obtener de @BotFather)
|
|
- **SHUTDOWN_TOKEN**: Token de seguridad (presiona Enter para usar el actual)
|
|
|
|
### 3. Verificar que los servicios están corriendo
|
|
|
|
```bash
|
|
systemctl status telegram-watcher
|
|
systemctl status shutdown-listener
|
|
```
|
|
|
|
---
|
|
|
|
## 📊 Monitorización
|
|
|
|
### Ver logs en tiempo real
|
|
|
|
```bash
|
|
# Telegram Watcher
|
|
journalctl -u telegram-watcher -f
|
|
|
|
# Shutdown Listener
|
|
journalctl -u shutdown-listener -f
|
|
```
|
|
|
|
### Ver mensajes de Telegram
|
|
|
|
```bash
|
|
tail -f /var/log/telegram_watcher/mensajes.log
|
|
```
|
|
|
|
### Reiniciar servicios
|
|
|
|
```bash
|
|
systemctl restart telegram-watcher
|
|
systemctl restart shutdown-listener
|
|
```
|
|
|
|
### Ver estado
|
|
|
|
```bash
|
|
systemctl status telegram-watcher
|
|
systemctl status shutdown-listener
|
|
```
|
|
|
|
---
|
|
|
|
## 🔧 Configuración del Shutdown Listener
|
|
|
|
### Requisitos previos
|
|
|
|
Para que el Shutdown Listener del VPS pueda apagar el PC:
|
|
|
|
1. **SSH habilitado en el PC Windows**
|
|
- Instalar OpenSSH Server (Configuración → Aplicaciones → Características opcionales → OpenSSH Server)
|
|
- Iniciar servicio: `Start-Service sshd`
|
|
- Habilitar al inicio: `Set-Service -Name sshd -StartupType 'Automatic'`
|
|
|
|
2. **Configurar clave SSH del VPS en el PC**
|
|
|
|
En el VPS:
|
|
```bash
|
|
# Generar clave si no existe
|
|
ssh-keygen -t rsa -b 4096 -f /root/.ssh/id_rsa -N ""
|
|
|
|
# Copiar clave pública
|
|
cat /root/.ssh/id_rsa.pub
|
|
```
|
|
|
|
En el PC Windows:
|
|
```powershell
|
|
# Crear directorio SSH si no existe
|
|
mkdir C:\Users\juanm\.ssh -ErrorAction SilentlyContinue
|
|
|
|
# Pegar la clave pública del VPS en authorized_keys
|
|
# (copiar el contenido de id_rsa.pub del VPS)
|
|
Add-Content C:\Users\juanm\.ssh\authorized_keys "<PEGAR_CLAVE_PUBLICA_AQUI>"
|
|
```
|
|
|
|
3. **Verificar conectividad desde el VPS**
|
|
|
|
```bash
|
|
ssh juanm@192.168.1.239 "echo 'Conexión OK'"
|
|
```
|
|
|
|
### Usar el Shutdown Listener
|
|
|
|
Desde cualquier lugar (Home Assistant, curl, etc.):
|
|
|
|
```bash
|
|
curl -X POST http://185.187.169.109:5555/shutdown \
|
|
-H "Authorization: Bearer H7iLGJixPp3J3pthCmnR2Ibq0tvERD5THyr_foAeN_8"
|
|
```
|
|
|
|
---
|
|
|
|
## 🔐 Seguridad
|
|
|
|
### Variables de entorno sensibles
|
|
|
|
Los servicios usan variables de entorno para credenciales:
|
|
- `TELEGRAM_API_ID`
|
|
- `TELEGRAM_API_HASH`
|
|
- `TELEGRAM_BOT_TOKEN`
|
|
- `SHUTDOWN_TOKEN`
|
|
|
|
Estas están en `/etc/systemd/system/*.service` (solo accesible por root).
|
|
|
|
### Firewall
|
|
|
|
El Shutdown Listener escucha en el puerto **5555**. Asegúrate de:
|
|
- Permitir conexiones desde tu red local
|
|
- **NO** exponer públicamente (solo VPN o red interna)
|
|
|
|
---
|
|
|
|
## 📂 Ubicaciones de archivos
|
|
|
|
### En el VPS
|
|
|
|
| Archivo | Ubicación |
|
|
|---------|-----------|
|
|
| Script Telegram Watcher | `/opt/telegram_watcher/telegram_watcher.py` |
|
|
| Script Shutdown Listener | `/opt/shutdown_listener/shutdown_listener.py` |
|
|
| Servicio Telegram Watcher | `/etc/systemd/system/telegram-watcher.service` |
|
|
| Servicio Shutdown Listener | `/etc/systemd/system/shutdown-listener.service` |
|
|
| Log de mensajes | `/var/log/telegram_watcher/mensajes.log` |
|
|
| Script de configuración | `/root/configurar_servicios.sh` |
|
|
|
|
---
|
|
|
|
## 🚨 Troubleshooting
|
|
|
|
### El Telegram Watcher no arranca
|
|
|
|
```bash
|
|
# Ver logs
|
|
journalctl -u telegram-watcher -n 50
|
|
|
|
# Problemas comunes:
|
|
# 1. Credenciales incorrectas → verificar variables en el .service
|
|
# 2. Telethon no instalado → pip3 install telethon
|
|
# 3. Permisos del log → mkdir -p /var/log/telegram_watcher && chmod 755 /var/log/telegram_watcher
|
|
```
|
|
|
|
### El Shutdown Listener no puede conectar al PC
|
|
|
|
```bash
|
|
# Verificar conectividad SSH
|
|
ssh -i /root/.ssh/id_rsa juanm@192.168.1.239 "echo OK"
|
|
|
|
# Si falla:
|
|
# 1. Verificar que el PC está encendido y en la red
|
|
# 2. Verificar IP del PC (puede haber cambiado)
|
|
# 3. Verificar que OpenSSH Server está corriendo en el PC
|
|
# 4. Verificar que la clave pública está en C:\Users\juanm\.ssh\authorized_keys
|
|
```
|
|
|
|
---
|
|
|
|
## 📝 Notas
|
|
|
|
1. **Sesión de Telegram**: El watcher usa el BOT_TOKEN (no sesión personal) para evitar el problema de `AuthKeyDuplicatedError`.
|
|
|
|
2. **Logs**: Los mensajes se guardan tanto en journald como en `/var/log/telegram_watcher/mensajes.log`.
|
|
|
|
3. **Reinicio automático**: Ambos servicios se reinician automáticamente si fallan (configurado en systemd).
|
|
|
|
4. **IP del PC**: La IP `192.168.1.239` está hardcodeada. Si cambia, actualizar en `/etc/systemd/system/shutdown-listener.service`.
|
|
|
|
---
|
|
|
|
## ✅ Checklist de migración
|
|
|
|
- [x] Cerrar procesos locales (Telegram Watcher, Shutdown Listener, Recordatorios Msgbox)
|
|
- [x] Eliminar tareas programadas de Windows
|
|
- [x] Crear scripts adaptados para VPS
|
|
- [x] Copiar archivos al VPS
|
|
- [x] Crear servicios systemd
|
|
- [ ] **Ejecutar `/root/configurar_servicios.sh` en el VPS**
|
|
- [ ] Configurar SSH en el PC Windows
|
|
- [ ] Copiar clave pública del VPS al PC
|
|
- [ ] Probar shutdown remoto
|
|
- [ ] Verificar que los logs se generan correctamente
|
|
- [ ] Actualizar AGENTS.md con la nueva configuración
|
|
|
|
---
|
|
|
|
**Última actualización:** 29 Sep 2026
|