From bc5b424a61d22ea6f57837b3f4fbfbc4971e88bb Mon Sep 17 00:00:00 2001 From: "Mauro Rosero P." Date: Sat, 22 Mar 2025 15:08:08 -0500 Subject: [PATCH] =?UTF-8?q?[IMPROVED]=20Funciones=20para=20detecci=C3=B3n?= =?UTF-8?q?=20de=20VPN=20en=20base.lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Añadida función 'is_connected_to_vpn' para detectar conexiones VPN activas - Añadida función 'get_vpn_type' para identificar el tipo específico de VPN - Mejorada la detección de Pritunl para identificarlo correctamente - Soporte para múltiples tipos de VPN: OpenVPN, Pritunl, WireGuard, NordVPN, etc. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- README.md | 1 + bin/lib/base.lib | 154 +++++++++++++++++++++++++++++++++++++++++++++++ bin/ollama.sh | 86 ++++++++++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100755 bin/ollama.sh diff --git a/README.md b/README.md index 494b63b..85d13ed 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ bin/update.sh | `bin/ai_token.sh` | Gestiona tokens de múltiples proveedores de IA vía SOPS | | `bin/ollama_up.sh` | Inicia el servicio Ollama (IA local) con podman-compose | | `bin/ollama_down.sh` | Detiene el servicio Ollama (IA local) | +| `bin/ollama.sh` | Cliente para interactuar con Ollama (auto-inicia el servicio si es necesario) | ### Herramientas para Editores de Código diff --git a/bin/lib/base.lib b/bin/lib/base.lib index 50f744e..0146de6 100644 --- a/bin/lib/base.lib +++ b/bin/lib/base.lib @@ -529,3 +529,157 @@ remove_cortana_alias() { return 2 fi } + +# Verifica si el sistema está conectado a una VPN +# Devuelve 0 si está conectado a una VPN, 1 si no lo está +is_connected_to_vpn() { + local vpn_interfaces=("tun" "tap" "ppp" "wg" "wireguard" "nordlynx" "mullvad" "proton") + local found_vpn=0 + + # Verificar si existe alguna interfaz VPN activa + for vpn_type in "${vpn_interfaces[@]}"; do + if ip addr | grep -q "${vpn_type}"; then + found_vpn=1 + break + fi + done + + # Verificar si hay procesos VPN en ejecución + if [ $found_vpn -eq 0 ]; then + for proc in "openvpn" "wireguard" "wg" "nordvpn" "protonvpn" "pritunl" "anyconnect" "vpnc"; do + if pgrep -x "$proc" >/dev/null; then + found_vpn=1 + break + fi + done + fi + + # Verificar conexiones de red que puedan indicar VPN + if [ $found_vpn -eq 0 ]; then + if netstat -rn | grep -q "tun\|tap\|ppp\|wg"; then + found_vpn=1 + fi + fi + + # Verificar reglas de firewall que puedan indicar VPN + if [ $found_vpn -eq 0 ]; then + if command -v iptables >/dev/null && iptables -L | grep -qi "vpn\|tunnel"; then + found_vpn=1 + fi + fi + + if [ $found_vpn -eq 1 ]; then + return 0 # Está conectado a una VPN + else + return 1 # No está conectado a una VPN + fi +} + +# Identifica el tipo de VPN al que está conectado el sistema +# Devuelve el nombre del tipo de VPN o "Unknown" si no se puede determinar +# Si no está conectado a ninguna VPN, devuelve "Not Connected" +get_vpn_type() { + # Primero verificamos si hay una conexión VPN + is_connected_to_vpn + if [ $? -ne 0 ]; then + echo "" + return 1 + fi + + # Mapa de procesos VPN y su tipo correspondiente + declare -A vpn_process_map=( + ["openvpn"]="OpenVPN" + ["pritunl"]="Pritunl" + ["pritunl-client"]="Pritunl" + ["nordvpnd"]="NordVPN" + ["protonvpn"]="ProtonVPN" + ["wg-quick"]="WireGuard" + ["wg"]="WireGuard" + ["mullvad"]="Mullvad" + ["expressvpnd"]="ExpressVPN" + ["anyconnect"]="Cisco AnyConnect" + ["vpnc"]="Cisco VPN" + ["openconnect"]="OpenConnect" + ["pppd"]="PPTP/L2TP" + ["sstp-client"]="SSTP" + ["tailscaled"]="Tailscale" + ["zerotier-one"]="ZeroTier" + ) + + # Buscar procesos VPN conocidos + for proc in "${!vpn_process_map[@]}"; do + if pgrep -x "$proc" >/dev/null; then + echo "${vpn_process_map[$proc]}" + return 0 + fi + done + + # Si no encontramos procesos específicos, intentamos identificar por interfaces + if ip addr | grep -q "tun"; then + # Identificar si es Pritunl (tiene prioridad) + if systemctl is-active --quiet pritunl-client.service || pgrep -f "pritunl-client" >/dev/null || ps aux | grep -q "[p]ritunl"; then + echo "Pritunl" + # Verificar configuración de Pritunl + elif [ -d "$HOME/.pritunl" ] || [ -d "/etc/pritunl-client" ]; then + echo "Pritunl" + # Buscar indicadores adicionales de Pritunl + elif grep -q "pritunl" /var/log/syslog 2>/dev/null || grep -q "pritunl" /var/log/messages 2>/dev/null; then + echo "Pritunl" + # Verificar si es OpenVPN genérico + elif ps aux | grep -q "[o]penvpn"; then + echo "OpenVPN" + else + echo "TUN-based VPN" + fi + return 0 + elif ip addr | grep -q "wg"; then + echo "WireGuard" + return 0 + elif ip addr | grep -q "ppp"; then + echo "PPP-based VPN" + return 0 + elif ip addr | grep -q "nordlynx"; then + echo "NordVPN (NordLynx)" + return 0 + elif ip addr | grep -q "proton"; then + echo "ProtonVPN" + return 0 + elif ip addr | grep -q "mullvad"; then + echo "Mullvad" + return 0 + elif ip addr | grep -q "tailscale"; then + echo "Tailscale" + return 0 + elif ip addr | grep -q "zt"; then + echo "ZeroTier" + return 0 + fi + + # Verificar servicios conocidos de VPN + if systemctl is-active --quiet nordvpnd; then + echo "NordVPN" + return 0 + elif systemctl is-active --quiet protonvpn; then + echo "ProtonVPN" + return 0 + elif systemctl is-active --quiet wg-quick@; then + echo "WireGuard" + return 0 + elif systemctl is-active --quiet tailscaled; then + echo "Tailscale" + return 0 + elif systemctl is-active --quiet zerotier-one; then + echo "ZeroTier" + return 0 + fi + + # Verificar específicamente Pritunl (adicional) + if systemctl is-active --quiet pritunl-client.service || [ -d "$HOME/.pritunl" ] || [ -d "/etc/pritunl-client" ]; then + echo "Pritunl" + return 0 + fi + + # Si llegamos hasta aquí, no pudimos identificar específicamente el tipo de VPN + echo "Unknown VPN" + return 0 +} diff --git a/bin/ollama.sh b/bin/ollama.sh new file mode 100755 index 0000000..44d3683 --- /dev/null +++ b/bin/ollama.sh @@ -0,0 +1,86 @@ +#!/bin/bash +#Script : ollama.sh +#Apps : MRDEVS TOOLS +#Description : Cliente para interactuar con Ollama (IA local) +#Author : Mauro Rosero Pérez +#Company Email : mauro@rosero.one +#Personal Email : mauro.rosero@gmail.com +#Created : $(date +%Y/%m/%d\ %H:%M:%S) +#Modified : $(date +%Y/%m/%d\ %H:%M:%S) +#Version : 1.0.0 +#============================================================================== +# Derechos de Autor [2025] [Mauro Rosero P. ] +#============================================================================== +# Este programa es software libre: usted puede redistribuirlo y/o modificarlo +# bajo los términos de la Licencia Pública Affero General de GNU tal como +# lo publica la Free Software Foundation, ya sea la versión 3 de la licencia, +# o (a su elección) cualquier versión posterior. +# +# Este programa se distribuye con la esperanza de que sea útil, +# pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de +# COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la +# Licencia Pública Affero General de GNU para obtener más detalles. +# +# Debería haber recibido una copia de la Licencia Pública Affero General +# junto con este programa. Si no la recibió, consulte . + +# Configuración inicial +# Usar DEVELOPER_DIR de base.lib +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +BIN_BASE="bin" +BIN_LIBS="lib" +BIN_MESG="msg" +BIN_CFGS="config" + +# Leer DEVSPATH desde el archivo de configuración o usar "devs" por defecto +if [ -f "$SCRIPT_DIR/config/devspath.dat" ]; then + DEVSPATH=$(cat "$SCRIPT_DIR/$BIN_CFGS/devspath.dat") +else + DEVSPATH="devs" +fi + +BIN_HOME="$HOME/$DEVSPATH" +BIN_PATH=$BIN_HOME/$BIN_BASE +VERSION=$(cat "$BIN_HOME/$BIN_BASE/$BIN_CFGS/version") + +# CHECK SHELL LANGUAGE +BIN_LANG=${LANG:0:2} + +# Importar bibliotecas necesarias +if [ -f "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/base.lib" ]; then + source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/base.lib" + # Cargar mensajes en el idioma del sistema o español por defecto + load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "head" + title="${head_000} ${head_002}" +fi + +# Check if we should use podman or docker (prefer podman) +if command -v podman >/dev/null 2>&1; then + CONTAINER_CMD="podman" +elif command -v docker >/dev/null 2>&1; then + CONTAINER_CMD="docker" +else + echo "Error: Neither podman nor docker is installed" + exit 1 +fi + +# Verificar si el contenedor Ollama está en ejecución +if ${CONTAINER_CMD} ps | grep -q "ollama"; then + # Si está en ejecución, ejecutar el comando pasado como argumento + ${CONTAINER_CMD} exec -it ollama ollama "$@" +else + echo "El servicio Ollama no está en ejecución. Iniciándolo..." + "${BIN_PATH}/ollama_up.sh" + + # Esperar un momento para que el servicio esté listo + echo "Esperando a que el servicio esté listo..." + sleep 5 + + # Verificar nuevamente si el contenedor está en ejecución + if ${CONTAINER_CMD} ps | grep -q "ollama"; then + ${CONTAINER_CMD} exec -it ollama ollama "$@" + else + echo "Error: No se pudo iniciar el servicio Ollama correctamente." + exit 1 + fi +fi \ No newline at end of file