From 50c4c9669f6f9411c6d6f0628d4d9763782ce93a Mon Sep 17 00:00:00 2001 From: nix Date: Fri, 7 Nov 2025 20:05:28 +0000 Subject: [PATCH] Actualizar berretin.py --- berretin.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/berretin.py b/berretin.py index ed10066..ef52168 100644 --- a/berretin.py +++ b/berretin.py @@ -4,7 +4,7 @@ import json import requests import subprocess from datetime import datetime, timedelta -from flask import Flask, jsonify, Response, request, abort +from flask import Flask, jsonify, Response, request BASE_API = "https://ws1.smn.gob.ar" CACHE_DIR = "json.api.cache" @@ -16,6 +16,7 @@ TIMEOUT = 10 app = Flask(__name__) app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False + @app.after_request def remove_server_header(response): response.headers["Server"] = "" @@ -27,6 +28,7 @@ def get_cache_filename(url: str) -> str: h = hashlib.sha256(url.encode()).hexdigest() return os.path.join(CACHE_DIR, f"{h}.json") + def load_cache(url: str): try: path = get_cache_filename(url) @@ -40,6 +42,7 @@ def load_cache(url: str): except Exception: return None + def save_cache(url: str, data: dict): try: os.makedirs(CACHE_DIR, exist_ok=True) @@ -61,6 +64,7 @@ def load_smn_token(): print(f"[TOKEN] Error loading token: {e}") return "" + def refresh_smn_token(): print("[TOKEN] Refreshing SMN token...") try: @@ -80,8 +84,10 @@ def refresh_smn_token(): def check_access_token(): header_token = request.headers.get("Authorization", "").strip() - if not header_token or header_token != ACCESS_TOKEN: - abort(401) # immediately reject unauthorized access + if header_token != ACCESS_TOKEN: + # Drop unauthorized message (empty response) + return Response("", status=401) + return None def fetch_from_smn(url: str, retry: bool = True): @@ -106,9 +112,11 @@ def fetch_from_smn(url: str, retry: bool = True): return resp -@app.route("/") # change here bc nginx +@app.route("/") def smn_proxy(subpath): - check_access_token() + unauthorized = check_access_token() + if unauthorized: + return unauthorized url = f"{BASE_API}/{subpath}"