From 103c877bc161d7a8c6cb4e8b623ec0cf40294d18 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 22 Aug 2019 21:22:56 -0400 Subject: [PATCH] Allow bgp lookups from CLI --- src/main.rs | 4 ++-- src/printer.rs | 1 + src/reader.rs | 10 ++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index ba206d6..13053b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -437,9 +437,9 @@ fn main() { unsafe { PRINTER = Some(Box::new(Printer::new(store))) }; let bgp_client = BGPClient::new(bgp_sockaddr, Duration::from_secs(600), unsafe { PRINTER.as_ref().unwrap() }); - make_trusted_conn(trusted_sockaddr, bgp_client); + make_trusted_conn(trusted_sockaddr, Arc::clone(&bgp_client)); - reader::read(store, unsafe { PRINTER.as_ref().unwrap() }); + reader::read(store, unsafe { PRINTER.as_ref().unwrap() }, bgp_client); future::ok(()) }).or_else(|_| { diff --git a/src/printer.rs b/src/printer.rs index 352dc7e..0bea277 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -93,6 +93,7 @@ impl Printer { store.get_u64(U64Setting::WasGoodTimeout) ).as_bytes()).expect("stdout broken?"); out.write_all(b"a x: Scan node x\n").expect("stdout broken?"); + out.write_all(b"b x: BGP Lookup IP x\n").expect("stdout broken?"); out.write_all(b"\x1b[s").expect("stdout broken?"); // Save cursor position and provide a blank line before cursor out.write_all(b"\x1b[;H\x1b[2K").expect("stdout broken?"); out.write_all(b"Most recent log:\n").expect("stdout broken?"); diff --git a/src/reader.rs b/src/reader.rs index fda65ba..08dc5f0 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1,6 +1,7 @@ +use std::sync::Arc; use std::sync::atomic::Ordering; use std::io::BufReader; -use std::net::SocketAddr; +use std::net::{IpAddr, SocketAddr}; use std::time::Instant; use tokio::prelude::*; @@ -8,12 +9,13 @@ use tokio::io::{stdin, lines}; use crate::printer::Printer; use crate::datastore::{Store, AddressState, U64Setting, RegexSetting}; +use crate::bgp_client::BGPClient; use crate::{START_SHUTDOWN, scan_node}; use regex::Regex; -pub fn read(store: &'static Store, printer: &'static Printer) { +pub fn read(store: &'static Store, printer: &'static Printer, bgp_client: Arc) { tokio::spawn(lines(BufReader::new(stdin())).for_each(move |line| { macro_rules! err { () => { { @@ -53,6 +55,10 @@ pub fn read(store: &'static Store, printer: &'static Printer) { }); }, "a" => scan_node(Instant::now(), try_parse_next_chunk!(SocketAddr), true), + "b" => { + let ip = try_parse_next_chunk!(IpAddr); + printer.add_line(format!("ASN for {} is {}", ip, bgp_client.get_asn(ip)), false); + }, "r" => { match AddressState::from_num(try_parse_next_chunk!(u8)) { Some(state) => store.set_u64(U64Setting::RescanInterval(state), try_parse_next_chunk!(u64)), -- 2.39.5