output.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //! Text and JSON output.
  2. use std::time::Duration;
  3. use dns::{Response, Query, Answer, ErrorCode, WireError};
  4. use dns::record::{Record, OPT, UnknownQtype};
  5. use dns_transport::Error as TransportError;
  6. use serde_json::{json, Value as JsonValue};
  7. use crate::colours::Colours;
  8. use crate::table::{Table, Section};
  9. /// How to format the output data.
  10. #[derive(PartialEq, Debug, Copy, Clone)]
  11. pub enum OutputFormat {
  12. /// Format the output as plain text, optionally adding ANSI colours.
  13. Text(UseColours, TextFormat),
  14. /// Format the output as one line of plain text.
  15. Short(TextFormat),
  16. /// Format the entries as JSON.
  17. JSON,
  18. }
  19. /// When to use colours in the output.
  20. #[derive(PartialEq, Debug, Copy, Clone)]
  21. pub enum UseColours {
  22. /// Always use colours.
  23. Always,
  24. /// Use colours if output is to a terminal; otherwise, do not.
  25. Automatic,
  26. /// Never use colours.
  27. Never,
  28. }
  29. /// Options that govern how text should be rendered in record summaries.
  30. #[derive(PartialEq, Debug, Copy, Clone)]
  31. pub struct TextFormat {
  32. /// Whether to format TTLs as hours, minutes, and seconds.
  33. pub format_durations: bool,
  34. }
  35. impl UseColours {
  36. /// Whether we should use colours or not. This checks whether the user has
  37. /// overridden the colour setting, and if not, whether output is to a
  38. /// terminal.
  39. pub fn should_use_colours(self) -> bool {
  40. self == Self::Always || (atty::is(atty::Stream::Stdout) && self != Self::Never)
  41. }
  42. /// Creates a palette of colours depending on the user’s wishes or whether
  43. /// output is to a terminal.
  44. pub fn palette(self) -> Colours {
  45. if self.should_use_colours() {
  46. Colours::pretty()
  47. }
  48. else {
  49. Colours::plain()
  50. }
  51. }
  52. }
  53. impl OutputFormat {
  54. pub fn print(self, responses: Vec<Response>, duration: Option<Duration>) -> bool {
  55. match self {
  56. Self::Short(tf) => {
  57. let all_answers = responses.into_iter().flat_map(|r| r.answers).collect::<Vec<_>>();
  58. if all_answers.is_empty() {
  59. eprintln!("No results");
  60. return false;
  61. }
  62. for answer in all_answers {
  63. match answer {
  64. Answer::Standard { record, .. } => {
  65. println!("{}", tf.record_payload_summary(&record))
  66. }
  67. Answer::Pseudo { opt, .. } => {
  68. println!("{}", tf.pseudo_record_payload_summary(&opt))
  69. }
  70. }
  71. }
  72. }
  73. Self::JSON => {
  74. let mut rs = Vec::new();
  75. for response in responses {
  76. let json = json!({
  77. "queries": self.json_queries(&response.queries),
  78. "answers": self.json_answers(&response.answers),
  79. "authorities": self.json_answers(&response.authorities),
  80. "additionals": self.json_answers(&response.additionals),
  81. });
  82. rs.push(json);
  83. }
  84. if let Some(duration) = duration {
  85. let object = json!({ "responses": rs, "duration": duration });
  86. println!("{}", object);
  87. }
  88. else {
  89. let object = json!({ "responses": rs });
  90. println!("{}", object);
  91. }
  92. }
  93. Self::Text(uc, tf) => {
  94. let mut table = Table::new(uc.palette(), tf);
  95. for response in responses {
  96. if let Some(rcode) = response.flags.error_code {
  97. print_error_code(rcode);
  98. }
  99. for a in response.answers {
  100. table.add_row(a, Section::Answer);
  101. }
  102. for a in response.authorities {
  103. table.add_row(a, Section::Authority);
  104. }
  105. for a in response.additionals {
  106. table.add_row(a, Section::Additional);
  107. }
  108. }
  109. table.print(duration);
  110. }
  111. }
  112. true
  113. }
  114. pub fn print_error(self, error: TransportError) {
  115. match self {
  116. Self::Short(..) | Self::Text(..) => {
  117. eprintln!("Error [{}]: {}", erroneous_phase(&error), error_message(error));
  118. }
  119. Self::JSON => {
  120. let object = json!({
  121. "error": true,
  122. "error_phase": erroneous_phase(&error),
  123. "error_message": error_message(error),
  124. });
  125. eprintln!("{}", object);
  126. }
  127. }
  128. }
  129. }
  130. fn erroneous_phase(error: &TransportError) -> &'static str {
  131. match error {
  132. TransportError::NetworkError(_) => "network",
  133. TransportError::HttpError(_) => "http",
  134. TransportError::TlsError(_) => "tls",
  135. TransportError::BadRequest => "http-status",
  136. TransportError::WireError(_) => "protocol",
  137. }
  138. }
  139. fn error_message(error: TransportError) -> String {
  140. match error {
  141. TransportError::NetworkError(e) => e.to_string(),
  142. TransportError::HttpError(e) => e.to_string(),
  143. TransportError::TlsError(e) => e.to_string(),
  144. TransportError::BadRequest => "Nameserver returned HTTP 400 Bad Request".into(),
  145. TransportError::WireError(e) => wire_error_message(e),
  146. }
  147. }
  148. fn wire_error_message(error: WireError) -> String {
  149. match error {
  150. WireError::IO => "Malformed packet: insufficient data".into(),
  151. WireError::WrongRecordLength { expected, got } => format!("Malformed packet: expected length {}, got {}", expected, got),
  152. WireError::WrongLabelLength { expected, got } => format!("Malformed packet: expected length {}, got {}", expected, got),
  153. WireError::TooMuchRecursion(indices) => format!("Malformed packet: too much recursion: {:?}", indices),
  154. WireError::OutOfBounds(index) => format!("Malformed packet: out of bounds ({})", index),
  155. }
  156. }
  157. impl TextFormat {
  158. pub fn record_payload_summary(self, record: &Record) -> String {
  159. match *record {
  160. Record::A(ref a) => {
  161. format!("{}", a.address)
  162. }
  163. Record::AAAA(ref aaaa) => {
  164. format!("{}", aaaa.address)
  165. }
  166. Record::CAA(ref caa) => {
  167. if caa.critical {
  168. format!("{:?} {:?} (critical)", caa.tag, caa.value)
  169. }
  170. else {
  171. format!("{:?} {:?} (non-critical)", caa.tag, caa.value)
  172. }
  173. }
  174. Record::CNAME(ref cname) => {
  175. format!("{:?}", cname.domain)
  176. }
  177. Record::MX(ref mx) => {
  178. format!("{} {:?}", mx.preference, mx.exchange)
  179. }
  180. Record::NS(ref ns) => {
  181. format!("{:?}", ns.nameserver)
  182. }
  183. Record::PTR(ref ptr) => {
  184. format!("{:?}", ptr.cname)
  185. }
  186. Record::SOA(ref soa) => {
  187. format!("{:?} {:?} {} {} {} {} {}",
  188. soa.mname, soa.rname, soa.serial,
  189. self.format_duration(soa.refresh_interval),
  190. self.format_duration(soa.retry_interval),
  191. self.format_duration(soa.expire_limit),
  192. self.format_duration(soa.minimum_ttl),
  193. )
  194. }
  195. Record::SRV(ref srv) => {
  196. format!("{} {} {:?}:{}", srv.priority, srv.weight, srv.target, srv.port)
  197. }
  198. Record::TXT(ref txt) => {
  199. format!("{:?}", txt.message)
  200. }
  201. Record::Other { ref bytes, .. } => {
  202. format!("{:?}", bytes)
  203. }
  204. }
  205. }
  206. pub fn pseudo_record_payload_summary(self, opt: &OPT) -> String {
  207. format!("{} {} {} {} {:?}",
  208. opt.udp_payload_size,
  209. opt.higher_bits,
  210. opt.edns0_version,
  211. opt.flags,
  212. opt.data)
  213. }
  214. pub fn format_duration(self, seconds: u32) -> String {
  215. if ! self.format_durations {
  216. format!("{}", seconds)
  217. }
  218. else if seconds < 60 {
  219. format!("{}s", seconds)
  220. }
  221. else if seconds < 60 * 60 {
  222. format!("{}m{:02}s", seconds / 60, seconds % 60)
  223. }
  224. else if seconds < 60 * 60 * 24{
  225. format!("{}h{:02}m{:02}s", seconds / 3600, (seconds % 3600) / 60, seconds % 60)
  226. }
  227. else {
  228. format!("{}d{}h{:02}m{:02}s", seconds / 86400, (seconds % 86400) / 3600, (seconds % 3600) / 60, seconds % 60)
  229. }
  230. }
  231. }
  232. impl OutputFormat {
  233. fn json_queries(self, queries: &[Query]) -> JsonValue {
  234. let queries = queries.iter().map(|q| {
  235. json!({
  236. "name": q.qname,
  237. "class": format!("{:?}", q.qclass),
  238. "type": q.qtype,
  239. })
  240. }).collect::<Vec<_>>();
  241. json!(queries)
  242. }
  243. fn json_answers(self, answers: &[Answer]) -> JsonValue {
  244. let answers = answers.iter().map(|a| {
  245. match a {
  246. Answer::Standard { qname, qclass, ttl, record } => {
  247. let mut object = self.json_record(record);
  248. let omut = object.as_object_mut().unwrap();
  249. omut.insert("name".into(), qname.as_str().into());
  250. omut.insert("class".into(), format!("{:?}", qclass).into());
  251. omut.insert("ttl".into(), (*ttl).into());
  252. json!(object)
  253. }
  254. Answer::Pseudo { qname, opt } => {
  255. let object = json!({
  256. "name": qname,
  257. "type": "OPT",
  258. "version": opt.edns0_version,
  259. "data": opt.data,
  260. });
  261. object
  262. }
  263. }
  264. }).collect::<Vec<_>>();
  265. json!(answers)
  266. }
  267. fn json_record(self, record: &Record) -> JsonValue {
  268. match record {
  269. Record::A(rec) => json!({ "type": "A", "address": rec.address.to_string() }),
  270. Record::AAAA(rec) => json!({ "type": "AAAA", "address": rec.address.to_string() }),
  271. Record::CAA(rec) => json!({ "type": "CAA", "critical": rec.critical, "tag": rec.tag, "value": rec.value }),
  272. Record::CNAME(rec) => json!({ "type": "CNAME", "domain": rec.domain.to_string() }),
  273. Record::MX(rec) => json!({ "type": "MX", "preference": rec.preference, "exchange": rec.exchange }),
  274. Record::NS(rec) => json!({ "type": "NS", "nameserver": rec.nameserver }),
  275. Record::PTR(rec) => json!({ "type": "PTR", "cname": rec.cname }),
  276. Record::SOA(rec) => json!({ "type": "SOA", "mname": rec.mname }),
  277. Record::SRV(rec) => json!({ "type": "SRV", "priority": rec.priority, "weight": rec.weight, "port": rec.port, "target": rec.target, }),
  278. Record::TXT(rec) => json!({ "type": "TXT", "message": rec.message }),
  279. Record::Other { type_number, bytes } => {
  280. let type_name = match type_number {
  281. UnknownQtype::HeardOf(name) => json!(name),
  282. UnknownQtype::UnheardOf(num) => json!(num),
  283. };
  284. json!({ "unknown": true, "type": type_name, "bytes": bytes })
  285. }
  286. }
  287. }
  288. }
  289. pub fn print_error_code(rcode: ErrorCode) {
  290. match rcode {
  291. ErrorCode::FormatError => println!("Status: Format Error"),
  292. ErrorCode::ServerFailure => println!("Status: Server Failure"),
  293. ErrorCode::NXDomain => println!("Status: NXDomain"),
  294. ErrorCode::NotImplemented => println!("Status: Not Implemented"),
  295. ErrorCode::QueryRefused => println!("Status: Query Refused"),
  296. ErrorCode::BadVersion => println!("Status: Bad Version"),
  297. ErrorCode::Other(num) => println!("Status: Other Failure ({})", num),
  298. }
  299. }