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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
use std::{
io::{self, ErrorKind, Write},
net::Ipv4Addr,
time,
};
use crossterm::event::{read, Event};
use pnet::datalink::{self, Channel::Ethernet, Config, DataLinkReceiver, NetworkInterface};
use tokio::runtime::Runtime;
use crate::{network::dns, os::errors::GetInterfaceError, OsInputOutput};
#[cfg(target_os = "linux")]
use crate::os::linux::get_open_sockets;
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
use crate::os::lsof::get_open_sockets;
#[cfg(target_os = "windows")]
use crate::os::windows::get_open_sockets;
pub struct TerminalEvents;
impl Iterator for TerminalEvents {
type Item = Event;
fn next(&mut self) -> Option<Event> {
match read() {
Ok(ev) => Some(ev),
Err(_) => None,
}
}
}
pub(crate) fn get_datalink_channel(
interface: &NetworkInterface,
) -> Result<Box<dyn DataLinkReceiver>, GetInterfaceError> {
let config = Config {
read_timeout: Some(time::Duration::new(1, 0)),
read_buffer_size: 65536,
..Default::default()
};
match datalink::channel(interface, config) {
Ok(Ethernet(_tx, rx)) => Ok(rx),
Ok(_) => Err(GetInterfaceError::OtherError(format!(
"{}: Unsupported interface type",
interface.name
))),
Err(e) => match e.kind() {
ErrorKind::PermissionDenied => Err(GetInterfaceError::PermissionError(
interface.name.to_owned(),
)),
_ => Err(GetInterfaceError::OtherError(format!(
"{}: {}",
&interface.name, e
))),
},
}
}
fn get_interface(interface_name: &str) -> Option<NetworkInterface> {
datalink::interfaces()
.into_iter()
.find(|iface| iface.name == interface_name)
}
fn create_write_to_stdout() -> Box<dyn FnMut(String) + Send> {
Box::new({
let mut stdout = io::stdout();
move |output: String| {
writeln!(stdout, "{}", output).unwrap();
}
})
}
#[derive(Debug)]
pub struct UserErrors {
permission: Option<String>,
other: Option<String>,
}
pub fn collect_errors<'a, I>(network_frames: I) -> String
where
I: Iterator<
Item = (
&'a NetworkInterface,
Result<Box<dyn DataLinkReceiver>, GetInterfaceError>,
),
>,
{
let errors = network_frames.fold(
UserErrors {
permission: None,
other: None,
},
|acc, (_, elem)| {
if let Some(iface_error) = elem.err() {
match iface_error {
GetInterfaceError::PermissionError(interface_name) => {
if let Some(prev_interface) = acc.permission {
return UserErrors {
permission: Some(format!("{}, {}", prev_interface, interface_name)),
..acc
};
} else {
return UserErrors {
permission: Some(interface_name),
..acc
};
}
}
error => {
if let Some(prev_errors) = acc.other {
return UserErrors {
other: Some(format!("{} \n {}", prev_errors, error)),
..acc
};
} else {
return UserErrors {
other: Some(format!("{}", error)),
..acc
};
}
}
};
}
acc
},
);
if let Some(interface_name) = errors.permission {
if let Some(other_errors) = errors.other {
format!(
"\n\n{}: {} \nAdditional Errors: \n {}",
interface_name,
eperm_message(),
other_errors
)
} else {
format!("\n\n{}: {}", interface_name, eperm_message())
}
} else {
let other_errors = errors
.other
.expect("asked to collect errors but found no errors");
format!("\n\n {}", other_errors)
}
}
pub fn get_input(
interface_name: &Option<String>,
resolve: bool,
dns_server: &Option<Ipv4Addr>,
) -> anyhow::Result<OsInputOutput> {
let network_interfaces = if let Some(name) = interface_name {
match get_interface(name) {
Some(interface) => vec![interface],
None => {
anyhow::bail!("Cannot find interface {}", name);
// the homebrew formula relies on this wording, please be careful when changing
}
}
} else {
datalink::interfaces()
};
#[cfg(target_os = "windows")]
let network_frames = network_interfaces
.iter()
.filter(|iface| !iface.ips.is_empty())
.map(|iface| (iface, get_datalink_channel(iface)));
#[cfg(not(target_os = "windows"))]
let network_frames = network_interfaces
.iter()
.filter(|iface| iface.is_up() && !iface.ips.is_empty())
.map(|iface| (iface, get_datalink_channel(iface)));
let (available_network_frames, network_interfaces) = {
let network_frames = network_frames.clone();
let mut available_network_frames = Vec::new();
let mut available_interfaces: Vec<NetworkInterface> = Vec::new();
for (iface, rx) in network_frames.filter_map(|(iface, channel)| {
if let Ok(rx) = channel {
Some((iface, rx))
} else {
None
}
}) {
available_interfaces.push(iface.clone());
available_network_frames.push(rx);
}
(available_network_frames, available_interfaces)
};
if available_network_frames.is_empty() {
let all_errors = collect_errors(network_frames.clone());
if !all_errors.is_empty() {
anyhow::bail!(all_errors);
}
anyhow::bail!("Failed to find any network interface to listen on.");
}
let keyboard_events = Box::new(TerminalEvents);
let write_to_stdout = create_write_to_stdout();
let dns_client = if resolve {
let runtime = Runtime::new()?;
let resolver = match runtime.block_on(dns::Resolver::new(dns_server)) {
Ok(resolver) => resolver,
Err(err) => anyhow::bail!(
"Could not initialize the DNS resolver. Are you offline?\n\nReason: {:?}",
err
),
};
let dns_client = dns::Client::new(resolver, runtime)?;
Some(dns_client)
} else {
None
};
Ok(OsInputOutput {
network_interfaces,
network_frames: available_network_frames,
get_open_sockets,
terminal_events: keyboard_events,
dns_client,
write_to_stdout,
})
}
#[inline]
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
fn eperm_message() -> &'static str {
"Insufficient permissions to listen on network interface(s). Try running with sudo."
}
#[inline]
#[cfg(target_os = "linux")]
fn eperm_message() -> &'static str {
r#"
Insufficient permissions to listen on network interface(s). You can work around
this issue like this:
* Try running `bandwhich` with `sudo`
* Build a `setcap(8)` wrapper for `bandwhich` with the following rules:
`cap_sys_ptrace,cap_dac_read_search,cap_net_raw,cap_net_admin+ep`
"#
}
#[inline]
#[cfg(target_os = "windows")]
fn eperm_message() -> &'static str {
"Insufficient permissions to listen on network interface(s). Try running with administrator rights."
}
|