add pfw show command

This commit is contained in:
lemonsh 2023-05-14 00:06:55 +02:00
parent 8e9b778f75
commit 470d5f5814
5 changed files with 65 additions and 11 deletions

View File

@ -15,4 +15,5 @@ clap = { version = "4.2", default-features = false, features = ["suggestions", "
dirs = "5.0"
connectbox = { path = "../connectbox" }
color-print = "0.3"
anstream = "0.3"
ascii_table = { version = "4.0", features = ["auto_table_width"] }
once_cell = "1.17"

View File

@ -19,7 +19,15 @@ pub(crate) struct Args {
pub(crate) enum ShellCommand {
Exit,
#[command(name = "pfw")]
PortForwards,
PortForwards {
#[command(subcommand)]
cmd: PortForwardsCommand
}
}
#[derive(Parser, Debug)]
pub(crate) enum PortForwardsCommand {
Show
}
pub(crate) fn shell_cmd() -> Command {

View File

@ -0,0 +1 @@
pub mod pfw;

View File

@ -0,0 +1,39 @@
use std::{vec, fmt::Display};
use ascii_table::{AsciiTable, Align::Right};
use color_eyre::Result;
use color_print::cprintln;
use once_cell::sync::OnceCell;
use crate::{cli::PortForwardsCommand, AppState};
static PORT_FORWARDING_TABLE: OnceCell<AsciiTable> = OnceCell::new();
fn init_port_forwarding_table() -> AsciiTable {
let mut t = AsciiTable::default();
t.column(0).set_header("ID").set_align(Right);
t.column(1).set_header("Local IP");
t.column(2).set_header("Start port");
t.column(3).set_header("End port");
t.column(4).set_header("In. start port");
t.column(5).set_header("In. end port");
t.column(6).set_header("Protocol");
t.column(7).set_header("Enabled");
t
}
pub(crate) async fn run(cmd: PortForwardsCommand, state: &AppState) -> Result<()> {
match cmd {
PortForwardsCommand::Show => {
cprintln!("<blue!>Retrieving the port forwarding table...");
let port_forwards = state.connect_box.port_forwards().await?;
let table_entries = port_forwards.entries.iter().map(|e| {
let v: Vec<&dyn Display> = vec![&e.id, &e.local_ip, &e.start_port, &e.end_port, &e.start_port_in, &e.end_port_in, &e.protocol, &e.enable];
v
});
let rendered_table = PORT_FORWARDING_TABLE.get_or_init(init_port_forwarding_table).format(table_entries);
cprintln!("<black!>LAN IP: {}\nSubnet mask: {}\n</black!>{rendered_table}", port_forwards.lan_ip, port_forwards.subnet_mask);
},
}
Ok(())
}

View File

@ -1,5 +1,4 @@
use anstream::println;
use color_print::cstr;
use color_print::{cstr, cprintln};
use clap::{FromArgMatches, Parser};
use cli::Args;
@ -11,6 +10,11 @@ use crate::{cli::ShellCommand, utils::QuotableArgs};
mod cli;
mod utils;
mod commands;
pub(crate) struct AppState {
connect_box: ConnectBox
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
@ -33,12 +37,13 @@ async fn main() -> Result<()> {
.join(".connectbox-shell-history");
let _err = rl.load_history(&history_path);
println!(cstr!("<blue!>Logging in..."));
let connectbox = ConnectBox::new(args.address, password, true)?;
connectbox.login().await?;
cprintln!("<blue!>Logging in...");
let connect_box = ConnectBox::new(args.address, password, true)?;
connect_box.login().await?;
let state = AppState { connect_box };
loop {
match rl.readline(cstr!("<green!> > ")) {
match rl.readline(cstr!("\n<green!> > ")) {
Ok(line) => {
if line.chars().all(char::is_whitespace) {
continue;
@ -52,10 +57,10 @@ async fn main() -> Result<()> {
};
match cmd {
ShellCommand::Exit => break,
ShellCommand::PortForwards => todo!(),
ShellCommand::PortForwards { cmd } => commands::pfw::run(cmd, &state).await?,
}
}
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,
Err(ReadlineError::Interrupted | ReadlineError::Eof) => break,
Err(err) => {
println!("{err:?}");
break;
@ -63,7 +68,7 @@ async fn main() -> Result<()> {
}
}
println!("Logging out...");
connectbox.logout().await?;
state.connect_box.logout().await?;
rl.save_history(&history_path)?;
Ok(())