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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
use analyze::{HostAnalysisSummary, AnalyzerResult, PortAnalysisReason, PortAnalysisResult}; use prettytable::cell::Cell; use prettytable::row::Row; use prettytable::{color, format, Attr, Table}; use serde_json; use std::io::Write; use std::net::IpAddr; use std::str::FromStr; error_chain!{ errors { InvalidOutputFormat(reason: String) { description("invalid output format selected") display("invalid output format '{}' selected", reason) } InvalidOutputDetail(reason: String) { description("invalid output detail selected") display("Invalid output detail '{}' selected", reason) } OutputFailed { description("output failed") } } } #[derive(Debug)] pub enum OutputFormat { Human, Json, None, } impl FromStr for OutputFormat { type Err = Error; fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> { match s.to_lowercase().as_ref() { "human" => Ok(OutputFormat::Human), "json" => Ok(OutputFormat::Json), "none" => Ok(OutputFormat::None), _ => Err(ErrorKind::InvalidOutputFormat(s.to_string()).into()), } } } #[derive(Debug, PartialEq)] pub enum OutputDetail { Fail, All, } impl FromStr for OutputDetail { type Err = Error; fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> { match s.to_lowercase().as_ref() { "fail" => Ok(OutputDetail::Fail), "all" => Ok(OutputDetail::All), _ => Err(ErrorKind::InvalidOutputDetail(s.to_string()).into()), } } } #[derive(Debug)] pub struct OutputConfig { pub detail: OutputDetail, pub format: OutputFormat, pub color: bool, } pub trait JsonOutput { fn output<T: Write>(&self, output_config: &OutputConfig, writer: &mut T) -> Result<()>; } pub trait HumanOutput { fn output<T: Write>(&self, output_config: &OutputConfig, writer: &mut T) -> Result<()>; fn output_tty(&self, output_config: &OutputConfig) -> Result<()>; } impl<'a> JsonOutput for AnalyzerResult<'a> { fn output<T: Write>(&self, _: &OutputConfig, writer: &mut T) -> Result<()> { let json_str = serde_json::to_string(self).chain_err(|| ErrorKind::OutputFailed)?; writer .write(json_str.as_bytes()) .chain_err(|| ErrorKind::OutputFailed)?; Ok(()) } } impl<'a> HumanOutput for AnalyzerResult<'a> { fn output<T: Write>(&self, output_config: &OutputConfig, writer: &mut T) -> Result<()> { self.build_table(output_config) .print(writer) .chain_err(|| ErrorKind::OutputFailed) } fn output_tty(&self, output_config: &OutputConfig) -> Result<()> { if output_config.color { self.build_table(output_config).printstd(); Ok(()) } else { let stdout = ::std::io::stdout(); let mut writer = stdout.lock(); self.build_table(output_config) .print(&mut writer) .chain_err(|| ErrorKind::OutputFailed) } } } impl<'a> AnalyzerResult<'a> { fn build_table(&self, output_config: &OutputConfig) -> Table { let mut table = Table::new(); table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE); table.set_titles(Row::new(vec![ Cell::new("Host"), Cell::new("Portspec"), Cell::new("Result"), Cell::new("Port"), Cell::new("Port Result"), Cell::new("Failue Reason"), ])); for a in &self.host_analysis_results { if output_config.detail == OutputDetail::Fail && a.summary == HostAnalysisSummary::Pass { continue; } let row = Row::new(vec![ Cell::new(ip_addr_to_string(a.ip).as_ref()), Cell::new(a.portspec_name.unwrap_or("")), analysis_result_to_cell(&a.summary), Cell::new(""), Cell::new(""), match a.summary { HostAnalysisSummary::Error { ref reason } => Cell::new(reason), _ => Cell::new(""), }, ]); table.add_row(row); for p in &a.port_results { if let PortAnalysisResult::Pass(_) = p { if output_config.detail == OutputDetail::Fail { continue; } } let row = Row::new(vec![ Cell::new(""), Cell::new(""), Cell::new(""), Cell::new(port_analysis_result_to_port_string(&p).as_ref()), port_analysis_result_to_port_result_cell(&p), Cell::new(port_analysis_result_to_port_result_reason(&p).as_ref()), ]); table.add_row(row); } } table } } fn ip_addr_to_string(ip_addr: &IpAddr) -> String { format!("{}", ip_addr) } fn analysis_result_to_cell(result: &HostAnalysisSummary) -> Cell { match result { HostAnalysisSummary::Pass => Cell::new("Pass").with_style(Attr::ForegroundColor(color::GREEN)), HostAnalysisSummary::Fail => Cell::new("Fail").with_style(Attr::ForegroundColor(color::RED)), HostAnalysisSummary::Error { .. } => { Cell::new("Error").with_style(Attr::ForegroundColor(color::RED)) } } } fn port_analysis_result_to_port_string(result: &PortAnalysisResult) -> String { let port = match result { PortAnalysisResult::Pass(x) => x, PortAnalysisResult::Fail(x, _) => x, PortAnalysisResult::NotScanned(x) => x, PortAnalysisResult::Unknown(x) => x, }; format!("{}", port) } fn port_analysis_result_to_port_result_cell(result: &PortAnalysisResult) -> Cell { match result { PortAnalysisResult::Pass(_) => { Cell::new("passed").with_style(Attr::ForegroundColor(color::GREEN)) } PortAnalysisResult::Fail(_, _) => { Cell::new("failed").with_style(Attr::ForegroundColor(color::RED)) } PortAnalysisResult::NotScanned(_) => { Cell::new("not scanned").with_style(Attr::ForegroundColor(color::YELLOW)) } PortAnalysisResult::Unknown(_) => { Cell::new("unknown").with_style(Attr::ForegroundColor(color::RED)) } } } fn port_analysis_result_to_port_result_reason(result: &PortAnalysisResult) -> String { match result { PortAnalysisResult::Pass(_) => "", PortAnalysisResult::Fail(_, PortAnalysisReason::OpenButClosed) => { "expected Open, found Closed" } PortAnalysisResult::Fail(_, PortAnalysisReason::ClosedButOpen) => { "expected Closed, found Open" } PortAnalysisResult::Fail(_, PortAnalysisReason::Unknown) => "unknown", PortAnalysisResult::NotScanned(_) => "", PortAnalysisResult::Unknown(_) => "", }.to_owned() }