Datasets:
Download source/src/simready/generation/runtime/egress_proxy.py from AndyZ2001/mingru-mojoco-runs: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/datasets/AndyZ2001/mingru-mojoco-runs/resolve/main/source/src/simready/generation/runtime/egress_proxy.py
- Command line
-
hf download hf://datasets/AndyZ2001/mingru-mojoco-runs/source/src/simready/generation/runtime/egress_proxy.py
-
curl -L -o egress_proxy.py https://huggingface.co/datasets/AndyZ2001/mingru-mojoco-runs/resolve/main/source/src/simready/generation/runtime/egress_proxy.py
3 kB
| """The only way out of a generation container: an HTTPS CONNECT proxy that opens tunnels to the agent CLI's own hosts. | |
| Agent containers sit on an internal Docker network with no route out; this proxy sits on that network and the default | |
| bridge. It tunnels CONNECT host:443 when host is in the allow-list (exact name or a subdomain of one), refuses everything | |
| else with 403, and logs one line per request. Plain HTTP is refused. Standard library only. | |
| python egress_proxy.py --port 3128 --allow api.anthropic.com,chatgpt.com [--log-all] | |
| """ | |
| import argparse | |
| import asyncio | |
| import sys | |
| def allowed(host, hosts): | |
| host = host.lower().rstrip(".") | |
| return any(host == h or host.endswith("." + h) for h in hosts) | |
| async def pipe(reader, writer): | |
| try: | |
| while data := await reader.read(65536): | |
| writer.write(data) | |
| await writer.drain() | |
| except (ConnectionError, asyncio.CancelledError): | |
| pass | |
| finally: | |
| writer.close() | |
| async def handle(reader, writer, hosts, log_all): | |
| try: | |
| head = await asyncio.wait_for(reader.readuntil(b"\r\n\r\n"), 30) | |
| except (asyncio.IncompleteReadError, asyncio.LimitOverrunError, asyncio.TimeoutError, ConnectionError): | |
| writer.close() | |
| return | |
| method, target = (head.split(b"\r\n", 1)[0].decode("latin-1").split(" ") + ["", ""])[:2] | |
| host, _, port = target.rpartition(":") | |
| ok = method == "CONNECT" and port == "443" and allowed(host, hosts) | |
| print(f"{'ALLOW' if ok else 'DENY'} {method} {target}", flush=True) | |
| if not ok and not log_all: | |
| writer.write(b"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n") | |
| await writer.drain() | |
| writer.close() | |
| return | |
| try: | |
| upstream_reader, upstream_writer = await asyncio.wait_for(asyncio.open_connection(host, int(port or 443)), 30) | |
| except (OSError, asyncio.TimeoutError, ValueError): | |
| writer.write(b"HTTP/1.1 502 Bad Gateway\r\nContent-Length: 0\r\n\r\n") | |
| await writer.drain() | |
| writer.close() | |
| return | |
| writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") | |
| await writer.drain() | |
| await asyncio.gather(pipe(reader, upstream_writer), pipe(upstream_reader, writer)) | |
| async def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--port", type=int, default=3128) | |
| parser.add_argument("--allow", required=True, help="Comma-separated host names; subdomains are included") | |
| parser.add_argument("--log-all", action="store_true", help="Discovery only: tunnel every CONNECT and log it") | |
| args = parser.parse_args() | |
| hosts = [h.strip().lower() for h in args.allow.split(",") if h.strip()] | |
| server = await asyncio.start_server(lambda r, w: handle(r, w, hosts, args.log_all), "0.0.0.0", args.port) | |
| print(f"egress proxy on :{args.port}, allowing {hosts}", flush=True) | |
| async with server: | |
| await server.serve_forever() | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |
| sys.exit(0) | |