refactor(server): extract helpers and simplify auth dispatch

- Move `sha256_hex` and cookie constants to module level; drop `token_is_hash` param in favour of regex auto-detection
- Make `_respond()` accept `status`/`headers`; add `_redirect()` helper to eliminate boilerplate in auth routes
- Simplify `_dispatch()` with a `PUBLIC_ROUTES` set and positive-logic early-return
- Restrict CORS header to file API; auth responses no longer send `Access-Control-Allow-Origin: *`
- Extract `clearPager()` in frontend and deduplicate `copyText` fallback path
This commit is contained in:
2026-07-29 18:08:36 +04:00
parent c467a4d254
commit a0c996b19d
4 changed files with 67 additions and 98 deletions
+3 -6
View File
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""Share — local file sharing server (HTTPS)."""
import hashlib
import json
import ssl
import sys
@@ -11,13 +10,13 @@ from pathlib import Path
from cert import ensure_cert, get_local_ip
from cleanup import cleanup_loop
from config import BASE_DIR, SECS_PER_DAY, client_cfg, load_config
from server import Handler, ShareServer
from server import Handler, ShareServer, sha256_hex
def main():
if len(sys.argv) > 1 and sys.argv[1] == "hash":
token = sys.argv[2] if len(sys.argv) > 2 else input("Token: ")
print(hashlib.sha256(token.encode()).hexdigest())
print(sha256_hex(token))
return
conf = load_config()
@@ -48,9 +47,7 @@ def main():
if not token:
print("\n WARNING: no token set — anyone on the network has full access.")
print(" Set `token` in share.config (see SETUP.md) or pass --token.")
# If token looks like a SHA-256 hash, pass it as pre-hashed
is_hash = len(token) == 64 and all(c in "0123456789abcdef" for c in token)
server = ShareServer(("0.0.0.0", port), Handler, upload_dir=upload_dir, html=html, ssl_ctx=ctx, token=token, token_is_hash=is_hash)
server = ShareServer(("0.0.0.0", port), Handler, upload_dir=upload_dir, html=html, ssl_ctx=ctx, token=token)
print("\n Share running")
print(f" Local: https://localhost:{port}")