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
use crate::errors::*; use crate::ipc::common::*; use crate::engine::Environment; use crate::geoip::MaxmindReader; use crate::psl::PslReader; use crate::worker::Event; use std::fmt::Debug; use std::io::prelude::*; use std::io::{self, Stdin, Stdout}; use std::sync::{Arc, Mutex}; pub trait IpcChild: Debug { fn send(&mut self, event: &Event) -> Result<()>; fn recv(&mut self) -> Result<serde_json::Value>; } #[derive(Debug)] pub struct StdioIpcChild { stdin: Stdin, stdout: Stdout, } impl StdioIpcChild { pub fn setup() -> StdioIpcChild { let stdin = io::stdin(); let stdout = io::stdout(); StdioIpcChild { stdin, stdout, } } pub fn recv_start(&mut self) -> Result<StartCommand> { let value = self.recv()?; let event = serde_json::from_value(value)?; Ok(event) } } impl IpcChild for StdioIpcChild { fn send(&mut self, event: &Event) -> Result<()> { let mut event = serde_json::to_string(&event)?; event.push('\n'); debug!("IpcChild sends: {:?}", event); self.stdout.write_all(event.as_bytes())?; Ok(()) } fn recv(&mut self) -> Result<serde_json::Value> { let mut line = String::new(); let len = self.stdin.read_line(&mut line)?; let event = serde_json::from_str(&line[..len])?; debug!("IpcChild received: {:?}", event); Ok(event) } } #[derive(Debug)] pub struct DummyIpcChild; impl DummyIpcChild { pub fn create() -> Arc<Mutex<Box<dyn IpcChild>>> { Arc::new(Mutex::new(Box::new(DummyIpcChild))) } } impl IpcChild for DummyIpcChild { fn send(&mut self, _event: &Event) -> Result<()> { Ok(()) } fn recv(&mut self) -> Result<serde_json::Value> { unimplemented!("DummyIpcChild::recv doesn't exist") } } pub fn run(geoip: Option<MaxmindReader>, asn: Option<MaxmindReader>, psl: PslReader) -> Result<()> { let mut ipc_child = StdioIpcChild::setup(); let start = ipc_child.recv_start()?; let environment = Environment { verbose: start.verbose, keyring: start.keyring, dns_config: start.dns_config, proxy: start.proxy, options: start.options, blobs: start.blobs, psl, geoip, asn, }; let mtx: Arc<Mutex<Box<dyn IpcChild>>> = Arc::new(Mutex::new(Box::new(ipc_child))); let result = start.module.run(environment, mtx.clone(), start.arg.into()); let mut ipc_child = Arc::try_unwrap(mtx).expect("Failed to consume Arc") .into_inner().expect("Failed to consume Mutex"); let event = result.into(); ipc_child.send(&Event::Exit(event))?; Ok(()) }