refactor: accept CLI args as options and minor restructuring
* Made constructed `App` instances use CLI args for `frame_rate` and `tick_rate` * Moved lazy constants from `LazyLock` to `lazy_static!` * Slightly restructure SSH config initialization * Also apply rustfmt formatting to some files
This commit is contained in:
parent
67380f6057
commit
36a62018e5
2 changed files with 61 additions and 36 deletions
55
src/main.rs
55
src/main.rs
|
@ -1,7 +1,14 @@
|
||||||
use std::{net::SocketAddr, sync::{Arc, LazyLock, OnceLock}};
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
|
|
||||||
|
use clap::Parser as _;
|
||||||
|
use cli::Cli;
|
||||||
use color_eyre::{eyre::eyre, Result};
|
use color_eyre::{eyre::eyre, Result};
|
||||||
use russh::{keys::PrivateKey, server::{Config, Server}, MethodSet};
|
use lazy_static::lazy_static;
|
||||||
|
use russh::{
|
||||||
|
keys::PrivateKey,
|
||||||
|
server::{Config, Server},
|
||||||
|
MethodSet,
|
||||||
|
};
|
||||||
use ssh::SshServer;
|
use ssh::SshServer;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
|
@ -11,38 +18,42 @@ mod cli;
|
||||||
mod components;
|
mod components;
|
||||||
mod config;
|
mod config;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
mod keycode;
|
||||||
mod logging;
|
mod logging;
|
||||||
mod ssh;
|
mod ssh;
|
||||||
mod tui;
|
mod tui;
|
||||||
mod keycode;
|
|
||||||
|
|
||||||
const SOCKET_ADDR: LazyLock<SocketAddr> = LazyLock::new(|| SocketAddr::from(([127, 0, 0, 1], 2222)));
|
const SSH_KEYS: &[&[u8]] = &[
|
||||||
pub static SSH_CONFIG: OnceLock<Arc<Config>> = OnceLock::new();
|
include_bytes!("../rsa.pem"),
|
||||||
|
include_bytes!("../ed25519.pem"),
|
||||||
|
];
|
||||||
|
lazy_static! {
|
||||||
|
pub(crate) static ref OPTIONS: Cli = Cli::parse();
|
||||||
|
pub(crate) static ref SOCKET_ADDR: SocketAddr = SocketAddr::from(([127, 0, 0, 1], 2222));
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
crate::errors::init()?;
|
crate::errors::init()?;
|
||||||
crate::logging::init()?;
|
crate::logging::init()?;
|
||||||
|
|
||||||
SSH_CONFIG.get_or_init(|| {
|
let config = ssh_config();
|
||||||
tracing::debug!("setting up ssh config");
|
tracing::info!("Attempting to listen on {}", *SOCKET_ADDR);
|
||||||
|
SshServer::default()
|
||||||
|
.run_on_socket(Arc::new(config), &TcpListener::bind(*SOCKET_ADDR).await?)
|
||||||
|
.await
|
||||||
|
.map_err(|err| eyre!(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ssh_config() -> Config {
|
||||||
let mut conf = Config::default();
|
let mut conf = Config::default();
|
||||||
conf.methods = MethodSet::NONE;
|
conf.methods = MethodSet::NONE;
|
||||||
conf.keys = vec![
|
conf.keys = SSH_KEYS
|
||||||
PrivateKey::from_openssh(include_bytes!("../rsa.pem")).unwrap(),
|
.to_vec()
|
||||||
PrivateKey::from_openssh(include_bytes!("../ed25519.pem")).unwrap()
|
.iter()
|
||||||
];
|
.filter_map(|pem| PrivateKey::from_openssh(pem).ok())
|
||||||
Arc::new(conf)
|
.collect();
|
||||||
});
|
|
||||||
|
|
||||||
// let args = Cli::parse();
|
tracing::trace!("SSH config: {:#?}", conf);
|
||||||
// let mut app = App::new(args.tick_rate, args.frame_rate)?;
|
conf
|
||||||
// app.run().await?;
|
|
||||||
|
|
||||||
tracing::info!("attemping to listen on {}", *SOCKET_ADDR);
|
|
||||||
SshServer::default().run_on_socket(
|
|
||||||
Arc::clone(SSH_CONFIG.get().unwrap()),
|
|
||||||
&TcpListener::bind(*SOCKET_ADDR).await?,
|
|
||||||
).await.map_err(|err| eyre!(err))
|
|
||||||
}
|
}
|
||||||
|
|
30
src/ssh.rs
30
src/ssh.rs
|
@ -16,6 +16,7 @@ use tracing::instrument;
|
||||||
use crate::{
|
use crate::{
|
||||||
app::App,
|
app::App,
|
||||||
tui::{Terminal, Tui},
|
tui::{Terminal, Tui},
|
||||||
|
OPTIONS,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -54,7 +55,10 @@ impl TermWriter {
|
||||||
impl Write for TermWriter {
|
impl Write for TermWriter {
|
||||||
#[instrument(skip(self, buf), level = "debug")]
|
#[instrument(skip(self, buf), level = "debug")]
|
||||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
tracing::trace!("Writing {} bytes into SSH terminal writer buffer", buf.len());
|
tracing::trace!(
|
||||||
|
"Writing {} bytes into SSH terminal writer buffer",
|
||||||
|
buf.len()
|
||||||
|
);
|
||||||
self.inner.extend(buf);
|
self.inner.extend(buf);
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
@ -72,14 +76,12 @@ pub struct SshSession {
|
||||||
tui: Arc<RwLock<Option<Tui>>>,
|
tui: Arc<RwLock<Option<Tui>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for SshSession {}
|
|
||||||
|
|
||||||
impl SshSession {
|
impl SshSession {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let (ssh_tx, ssh_rx) = mpsc::unbounded_channel();
|
let (ssh_tx, ssh_rx) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
app: App::new(10f64, 60f64, ssh_rx)
|
app: App::new(OPTIONS.tick_rate, OPTIONS.frame_rate, ssh_rx)
|
||||||
.ok()
|
.ok()
|
||||||
.map(|app| Arc::new(Mutex::new(app))),
|
.map(|app| Arc::new(Mutex::new(app))),
|
||||||
tui: Arc::new(RwLock::new(None)),
|
tui: Arc::new(RwLock::new(None)),
|
||||||
|
@ -87,10 +89,22 @@ impl SshSession {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_app(app: Arc<Mutex<App>>, writer: Arc<Mutex<Terminal>>, tui: Arc<RwLock<Option<Tui>>>, session: &Handle, channel_id: ChannelId) -> eyre::Result<()> {
|
async fn run_app(
|
||||||
|
app: Arc<Mutex<App>>,
|
||||||
|
writer: Arc<Mutex<Terminal>>,
|
||||||
|
tui: Arc<RwLock<Option<Tui>>>,
|
||||||
|
session: &Handle,
|
||||||
|
channel_id: ChannelId,
|
||||||
|
) -> eyre::Result<()> {
|
||||||
app.lock_owned().await.run(writer, tui).await?;
|
app.lock_owned().await.run(writer, tui).await?;
|
||||||
session.close(channel_id).await.map_err(|_| eyre!("failed to close session"))?;
|
session
|
||||||
session.exit_status_request(channel_id, 0).await.map_err(|_| eyre!("failed to send session exit status"))
|
.close(channel_id)
|
||||||
|
.await
|
||||||
|
.map_err(|_| eyre!("failed to close session"))?;
|
||||||
|
session
|
||||||
|
.exit_status_request(channel_id, 0)
|
||||||
|
.await
|
||||||
|
.map_err(|_| eyre!("failed to send session exit status"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +140,7 @@ impl Handler for SshSession {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
tracing::error!("Session exited with error: {err}");
|
tracing::error!("Session exited with error: {err}");
|
||||||
let _ = session_handle.channel_failure(channel_id).await;
|
let _ = session_handle.channel_failure(channel_id).await;
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue