summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 0d44a61c2ee4b203e13bc95a4bce68baf6122890 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
mod display;
mod network;
mod os;
#[cfg(test)]
mod tests;

use display::Ui;
use network::{Connection, DnsQueue, Sniffer, Utilization};

use ::std::net:: IpAddr;

use ::pnet::datalink::{DataLinkReceiver, NetworkInterface};
use ::std::collections::HashMap;
use ::std::sync::atomic::{AtomicBool, Ordering};
use ::std::sync::{Arc, Mutex};
use ::std::{thread, time};
use ::std::thread::park_timeout;
use ::termion::event::{Event, Key};
use ::tui::backend::Backend;

use ::std::io;
use ::termion::raw::IntoRawMode;
use ::tui::backend::TermionBackend;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "what")]
struct Opt {
    #[structopt(short, long)]
    interface: String,
}

fn main() {
    #[cfg(not(target_os = "linux"))]
    compile_error!(
        "Sorry, no implementations for platforms other than linux yet :( - PRs welcome!"
    );

    use os::{get_datalink_channel, get_interface, get_open_sockets, lookup_addr, receive_winch, KeyboardEvents};

    let opt = Opt::from_args();
    let stdout = io::stdout().into_raw_mode().unwrap();
    let terminal_backend = TermionBackend::new(stdout);

    let keyboard_events = Box::new(KeyboardEvents);
    let network_interface = get_interface(&opt.interface).unwrap();
    let network_frames = get_datalink_channel(&network_interface);
    let lookup_addr = Box::new(lookup_addr);
    let receive_winch = Box::new(receive_winch);

    let os_input = OsInput {
        network_interface,
        network_frames,
        get_open_sockets,
        keyboard_events,
        lookup_addr,
        receive_winch,
    };

    start(terminal_backend, os_input)
}

pub struct OsInput {
    pub network_interface: NetworkInterface,
    pub network_frames: Box<DataLinkReceiver>,
    pub get_open_sockets: fn() -> HashMap<Connection, String>,
    pub keyboard_events: Box<Iterator<Item = Event> + Send + Sync + 'static>,
    pub lookup_addr: Box<Fn(&IpAddr) -> Option<String> + Send + Sync + 'static>,
    pub receive_winch: Box<Fn(&Arc<AtomicBool>)>,
}

pub fn start<B>(terminal_backend: B, os_input: OsInput)
where
    B: Backend + Send + Sync + 'static,
{
    let running = Arc::new(AtomicBool::new(true));

    let keyboard_events = os_input.keyboard_events; // TODO: as methods in os_interface
    let get_open_sockets = os_input.get_open_sockets;
    let lookup_addr = os_input.lookup_addr;
    let receive_winch = os_input.receive_winch;

    let mut sniffer = Sniffer::new(os_input.network_interface, os_input.network_frames);
    let network_utilization = Arc::new(Mutex::new(Utilization::new()));

    let dns_queue = Arc::new(DnsQueue::new());
    let ip_to_host = Arc::new(Mutex::new(HashMap::new()));

    let dns_handler = thread::spawn({
        let dns_queue = dns_queue.clone();
        let ip_to_host = ip_to_host.clone();
        move || {
            while let Some(ip) = dns_queue.wait_for_job() {
                if let Some(addr) = lookup_addr(&IpAddr::V4(ip.clone())) {
                    ip_to_host.lock().unwrap().insert(ip, addr);
                }
            }
        }
    });

    let ui = Arc::new(Mutex::new(Ui::new(terminal_backend)));
    let winch = Arc::new(AtomicBool::new(false));
    receive_winch(&winch);

    let resize_handler = thread::spawn({
        let running = running.clone();
        let winch = winch.clone();
        let ui = ui.clone();
        move || {
            while running.load(Ordering::Acquire) {
                while winch.load(Ordering::Acquire) {
                    let mut ui = ui.lock().unwrap();
                    ui.draw();
                    winch.store(false, Ordering::Release);
                }
            }
        }
    });

    let display_handler = thread::spawn({
        let running = running.clone();
        let network_utilization = network_utilization.clone();
        let ip_to_host = ip_to_host.clone();
        let dns_queue = dns_queue.clone();
        let ui = ui.clone();
        move || {
            while running.load(Ordering::Acquire) {
                let connections_to_procs = get_open_sockets();
                let ip_to_host = {
                    ip_to_host.lock().unwrap().clone()
                };
                let utilization = {
                    let mut network_utilization = network_utilization.lock().unwrap();
                    network_utilization.clone_and_reset()
                };
                dns_queue.find_ips_to_resolve(&connections_to_procs, &ip_to_host);
                {
                    let mut ui = ui.lock().unwrap();
                    ui.update_state(connections_to_procs, utilization, ip_to_host);
                    ui.draw();
                }
                park_timeout(time::Duration::from_secs(1));
            }
            let mut ui = ui.lock().unwrap();
            ui.end();
            dns_queue.end();
        }
    });

    let stdin_handler = thread::spawn({
        let running = running.clone();
        let display_handler = display_handler.thread().clone(); // TODO: better
        move || {
            for evt in keyboard_events {
                match evt {
                    Event::Key(Key::Ctrl('c')) | Event::Key(Key::Char('q')) => {
                        running.store(false, Ordering::Release);
                        display_handler.unpark();
                        break;
                    }
                    _ => (),
                };
            }
        }
    });


    let sniffing_handler = thread::spawn(move || {
        while running.load(Ordering::Acquire) {
            if let Some(segment) = sniffer.next() {
                network_utilization.lock().unwrap().update(&segment)
            }
        }
    });
    display_handler.join().unwrap();
    sniffing_handler.join().unwrap();
    stdin_handler.join().unwrap();
    dns_handler.join().unwrap();
    resize_handler.join().unwrap();
}