From 0fb36aa77d1bd8448f63732d21090dc6631c9ff5 Mon Sep 17 00:00:00 2001 From: ByteOtter Date: Thu, 25 Jul 2024 12:55:42 +0200 Subject: [PATCH] Add test application --- vnc-test/Cargo.toml | 10 +++++ vnc-test/src/main.rs | 102 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 vnc-test/Cargo.toml create mode 100644 vnc-test/src/main.rs diff --git a/vnc-test/Cargo.toml b/vnc-test/Cargo.toml new file mode 100644 index 0000000..02f558f --- /dev/null +++ b/vnc-test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "vnc-test" +version = "0.1.0" +edition = "2021" + +[dependencies] +isototest = { package = "isototest", path = "../isototest" } +nix = { version = "0.29.0", features = ["socket", "net"] } +tokio = { version = "1.38.1", features = ["full"] } +vnc-rs = "0.5.1" diff --git a/vnc-test/src/main.rs b/vnc-test/src/main.rs new file mode 100644 index 0000000..b4236f5 --- /dev/null +++ b/vnc-test/src/main.rs @@ -0,0 +1,102 @@ +use isototest::action::write_to_console; +use isototest::connection::create_vnc_client; +use nix::sys::socket::{self, sockaddr_in, AddressFamily, SockType}; +use std::process::exit; +use std::process::{Command, Stdio}; +use std::ptr::null_mut; +use tokio::{ + self, + net::{TcpListener, TcpStream}, +}; +use vnc::VncError; + +// pub struct VncServer { +// srv: Option, +// } + +// impl VncServer { +// pub async fn start(&mut self) -> Result<(), Box> { +// let addr = Self::get_dynamic_port_address()?; +// self.srv = Some(TcpListener::bind(addr).await?); +// Ok(()) +// } + +// pub async fn stop(&self) -> Result<(), Box> { +// if let Some(srv) = &self.srv { +// drop(srv); +// } +// Ok(()) +// } + +// pub async fn spawn_vnc_server(&self) { +// if let Some(srv) = &self.srv { +// let mut socket = srv.accept().await.unwrap(); +// tokio::spawn(async move { +// let output = Command::new("vncserver") +// .stdin(Stdio::piped()) +// .stdout(Stdio::piped()) +// .stderr(Stdio::piped()) +// .spawn() +// .unwrap(); + +// let _ = output.wait_with_output().unwrap(); +// }); +// } +// } + +// fn get_dynamic_port_address() -> Result> { +// let sock = unsafe { socket::socket(AddressFamily::AF_INET, SockType::SOCK_STREAM, 0)? }; +// let mut sin = sockaddr_in { +// sin_family: AddressFamily::AF_INET as u16, +// sin_port: 0, +// sin_addr: std::net::Ipv4Addr::LOCALHOST.into(), +// sin_zero: [0; 8], +// }; + +// let res = +// unsafe { socket::getaddrinfo(null_mut(), b"vnc\0".as_ptr(), null_mut(), null_mut()) }; +// let addr = unsafe { res.get(0)?.addr }; +// let port = ((addr as *const sockaddr_in).cast::() as usize) % 65536; + +// Ok(format!("127.0.0.1:{}", port)) +// } +// } + +#[tokio::main] +async fn main() -> Result<(), Box> { + // let mut vnc_server = VncServer { srv: None }; + + // println!("Starting..."); + + // vnc_server.start().await?; + // println!("Spawning connection..."); + // vnc_server.spawn_vnc_server().await; + + // println!("Creating client..."); + + // println!("Getting port..."); + // let addr = vnc_server.srv.as_ref().unwrap().local_addr()?; + let addr = ""; + let client = match create_vnc_client(addr.to_string(), None).await { + Ok(client) => { + println!("Client created. Handshake successful."); + client + } + Err(e) => { + eprintln!("[Error] {:?}.", e); + exit(1); + } + }; + + match write_to_console(&client, include_str!("lore.txt").to_string(), None).await { + Ok(_) => { + println!("Test text sent!"); + } + Err(e) => { + eprintln!("[error] {:?}.", e); + exit(1); + } + } + + Ok(()) +}