use termimad::crossterm::style::{Color::*, Attribute::*};
use termimad::*;
use crate::connections;
use crate::address_checkers;
use crate::string_utils;
fn create_table_style() -> MadSkin {
let mut skin = MadSkin::default();
skin.bold.set_fg(Cyan);
skin.italic.set_fg(gray(11));
skin.strikeout = CompoundStyle::new(Some(Red), None, RapidBlink.into());
skin.paragraph.align = Alignment::Left;
skin.table.align = Alignment::Center;
skin.inline_code = CompoundStyle::new(Some(Yellow), None, Encircled.into());
skin
}
fn format_abuse_checked_address(remote_address: &String, abuse_score: Option<i64>) -> String {
let checked_remote_address: String;
if abuse_score >= Some(50) {
checked_remote_address = format!("{} ~~high abuse score: {}~~", remote_address, abuse_score.unwrap());
}
else if abuse_score > Some(25) {
checked_remote_address = format!("{} `moderate abuse score: {}`", remote_address, abuse_score.unwrap());
}
else if abuse_score >= Some(1) {
checked_remote_address = format!("{} *low abuse score: {}*", remote_address, abuse_score.unwrap());
}
else if abuse_score == Some(0) {
checked_remote_address = format!("{} **✓**", remote_address);
}
else {
checked_remote_address = (&remote_address).to_string();
}
checked_remote_address
}
fn format_known_address(remote_address: &String, address_type: &address_checkers::IPType) -> String {
match address_type {
address_checkers::IPType::Unspecified => {
format!("*{}*", remote_address)
}
address_checkers::IPType::Localhost => {
format!("*{} localhost*", remote_address)
}
address_checkers::IPType::Extern => {
remote_address.to_string()
}
}
}
pub fn get_connections_table(all_connections: &Vec<connections::Connection>) {
let skin: MadSkin = create_table_style();
let (terminal_width, _) = terminal_size();
string_utils::pretty_print_info(&format!("Connections: **{}**", all_connections.len()));
static CENTER_MARKDOWN_ROW: &str = "| :-: | :-: | :-: | :-: | :-: | :-: | :-: |\n";
let mut markdown = CENTER_MARKDOWN_ROW.to_string();
markdown.push_str("| **#** | **proto** | **local port** | **remote address** | **remote port** | **program***/pid* | **state** |\n");
for (idx, connection) in all_connections.iter().enumerate() {
markdown.push_str(CENTER_MARKDOWN_ROW);
let remote_address = &connection.remote_address;
let mut formatted_remote_address: String = format_known_address(remote_address, &connection.address_type);
formatted_remote_address = format_abuse_checked_address(&formatted_remote_address, connection.abuse_score);
markdown.push_str(&format!("| *{}* | {} | {} | {} | {} | {}*/{}* | {} |\n",
idx + 1, connection.proto, connection.local_port, &formatted_remote_address, connection.remote_port, connection.program, connection.pid, connection.state
));
}
let max_column_spaces: [u16; 7] = [5, 5, 7, 32, 7, 24, 13];
let terminal_filling_row: String = string_utils::fill_terminal_width(terminal_width, max_column_spaces);
markdown.push_str(&terminal_filling_row);
markdown.push_str(CENTER_MARKDOWN_ROW);
println!("{}\n", skin.term_text(&markdown));
}