Skip to content

Commit 13003b5

Browse files
committed
Refactored scanner struct for accessibility, fixed macros
1 parent 03baf43 commit 13003b5

File tree

4 files changed

+98
-78
lines changed

4 files changed

+98
-78
lines changed

src/input.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ pub struct Opts {
7171
#[structopt(short, long)]
7272
pub no_config: bool,
7373

74-
/// Quiet mode. Only output the ports. No Nmap. Useful for grep or outputting to a file.
74+
/// Greppable mode. Only output the ports. No Nmap. Useful for grep or outputting to a file.
7575
#[structopt(short, long)]
76-
pub quiet: bool,
76+
pub greppable: bool,
7777

7878
/// Accessible mode. Turns off features which negatively affect screen readers.
7979
#[structopt(long)]
@@ -151,7 +151,7 @@ impl Opts {
151151
}
152152
}
153153

154-
merge_required!(addresses, quiet, accessible, batch_size, timeout, scan_order, command);
154+
merge_required!(addresses, greppable, accessible, batch_size, timeout, scan_order, command);
155155
}
156156

157157
fn merge_optional(&mut self, config: &Config) {
@@ -188,7 +188,7 @@ pub struct Config {
188188
addresses: Option<Vec<String>>,
189189
ports: Option<HashMap<String, u16>>,
190190
range: Option<PortRange>,
191-
quiet: Option<bool>,
191+
greppable: Option<bool>,
192192
accessible: Option<bool>,
193193
batch_size: Option<u16>,
194194
timeout: Option<u32>,
@@ -210,12 +210,6 @@ impl Config {
210210
/// scan_order: "Serial"
211211
///
212212
pub fn read() -> Self {
213-
let mut config_dir = match dirs::config_dir() {
214-
Some(dir) => dir,
215-
None => panic!("Could not infer config file path."),
216-
};
217-
config_dir.push("rustscan");
218-
config_dir.push("config.toml");
219213

220214
let mut home_dir = match dirs::home_dir() {
221215
Some(dir) => dir,
@@ -231,13 +225,6 @@ impl Config {
231225
}
232226
}
233227

234-
if config_dir.exists() && content == String::new() {
235-
content = match fs::read_to_string(config_dir) {
236-
Ok(content) => content,
237-
Err(_) => String::new(),
238-
}
239-
}
240-
241228
let config: Config = match toml::from_str(&content) {
242229
Ok(config) => config,
243230
Err(e) => {
@@ -260,7 +247,7 @@ mod tests {
260247
addresses: vec![],
261248
ports: None,
262249
range: None,
263-
quiet: false,
250+
greppable: false,
264251
batch_size: 0,
265252
timeout: 0,
266253
ulimit: None,
@@ -276,7 +263,7 @@ mod tests {
276263
addresses: Some(vec!["127.0.0.1".to_owned()]),
277264
ports: None,
278265
range: None,
279-
quiet: Some(true),
266+
greppable: Some(true),
280267
batch_size: Some(25_000),
281268
timeout: Some(1_000),
282269
ulimit: None,
@@ -289,7 +276,7 @@ mod tests {
289276
opts.merge(&config);
290277

291278
assert_eq!(opts.addresses, vec![] as Vec<String>);
292-
assert_eq!(opts.quiet, false);
279+
assert_eq!(opts.greppable, false);
293280
assert_eq!(opts.accessible, false);
294281
assert_eq!(opts.timeout, 0);
295282
assert_eq!(opts.command, vec![] as Vec<String>);
@@ -302,7 +289,7 @@ mod tests {
302289
addresses: vec![],
303290
ports: None,
304291
range: None,
305-
quiet: false,
292+
greppable: false,
306293
batch_size: 0,
307294
timeout: 0,
308295
ulimit: None,
@@ -319,7 +306,7 @@ mod tests {
319306
ports: None,
320307
no_nmap: Some(false),
321308
range: None,
322-
quiet: Some(true),
309+
greppable: Some(true),
323310
batch_size: Some(25_000),
324311
timeout: Some(1_000),
325312
ulimit: None,
@@ -331,7 +318,7 @@ mod tests {
331318
opts.merge_required(&config);
332319

333320
assert_eq!(opts.addresses, config.addresses.unwrap());
334-
assert_eq!(opts.quiet, config.quiet.unwrap());
321+
assert_eq!(opts.greppable, config.greppable.unwrap());
335322
assert_eq!(opts.timeout, config.timeout.unwrap());
336323
assert_eq!(opts.command, config.command.unwrap());
337324
assert_eq!(opts.accessible, config.accessible.unwrap());
@@ -344,7 +331,7 @@ mod tests {
344331
addresses: vec![],
345332
ports: None,
346333
range: None,
347-
quiet: false,
334+
greppable: false,
348335
batch_size: 0,
349336
timeout: 0,
350337
ulimit: None,
@@ -363,7 +350,7 @@ mod tests {
363350
start: 1,
364351
end: 1_000,
365352
}),
366-
quiet: None,
353+
greppable: None,
367354
batch_size: None,
368355
timeout: None,
369356
no_nmap: Some(false),

src/main.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ fn main() {
5050

5151
debug!("Main() `opts` arguments are {:?}", opts);
5252

53-
if !opts.quiet && !opts.accessible {
54-
print_opening();
53+
if !opts.greppable && !opts.accessible {
54+
print_opening(&opts);
5555
}
5656

5757
let ips: Vec<IpAddr> = parse_addresses(&opts);
5858

5959
if ips.is_empty() {
60-
warning!("No IPs could be resolved, aborting scan.", false);
60+
warning!("No IPs could be resolved, aborting scan.", opts.greppable, opts.accessible);
6161
std::process::exit(1);
6262
}
6363

@@ -68,8 +68,9 @@ fn main() {
6868
&ips,
6969
batch_size,
7070
Duration::from_millis(opts.timeout.into()),
71-
opts.quiet,
71+
opts.greppable,
7272
PortStrategy::pick(opts.range, opts.ports, opts.scan_order),
73+
opts.accessible,
7374
);
7475
debug!("Scanner finished building: {:?}", scanner);
7576

@@ -101,23 +102,22 @@ fn main() {
101102
ip,
102103
opts.batch_size,
103104
"'rustscan -b <batch_size> <ip address>'");
104-
warning!(x, opts.quiet);
105+
warning!(x, opts.greppable, opts.accessible);
105106
}
106107

107108
let mut nmap_bench = NamedTimer::start("Nmap");
108109
for (ip, ports) in ports_per_ip.iter_mut() {
109110
let nmap_str_ports: Vec<String> = ports.into_iter().map(|port| port.to_string()).collect();
110111

111-
detail!("Starting Nmap", opts.quiet || opts.no_nmap);
112-
113112
// nmap port style is 80,443. Comma separated with no spaces.
114113
let ports_str = nmap_str_ports.join(",");
115114

116115
// if quiet mode is on nmap should not be spawned
117-
if opts.quiet || opts.no_nmap {
116+
if opts.greppable || opts.no_nmap {
118117
println!("{} -> [{}]", &ip, ports_str);
119118
continue;
120119
}
120+
detail!("Starting Nmap", opts.greppable, opts.accessible);
121121

122122
let addr = ip.to_string();
123123
let user_nmap_args =
@@ -127,7 +127,7 @@ fn main() {
127127
output!(format!(
128128
"The Nmap command to be run is nmap {}\n",
129129
&nmap_args.join(" ")
130-
));
130+
), opts.greppable.clone(), opts.accessible.clone());
131131

132132
// Runs the nmap command and spawns it as a process.
133133
let mut child = Command::new("nmap")
@@ -148,7 +148,7 @@ fn main() {
148148
}
149149

150150
/// Prints the opening title of RustScan
151-
fn print_opening() {
151+
fn print_opening(opts: &Opts) {
152152
debug!("Printing opening");
153153
let s = r#".----. .-. .-. .----..---. .----. .---. .--. .-. .-.
154154
| {} }| { } |{ {__ {_ _}{ {__ / ___} / {} \ | `| |
@@ -163,23 +163,16 @@ Faster Nmap scanning with Rust."#;
163163
println!("{}", info.gradient(Color::Yellow).bold());
164164
funny_opening!();
165165

166-
let mut config_dir = match dirs::config_dir() {
167-
Some(dir) => dir,
168-
None => panic!("Could not infer config file path."),
169-
};
170-
config_dir.push("rustscan");
171-
config_dir.push("config.toml");
172-
173166
let mut home_dir = match dirs::home_dir() {
174167
Some(dir) => dir,
175168
None => panic!("Could not infer config file path."),
176169
};
177170
home_dir.push(".rustscan.toml");
178171

179172
detail!(format!(
180-
"The config file is expected to be at {:?} or {:?}",
181-
config_dir, home_dir
182-
));
173+
"The config file is expected to be at {:?}",
174+
home_dir
175+
), opts.greppable, opts.accessible);
183176
}
184177
#[cfg(not(tarpaulin_include))]
185178
fn build_nmap_arguments<'a>(
@@ -212,7 +205,7 @@ fn parse_addresses(opts: &Opts) -> Vec<IpAddr> {
212205
Ok(mut iter) => ips.push(iter.nth(0).unwrap().ip()),
213206
_ => {
214207
let failed_to_resolve = format!("Host {:?} could not be resolved.", ip_or_host);
215-
warning!(failed_to_resolve, opts.quiet);
208+
warning!(failed_to_resolve, opts.greppable, opts.accessible);
216209
}
217210
},
218211
}
@@ -229,7 +222,7 @@ fn adjust_ulimit_size(opts: &Opts) -> rlimit::rlim {
229222
Ok(_) => {
230223
detail!(
231224
format!("Automatically increasing ulimit value to {}.", limit),
232-
opts.quiet
225+
opts.greppable, opts.accessible
233226
);
234227
}
235228
Err(_) => println!("{}", "ERROR. Failed to set ulimit value."),
@@ -247,7 +240,7 @@ fn infer_batch_size(opts: &Opts, ulimit: rlimit::rlim) -> u16 {
247240
// Adjust the batch size when the ulimit value is lower than the desired batch size
248241
if ulimit < batch_size {
249242
warning!("File limit is lower than default batch size. Consider upping with --ulimit. May cause harm to sensitive servers",
250-
opts.quiet
243+
opts.greppable, opts.accessible
251244
);
252245

253246
// When the OS supports high file limits like 8000, but the user
@@ -273,7 +266,7 @@ fn infer_batch_size(opts: &Opts, ulimit: rlimit::rlim) -> u16 {
273266
detail!(format!(
274267
"File limit higher than batch size. Can increase speed by increasing batch size '-b {}'.",
275268
ulimit - 100
276-
), opts.quiet);
269+
), opts.greppable, opts.accessible);
277270
}
278271

279272
batch_size as u16
@@ -292,7 +285,7 @@ mod tests {
292285
addresses: vec!["127.0.0.1".to_owned()],
293286
ports: None,
294287
range: None,
295-
quiet: true,
288+
greppable: true,
296289
batch_size: 50_000,
297290
timeout: 1_000,
298291
ulimit: Some(2_000),
@@ -314,7 +307,7 @@ mod tests {
314307
addresses: vec!["127.0.0.1".to_owned()],
315308
ports: None,
316309
range: None,
317-
quiet: true,
310+
greppable: true,
318311
batch_size: 50_000,
319312
timeout: 1_000,
320313
ulimit: Some(2_000),
@@ -337,7 +330,7 @@ mod tests {
337330
addresses: vec!["127.0.0.1".to_owned()],
338331
ports: None,
339332
range: None,
340-
quiet: true,
333+
greppable: true,
341334
batch_size: 50_000,
342335
timeout: 1_000,
343336
ulimit: Some(2_000),
@@ -359,7 +352,7 @@ mod tests {
359352
addresses: vec!["127.0.0.1".to_owned()],
360353
ports: None,
361354
range: None,
362-
quiet: true,
355+
greppable: true,
363356
batch_size: 50_000,
364357
timeout: 1_000,
365358
ulimit: Some(2_000),
@@ -376,8 +369,23 @@ mod tests {
376369
}
377370
#[test]
378371
fn test_print_opening_no_panic() {
372+
let opts = Opts {
373+
addresses: vec!["127.0.0.1".to_owned()],
374+
ports: None,
375+
range: None,
376+
greppable: true,
377+
batch_size: 50_000,
378+
timeout: 1_000,
379+
ulimit: Some(2_000),
380+
command: Vec::new(),
381+
accessible: false,
382+
scan_order: ScanOrder::Serial,
383+
no_config: false,
384+
no_nmap: false,
385+
top: false,
386+
};
379387
// print opening should not panic
380-
print_opening();
388+
print_opening(&opts);
381389
assert!(1 == 1);
382390
}
383391
#[test]
@@ -386,7 +394,7 @@ mod tests {
386394
addresses: vec!["127.0.0.1".to_owned()],
387395
ports: None,
388396
range: None,
389-
quiet: false,
397+
greppable: false,
390398
batch_size: 10,
391399
timeout: 1_000,
392400
ulimit: None,
@@ -409,7 +417,7 @@ mod tests {
409417
addresses: vec!["127.0.0.1".to_owned(), "192.168.0.0/30".to_owned()],
410418
ports: None,
411419
range: None,
412-
quiet: true,
420+
greppable: true,
413421
batch_size: 10,
414422
timeout: 1_000,
415423
ulimit: Some(2_000),
@@ -440,7 +448,7 @@ mod tests {
440448
addresses: vec!["google.com".to_owned()],
441449
ports: None,
442450
range: None,
443-
quiet: true,
451+
greppable: true,
444452
batch_size: 10,
445453
timeout: 1_000,
446454
ulimit: Some(2_000),
@@ -462,7 +470,7 @@ mod tests {
462470
addresses: vec!["127.0.0.1".to_owned(), "im_wrong".to_owned()],
463471
ports: None,
464472
range: None,
465-
quiet: true,
473+
greppable: true,
466474
batch_size: 10,
467475
timeout: 1_000,
468476
ulimit: Some(2_000),
@@ -484,7 +492,7 @@ mod tests {
484492
addresses: vec!["im_wrong".to_owned(), "300.10.1.1".to_owned()],
485493
ports: None,
486494
range: None,
487-
quiet: true,
495+
greppable: true,
488496
batch_size: 10,
489497
timeout: 1_000,
490498
ulimit: Some(2_000),

0 commit comments

Comments
 (0)