|
| 1 | +use clap::Args; |
| 2 | +use monocle::{As2rel, As2relSearchResult, As2relSortOrder, MonocleConfig}; |
| 3 | +use serde_json::json; |
| 4 | +use tabled::settings::Style; |
| 5 | +use tabled::Table; |
| 6 | + |
| 7 | +/// Arguments for the As2rel command |
| 8 | +#[derive(Args)] |
| 9 | +pub struct As2relArgs { |
| 10 | + /// One or two ASNs to query relationships for |
| 11 | + #[clap(required = true)] |
| 12 | + pub asns: Vec<u32>, |
| 13 | + |
| 14 | + /// Force update the local as2rel database |
| 15 | + #[clap(short, long)] |
| 16 | + pub update: bool, |
| 17 | + |
| 18 | + /// Update with a custom data file (local path or URL) |
| 19 | + #[clap(long)] |
| 20 | + pub update_with: Option<String>, |
| 21 | + |
| 22 | + /// Output to pretty table, default markdown table |
| 23 | + #[clap(short, long)] |
| 24 | + pub pretty: bool, |
| 25 | + |
| 26 | + /// Hide the explanation text |
| 27 | + #[clap(long)] |
| 28 | + pub no_explain: bool, |
| 29 | + |
| 30 | + /// Sort by ASN2 ascending instead of connected percentage descending |
| 31 | + #[clap(long)] |
| 32 | + pub sort_by_asn: bool, |
| 33 | + |
| 34 | + /// Show organization name for ASN2 (from as2org database) |
| 35 | + #[clap(long)] |
| 36 | + pub show_name: bool, |
| 37 | +} |
| 38 | + |
| 39 | +pub fn run(config: &MonocleConfig, args: As2relArgs, json_output: bool) { |
| 40 | + let As2relArgs { |
| 41 | + asns, |
| 42 | + update, |
| 43 | + update_with, |
| 44 | + pretty, |
| 45 | + no_explain, |
| 46 | + sort_by_asn, |
| 47 | + show_name, |
| 48 | + } = args; |
| 49 | + |
| 50 | + if asns.is_empty() || asns.len() > 2 { |
| 51 | + eprintln!("ERROR: Please provide one or two ASNs"); |
| 52 | + std::process::exit(1); |
| 53 | + } |
| 54 | + |
| 55 | + let data_dir = config.data_dir.as_str(); |
| 56 | + let db_path = format!("{data_dir}/monocle-data.sqlite3"); |
| 57 | + let as2rel = match As2rel::new(&Some(db_path.clone())) { |
| 58 | + Ok(as2rel) => as2rel, |
| 59 | + Err(e) => { |
| 60 | + eprintln!("Failed to create AS2rel database: {}", e); |
| 61 | + std::process::exit(1); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // Handle updates |
| 66 | + if update || update_with.is_some() { |
| 67 | + println!("Updating AS2rel data..."); |
| 68 | + let result = match &update_with { |
| 69 | + Some(path) => as2rel.update_with(path), |
| 70 | + None => as2rel.update(), |
| 71 | + }; |
| 72 | + if let Err(e) = result { |
| 73 | + eprintln!("Failed to update AS2rel data: {}", e); |
| 74 | + std::process::exit(1); |
| 75 | + } |
| 76 | + println!("AS2rel data updated successfully"); |
| 77 | + } |
| 78 | + |
| 79 | + // Check if data needs to be initialized or updated |
| 80 | + if as2rel.should_update() && !update && update_with.is_none() { |
| 81 | + println!("AS2rel data is empty or outdated, updating now..."); |
| 82 | + if let Err(e) = as2rel.update() { |
| 83 | + eprintln!("Failed to update AS2rel data: {}", e); |
| 84 | + std::process::exit(1); |
| 85 | + } |
| 86 | + println!("AS2rel data updated successfully"); |
| 87 | + } |
| 88 | + |
| 89 | + // Query relationships (use JOIN-based lookup if names are requested) |
| 90 | + let mut results: Vec<As2relSearchResult> = match asns.len() { |
| 91 | + 1 => { |
| 92 | + let asn = asns[0]; |
| 93 | + let search_result = if show_name { |
| 94 | + as2rel.search_asn_with_names(asn) |
| 95 | + } else { |
| 96 | + as2rel.search_asn(asn) |
| 97 | + }; |
| 98 | + match search_result { |
| 99 | + Ok(r) => r, |
| 100 | + Err(e) => { |
| 101 | + eprintln!("Error searching for ASN {}: {}", asn, e); |
| 102 | + std::process::exit(1); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + 2 => { |
| 107 | + let asn1 = asns[0]; |
| 108 | + let asn2 = asns[1]; |
| 109 | + let search_result = if show_name { |
| 110 | + as2rel.search_pair_with_names(asn1, asn2) |
| 111 | + } else { |
| 112 | + as2rel.search_pair(asn1, asn2) |
| 113 | + }; |
| 114 | + match search_result { |
| 115 | + Ok(r) => r, |
| 116 | + Err(e) => { |
| 117 | + eprintln!("Error searching for ASN pair {} - {}: {}", asn1, asn2, e); |
| 118 | + std::process::exit(1); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + _ => { |
| 123 | + eprintln!("ERROR: Please provide one or two ASNs"); |
| 124 | + std::process::exit(1); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + if results.is_empty() { |
| 129 | + if asns.len() == 1 { |
| 130 | + println!("No relationships found for ASN {}", asns[0]); |
| 131 | + } else { |
| 132 | + println!( |
| 133 | + "No relationship found between ASN {} and ASN {}", |
| 134 | + asns[0], asns[1] |
| 135 | + ); |
| 136 | + } |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // Sort results |
| 141 | + let sort_order = if sort_by_asn { |
| 142 | + As2relSortOrder::Asn2Asc |
| 143 | + } else { |
| 144 | + As2relSortOrder::ConnectedDesc |
| 145 | + }; |
| 146 | + As2rel::sort_results(&mut results, sort_order); |
| 147 | + |
| 148 | + // Output results |
| 149 | + if json_output { |
| 150 | + let max_peers = as2rel.get_max_peers_count(); |
| 151 | + let json_results: Vec<_> = results |
| 152 | + .iter() |
| 153 | + .map(|r| { |
| 154 | + if show_name { |
| 155 | + json!({ |
| 156 | + "asn1": r.asn1, |
| 157 | + "asn2": r.asn2, |
| 158 | + "asn2_name": r.asn2_name.as_deref().unwrap_or(""), |
| 159 | + "connected": &r.connected, |
| 160 | + "peer": &r.peer, |
| 161 | + "as1_upstream": &r.as1_upstream, |
| 162 | + "as2_upstream": &r.as2_upstream, |
| 163 | + }) |
| 164 | + } else { |
| 165 | + json!({ |
| 166 | + "asn1": r.asn1, |
| 167 | + "asn2": r.asn2, |
| 168 | + "connected": &r.connected, |
| 169 | + "peer": &r.peer, |
| 170 | + "as1_upstream": &r.as1_upstream, |
| 171 | + "as2_upstream": &r.as2_upstream, |
| 172 | + }) |
| 173 | + } |
| 174 | + }) |
| 175 | + .collect(); |
| 176 | + let output = json!({ |
| 177 | + "max_peers_count": max_peers, |
| 178 | + "results": json_results, |
| 179 | + }); |
| 180 | + match serde_json::to_string_pretty(&output) { |
| 181 | + Ok(s) => println!("{}", s), |
| 182 | + Err(e) => eprintln!("Error serializing JSON: {}", e), |
| 183 | + } |
| 184 | + } else { |
| 185 | + // Print explanation unless --no-explain is set |
| 186 | + if !no_explain { |
| 187 | + println!("{}", as2rel.get_explanation()); |
| 188 | + } |
| 189 | + |
| 190 | + if show_name { |
| 191 | + let results_with_name: Vec<_> = results.into_iter().map(|r| r.with_name()).collect(); |
| 192 | + let mut table = Table::new(&results_with_name); |
| 193 | + if pretty { |
| 194 | + println!("{}", table.with(Style::rounded())); |
| 195 | + } else { |
| 196 | + println!("{}", table.with(Style::markdown())); |
| 197 | + } |
| 198 | + } else { |
| 199 | + let mut table = Table::new(&results); |
| 200 | + if pretty { |
| 201 | + println!("{}", table.with(Style::rounded())); |
| 202 | + } else { |
| 203 | + println!("{}", table.with(Style::markdown())); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments