Files
unshell/examples/protocol/remote_shell_receive.rs
T

71 lines
2.1 KiB
Rust
Raw Normal View History

2026-04-25 16:27:10 -06:00
#[path = "../../src/leaf/remote_shell/mod.rs"]
mod remote_shell;
2026-04-25 14:41:00 -06:00
use std::error::Error;
use std::net::TcpListener;
use unshell::protocol::tree::{Endpoint, Ingress, LocalEvent};
fn main() -> Result<(), Box<dyn Error>> {
2026-04-25 16:27:10 -06:00
let listener = TcpListener::bind(remote_shell::LISTEN_ADDR)?;
println!("listening on {}", remote_shell::LISTEN_ADDR);
2026-04-25 14:41:00 -06:00
let (mut stream, peer_addr) = listener.accept()?;
println!("accepted endpoint connection from {peer_addr}");
2026-04-25 16:27:10 -06:00
let frame_rx = remote_shell::spawn_frame_reader(stream.try_clone()?);
let mut endpoint = remote_shell::build_controller_endpoint();
2026-04-25 14:41:00 -06:00
let hook_id = endpoint.allocate_hook_id();
2026-04-25 16:27:10 -06:00
let shell_leaf_name = remote_shell::shell_leaf_name();
let open_procedure = remote_shell::shell_open_procedure();
2026-04-25 14:41:00 -06:00
2026-04-25 16:27:10 -06:00
remote_shell::send_forward(
2026-04-25 15:35:08 -06:00
&mut stream,
2026-04-25 16:27:10 -06:00
endpoint.send_call(
remote_shell::agent_path(),
Some(shell_leaf_name),
open_procedure.clone(),
Some(hook_id),
remote_shell::shell_open_payload(),
)?,
2026-04-25 14:41:00 -06:00
)?;
2026-04-25 15:35:08 -06:00
for (index, command) in ["pwd\n", "whoami\n", "exit\n"].iter().enumerate() {
2026-04-25 16:27:10 -06:00
remote_shell::send_forward(
2026-04-25 15:35:08 -06:00
&mut stream,
2026-04-25 16:27:10 -06:00
endpoint.send_data(
remote_shell::agent_path(),
hook_id,
open_procedure.clone(),
command.as_bytes().to_vec(),
index == 2,
)?,
2026-04-25 15:35:08 -06:00
)?;
}
2026-04-25 14:41:00 -06:00
for result in frame_rx {
let frame = result?;
2026-04-25 16:27:10 -06:00
let outcome = endpoint.receive(&Ingress::Child(remote_shell::agent_path()), frame)?;
2026-04-25 15:35:08 -06:00
let Some(event) = outcome.event else {
2026-04-25 14:41:00 -06:00
continue;
};
match event {
LocalEvent::Data { message, .. } => {
print!("{}", String::from_utf8_lossy(&message.data));
if message.end_hook {
break;
}
}
LocalEvent::Fault { message, .. } => {
eprintln!("received protocol fault: 0x{:02X}", message.fault.0);
break;
}
LocalEvent::Call { .. } => {}
}
}
Ok(())
}