#!/usr/bin/env python3 """Serve ONE test-build file on a private IP for a limited time, behind a random URL token. Use when a test APK/IPA is larger than the chat upload cap and the user wants to install it from the phone. Prefer a private overlay-network IP (e.g. `tailscale ip -4`): reachable only from the user's own devices, never from the public internet. A home LAN IP works too, but anyone on that network who learns the URL can download the file: use it only on a trusted network and stop the server when done. Safety: refuses 0.0.0.0 / :: and public addresses, serves only the named file (no directory listing), and puts a random token in the path. The server stops by itself after the time limit. Usage (terminal background=true; it is a server, no notify needed): env -u PYTHONPATH python3 -u serve-test-build.py [hours=3] > The first log line is the URL. Verify before sending: curl -sI "$(head -1 )" | head -3 """ import http.server, ipaddress, os, secrets, shutil, socketserver, sys, time folder, name, host = sys.argv[1], sys.argv[2], sys.argv[3] hours = float(sys.argv[4]) if len(sys.argv) > 4 else 3.0 ip = ipaddress.ip_address(host) cgnat = ipaddress.ip_network("100.64.0.0/10") # Tailscale / overlay range if ip.is_unspecified or not (ip.is_loopback or ip.is_private or ip in cgnat): sys.exit(f"Refusing to bind {host}: use a loopback, private LAN or overlay-network (100.64/10) address.") path = os.path.realpath(os.path.join(folder, name)) if os.path.dirname(path) != os.path.realpath(folder) or not os.path.isfile(path): sys.exit("File not found inside the given folder.") token = secrets.token_urlsafe(16) url_path = f"/{token}/{name}" class OneFile(http.server.BaseHTTPRequestHandler): def _send(self, body): if self.path != url_path: self.send_error(404) return self.send_response(200) self.send_header("Content-Type", "application/octet-stream") self.send_header("Content-Length", str(os.path.getsize(path))) self.send_header("Content-Disposition", f'attachment; filename="{name}"') self.end_headers() if body: with open(path, "rb") as f: shutil.copyfileobj(f, self.wfile) def do_GET(self): self._send(True) def do_HEAD(self): self._send(False) class Server(socketserver.ThreadingMixIn, http.server.HTTPServer): daemon_threads = True with Server((host, 0), OneFile) as s: # port 0: free port chosen by the OS, printed below print(f"http://{host}:{s.server_address[1]}{url_path}", flush=True) s.timeout = 1 deadline = time.time() + hours * 3600 while time.time() < deadline: s.handle_request()