#!/usr/bin/env python3
from pathlib import Path
import re
files=[Path('/etc/caddy/Caddyfile'), Path('/srv/www/webpi/index.html'), Path('/srv/apps/sotlab-users/sotlab_users_app.py')]
for p in files:
    print(f'--- {p} exists={p.exists()} ---')
    if not p.exists(): continue
    text=p.read_text(errors='replace')
    if p.name == 'sotlab_users_app.py':
        ranges=[(400,565)]
        lines=text.splitlines()
        for start,end in ranges:
            for i,line in enumerate(lines[start-1:end], start):
                if any(x in line for x in ['def maybe_serve_protected','def do_GET','def do_POST','protected_service','path ==','authenticate','set_session','send_html','safe_static','root']):
                    line=re.sub(r'(password|hash|secret|token)([^\s]*)', r'\1_REDACTED', line, flags=re.I)
                    print(f'{i}:{line}')
    else:
        for i,line in enumerate(text.splitlines(),1):
            if re.search(r'sotlab|login|form|action|reverse_proxy|root|main_portal|music|8090|file_server|handle', line, re.I):
                line=re.sub(r'(password|hash|secret|token)([^\s]*)', r'\1_REDACTED', line, flags=re.I)
                print(f'{i}:{line[:240]}')
