Skip to content

Commit

Permalink
Add more comments and improve item naming
Browse files Browse the repository at this point in the history
Fix path for "size" of target in ci
  • Loading branch information
mkj committed Aug 2, 2023
1 parent 05acaa3 commit e1e36f8
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 84 deletions.
63 changes: 37 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ cipher = { version = "0.4", features = ["zeroize"] }
subtle = { version = "2.4", default-features = false }
# ed25519/x25519
# fork allows hashing by parts (sign/verify from sshwire), and zeroize
ed25519-dalek = { version = "2.0.0-rc.2", default-features = false, features = ["zeroize", "rand_core", "hazmat"] }
x25519-dalek = { version = "2.0.0-rc.2", default-features = false, features = ["zeroize"] }
curve25519-dalek = { version = "4.0.0-rc.2", default-features = false, features = ["zeroize"] }
ed25519-dalek = { version = "2.0.0-rc.3", default-features = false, features = ["zeroize", "rand_core", "hazmat"] }
x25519-dalek = { version = "2.0.0-rc.3", default-features = false, features = ["zeroize"] }
curve25519-dalek = { version = "4.0.0", default-features = false, features = ["zeroize"] }

rsa = { version = "0.8", default-features = false, optional = true, features = ["sha2"] }
# TODO: getrandom feature is a workaround for missing ssh-key dependency with rsa. fixed in pending 0.6
Expand Down Expand Up @@ -81,10 +81,12 @@ simplelog = { version = "0.12", features = ["test"] }


[patch.crates-io]
curve25519-dalek = { git = "https://github.com/dalek-cryptography/curve25519-dalek" }
x25519-dalek = { git = "https://github.com/dalek-cryptography/x25519-dalek" }
ed25519-dalek = { git = "https://github.com/mkj/ed25519-dalek", branch = "sunset" }
# ed25519-dalek = { path = "/home/matt/3rd/rs/crypto/ed25519-dalek" }
curve25519-dalek = { git = "https://github.com/mkj/curve25519-dalek", branch = "sunset" }
ed25519-dalek = { git = "https://github.com/mkj/curve25519-dalek", branch = "sunset" }
x25519-dalek = { git = "https://github.com/mkj/curve25519-dalek", branch = "sunset" }
# curve25519-dalek = { path = "/home/matt/3rd/rs/crypto/curve25519-dalek/curve25519-dalek" }
# ed25519-dalek = { path = "/home/matt/3rd/rs/crypto/curve25519-dalek/ed25519-dalek" }
# x25519-dalek = { path = "/home/matt/3rd/rs/crypto/curve25519-dalek/x25519-dalek" }

embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "2eb7a67c7027c6768fa95031caf60bcd0eade1ad" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "2eb7a67c7027c6768fa95031caf60bcd0eade1ad" }
Expand Down
18 changes: 9 additions & 9 deletions async/examples/sunsetc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async fn run(args: Args) -> Result<()> {
let ssh_task = spawn_local(async move {
let mut rxbuf = Zeroizing::new(vec![0; 3000]);
let mut txbuf = Zeroizing::new(vec![0; 3000]);
let cli = SSHClient::new(&mut rxbuf, &mut txbuf)?;
let ssh = SSHClient::new(&mut rxbuf, &mut txbuf)?;

let mut app = CmdlineClient::new(
args.username.as_ref().unwrap(),
Expand Down Expand Up @@ -110,27 +110,27 @@ async fn run(args: Args) -> Result<()> {
let mut rsock = FromTokio::new(rsock);
let mut wsock = FromTokio::new(wsock);

let (hooks, mut cmd) = app.split();
let (hooks, mut cmdrun) = app.split();

let hooks = Mutex::<SunsetRawMutex, _>::new(hooks);

let ssh = async {
let r = cli.run(&mut rsock, &mut wsock, &hooks).await;
// SSH connection future
let ssh_fut = async {
let r = ssh.run(&mut rsock, &mut wsock, &hooks).await;
trace!("ssh run finished {r:?}");
hooks.lock().await.exited().await;
r
};

// Circular reference here, cli -> cmd and cmd->cli
let session = cmd.run(&cli);
// Client session future
let session = async {
let r = session.await;
let r = cmdrun.run(&ssh).await;
trace!("client session run finished");
cli.exit().await;
ssh.exit().await;
r
};

let (res_ssh, res_session) = futures::future::join(ssh, session).await;
let (res_ssh, res_session) = futures::future::join(ssh_fut, session).await;
debug!("res_ssh {res_ssh:?}");
debug!("res_session {res_session:?}");
res_ssh?;
Expand Down
2 changes: 1 addition & 1 deletion embassy/demos/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod config;
pub mod menu;
pub mod demo_menu;

pub use server::{Shell, listener};
pub use server::{DemoServer, listener};
pub use config::SSHConfig;
pub use demo_menu::BufOutput;

Expand Down
31 changes: 19 additions & 12 deletions embassy/demos/common/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ macro_rules! singleton {


// common entry point
pub async fn listener<D: Driver, S: Shell>(stack: &'static Stack<D>,
pub async fn listener<D: Driver, S: DemoServer>(stack: &'static Stack<D>,
config: &SunsetMutex<SSHConfig>,
init: S::Init) -> ! {
// TODO: buffer size?
Expand Down Expand Up @@ -83,24 +83,24 @@ pub async fn listener<D: Driver, S: Shell>(stack: &'static Stack<D>,
}

/// Run a SSH session when a socket accepts a connection
async fn session<S: Shell>(socket: &mut TcpSocket<'_>, config: &SunsetMutex<SSHConfig>,
async fn session<S: DemoServer>(socket: &mut TcpSocket<'_>, config: &SunsetMutex<SSHConfig>,
init: &S::Init) -> sunset::Result<()> {
// OK unwrap: has been accepted
let src = socket.remote_endpoint().unwrap();
info!("Connection from {}:{}", src.addr, src.port);

let shell = S::new(init);
let s = S::new(init);

let conf = config.lock().await.clone();
let app = DemoServer::new(&shell, conf)?;
let app = ServerApp::new(&s, conf)?;
let app = Mutex::<NoopRawMutex, _>::new(app);

let mut ssh_rxbuf = [0; 2000];
let mut ssh_txbuf = [0; 2000];
let serv = SSHServer::new(&mut ssh_rxbuf, &mut ssh_txbuf)?;
let serv = &serv;

let session = shell.run(serv);
let session = s.run(serv);

let (mut rsock, mut wsock) = socket.split();

Expand All @@ -115,7 +115,10 @@ async fn session<S: Shell>(socket: &mut TcpSocket<'_>, config: &SunsetMutex<SSHC
Ok(())
}

struct DemoServer<'a, S: Shell> {
/// Provides `ServBehaviour` for the server
///
/// Further customisations are provided by `DemoServer` generic
struct ServerApp<'a, S: DemoServer> {
config: SSHConfig,

handle: Option<ChanHandle>,
Expand All @@ -124,7 +127,7 @@ struct DemoServer<'a, S: Shell> {
shell: &'a S,
}

impl<'a, S: Shell> DemoServer<'a, S> {
impl<'a, S: DemoServer> ServerApp<'a, S> {
const ADMIN_USER: &'static str = "config";

fn new(shell: &'a S, config: SSHConfig) -> Result<Self> {
Expand All @@ -142,7 +145,7 @@ impl<'a, S: Shell> DemoServer<'a, S> {
}
}

impl<'a, S: Shell> ServBehaviour for DemoServer<'a, S> {
impl<'a, S: DemoServer> ServBehaviour for ServerApp<'a, S> {

fn hostkeys(&mut self) -> BhResult<heapless::Vec<&SignKey, 2>> {
// OK unwrap: only one element
Expand Down Expand Up @@ -226,19 +229,23 @@ impl<'a, S: Shell> ServBehaviour for DemoServer<'a, S> {
}
}

pub trait Shell {
type Init: Copy;
pub trait DemoServer {
/// State to be passed to each new connection by the server
type Init;

fn new(init: &Self::Init) -> Self;

/// Called when auth succeeds
#[allow(unused_variables)]
// TODO: eventually the compiler should add must_use automatically?
async fn authed(&self, username: &str) {
info!("Authenticated")
}

/// Called when a shell is opened after auth succeeds
fn open_shell(&self, handle: ChanHandle);

/// A task to run for each incoming connection.
// TODO: eventually the compiler should add must_use automatically?
#[must_use]
async fn run<'f, S: ServBehaviour>(&self, serv: &'f SSHServer<'f, S>) -> Result<()>;
}
Expand All @@ -250,7 +257,7 @@ pub trait Shell {
pub struct BufOutput {
/// Sufficient to hold output produced from a single keystroke input. Further output will be discarded
// pub s: String<300>,
// todo
// todo size
pub s: String<3000>,
}

Expand Down
Loading

0 comments on commit e1e36f8

Please sign in to comment.