"""Forwards 127.0.0.1: to : for each port given, until killed. Called by scripts/ci/redis-topology.sh when it runs inside a CI job container. The runner's job container does not share the network of the Docker daemon that starts the Compose lanes, so the daemon's published ports are on the daemon host, not on the job's localhost — while the Redis lanes are built around localhost: the cluster announces 127.0.0.1 in its redirects and the TLS lane's certificate names localhost. Forwarding keeps those lanes exactly as they run on a workstation. python3 scripts/ci/forward-ports.py ... """ import asyncio import sys async def pipe(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: try: while data := await reader.read(65536): writer.write(data) await writer.drain() except (ConnectionError, asyncio.CancelledError): pass finally: writer.close() async def serve(host: str, port: int) -> asyncio.base_events.Server: async def handle(client_reader, client_writer): try: upstream_reader, upstream_writer = await asyncio.open_connection(host, port) except OSError: client_writer.close() return await asyncio.gather(pipe(client_reader, upstream_writer), pipe(upstream_reader, client_writer)) return await asyncio.start_server(handle, "127.0.0.1", port) async def main(host: str, ports: list[int]) -> None: servers = [await serve(host, port) for port in ports] print(f"forward-ports: 127.0.0.1 -> {host} for {ports}", flush=True) await asyncio.gather(*(server.serve_forever() for server in servers)) if __name__ == "__main__": if len(sys.argv) < 3: sys.exit(__doc__) asyncio.run(main(sys.argv[1], [int(port) for port in sys.argv[2:]]))