phoebus_os/installer/tests/vm/oc-forward.py
Phoebus 359f083bc2
Some checks are pending
ci / test (push) Waiting to run
test(gui): drive the OpenCode AI step through the real UI
Add an 'opencode' harness mode with a guest-loopback relay (the tunnel
shape), keyboard selection of the OpenCode chip, UI entry of the server/
provider/model (and a 0600 server password), and assertions on the
frontend's secret-free state (tested, validated, applied). Record the VM
evidence and the remaining not-run full-install gate.
2026-09-22 03:32:07 -07:00

71 lines
2.2 KiB
Bash

#!/usr/bin/env python3
"""Guest-local TCP forwarder for OpenCode VM testing.
The Phoebus transport policy allows plain `http://` only to a **loopback**
destination. A VM guest cannot name the host's loopback directly, and the QEMU
gateway address (`10.0.2.2`) is not loopback. This forwarder is the same shape
as an SSH tunnel: the GUI connects to the guest's own loopback, and the
forwarder relays to the host's OpenCode server over the QEMU host gateway. The
connection therefore never leaves the machine pair.
oc-forward.py [listen_port] [target_host] [target_port]
It is test infrastructure, not a product component.
"""
from __future__ import annotations
import socket
import sys
import threading
LISTEN_HOST = "127.0.0.1"
def pipe(src: socket.socket, dst: socket.socket) -> None:
try:
while True:
data = src.recv(65536)
if not data:
break
dst.sendall(data)
except OSError:
pass
finally:
for sock in (src, dst):
try:
sock.shutdown(socket.SHUT_RDWR)
except OSError:
pass
def handle(client: socket.socket, target: tuple[str, int]) -> None:
try:
upstream = socket.create_connection(target)
except OSError:
client.close()
return
threading.Thread(target=pipe, args=(client, upstream), daemon=True).start()
threading.Thread(target=pipe, args=(upstream, client), daemon=True).start()
def main() -> int:
listen_port = int(sys.argv[1]) if len(sys.argv) > 1 else 4096
target_host = sys.argv[2] if len(sys.argv) > 2 else "10.0.2.2"
target_port = int(sys.argv[3]) if len(sys.argv) > 3 else 41999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((LISTEN_HOST, listen_port))
server.listen(32)
print(f"oc-forward listening on {LISTEN_HOST}:{listen_port} -> {target_host}:{target_port}", flush=True)
while True:
try:
client, _ = server.accept()
except OSError:
break
threading.Thread(target=handle, args=(client, (target_host, target_port)), daemon=True).start()
return 0
if __name__ == "__main__":
sys.exit(main())