From 0204367f209c825fbbe0df69d1f880db83c5facc Mon Sep 17 00:00:00 2001 From: Marcos Date: Sun, 22 Mar 2026 11:55:12 -0300 Subject: [PATCH] Fix: Hardened path mapping and verbose logs --- tools.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tools.py b/tools.py index 75f34f0..5be07a1 100644 --- a/tools.py +++ b/tools.py @@ -44,18 +44,28 @@ def get_system_health() -> str: def read_vps_file(filepath: str) -> str: """Lê um arquivo do sistema de arquivos da VPS através do mapeamento /host_root.""" - host_path = f"/host_root{filepath}" if not filepath.startswith("/host_root") else filepath - # Se ainda assim não começar com /host_root e não for absoluto para o container, tentamos prefixar - if not host_path.startswith("/host_root") and filepath.startswith("/"): - host_path = "/host_root" + filepath + # Garante que o caminho comece com /host_root + clean_path = filepath + if clean_path.startswith("/host_root"): + host_path = clean_path + else: + # Se o usuário mandou /root/vps..., vira /host_root/root/vps... + host_path = os.path.join("/host_root", clean_path.lstrip("/")) try: + if not os.path.exists("/host_root"): + return "ERRO CRÍTICO: O diretório /host_root não existe no container! O mapeamento de volume falhou." + if not os.path.exists(host_path): - return f"Erro: Arquivo {filepath} não encontrado no host (Caminho tentado: {host_path})." + # Tenta listar o diretório pai para ajudar no debug + parent = os.path.dirname(host_path) + content = os.listdir(parent) if os.path.exists(parent) else "Pai não existe" + return f"Erro: {filepath} não encontrado (Caminho real: {host_path}). Conteúdo do pai: {content}" + with open(host_path, 'r') as f: - return f.read(2000) # Limite para não estourar o contexto + return f.read(2000) except Exception as e: - return f"Erro ao ler arquivo: {e}" + return f"Erro ao acessar {filepath}: {e}" def get_docker_stats() -> str: """Retorna o uso de CPU/RAM de todos os containers ativos via comando docker stats."""