From 63e86f5b576f22caeb02b2798ef3fab3cfcf3217 Mon Sep 17 00:00:00 2001 From: Konrad Dybcio Date: Wed, 22 May 2024 00:20:35 +0200 Subject: [PATCH] treewide: Move things around More binaries inbound Signed-off-by: Konrad Dybcio --- Cargo.lock | 15 ++++++++++++++ Cargo.toml | 35 +++---------------------------- client/Cargo.toml | 29 ++++++++++++++++++++++++++ {src => client/src}/main.rs | 10 ++++----- proto/Cargo.toml | 31 ++++++++++++++++++++++++++++ {src => proto/src}/lib.rs | 4 +++- proto/src/ssh.rs | 41 +++++++++++++++++++++++++++++++++++++ 7 files changed, 126 insertions(+), 39 deletions(-) create mode 100644 client/Cargo.toml rename {src => client/src}/main.rs (95%) create mode 100644 proto/Cargo.toml rename {src => proto/src}/lib.rs (99%) create mode 100644 proto/src/ssh.rs diff --git a/Cargo.lock b/Cargo.lock index 062b721..27c26ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -520,6 +520,21 @@ dependencies = [ [[package]] name = "sk8brd" version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "colored", + "crossterm", + "os_pipe", + "serde", + "sk8brd-proto", + "ssh2", + "tokio", +] + +[[package]] +name = "sk8brd-proto" +version = "0.1.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 3fbad0f..5ecdc09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,32 +1,3 @@ -[package] -name = "sk8brd" -version = "0.1.0" -edition = "2021" -authors = ["Konrad Dybcio "] -license = "BSD-3-Clause" -description = "Simple remote devboard control software" -readme = "README.md" -repository = "https://github.com/linux-msm/sk8brd" -categories = ["command-line-utilities"] -publish = false # TODO - -[badges] -maintenance = { status = "actively-developed" } - -[[bin]] -name = "sk8brd" -path = "src/main.rs" - -[lib] -name = "sk8brd" -path = "src/lib.rs" - -[dependencies] -anyhow = "1.0" -clap = { version = "4.5.4", features = ["derive"] } -colored = "2.1.0" -crossterm = "0.27.0" -os_pipe = "1.1.5" -serde = { version = "^1.0.198", features = ["derive"] } -ssh2 = "^0.9.4" -tokio = { version = "^1.37.0", features = ["full"] } +[workspace] +members = ["client", "proto"] +resolver = "2" diff --git a/client/Cargo.toml b/client/Cargo.toml new file mode 100644 index 0000000..3358fc5 --- /dev/null +++ b/client/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "sk8brd" +version = "0.1.0" +edition = "2021" +authors = ["Konrad Dybcio "] +license = "BSD-3-Clause" +description = "Simple remote devboard control software" +readme = "README.md" +repository = "https://github.com/linux-msm/sk8brd" +categories = ["command-line-utilities"] +publish = false # TODO + +[badges] +maintenance = { status = "actively-developed" } + +[[bin]] +name = "sk8brd" +path = "src/main.rs" + +[dependencies] +anyhow = "1.0" +clap = { version = "4.5.4", features = ["derive"] } +colored = "2.1.0" +crossterm = "0.27.0" +sk8brd-proto = { path = "../proto", features = ["ssh"] } +os_pipe = "1.1.5" +serde = { version = "^1.0.198", features = ["derive"] } +ssh2 = "^0.9.4" +tokio = { version = "^1.37.0", features = ["full"] } diff --git a/src/main.rs b/client/src/main.rs similarity index 95% rename from src/main.rs rename to client/src/main.rs index 0a5ebac..e8ddac3 100644 --- a/src/main.rs +++ b/client/src/main.rs @@ -1,17 +1,15 @@ use clap::Parser; use colored::Colorize; +use sk8brd::ssh::{ssh_connect, ssh_disconnect, ssh_get_chan}; use sk8brd::{ console_print, parse_recv_msg, print_string_msg, select_brd, send_ack, send_break, - send_console, send_image, send_msg, todo, Sk8brdMsgs, MSG_HDR_SIZE, + send_console, send_image, send_msg, todo, Sk8brdMsgs, CDBA_SERVER_BIN_NAME, MSG_HDR_SIZE, }; -use ssh::{ssh_connect, ssh_disconnect, ssh_get_chan}; use std::fs; use std::io::{stdout, Read, Write}; use std::sync::Arc; use tokio::sync::Mutex; -mod ssh; - const SSH_BUFFER_SIZE: usize = 2048; macro_rules! get_arc { @@ -90,8 +88,8 @@ async fn main() -> anyhow::Result<()> { println!("sk8brd {}", env!("CARGO_PKG_VERSION")); - let mut sess = ssh_connect(args.farm, args.port).await?; - let mut chan = ssh_get_chan(&mut sess).await?; + let mut sess = ssh_connect(args.farm, args.port, args.user).await?; + let mut chan = ssh_get_chan(&mut sess, CDBA_SERVER_BIN_NAME).await?; sess.set_blocking(false); send_ack(&mut chan, Sk8brdMsgs::MsgListDevices).await?; diff --git a/proto/Cargo.toml b/proto/Cargo.toml new file mode 100644 index 0000000..edd9eac --- /dev/null +++ b/proto/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "sk8brd-proto" +version = "0.1.0" +edition = "2021" +authors = ["Konrad Dybcio "] +license = "BSD-3-Clause" +description = "Simple remote devboard control software" +readme = "README.md" +repository = "https://github.com/linux-msm/sk8brd" +categories = ["command-line-utilities"] +publish = false # TODO + +[badges] +maintenance = { status = "actively-developed" } + +[lib] +name = "sk8brd" +path = "src/lib.rs" + +[features] +ssh = [] + +[dependencies] +anyhow = "1.0" +clap = { version = "4.5.4", features = ["derive"] } +colored = "2.1.0" +crossterm = "0.27.0" +os_pipe = "1.1.5" +serde = { version = "^1.0.198", features = ["derive"] } +ssh2 = "^0.9.4" +tokio = { version = "^1.37.0", features = ["full"] } diff --git a/src/lib.rs b/proto/src/lib.rs similarity index 99% rename from src/lib.rs rename to proto/src/lib.rs index c503c08..8559ccd 100644 --- a/src/lib.rs +++ b/proto/src/lib.rs @@ -5,8 +5,10 @@ use std::mem::size_of; use std::sync::Arc; use tokio::sync::Mutex; +#[cfg(feature = "ssh")] +pub mod ssh; + pub const CDBA_SERVER_BIN_NAME: &str = "cdba-server"; -pub const USERNAME: &str = "cdba"; #[repr(u8)] #[derive(Debug, PartialEq)] diff --git a/proto/src/ssh.rs b/proto/src/ssh.rs new file mode 100644 index 0000000..82a7b5a --- /dev/null +++ b/proto/src/ssh.rs @@ -0,0 +1,41 @@ +use anyhow::Context as _; +use ssh2::{Channel, Session}; +use std::net::TcpStream; +use std::sync::Arc; +use tokio::sync::Mutex; + +pub async fn ssh_connect(farm: String, port: String, username: String) -> anyhow::Result { + // Connect to the local SSH server + let tcp = TcpStream::connect(format!("{}:{}", farm, port)) + .with_context(|| format!("Couldn't connect to {}:{}", farm, port))?; + let mut sess = Session::new()?; + sess.set_tcp_stream(tcp); + sess.handshake()?; + + // Try to authenticate with the first identity in the agent. + sess.userauth_agent(&username) + .with_context(|| format!("Couldn't authenticate as {username}"))?; + + Ok(sess) +} + +pub async fn ssh_get_chan( + sess: &mut Session, + server_bin_name: &str, +) -> anyhow::Result>> { + let chan = Arc::new(Mutex::new(sess.channel_session()?)); + (*chan.lock().await) + .exec(server_bin_name) + .with_context(|| format!("Couldn't execute {server_bin_name} on remote host"))?; + + Ok(chan) +} + +pub async fn ssh_disconnect(sess: &mut Session) -> anyhow::Result<()> { + sess.disconnect( + Option::Some(ssh2::DisconnectCode::ConnectionLost), + "bye", + Option::Some("C"), + ) + .context("Couldn't disconnect cleanly") +}