Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /zen-alpha-ui /ws-pty-server.py
| #!/usr/bin/env python3 | |
| """WebSocket PTY bridge — spawns a real bash shell and bridges xterm.js. | |
| SECURITY: disabled by default. Must be started with --enable. | |
| Connections are rejected unless the client sends the shared token | |
| (?token=... in the WebSocket URL, or ZEN_ALPHA_TOKEN env for the server). | |
| """ | |
| import argparse | |
| import asyncio | |
| import hmac | |
| import json | |
| import os | |
| import pty | |
| import fcntl | |
| import struct | |
| import termios | |
| import signal | |
| import sys | |
| import websockets | |
| def _expected_token(): | |
| return os.environ.get("ZEN_ALPHA_TOKEN", "").strip() | |
| async def handler(ws): | |
| expected = _expected_token() | |
| query = dict((k, v) for k, _, v in (part.partition("=") for part in ws.request.path.split("?", 1)[-1].split("&") if "=" in part)) | |
| supplied = query.get("token", "") | |
| if not expected or not hmac.compare_digest(supplied, expected): | |
| await ws.close(code=4001, reason="unauthorized") | |
| return | |
| loop = asyncio.get_event_loop() | |
| pid, master_fd = pty.fork() | |
| if pid == 0: | |
| os.environ["TERM"] = "xterm-256color" | |
| os.environ["COLORTERM"] = "truecolor" | |
| os.execvp("/bin/bash", ["/bin/bash", "-l"]) | |
| def set_size(cols, rows): | |
| try: | |
| fcntl.ioctl(master_fd, termios.TIOCSWINSZ, struct.pack("HHHH", rows, cols, 0, 0)) | |
| except OSError: | |
| pass | |
| set_size(80, 24) | |
| def cleanup(): | |
| try: | |
| loop.remove_reader(master_fd) | |
| os.close(master_fd) | |
| except Exception: | |
| pass | |
| try: | |
| os.kill(pid, signal.SIGHUP) | |
| except Exception: | |
| pass | |
| async def pump(): | |
| loop.add_reader(master_fd, on_readable) | |
| def on_readable(): | |
| try: | |
| data = os.read(master_fd, 65536) | |
| except OSError: | |
| cleanup() | |
| return | |
| if not data: | |
| cleanup() | |
| return | |
| asyncio.ensure_future(ws.send(data.decode("utf-8", "replace"))) | |
| await pump() | |
| try: | |
| async for message in ws: | |
| if message.startswith("{"): | |
| try: | |
| payload = json.loads(message) | |
| if payload.get("type") == "resize": | |
| set_size(int(payload["cols"]), int(payload["rows"])) | |
| continue | |
| except Exception: | |
| pass | |
| try: | |
| os.write(master_fd, message.encode()) | |
| except OSError: | |
| break | |
| except websockets.ConnectionClosed: | |
| pass | |
| finally: | |
| cleanup() | |
| async def main(port): | |
| host = "127.0.0.1" | |
| async with websockets.serve(handler, host, port, max_size=2**20): | |
| print(f"PTY bridge listening on ws://{host}:{port} (auth required)", flush=True) | |
| await asyncio.Future() | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="WebSocket PTY bridge (opt-in, auth-required)") | |
| parser.add_argument("--enable", action="store_true", help="explicitly enable the PTY bridge (default: off)") | |
| parser.add_argument("--port", type=int, default=8765) | |
| args = parser.parse_args() | |
| if not args.enable: | |
| print("REFUSED: PTY bridge is disabled by default for security. Start with --enable.", flush=True) | |
| sys.exit(2) | |
| if not _expected_token(): | |
| print("REFUSED: ZEN_ALPHA_TOKEN env var must be set (matches backend auth).", flush=True) | |
| sys.exit(2) | |
| try: | |
| asyncio.run(main(args.port)) | |
| except KeyboardInterrupt: | |
| sys.exit(0) | |
Xet Storage Details
- Size:
- 3.51 kB
- Xet hash:
- 930155c19d866c2696b8aa0688f589195af3b8fc09182a5da604193b0a707e5b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.