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
mod scanner;
static DOMAIN_REGEX: &str =
r"^(([a-zA-Z_\-]{1,63}\.)*?)*?([a-zA-Z_\-]{1,63})(\.[a-zA-Z_\-]{1,63})?$";
#[derive(Debug, PartialEq, Clone)]
pub enum ToCheck {
HostnameAndPort(String, u16),
#[cfg(feature = "http")]
#[allow(rustdoc::bare_urls)]
HttpOrHttpsUrl(hyper::Uri),
}
impl std::fmt::Display for ToCheck {
#[cfg(feature = "http")]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::HostnameAndPort(domain, port) => format!("{}:{}", domain, port).fmt(f),
Self::HttpOrHttpsUrl(uri) => uri.fmt(f),
}
}
#[cfg(not(feature = "http"))]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::HostnameAndPort(domain, port) => format!("{}:{}", domain, port).fmt(f),
}
}
}
impl ToCheck {
fn from_host_and_port(domain_and_port: &str) -> Result<Self, String> {
let parts: Vec<String> = domain_and_port.split(':').map(String::from).collect();
if parts.len() != 2 {
return Err(format!(
"'{}' doesn't match <hostname>:<port> pattern",
domain_and_port
));
}
let port: u16 = parts[1]
.parse()
.map_err(|err| format!("'{}', port error: {}", domain_and_port, err))?;
if port == 0 {
return Err("dynamic port number (0) can't be used here".into());
}
let hostname = parts[0].clone();
let regex = regex::Regex::new(DOMAIN_REGEX).unwrap();
let ip: Result<std::net::IpAddr, _> = hostname.parse();
if !regex.is_match(&hostname) && ip.is_err() {
return Err(format!("'{}' is not a valid hostname", hostname));
}
Ok(Self::HostnameAndPort(hostname, port))
}
#[cfg(feature = "http")]
fn from_http_url(http_url: &str) -> Result<Self, String> {
Ok(Self::HttpOrHttpsUrl(
http_url.parse::<hyper::Uri>().map_err(|e| e.to_string())?,
))
}
#[cfg(not(feature = "http"))]
fn from_http_url(_uri: &str) -> Result<Self, String> {
panic!("Not compiled with 'http' feature")
}
}
impl std::str::FromStr for ToCheck {
type Err = String;
#[cfg(feature = "http")]
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("http://") || s.starts_with("https://") {
Self::from_http_url(s)
} else {
Self::from_host_and_port(s)
}
}
#[cfg(not(feature = "http"))]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_host_and_port(s)
}
}
pub async fn wait_for_them(
hosts_ports_or_http_urls: &[ToCheck],
timeout: Option<u64>,
start_time: Option<std::time::Instant>,
silent: bool,
) -> Vec<Option<u64>> {
let start_time = start_time.unwrap_or_else(std::time::Instant::now);
let futures = if silent {
scanner::wait_silent(hosts_ports_or_http_urls, timeout, start_time)
} else {
scanner::wait(hosts_ports_or_http_urls, timeout, start_time)
};
futures::future::join_all(futures).await
}