config.py — share.config loading, constants, load_config(), client_cfg() cert.py — get_local_ip(), ensure_cert() cleanup.py — cleanup_loop() server.py — ShareServer, Handler, file_type() share.py — main() entry point only Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
52 lines
1.5 KiB
Python
Executable File
52 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Share — local file sharing server (HTTPS)."""
|
|
|
|
import json
|
|
import ssl
|
|
import threading
|
|
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
|
|
|
|
|
|
def main():
|
|
conf = load_config()
|
|
|
|
port = int(conf["port"])
|
|
upload_dir = Path(conf["dir"]).expanduser().resolve()
|
|
max_age = int(conf["ttl"]) * SECS_PER_DAY
|
|
san_list = conf["san"].split() if conf["san"] else None
|
|
|
|
upload_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
threading.Thread(target=cleanup_loop, args=(upload_dir, max_age), daemon=True).start()
|
|
|
|
ip = get_local_ip()
|
|
cfg_tag = f"<script>const CFG={json.dumps(client_cfg(conf))}</script>".encode()
|
|
html = (BASE_DIR / "static" / "index.html").read_bytes()
|
|
html = html.replace(b"<!--CFG-->", cfg_tag)
|
|
|
|
cert_dir = BASE_DIR / ".certs"
|
|
cert, key = ensure_cert(cert_dir, extra_ips=san_list)
|
|
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
ctx.load_cert_chain(cert, key)
|
|
|
|
server = ShareServer(("0.0.0.0", port), Handler, upload_dir=upload_dir, html=html, ssl_ctx=ctx)
|
|
|
|
print("\n Share running")
|
|
print(f" Local: https://localhost:{port}")
|
|
print(f" Network: https://{ip}:{port}")
|
|
print(f" Files: {upload_dir}\n")
|
|
try:
|
|
server.serve_forever()
|
|
except KeyboardInterrupt:
|
|
print("\n Stopped.")
|
|
server.server_close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|