1 use std::sync::atomic::Ordering;
2 use std::io::BufReader;
3 use std::net::SocketAddr;
4 use std::time::Instant;
7 use tokio::io::{stdin, lines};
9 use crate::printer::Printer;
10 use crate::datastore::{Store, AddressState, U64Setting, RegexSetting};
12 use crate::{START_SHUTDOWN, scan_node};
16 pub fn read(store: &'static Store, printer: &'static Printer) {
17 tokio::spawn(lines(BufReader::new(stdin())).for_each(move |line| {
20 printer.add_line(format!("Unparsable input: \"{}\"", line), true);
21 return future::ok(());
24 let mut line_iter = line.split(' ');
25 macro_rules! get_next_chunk {
27 match line_iter.next() {
33 macro_rules! try_parse_next_chunk {
35 match get_next_chunk!().parse::<$type>() {
41 match get_next_chunk!() {
42 "c" => store.set_u64(U64Setting::ConnsPerSec, try_parse_next_chunk!(u64)),
43 "t" => store.set_u64(U64Setting::RunTimeout, try_parse_next_chunk!(u64)),
44 "v" => store.set_u64(U64Setting::MinProtocolVersion, try_parse_next_chunk!(u64)),
45 "w" => store.set_u64(U64Setting::WasGoodTimeout, try_parse_next_chunk!(u64)),
47 if line.len() < 3 || !line.starts_with("s ") {
50 store.set_regex(RegexSetting::SubverRegex, match line[2..].parse::<Regex>() {
55 "a" => scan_node(Instant::now(), try_parse_next_chunk!(SocketAddr), true),
57 match AddressState::from_num(try_parse_next_chunk!(u8)) {
58 Some(state) => store.set_u64(U64Setting::RescanInterval(state), try_parse_next_chunk!(u64)),
63 START_SHUTDOWN.store(true, Ordering::SeqCst);
64 return future::err(std::io::Error::new(std::io::ErrorKind::Other, ""));
70 printer.add_line("Shutting down...".to_string(), true);