Skip to content

Commit a8046f9

Browse files
committed
new: using tcp_nodelay and single http client for port.scanner
1 parent f7c3ad3 commit a8046f9

File tree

7 files changed

+43
-22
lines changed

7 files changed

+43
-22
lines changed

src/plugins/port_scanner/grabbers/http.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ pub(crate) async fn parse_http_raw_response(
161161
}
162162
}
163163

164+
#[allow(clippy::too_many_arguments)]
164165
pub(crate) async fn http_grabber(
166+
http_client: &reqwest::Client,
165167
opts: &options::Options,
166168
host: &str,
167169
address: &str,
@@ -252,23 +254,7 @@ pub(crate) async fn http_grabber(
252254

253255
log::debug!("grabbing http banner for {} ...", &url);
254256

255-
let cli = reqwest::Client::builder()
256-
.no_proxy() // used to set auto_sys_proxy to false, see https://github.com/evilsocket/legba/issues/8
257-
.danger_accept_invalid_certs(true)
258-
.build();
259-
let cli = if let Ok(c) = cli {
260-
c
261-
} else {
262-
log::error!(
263-
"can't create http client for {}:{}: {:?}",
264-
address,
265-
port,
266-
cli.err()
267-
);
268-
return banner;
269-
};
270-
271-
let resp = cli
257+
let resp = http_client
272258
.get(&url)
273259
.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36")
274260
.timeout(timeout)

src/plugins/port_scanner/grabbers/line.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ pub(crate) async fn line_grabber(
4545

4646
// send something
4747
let _ = stream
48-
.write_all(format!("GET / HTTP/1.1\r\nHost: {}\r\n\r\n", address).as_bytes())
48+
.write_all(
49+
format!(
50+
"GET / HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n",
51+
address
52+
)
53+
.as_bytes(),
54+
)
4955
.await;
5056

5157
let response = read_response_from(stream, timeout).await;

src/plugins/port_scanner/grabbers/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod mysql;
1414
pub(crate) type Banner = HashMap<String, String>;
1515

1616
pub(crate) async fn grab_tcp_banner(
17+
http_client: &reqwest::Client,
1718
opts: &options::Options,
1819
host: &str,
1920
address: &str,
@@ -26,7 +27,17 @@ pub(crate) async fn grab_tcp_banner(
2627
} else if dns::is_dns_port(port) {
2728
dns::tcp_grabber(address, port, stream, timeout).await
2829
} else if let (true, with_ssl) = http::is_http_port(opts, port) {
29-
http::http_grabber(opts, host, address, port, stream, with_ssl, timeout).await
30+
http::http_grabber(
31+
http_client,
32+
opts,
33+
host,
34+
address,
35+
port,
36+
stream,
37+
with_ssl,
38+
timeout,
39+
)
40+
.await
3041
} else {
3142
// default to an attempt at line grabbing
3243
line::line_grabber(opts, address, port, stream, timeout).await

src/plugins/port_scanner/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ super::manager::register_plugin! {
2525
pub(crate) struct PortScanner {
2626
ports: Expression,
2727
opts: options::Options,
28+
http_client: reqwest::Client,
2829
}
2930

3031
impl PortScanner {
3132
pub fn new() -> Self {
3233
PortScanner {
3334
ports: Expression::default(),
3435
opts: options::Options::default(),
36+
http_client: reqwest::Client::builder()
37+
.no_proxy() // used to set auto_sys_proxy to false, see https://github.com/evilsocket/legba/issues/8
38+
.danger_accept_invalid_certs(true)
39+
.tcp_nodelay(true)
40+
.build()
41+
.unwrap(),
3542
}
3643
}
3744

@@ -58,6 +65,7 @@ impl PortScanner {
5865

5966
if !self.opts.port_scanner_no_banners {
6067
let banner = grabbers::grab_tcp_banner(
68+
&self.http_client,
6169
&self.opts,
6270
&target,
6371
&target,

src/session/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,34 @@ pub(crate) struct Statistics {
5959
done_percent: f32,
6060
errors: usize,
6161
reqs_per_sec: usize,
62+
timeout: u64,
6263
}
6364

6465
impl Statistics {
6566
pub fn to_text(&self) -> String {
6667
if self.errors > 0 {
6768
format!(
68-
"tasks={} mem={} targets={} attempts={} done={} ({:.2?}%) errors={} speed={:.2?} reqs/s",
69+
"tasks={} mem={} targets={} attempts={} done={} ({:.2?}%) errors={} timeout={}ms speed={:.2?} reqs/s",
6970
self.tasks,
7071
human_bytes(self.memory),
7172
self.targets,
7273
self.attempts,
7374
self.done,
7475
self.done_percent,
7576
self.errors,
77+
self.timeout,
7678
self.reqs_per_sec,
7779
)
7880
} else {
7981
format!(
80-
"tasks={} mem={} targets={} attempts={} done={} ({:.2?}%) speed={:.2?} reqs/s",
82+
"tasks={} mem={} targets={} attempts={} done={} ({:.2?}%) timeout={}ms speed={:.2?} reqs/s",
8183
self.tasks,
8284
human_bytes(self.memory),
8385
self.targets,
8486
self.attempts,
8587
self.done,
8688
self.done_percent,
89+
self.timeout,
8790
self.reqs_per_sec,
8891
)
8992
}
@@ -362,6 +365,7 @@ impl Session {
362365
done_percent: perc,
363366
errors,
364367
reqs_per_sec: speed,
368+
timeout: self.runtime.get_timeout_ms(),
365369
};
366370

367371
if self.options.json {

src/session/runtime.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ impl Runtime {
6464
Duration::from_millis(self.timeout_ms.load(Ordering::Relaxed))
6565
}
6666

67+
pub fn get_timeout_ms(&self) -> u64 {
68+
self.timeout_ms.load(Ordering::Relaxed)
69+
}
70+
6771
pub fn set_timeout(&self, timeout_ms: u64) {
68-
log::info!(
72+
log::debug!(
6973
"adjusting timeout from {:?} to {}ms",
7074
self.get_timeout(),
7175
timeout_ms

src/utils/net.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub(crate) async fn async_tcp_stream(
5050
.map_err(|e| e.to_string())?
5151
.map_err(|e| e.to_string())?;
5252

53+
tcp_stream.set_nodelay(true).map_err(|e| e.to_string())?;
54+
5355
if ssl {
5456
upgrade_tcp_stream_to_ssl(Box::new(tcp_stream), host, timeout).await
5557
} else {

0 commit comments

Comments
 (0)