96 lines
3.3 KiB
Python
96 lines
3.3 KiB
Python
# Název: janssenpc_file_receive_v1.1.py
|
|
# Verze: 1.1
|
|
# Datum: 2026-06-08
|
|
# Popis: Stáhne soubory čekající na serveru (msgs.buzalka.cz) do ##JNJPrenos\ZHovorcovic\.
|
|
# Spouštět ručně dle potřeby.
|
|
#
|
|
# Změna v1.1:
|
|
# Jména souborů ze /status jsou Fernet tokeny (zašifrované původní názvy).
|
|
# Klient je pošle beze změny jako URL token do /item/{token} — Zscaler vidí
|
|
# jen neprůhledný řetězec, ne skutečné jméno souboru (bypass DLP).
|
|
# Po stažení a dešifrování obsahu klient dešifruje i token → původní jméno → uloží.
|
|
|
|
import base64
|
|
import hashlib
|
|
import requests
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
from cryptography.fernet import Fernet
|
|
|
|
TOKEN = "13e1bb01-9fd5-44a8-8ce9-4ee27133d340"
|
|
PENDING_URL = "https://msgs.buzalka.cz/status"
|
|
DOWNLOAD_URL = "https://msgs.buzalka.cz/item"
|
|
RECEIVE_DIR = Path(r"C:\Users\vbuzalka\OneDrive - JNJ\##JNJPrenos\ZHovorcovic")
|
|
LOG_FILE = Path(__file__).parent / "file_send.log"
|
|
_FERNET = Fernet(base64.urlsafe_b64encode(hashlib.sha256(TOKEN.encode()).digest()))
|
|
|
|
|
|
def log(msg: str):
|
|
ts = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
line = f"[{ts}] {msg}"
|
|
print(line)
|
|
with LOG_FILE.open("a", encoding="utf-8") as lf:
|
|
lf.write(line + "\n")
|
|
|
|
|
|
def resolve_dest(directory: Path, filename: str) -> Path:
|
|
"""Přepíše existující soubor, pokud je zamčený → name (2), (3)..."""
|
|
dest = directory / filename
|
|
if not dest.exists():
|
|
return dest
|
|
try:
|
|
dest.open('r+b').close()
|
|
return dest
|
|
except PermissionError:
|
|
pass
|
|
stem = Path(filename).stem
|
|
suffix = Path(filename).suffix
|
|
n = 2
|
|
while True:
|
|
candidate = directory / f"{stem} ({n}){suffix}"
|
|
if not candidate.exists():
|
|
return candidate
|
|
try:
|
|
candidate.open('r+b').close()
|
|
return candidate
|
|
except PermissionError:
|
|
n += 1
|
|
|
|
|
|
log("=== file_receive: Spuštění ===")
|
|
|
|
try:
|
|
resp = requests.get(PENDING_URL, headers={"Authorization": f"Bearer {TOKEN}"}, timeout=30)
|
|
resp.raise_for_status()
|
|
pending = resp.json().get("files", []) # seznam Fernet tokenů
|
|
log(f"Souborů čeká na serveru: {len(pending)}")
|
|
except Exception as e:
|
|
log(f"CHYBA při dotazu na server: {e}")
|
|
pending = []
|
|
|
|
if pending:
|
|
RECEIVE_DIR.mkdir(parents=True, exist_ok=True)
|
|
for enc_token in pending:
|
|
# Dešifruj token → původní jméno souboru (pro log + uložení)
|
|
try:
|
|
orig_filename = _FERNET.decrypt(enc_token.encode()).decode()
|
|
except Exception as e:
|
|
log(f" CHYBA (dešifrování jména) | {enc_token[:20]}... | {e}")
|
|
continue
|
|
|
|
try:
|
|
r = requests.get(
|
|
f"{DOWNLOAD_URL}/{enc_token}",
|
|
headers={"Authorization": f"Bearer {TOKEN}"},
|
|
timeout=120,
|
|
)
|
|
r.raise_for_status()
|
|
decrypted = _FERNET.decrypt(r.content)
|
|
dest = resolve_dest(RECEIVE_DIR, orig_filename)
|
|
dest.write_bytes(decrypted)
|
|
log(f" STAŽENO | {orig_filename}{' → ' + dest.name if dest.name != orig_filename else ''}")
|
|
except Exception as e:
|
|
log(f" CHYBA | {orig_filename} | {e}")
|
|
|
|
log("=== file_receive: Hotovo ===")
|