diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3637fc7 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,13 @@ +name: CI +on: [push, pull_request] +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + - run: python -m unittest discover -s tests -v + - run: python -m py_compile panel.py backup.py + - run: sudo apt-get install -y shellcheck && shellcheck -S warning install.sh diff --git a/backup.py b/backup.py index ca1854a..aa88f25 100755 --- a/backup.py +++ b/backup.py @@ -173,6 +173,16 @@ def rd(): return False, str(e) +def verify_archive(path): + """Lee el tar.gz completo (valida CRC de gzip y estructura). Lanza si está corrupto.""" + with _tarfile.open(path, "r:gz") as tf: + for m in tf: + if m.isfile(): + f = tf.extractfile(m) + while f.read(1 << 20): + pass + + def run_backup(server_dir, world_name, backup_dir, send_fn=None, tag=None, retention_days=7, keep_monthly=True, log=None, progress=None): @@ -236,6 +246,7 @@ def prog(pct, stage): say(f"omitidos {skipped} temporales que el server borró a mitad del backup") size = _os.path.getsize(dest) / 1e9 prog(90, f"verificando ({size:.2f} GB)") + verify_archive(dest) say(f"OK {name} ({size:.2f} GB)") except Exception as e: try: diff --git a/install.sh b/install.sh index d469293..f2afd3e 100755 --- a/install.sh +++ b/install.sh @@ -65,7 +65,6 @@ ask RCON_PORT "Puerto RCON" "${PK_RCON_PORT:-25 ask MAX_MEM "RAM máxima del servidor (GB, solo visual)" "${PK_MAX_MEM:-4.0}" # ── Backups diarios ────────────────────────────────────────────── -BK_DEF=y if [ "$NON_INT" = 1 ]; then BK_ANS="${PK_BACKUP:-y}" else @@ -96,28 +95,26 @@ if [ -f "$CFG_FILE" ] && [ "$FORCE" = 0 ] && [ "$NON_INT" = 0 ]; then [[ "$ow" =~ ^[sSyY]$ ]] || { say "conservo config existente"; } fi if [ ! -f "$CFG_FILE" ] || [ "$FORCE" = 1 ] || [[ "${ow:-}" =~ ^[sSyY]$ ]]; then - # start_cmd como array JSON: respeta comillas simples/dobles - # shellcheck disable=SC2206 - read -r -a CMD_ARR <<< "$START_CMD" - START_JSON=$(printf '"%s",' "${CMD_ARR[@]}"); START_JSON="[${START_JSON%,}]" - cat > "$CFG_FILE" <= LOGIN_MAX_FAILS + + +def login_failed(ip): + with _sessions_lock: + _login_fails.setdefault(ip, []).append(time.time()) + + +def login_ok(ip): + with _sessions_lock: + _login_fails.pop(ip, None) + + +HTTPS_ON = False + + CFG = load_config() SERVER_DIR = CFG["server_dir"] @@ -393,20 +424,18 @@ def get_rcon_password(): def rcon_send(cmd, timeout=5): """Cliente RCON minimo (stdlib). Devuelve (ok, respuesta).""" - import socket as _sock - import struct as _struct srv = S() pw = srv.rcon_password() if not pw: return False, "RCON sin password configurado" try: - s = _sock.create_connection((RCON_HOST, srv.rcon_port), timeout=timeout) + s = socket.create_connection((RCON_HOST, srv.rcon_port), timeout=timeout) s.settimeout(timeout) _id = 1 def pkt(pid, ptype, payload): - body = _struct.pack("/fd/0 sin bloquear (O_NONBLOCK).""" - import os as _os - import fcntl as _fcntl path = f"/proc/{pid}/fd/0" fd = None try: - fd = _os.open(path, _os.O_WRONLY | _os.O_NONBLOCK) - _os.write(fd, (text + "\n").encode("utf-8")) + fd = os.open(path, os.O_WRONLY | os.O_NONBLOCK) + os.write(fd, (text + "\n").encode("utf-8")) return True, "enviado" except Exception as e: return False, str(e) finally: try: if fd is not None: - _os.close(fd) + os.close(fd) except Exception: pass @@ -498,13 +525,11 @@ def set_public_ip(ip): def get_playit_status(): """Best-effort: estado del agente playit sin requerir root.""" - import shutil as _sh - import subprocess as _sp info = {"installed": False, "service_active": False, "reachable": False, "note": ""} - if _sh.which("playit"): + if shutil.which("playit"): info["installed"] = True try: - r = _sp.run(["systemctl", "is-active", "playit.service"], + r = subprocess.run(["systemctl", "is-active", "playit.service"], capture_output=True, text=True, timeout=5) info["service_active"] = (r.stdout.strip() == "active") except Exception: @@ -1393,7 +1418,6 @@ def _running(): def backups_status(): - import shutil as _sh srv = S() c = {"enabled": srv.backup_enabled, "dir": srv.backup_dir, "retention_days": srv.retention_days, "keep_monthly": srv.keep_monthly, @@ -1415,7 +1439,7 @@ def backups_status(): info["writable"] = False info["writable_msg"] = str(e) try: - info["free_gb"] = round(_sh.disk_usage(c["dir"]).free / 1e9, 1) if os.path.isdir(c["dir"]) else 0 + info["free_gb"] = round(shutil.disk_usage(c["dir"]).free / 1e9, 1) if os.path.isdir(c["dir"]) else 0 except Exception: info["free_gb"] = 0 try: @@ -1448,10 +1472,9 @@ def discord_send(text): if not url: return False, "sin webhook configurado" try: - import urllib.request as _url - req = _url.Request(url, data=json.dumps({"content": text[:1800]}).encode(), + req = urllib.request.Request(url, data=json.dumps({"content": text[:1800]}).encode(), headers={"Content-Type": "application/json"}) - with _url.urlopen(req, timeout=8): + with urllib.request.urlopen(req, timeout=8): pass return True, "enviado" except Exception as e: @@ -1677,8 +1700,6 @@ def read_console_lines(limit=250): - - + @@ -2581,32 +2650,32 @@ def read_console_lines(limit=250): -