1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
pub(crate) mod ssh;
#[macro_use]
extern crate lazy_static;
use std::{error::Error, sync::Arc};
use cursive::View;
pub use cursive;
pub use russh_keys;
use russh_keys::key::KeyPair;
use ssh::{plugin::set_plugin, server::Server, session_manager::SessionManager};
use tokio::sync::{mpsc, watch};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SessionHandle(u64);
pub trait AppSession {
fn on_start(
&mut self,
siv: &mut cursive::Cursive,
session_handle: SessionHandle,
pub_key: russh_keys::key::PublicKey,
) -> Result<Box<dyn View>, Box<dyn Error>>;
fn on_tick(&mut self, _siv: &mut cursive::Cursive) -> Result<(), Box<dyn Error>> {
Ok(())
}
}
pub trait App: Send + Sync {
fn on_load(&mut self) -> Result<(), Box<dyn Error>>;
fn new_session(&self) -> Box<dyn AppSession>;
}
pub struct AppServer {
port: u16,
}
impl AppServer {
pub fn new_with_port(port: u16) -> Self {
Self { port }
}
pub async fn run(
&mut self,
key_pair: KeyPair,
plugin: Arc<dyn App>,
) -> Result<(), Box<dyn Error>> {
set_plugin(plugin);
let (sender, receiver) = mpsc::channel(100);
let (_tx, rx) = watch::channel(false);
let repo = SessionManager::new(sender.clone(), receiver);
let sh = Server::new(key_pair.into(), rx, sender, self.port).await;
sh.listen(repo).await.unwrap();
Ok(())
}
}