add pfw show command
This commit is contained in:
		| @@ -15,4 +15,5 @@ clap = { version = "4.2", default-features = false, features = ["suggestions", " | |||||||
| dirs = "5.0" | dirs = "5.0" | ||||||
| connectbox = { path = "../connectbox" } | connectbox = { path = "../connectbox" } | ||||||
| color-print = "0.3" | color-print = "0.3" | ||||||
| anstream = "0.3" | ascii_table = { version = "4.0", features = ["auto_table_width"] } | ||||||
|  | once_cell = "1.17" | ||||||
|   | |||||||
| @@ -19,7 +19,15 @@ pub(crate) struct Args { | |||||||
| pub(crate) enum ShellCommand { | pub(crate) enum ShellCommand { | ||||||
|     Exit, |     Exit, | ||||||
|     #[command(name = "pfw")] |     #[command(name = "pfw")] | ||||||
|     PortForwards, |     PortForwards { | ||||||
|  |         #[command(subcommand)] | ||||||
|  |         cmd: PortForwardsCommand | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #[derive(Parser, Debug)] | ||||||
|  | pub(crate) enum PortForwardsCommand { | ||||||
|  |     Show | ||||||
| } | } | ||||||
|  |  | ||||||
| pub(crate) fn shell_cmd() -> Command { | pub(crate) fn shell_cmd() -> Command { | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								connectbox-shell/src/commands/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								connectbox-shell/src/commands/mod.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | pub mod pfw; | ||||||
							
								
								
									
										39
									
								
								connectbox-shell/src/commands/pfw.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								connectbox-shell/src/commands/pfw.rs
									
									
									
									
									
										Normal 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(()) | ||||||
|  | } | ||||||
| @@ -1,5 +1,4 @@ | |||||||
| use anstream::println; | use color_print::{cstr, cprintln}; | ||||||
| use color_print::cstr; |  | ||||||
|  |  | ||||||
| use clap::{FromArgMatches, Parser}; | use clap::{FromArgMatches, Parser}; | ||||||
| use cli::Args; | use cli::Args; | ||||||
| @@ -11,6 +10,11 @@ use crate::{cli::ShellCommand, utils::QuotableArgs}; | |||||||
|  |  | ||||||
| mod cli; | mod cli; | ||||||
| mod utils; | mod utils; | ||||||
|  | mod commands; | ||||||
|  |  | ||||||
|  | pub(crate) struct AppState { | ||||||
|  |     connect_box: ConnectBox | ||||||
|  | } | ||||||
|  |  | ||||||
| #[tokio::main(flavor = "current_thread")] | #[tokio::main(flavor = "current_thread")] | ||||||
| async fn main() -> Result<()> { | async fn main() -> Result<()> { | ||||||
| @@ -33,12 +37,13 @@ async fn main() -> Result<()> { | |||||||
|         .join(".connectbox-shell-history"); |         .join(".connectbox-shell-history"); | ||||||
|     let _err = rl.load_history(&history_path); |     let _err = rl.load_history(&history_path); | ||||||
|  |  | ||||||
|     println!(cstr!("<blue!>Logging in...")); |     cprintln!("<blue!>Logging in..."); | ||||||
|     let connectbox = ConnectBox::new(args.address, password, true)?; |     let connect_box = ConnectBox::new(args.address, password, true)?; | ||||||
|     connectbox.login().await?; |     connect_box.login().await?; | ||||||
|  |     let state = AppState { connect_box }; | ||||||
|  |  | ||||||
|     loop { |     loop { | ||||||
|         match rl.readline(cstr!("<green!> > ")) { |         match rl.readline(cstr!("\n<green!> > ")) { | ||||||
|             Ok(line) => { |             Ok(line) => { | ||||||
|                 if line.chars().all(char::is_whitespace) { |                 if line.chars().all(char::is_whitespace) { | ||||||
|                     continue; |                     continue; | ||||||
| @@ -52,10 +57,10 @@ async fn main() -> Result<()> { | |||||||
|                 }; |                 }; | ||||||
|                 match cmd { |                 match cmd { | ||||||
|                     ShellCommand::Exit => break, |                     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) => { |             Err(err) => { | ||||||
|                 println!("{err:?}"); |                 println!("{err:?}"); | ||||||
|                 break; |                 break; | ||||||
| @@ -63,7 +68,7 @@ async fn main() -> Result<()> { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     println!("Logging out..."); |     println!("Logging out..."); | ||||||
|     connectbox.logout().await?; |     state.connect_box.logout().await?; | ||||||
|  |  | ||||||
|     rl.save_history(&history_path)?; |     rl.save_history(&history_path)?; | ||||||
|     Ok(()) |     Ok(()) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user