File size: 1,703 Bytes
341e402 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | """Byte forwarder between a TCP port and a Unix socket. Python stdlib only.
The predictor's sandbox has its own empty network namespace (unshare -rn). Its only way out is this bridge:
inside the namespace: fwd.py tcp2unix 127.0.0.1:8844 /tmp/value_shim.sock (Claude Code -> socket)
on the host: fwd.py unix2tcp /tmp/value_shim.sock 127.0.0.1:8844 (socket -> Carbon shim)
So the sandbox can reach the Carbon shim and nothing else (the predictor spec's network allowlist).
"""
import asyncio
import os
import sys
async def pipe(r, w):
try:
while True:
b = await r.read(65536)
if not b:
break
w.write(b)
await w.drain()
except Exception:
pass
finally:
try:
w.close()
except Exception:
pass
async def main():
mode, src, dst = sys.argv[1:4]
async def handle(r, w):
try:
if mode == 'tcp2unix':
r2, w2 = await asyncio.open_unix_connection(dst)
else:
h, p = dst.rsplit(':', 1)
r2, w2 = await asyncio.open_connection(h, int(p))
except Exception:
w.close()
return
await asyncio.gather(pipe(r, w2), pipe(r2, w))
if mode == 'tcp2unix':
h, p = src.rsplit(':', 1)
srv = await asyncio.start_server(handle, h, int(p))
else:
if os.path.exists(src):
os.unlink(src)
srv = await asyncio.start_unix_server(handle, src)
os.chmod(src, 0o600)
print('ready', flush=True)
async with srv:
await srv.serve_forever()
if __name__ == '__main__':
asyncio.run(main())
|