diff --git a/Cargo.lock b/Cargo.lock index 1976a1f..3c3c3e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys 0.59.0", +] + [[package]] name = "argon2" version = "0.5.3" @@ -264,6 +314,52 @@ dependencies = [ "inout", ] +[[package]] +name = "clap" +version = "4.5.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8aa86934b44c19c50f87cc2790e19f54f7a67aedb64101c2e1a2e5ecfb73944" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2414dbb2dd0695280da6ea9261e327479e9d37b0630f6b53ba2a11c60c679fd9" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "const-oid" version = "0.9.6" @@ -674,6 +770,12 @@ version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -873,6 +975,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itoa" version = "1.0.15" @@ -1724,6 +1832,12 @@ dependencies = [ "sha2", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -1904,6 +2018,7 @@ name = "tunnel_rs" version = "0.1.0" dependencies = [ "bytes", + "clap", "dotenvy", "http-body-util", "hyper", @@ -1937,6 +2052,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "valuable" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index a0d8499..f0c2d9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ default-run = "tunnel_rs" [dependencies] bytes = "1.10.1" +clap = { version = "4.5.35", features = ["derive"] } dotenvy = "0.15.7" http-body-util = { version = "0.1.3", features = ["full"] } hyper = { version = "1.6.0", features = ["full"] } diff --git a/src/ssh.rs b/src/ssh.rs index a3f0629..589c69e 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -1,5 +1,6 @@ -use std::{collections::HashSet, net::SocketAddr, sync::Arc, time::Duration}; +use std::{collections::HashSet, iter::once, net::SocketAddr, sync::Arc, time::Duration}; +use clap::Parser; use russh::{ ChannelId, keys::PrivateKey, @@ -31,6 +32,12 @@ impl Handler { } } +/// Quickly create http tunnels for development +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { +} + impl russh::server::Handler for Handler { type Error = russh::Error; @@ -89,13 +96,28 @@ impl russh::server::Handler for Handler { async fn exec_request( &mut self, - _channel: ChannelId, + channel: ChannelId, data: &[u8], - _session: &mut Session, + session: &mut Session, ) -> Result<(), Self::Error> { - trace!(data, "exec_request"); + let cmd = String::from_utf8_lossy(data); - Ok(()) + trace!(?cmd, "exec_request"); + + let cmd = once("").chain(cmd.split_whitespace()); + match Args::try_parse_from(cmd) { + Ok(args) => { + debug!("{args:?}"); + } + Err(err) => { + self.send(format!("{err}")); + // TODO: This closes the whole thing before we can send the message + // Instead use the graceful shutdown once that is implemented + // session.channel_failure(channel) + } + }; + + session.channel_success(channel) } async fn tcpip_forward(