2026-04-25 14:46:59 -06:00
|
|
|
#[path = "support/remote_shell_common.rs"]
|
2026-04-25 14:41:00 -06:00
|
|
|
mod common;
|
|
|
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
use std::net::TcpListener;
|
|
|
|
|
|
|
|
|
|
use unshell::protocol::tree::{Endpoint, Ingress, LocalEvent};
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
|
let listener = TcpListener::bind(common::LISTEN_ADDR)?;
|
|
|
|
|
println!("listening on {}", common::LISTEN_ADDR);
|
|
|
|
|
|
|
|
|
|
let (mut stream, peer_addr) = listener.accept()?;
|
|
|
|
|
println!("accepted endpoint connection from {peer_addr}");
|
|
|
|
|
|
|
|
|
|
let frame_rx = common::spawn_frame_reader(stream.try_clone()?);
|
|
|
|
|
let mut endpoint = common::build_controller_endpoint();
|
|
|
|
|
let hook_id = endpoint.allocate_hook_id();
|
|
|
|
|
let shell_leaf_name = common::shell_leaf_name();
|
2026-04-25 15:35:08 -06:00
|
|
|
let open_procedure = common::shell_open_procedure();
|
2026-04-25 14:41:00 -06:00
|
|
|
|
|
|
|
|
let outcome = endpoint.send_call(
|
|
|
|
|
common::agent_path(),
|
|
|
|
|
Some(shell_leaf_name),
|
2026-04-25 15:35:08 -06:00
|
|
|
open_procedure.clone(),
|
2026-04-25 14:41:00 -06:00
|
|
|
Some(hook_id),
|
2026-04-25 15:35:08 -06:00
|
|
|
common::shell_open_payload(),
|
|
|
|
|
)?;
|
|
|
|
|
common::write_frames(
|
|
|
|
|
&mut stream,
|
|
|
|
|
&outcome
|
|
|
|
|
.forward
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(_, frame)| frame)
|
|
|
|
|
.collect::<Vec<_>>(),
|
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() {
|
|
|
|
|
let outcome = endpoint.send_data(
|
|
|
|
|
common::agent_path(),
|
|
|
|
|
hook_id,
|
|
|
|
|
open_procedure.clone(),
|
|
|
|
|
command.as_bytes().to_vec(),
|
|
|
|
|
index == 2,
|
|
|
|
|
)?;
|
|
|
|
|
common::write_frames(
|
|
|
|
|
&mut stream,
|
|
|
|
|
&outcome
|
|
|
|
|
.forward
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(_, frame)| frame)
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
2026-04-25 14:41:00 -06:00
|
|
|
|
|
|
|
|
for result in frame_rx {
|
|
|
|
|
let frame = result?;
|
|
|
|
|
let outcome = endpoint.receive(&Ingress::Child(common::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(())
|
|
|
|
|
}
|