feat: agor-pty crate — standalone PTY multiplexer daemon (Phase 1 WIP)
New crate at agor-pty/ (standalone, not in workspace — portable to Tauri/Electrobun): - Rust daemon (agor-ptyd) with Unix socket IPC, JSON-framed protocol - PTY session lifecycle: create, resize, write, close, output fanout - 256-bit token auth, multi-client support, session persistence - TypeScript IPC client at clients/ts/pty-client.ts (Bun + Node.js) - Protocol: 9 client messages, 7 daemon messages - Based on tribunal ruling (78% confidence, 4 rounds, 55 objections) WIP: Rust implementation in progress (protocol.rs + auth.rs done)
This commit is contained in:
parent
e8132b7dc6
commit
4b5583430d
6 changed files with 861 additions and 0 deletions
31
agor-pty/Cargo.toml
Normal file
31
agor-pty/Cargo.toml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
[package]
|
||||
name = "agor-pty"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "Standalone PTY multiplexer daemon — manages terminal sessions via Unix socket IPC"
|
||||
license = "MIT"
|
||||
|
||||
# Binary: the daemon process
|
||||
[[bin]]
|
||||
name = "agor-ptyd"
|
||||
path = "src/main.rs"
|
||||
|
||||
# Library: shared types for IPC clients (Tauri, Electrobun, tests)
|
||||
[lib]
|
||||
name = "agor_pty"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
portable-pty = "0.8"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
log = "0.4"
|
||||
env_logger = "0.11"
|
||||
nix = { version = "0.29", features = ["process", "signal", "ioctl", "term"] }
|
||||
rand = "0.8"
|
||||
hex = "0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio-test = "0.4"
|
||||
90
agor-pty/README.md
Normal file
90
agor-pty/README.md
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
# agor-pty — PTY Multiplexer Daemon
|
||||
|
||||
Standalone Rust daemon that manages terminal sessions via Unix socket IPC.
|
||||
Portable across Tauri and Electrobun frontends.
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
Frontend (Electrobun/Tauri)
|
||||
↕ Unix Socket IPC (JSON-framed)
|
||||
agor-ptyd (Rust daemon)
|
||||
↕ PTY master FDs
|
||||
Shell processes (/bin/bash, etc.)
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- **PTY multiplexing**: single daemon manages all terminal sessions
|
||||
- **Session persistence**: sessions survive frontend disconnects/restarts
|
||||
- **Multi-client**: multiple frontends can connect and subscribe to session output
|
||||
- **Auth**: 256-bit token authentication per connection
|
||||
- **Output fanout**: PTY output fanned to all subscribed clients (non-blocking)
|
||||
- **Graceful shutdown**: SIGTERM/SIGINT cleanup
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
# Build
|
||||
cd agor-pty && cargo build --release
|
||||
|
||||
# Run daemon
|
||||
./target/release/agor-ptyd --verbose
|
||||
|
||||
# Custom socket directory
|
||||
./target/release/agor-ptyd --socket-dir /tmp/agor-test
|
||||
|
||||
# Custom default shell
|
||||
./target/release/agor-ptyd --shell /bin/zsh
|
||||
```
|
||||
|
||||
## IPC Protocol
|
||||
|
||||
JSON messages over Unix socket, newline-delimited.
|
||||
|
||||
### Client → Daemon
|
||||
- `Auth { token }` — authenticate (must be first message)
|
||||
- `CreateSession { id, shell, cwd, env, cols, rows }` — spawn shell
|
||||
- `WriteInput { session_id, data }` — send input to PTY
|
||||
- `Resize { session_id, cols, rows }` — resize terminal
|
||||
- `Subscribe { session_id }` — receive output from session
|
||||
- `Unsubscribe { session_id }` — stop receiving output
|
||||
- `CloseSession { session_id }` — kill session
|
||||
- `ListSessions` — get all active sessions
|
||||
- `Ping` — keepalive
|
||||
|
||||
### Daemon → Client
|
||||
- `AuthResult { ok }` — auth response
|
||||
- `SessionCreated { session_id, pid }` — session spawned
|
||||
- `SessionOutput { session_id, data }` — PTY output (base64)
|
||||
- `SessionClosed { session_id, exit_code }` — session ended
|
||||
- `SessionList { sessions }` — list response
|
||||
- `Pong` — keepalive response
|
||||
- `Error { message }` — error
|
||||
|
||||
## Integration
|
||||
|
||||
### As library (for Tauri)
|
||||
```rust
|
||||
use agor_pty::protocol::{ClientMessage, DaemonMessage};
|
||||
```
|
||||
|
||||
### TypeScript client (for Electrobun)
|
||||
See `clients/ts/` for the IPC client module.
|
||||
|
||||
## Directory Structure
|
||||
|
||||
```
|
||||
agor-pty/
|
||||
├── Cargo.toml
|
||||
├── README.md
|
||||
├── src/
|
||||
│ ├── main.rs — daemon entry, CLI, signals
|
||||
│ ├── lib.rs — library re-exports
|
||||
│ ├── protocol.rs — IPC message types
|
||||
│ ├── session.rs — PTY session management
|
||||
│ ├── daemon.rs — Unix socket server
|
||||
│ └── auth.rs — token authentication
|
||||
└── clients/
|
||||
└── ts/ — TypeScript IPC client (for Electrobun/Bun)
|
||||
```
|
||||
233
agor-pty/clients/ts/pty-client.ts
Normal file
233
agor-pty/clients/ts/pty-client.ts
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
/**
|
||||
* agor-pty TypeScript IPC client.
|
||||
* Connects to the agor-ptyd daemon via Unix socket.
|
||||
* Works with both Bun (Electrobun) and Node.js (Tauri sidecar).
|
||||
*/
|
||||
|
||||
import { connect, type Socket } from "net";
|
||||
import { readFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
// ── IPC Protocol Types ──────────────────────────────────────────
|
||||
|
||||
export interface SessionInfo {
|
||||
id: string;
|
||||
pid: number;
|
||||
shell: string;
|
||||
cwd: string;
|
||||
cols: number;
|
||||
rows: number;
|
||||
created_at: number;
|
||||
alive: boolean;
|
||||
}
|
||||
|
||||
export type DaemonEvent =
|
||||
| { type: "auth_result"; ok: boolean }
|
||||
| { type: "session_created"; session_id: string; pid: number }
|
||||
| { type: "session_output"; session_id: string; data: number[] }
|
||||
| { type: "session_closed"; session_id: string; exit_code: number | null }
|
||||
| { type: "session_list"; sessions: SessionInfo[] }
|
||||
| { type: "pong" }
|
||||
| { type: "error"; message: string };
|
||||
|
||||
// ── Client ──────────────────────────────────────────────────────
|
||||
|
||||
export class PtyClient extends EventEmitter {
|
||||
private socket: Socket | null = null;
|
||||
private buffer = "";
|
||||
private authenticated = false;
|
||||
private socketPath: string;
|
||||
private tokenPath: string;
|
||||
|
||||
constructor(socketDir?: string) {
|
||||
super();
|
||||
const dir =
|
||||
socketDir ??
|
||||
(process.env.XDG_RUNTIME_DIR
|
||||
? join(process.env.XDG_RUNTIME_DIR, "agor")
|
||||
: join(
|
||||
process.env.HOME ?? "/tmp",
|
||||
".local",
|
||||
"share",
|
||||
"agor",
|
||||
"run"
|
||||
));
|
||||
this.socketPath = join(dir, "ptyd.sock");
|
||||
this.tokenPath = join(dir, "ptyd.token");
|
||||
}
|
||||
|
||||
/** Connect to daemon and authenticate. */
|
||||
async connect(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let token: string;
|
||||
try {
|
||||
token = readFileSync(this.tokenPath, "utf-8").trim();
|
||||
} catch {
|
||||
reject(new Error(`Cannot read token at ${this.tokenPath}. Is agor-ptyd running?`));
|
||||
return;
|
||||
}
|
||||
|
||||
this.socket = connect(this.socketPath);
|
||||
|
||||
this.socket.on("connect", () => {
|
||||
this.send({ type: "auth", token });
|
||||
});
|
||||
|
||||
this.socket.on("data", (chunk: Buffer) => {
|
||||
this.buffer += chunk.toString("utf-8");
|
||||
let newlineIdx: number;
|
||||
while ((newlineIdx = this.buffer.indexOf("\n")) !== -1) {
|
||||
const line = this.buffer.slice(0, newlineIdx);
|
||||
this.buffer = this.buffer.slice(newlineIdx + 1);
|
||||
try {
|
||||
const msg = JSON.parse(line) as DaemonEvent;
|
||||
if (!this.authenticated && msg.type === "auth_result") {
|
||||
if (msg.ok) {
|
||||
this.authenticated = true;
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error("Authentication failed"));
|
||||
}
|
||||
} else {
|
||||
this.emit("message", msg);
|
||||
this.emit(msg.type, msg);
|
||||
}
|
||||
} catch {
|
||||
// Ignore malformed lines
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.socket.on("error", (err) => {
|
||||
if (!this.authenticated) reject(err);
|
||||
this.emit("error", err);
|
||||
});
|
||||
|
||||
this.socket.on("close", () => {
|
||||
this.authenticated = false;
|
||||
this.emit("close");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/** Create a new PTY session. */
|
||||
createSession(opts: {
|
||||
id: string;
|
||||
shell?: string;
|
||||
cwd?: string;
|
||||
env?: Record<string, string>;
|
||||
cols?: number;
|
||||
rows?: number;
|
||||
}): void {
|
||||
this.send({
|
||||
type: "create_session",
|
||||
id: opts.id,
|
||||
shell: opts.shell ?? null,
|
||||
cwd: opts.cwd ?? null,
|
||||
env: opts.env ?? null,
|
||||
cols: opts.cols ?? 80,
|
||||
rows: opts.rows ?? 24,
|
||||
});
|
||||
}
|
||||
|
||||
/** Write input to a session's PTY. */
|
||||
writeInput(sessionId: string, data: string | Uint8Array): void {
|
||||
const bytes =
|
||||
typeof data === "string"
|
||||
? Array.from(new TextEncoder().encode(data))
|
||||
: Array.from(data);
|
||||
this.send({ type: "write_input", session_id: sessionId, data: bytes });
|
||||
}
|
||||
|
||||
/** Resize a session's terminal. */
|
||||
resize(sessionId: string, cols: number, rows: number): void {
|
||||
this.send({ type: "resize", session_id: sessionId, cols, rows });
|
||||
}
|
||||
|
||||
/** Subscribe to a session's output. */
|
||||
subscribe(sessionId: string): void {
|
||||
this.send({ type: "subscribe", session_id: sessionId });
|
||||
}
|
||||
|
||||
/** Unsubscribe from a session's output. */
|
||||
unsubscribe(sessionId: string): void {
|
||||
this.send({ type: "unsubscribe", session_id: sessionId });
|
||||
}
|
||||
|
||||
/** Close/kill a session. */
|
||||
closeSession(sessionId: string): void {
|
||||
this.send({ type: "close_session", session_id: sessionId });
|
||||
}
|
||||
|
||||
/** List all active sessions. */
|
||||
listSessions(): void {
|
||||
this.send({ type: "list_sessions" });
|
||||
}
|
||||
|
||||
/** Ping the daemon. */
|
||||
ping(): void {
|
||||
this.send({ type: "ping" });
|
||||
}
|
||||
|
||||
/** Disconnect from daemon (sessions stay alive). */
|
||||
disconnect(): void {
|
||||
this.socket?.end();
|
||||
this.socket = null;
|
||||
this.authenticated = false;
|
||||
}
|
||||
|
||||
/** Check if connected and authenticated. */
|
||||
get isConnected(): boolean {
|
||||
return this.authenticated && this.socket !== null && !this.socket.destroyed;
|
||||
}
|
||||
|
||||
private send(msg: Record<string, unknown>): void {
|
||||
if (!this.socket || this.socket.destroyed) {
|
||||
throw new Error("Not connected to daemon");
|
||||
}
|
||||
this.socket.write(JSON.stringify(msg) + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
// ── Convenience: auto-connect + session helpers ─────────────────
|
||||
|
||||
/**
|
||||
* Connect to daemon, create a session, and return output as an async iterator.
|
||||
* Usage:
|
||||
* for await (const chunk of ptySession("my-shell", { cols: 120, rows: 40 })) {
|
||||
* terminal.write(new Uint8Array(chunk));
|
||||
* }
|
||||
*/
|
||||
export async function* ptySession(
|
||||
sessionId: string,
|
||||
opts?: { shell?: string; cwd?: string; cols?: number; rows?: number; socketDir?: string }
|
||||
): AsyncGenerator<number[], void, void> {
|
||||
const client = new PtyClient(opts?.socketDir);
|
||||
await client.connect();
|
||||
|
||||
client.createSession({
|
||||
id: sessionId,
|
||||
shell: opts?.shell,
|
||||
cwd: opts?.cwd,
|
||||
cols: opts?.cols ?? 80,
|
||||
rows: opts?.rows ?? 24,
|
||||
});
|
||||
client.subscribe(sessionId);
|
||||
|
||||
try {
|
||||
while (client.isConnected) {
|
||||
const msg: DaemonEvent = await new Promise((resolve) => {
|
||||
client.once("message", resolve);
|
||||
});
|
||||
|
||||
if (msg.type === "session_output" && msg.session_id === sessionId) {
|
||||
yield msg.data;
|
||||
} else if (msg.type === "session_closed" && msg.session_id === sessionId) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
59
agor-pty/src/auth.rs
Normal file
59
agor-pty/src/auth.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use std::fs;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::Path;
|
||||
|
||||
use rand::RngCore;
|
||||
|
||||
/// Holds the 256-bit authentication token for this daemon process.
|
||||
#[derive(Clone)]
|
||||
pub struct AuthToken {
|
||||
hex: String,
|
||||
}
|
||||
|
||||
impl AuthToken {
|
||||
/// Generate a fresh random token and write it to `<socket_dir>/ptyd.token`
|
||||
/// with 0600 permissions so only the owning user can read it.
|
||||
pub fn generate_and_persist(socket_dir: &Path) -> Result<Self, String> {
|
||||
let mut raw = [0u8; 32];
|
||||
rand::thread_rng().fill_bytes(&mut raw);
|
||||
let hex = hex::encode(raw);
|
||||
|
||||
let token_path = socket_dir.join("ptyd.token");
|
||||
fs::write(&token_path, &hex)
|
||||
.map_err(|e| format!("failed to write token file {token_path:?}: {e}"))?;
|
||||
fs::set_permissions(&token_path, fs::Permissions::from_mode(0o600))
|
||||
.map_err(|e| format!("failed to chmod token file: {e}"))?;
|
||||
|
||||
log::info!("token written to {:?}", token_path);
|
||||
Ok(Self { hex })
|
||||
}
|
||||
|
||||
/// Return true if the provided string matches the stored token.
|
||||
/// Comparison is done in constant time via `hex::decode` + byte-level loop
|
||||
/// to avoid short-circuit timing leaks.
|
||||
pub fn verify(&self, presented: &str) -> bool {
|
||||
// Decode both sides; if either fails the token is wrong.
|
||||
let expected = match hex::decode(&self.hex) {
|
||||
Ok(b) => b,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let provided = match hex::decode(presented) {
|
||||
Ok(b) => b,
|
||||
Err(_) => return false,
|
||||
};
|
||||
if expected.len() != provided.len() {
|
||||
return false;
|
||||
}
|
||||
// Constant-time compare.
|
||||
let mut diff = 0u8;
|
||||
for (a, b) in expected.iter().zip(provided.iter()) {
|
||||
diff |= a ^ b;
|
||||
}
|
||||
diff == 0
|
||||
}
|
||||
|
||||
/// Expose the hex string for logging (first 8 chars only, for safety).
|
||||
pub fn redacted(&self) -> String {
|
||||
format!("{}...", &self.hex[..8])
|
||||
}
|
||||
}
|
||||
166
agor-pty/src/protocol.rs
Normal file
166
agor-pty/src/protocol.rs
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
use std::collections::HashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Messages sent from client → daemon.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum ClientMessage {
|
||||
Auth {
|
||||
token: String,
|
||||
},
|
||||
CreateSession {
|
||||
id: String,
|
||||
shell: Option<String>,
|
||||
cwd: Option<String>,
|
||||
env: Option<HashMap<String, String>>,
|
||||
cols: u16,
|
||||
rows: u16,
|
||||
},
|
||||
WriteInput {
|
||||
session_id: String,
|
||||
/// Raw bytes encoded as a base64 string.
|
||||
data: String,
|
||||
},
|
||||
Resize {
|
||||
session_id: String,
|
||||
cols: u16,
|
||||
rows: u16,
|
||||
},
|
||||
Subscribe {
|
||||
session_id: String,
|
||||
},
|
||||
Unsubscribe {
|
||||
session_id: String,
|
||||
},
|
||||
CloseSession {
|
||||
session_id: String,
|
||||
},
|
||||
ListSessions,
|
||||
Ping,
|
||||
}
|
||||
|
||||
/// Messages sent from daemon → client.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum DaemonMessage {
|
||||
AuthResult {
|
||||
ok: bool,
|
||||
},
|
||||
SessionCreated {
|
||||
session_id: String,
|
||||
pid: u32,
|
||||
},
|
||||
/// PTY output bytes base64-encoded so they survive JSON transport.
|
||||
SessionOutput {
|
||||
session_id: String,
|
||||
data: String,
|
||||
},
|
||||
SessionClosed {
|
||||
session_id: String,
|
||||
exit_code: Option<i32>,
|
||||
},
|
||||
SessionList {
|
||||
sessions: Vec<SessionInfo>,
|
||||
},
|
||||
Pong,
|
||||
Error {
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Snapshot of a running or recently-exited session.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SessionInfo {
|
||||
pub id: String,
|
||||
pub pid: u32,
|
||||
pub shell: String,
|
||||
pub cwd: String,
|
||||
pub cols: u16,
|
||||
pub rows: u16,
|
||||
/// Unix timestamp of session creation.
|
||||
pub created_at: u64,
|
||||
pub alive: bool,
|
||||
}
|
||||
|
||||
/// Encode raw bytes as base64 for embedding in JSON.
|
||||
pub fn encode_output(bytes: &[u8]) -> String {
|
||||
use std::fmt::Write as FmtWrite;
|
||||
// Manual base64 — avoids adding a new crate; the hex crate IS available but
|
||||
// base64 better compresses binary PTY data (33% overhead vs 100% for hex).
|
||||
// We use a simple lookup table implementation that stays within 300 lines.
|
||||
const TABLE: &[u8; 64] =
|
||||
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
let mut out = String::with_capacity((bytes.len() + 2) / 3 * 4);
|
||||
let mut i = 0;
|
||||
while i + 2 < bytes.len() {
|
||||
let b0 = bytes[i] as usize;
|
||||
let b1 = bytes[i + 1] as usize;
|
||||
let b2 = bytes[i + 2] as usize;
|
||||
let _ = write!(
|
||||
out,
|
||||
"{}{}{}{}",
|
||||
TABLE[b0 >> 2] as char,
|
||||
TABLE[((b0 & 3) << 4) | (b1 >> 4)] as char,
|
||||
TABLE[((b1 & 0xf) << 2) | (b2 >> 6)] as char,
|
||||
TABLE[b2 & 0x3f] as char
|
||||
);
|
||||
i += 3;
|
||||
}
|
||||
let rem = bytes.len() - i;
|
||||
if rem == 1 {
|
||||
let b0 = bytes[i] as usize;
|
||||
let _ = write!(
|
||||
out,
|
||||
"{}{}==",
|
||||
TABLE[b0 >> 2] as char,
|
||||
TABLE[(b0 & 3) << 4] as char
|
||||
);
|
||||
} else if rem == 2 {
|
||||
let b0 = bytes[i] as usize;
|
||||
let b1 = bytes[i + 1] as usize;
|
||||
let _ = write!(
|
||||
out,
|
||||
"{}{}{}=",
|
||||
TABLE[b0 >> 2] as char,
|
||||
TABLE[((b0 & 3) << 4) | (b1 >> 4)] as char,
|
||||
TABLE[(b1 & 0xf) << 2] as char
|
||||
);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Decode base64 string back to bytes. Returns error string on invalid input.
|
||||
pub fn decode_input(s: &str) -> Result<Vec<u8>, String> {
|
||||
fn val(c: u8) -> Result<u8, String> {
|
||||
match c {
|
||||
b'A'..=b'Z' => Ok(c - b'A'),
|
||||
b'a'..=b'z' => Ok(c - b'a' + 26),
|
||||
b'0'..=b'9' => Ok(c - b'0' + 52),
|
||||
b'+' => Ok(62),
|
||||
b'/' => Ok(63),
|
||||
b'=' => Ok(0),
|
||||
_ => Err(format!("invalid base64 char: {c}")),
|
||||
}
|
||||
}
|
||||
let bytes = s.as_bytes();
|
||||
if bytes.len() % 4 != 0 {
|
||||
return Err("base64 length not a multiple of 4".into());
|
||||
}
|
||||
let mut out = Vec::with_capacity(bytes.len() / 4 * 3);
|
||||
let mut i = 0;
|
||||
while i < bytes.len() {
|
||||
let v0 = val(bytes[i])?;
|
||||
let v1 = val(bytes[i + 1])?;
|
||||
let v2 = val(bytes[i + 2])?;
|
||||
let v3 = val(bytes[i + 3])?;
|
||||
out.push((v0 << 2) | (v1 >> 4));
|
||||
if bytes[i + 2] != b'=' {
|
||||
out.push((v1 << 4) | (v2 >> 2));
|
||||
}
|
||||
if bytes[i + 3] != b'=' {
|
||||
out.push((v2 << 6) | v3);
|
||||
}
|
||||
i += 4;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
282
agor-pty/src/session.rs
Normal file
282
agor-pty/src/session.rs
Normal file
|
|
@ -0,0 +1,282 @@
|
|||
use std::collections::HashMap;
|
||||
use std::io::{Read, Write as IoWrite};
|
||||
use std::sync::Arc;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use portable_pty::{native_pty_system, CommandBuilder, PtySize};
|
||||
use tokio::sync::{broadcast, Mutex};
|
||||
|
||||
use crate::protocol::SessionInfo;
|
||||
|
||||
const OUTPUT_CHANNEL_CAP: usize = 256;
|
||||
|
||||
/// A live PTY session.
|
||||
pub struct Session {
|
||||
pub id: String,
|
||||
pub pid: u32,
|
||||
pub shell: String,
|
||||
pub cwd: String,
|
||||
pub cols: u16,
|
||||
pub rows: u16,
|
||||
pub created_at: u64,
|
||||
/// Used to write input into the PTY master.
|
||||
writer: Arc<Mutex<Box<dyn IoWrite + Send>>>,
|
||||
/// Broadcast channel — subscribers receive raw output chunks.
|
||||
pub tx: broadcast::Sender<Vec<u8>>,
|
||||
/// Set to false when the child process exits.
|
||||
pub alive: Arc<std::sync::atomic::AtomicBool>,
|
||||
/// Last known exit code (populated by the reader task on process exit).
|
||||
pub exit_code: Arc<Mutex<Option<i32>>>,
|
||||
/// Keep the master alive so the PTY stays open.
|
||||
_master: Box<dyn portable_pty::MasterPty + Send>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn snapshot(&self) -> SessionInfo {
|
||||
SessionInfo {
|
||||
id: self.id.clone(),
|
||||
pid: self.pid,
|
||||
shell: self.shell.clone(),
|
||||
cwd: self.cwd.clone(),
|
||||
cols: self.cols,
|
||||
rows: self.rows,
|
||||
created_at: self.created_at,
|
||||
alive: self.alive.load(std::sync::atomic::Ordering::Relaxed),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write bytes into the PTY (user keystrokes, paste, etc.).
|
||||
pub async fn write_input(&self, data: &[u8]) -> Result<(), String> {
|
||||
let mut w = self.writer.lock().await;
|
||||
w.write_all(data)
|
||||
.map_err(|e| format!("PTY write failed for session {}: {e}", self.id))
|
||||
}
|
||||
|
||||
/// Send TIOCSWINSZ to resize the PTY.
|
||||
pub fn resize(&mut self, cols: u16, rows: u16) -> Result<(), String> {
|
||||
self.cols = cols;
|
||||
self.rows = rows;
|
||||
// portable-pty exposes resize via the master handle which we've moved.
|
||||
// We reach into nix directly via the stored master fd.
|
||||
// portable-pty's MasterPty trait has `resize` on nightly targets; on
|
||||
// stable we use nix ourselves.
|
||||
log::debug!(
|
||||
"session {} resize → {}x{} (handled via pty master)",
|
||||
self.id, cols, rows
|
||||
);
|
||||
// The resize is done by the caller via `master.resize()` before this
|
||||
// method; this method just updates our cached dimensions.
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn subscribe(&self) -> broadcast::Receiver<Vec<u8>> {
|
||||
self.tx.subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
/// Owns all sessions and serialises mutations.
|
||||
pub struct SessionManager {
|
||||
sessions: HashMap<String, Session>,
|
||||
default_shell: String,
|
||||
}
|
||||
|
||||
impl SessionManager {
|
||||
pub fn new(default_shell: String) -> Self {
|
||||
Self {
|
||||
sessions: HashMap::new(),
|
||||
default_shell,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create and start a new PTY session. Returns the session id, pid, and a
|
||||
/// receiver end of the output broadcast channel.
|
||||
pub fn create_session(
|
||||
&mut self,
|
||||
id: String,
|
||||
shell: Option<String>,
|
||||
cwd: Option<String>,
|
||||
env: Option<HashMap<String, String>>,
|
||||
cols: u16,
|
||||
rows: u16,
|
||||
// Callback invoked from the reader task when the child exits.
|
||||
on_exit: impl FnOnce(String, Option<i32>) + Send + 'static,
|
||||
) -> Result<(u32, broadcast::Receiver<Vec<u8>>), String> {
|
||||
if self.sessions.contains_key(&id) {
|
||||
return Err(format!("session {id} already exists"));
|
||||
}
|
||||
|
||||
let shell_path = shell.unwrap_or_else(|| self.default_shell.clone());
|
||||
let pty_system = native_pty_system();
|
||||
let pair = pty_system
|
||||
.openpty(PtySize {
|
||||
rows,
|
||||
cols,
|
||||
pixel_width: 0,
|
||||
pixel_height: 0,
|
||||
})
|
||||
.map_err(|e| format!("openpty failed: {e}"))?;
|
||||
|
||||
let mut cmd = CommandBuilder::new(&shell_path);
|
||||
if let Some(ref dir) = cwd {
|
||||
cmd.cwd(dir);
|
||||
}
|
||||
if let Some(ref vars) = env {
|
||||
for (k, v) in vars {
|
||||
cmd.env(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
let child = pair
|
||||
.slave
|
||||
.spawn_command(cmd)
|
||||
.map_err(|e| format!("spawn failed: {e}"))?;
|
||||
|
||||
let pid = child.process_id().unwrap_or(0);
|
||||
let cwd_str = cwd.unwrap_or_else(|| std::env::current_dir()
|
||||
.map(|p| p.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|_| "/".into()));
|
||||
|
||||
// portable-pty requires us to take the writer from the master.
|
||||
let writer = pair
|
||||
.master
|
||||
.take_writer()
|
||||
.map_err(|e| format!("take_writer failed: {e}"))?;
|
||||
|
||||
// Obtain a blocking reader for the reader task.
|
||||
let reader = pair
|
||||
.master
|
||||
.try_clone_reader()
|
||||
.map_err(|e| format!("clone_reader failed: {e}"))?;
|
||||
|
||||
let (tx, rx) = broadcast::channel(OUTPUT_CHANNEL_CAP);
|
||||
let alive = Arc::new(std::sync::atomic::AtomicBool::new(true));
|
||||
let exit_code = Arc::new(Mutex::new(None::<i32>));
|
||||
|
||||
// Spawn a blocking task to drain PTY output and broadcast it.
|
||||
let tx_clone = tx.clone();
|
||||
let alive_clone = alive.clone();
|
||||
let exit_code_clone = exit_code.clone();
|
||||
let id_clone = id.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
read_pty_output(
|
||||
reader,
|
||||
tx_clone,
|
||||
alive_clone,
|
||||
exit_code_clone,
|
||||
id_clone,
|
||||
on_exit,
|
||||
child,
|
||||
);
|
||||
});
|
||||
|
||||
let session = Session {
|
||||
id: id.clone(),
|
||||
pid,
|
||||
shell: shell_path,
|
||||
cwd: cwd_str,
|
||||
cols,
|
||||
rows,
|
||||
created_at: unix_now(),
|
||||
writer: Arc::new(Mutex::new(writer)),
|
||||
tx,
|
||||
alive,
|
||||
exit_code,
|
||||
_master: pair.master,
|
||||
};
|
||||
|
||||
log::info!("created session {id} pid={pid}");
|
||||
self.sessions.insert(id, session);
|
||||
Ok((pid, rx))
|
||||
}
|
||||
|
||||
pub fn get(&self, id: &str) -> Option<&Session> {
|
||||
self.sessions.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, id: &str) -> Option<&mut Session> {
|
||||
self.sessions.get_mut(id)
|
||||
}
|
||||
|
||||
pub fn list(&self) -> Vec<SessionInfo> {
|
||||
self.sessions.values().map(|s| s.snapshot()).collect()
|
||||
}
|
||||
|
||||
/// Close a session: the child is killed if still alive and the entry is
|
||||
/// removed after a brief wait for the reader task to notice.
|
||||
pub fn close_session(&mut self, id: &str) -> Result<(), String> {
|
||||
if self.sessions.remove(id).is_some() {
|
||||
log::info!("closed session {id}");
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("session {id} not found"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sessions(&self) -> &HashMap<String, Session> {
|
||||
&self.sessions
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Private helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn unix_now() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_secs())
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Blocking PTY reader — lives in a `spawn_blocking` task.
|
||||
fn read_pty_output(
|
||||
mut reader: Box<dyn Read + Send>,
|
||||
tx: broadcast::Sender<Vec<u8>>,
|
||||
alive: Arc<std::sync::atomic::AtomicBool>,
|
||||
exit_code_cell: Arc<Mutex<Option<i32>>>,
|
||||
id: String,
|
||||
on_exit: impl FnOnce(String, Option<i32>),
|
||||
mut child: Box<dyn portable_pty::Child + Send>,
|
||||
) {
|
||||
let mut buf = [0u8; 4096];
|
||||
loop {
|
||||
match reader.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
let chunk = buf[..n].to_vec();
|
||||
// Non-blocking send — if all receivers are gone, ignore.
|
||||
let _ = tx.send(chunk);
|
||||
}
|
||||
Err(e) => {
|
||||
log::debug!("session {id} reader error: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PTY EOF — child has exited (or master was closed).
|
||||
alive.store(false, std::sync::atomic::AtomicBool::from(false).load(std::sync::atomic::Ordering::SeqCst).into());
|
||||
alive.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||
|
||||
let code = child.wait().ok().and_then(|status| {
|
||||
if let Some(exit) = status.exit_code() {
|
||||
Some(exit as i32)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
// Write exit code into the shared cell.
|
||||
// We're in a blocking context so we use try_lock in a tight spin — the
|
||||
// lock is never held for long.
|
||||
loop {
|
||||
if let Ok(mut guard) = exit_code_cell.try_lock() {
|
||||
*guard = code;
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
|
||||
log::info!("session {id} exited with code {:?}", code);
|
||||
on_exit(id, code);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue