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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Domain struct and methods to work with it.
use crate::cf_ips::CFIPs;
use rustls::{Certificate, OwnedTrustAnchor, RootCertStore};
use std::{error::Error, sync::Arc};
use std::{io::Write, net::TcpStream};
/// A struct to represent bits of a domain checking result.
pub mod check_result {
pub const EMPTY: u8 = 0b00000;
pub const CF_IP: u8 = 0b00001;
pub const CF_RAY_HEADER: u8 = 0b00010;
pub const CF_CACHE_STATUS_HEADER: u8 = 0b00100;
pub const CF_SERVER: u8 = 0b01000;
pub const CF_SSL: u8 = 0b10000;
}
#[derive(Debug, Clone)]
pub struct Domain {
/// A domain name.
pub name: String,
/// A bit mask to represent the result of the check.
pub check_result: u8,
/// If the domain is unreachable, it will be set to true.
pub is_unreachable: bool,
}
impl Domain {
/// Builds a new domain instance.
/// The function takes a domain name as input.
/// #Example:
/// ```
/// use cfd::domain::Domain;
/// #[tokio::main]
/// async fn main(){
/// let target = "example.com";
/// let fail_target = "-example.com!";
/// let domain = Domain::build(target.to_string());
/// assert!(domain.is_ok());
/// let fail_domain = Domain::build(fail_target.to_string());
/// assert!(fail_domain.is_err());
/// }
/// ```
pub fn build(name: String) -> Result<Self, String> {
if Self::is_valid(&name) {
return Ok(Self {
name: Domain::clear_name_from_proto(&name),
check_result: check_result::EMPTY,
is_unreachable: false,
});
} else {
return Err(format!("Invalid domain name: {}", name));
};
}
}
impl Domain {
/// Checks domain's basic validity.
/// #Example:
/// ```
/// use cfd::domain::Domain;
/// #[tokio::main]
/// async fn main(){
/// let target = "example.com";
/// let fail_target = "-example.com!";
/// let is_valid = Domain::is_valid(&target.to_string());
/// assert_eq!(is_valid, true);
/// let is_not_valid = Domain::is_valid(&fail_target.to_string());
/// assert_ne!(is_not_valid, true);
/// }
/// ```
pub fn is_valid(domain: &String) -> bool {
let domain = Domain::clear_name_from_proto(domain);
if domain.is_empty() {
return false;
}
if domain.len() > 253 {
return false;
}
let labels = domain.split('.').collect::<Vec<_>>();
if labels.len() < 2 {
return false;
}
for label in labels {
if label.len() > 63 {
return false;
}
if !label.chars().all(|c| c.is_alphanumeric() || c == '-') {
return false;
}
if label.starts_with('-') || label.ends_with('-') {
return false;
}
}
true
}
/// Clears a domain name from http(s):// prefix.
/// /// #Example:
/// ```
/// use cfd::domain::Domain;
/// #[tokio::main]
/// async fn main(){
/// let target = vec!["http://example.com","https://example.com"];
/// for domain in target {
/// let res = Domain::clear_name_from_proto(&domain.to_string());
/// assert_eq!(res, "example.com".to_string());
/// }
/// }
/// ```
pub fn clear_name_from_proto(domain: &String) -> String {
let domain = domain.trim();
let domain_chunks = domain.split("://").collect::<Vec<&str>>();
if domain_chunks.len() > 1 {
if domain_chunks[0] == "http" || domain_chunks[0] == "https" {
domain_chunks[1].to_string()
} else {
panic!("Invalid proto: {}", domain_chunks[0]);
}
} else {
domain.to_string()
}
}
}
impl Domain {
/// Checks the domain for five signs to see if it is behind CF.
/// The function takes a CFIPs (CloudFlare IPs) instance as input.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// domain.verify_domain(cf_ips).await.unwrap();
/// assert_eq!(domain.check_result, 0b11111);
/// }
/// ```
pub async fn verify_domain(&mut self, cf_ips: Arc<CFIPs>) -> Result<(), Box<dyn Error>> {
let mut result = check_result::EMPTY;
if let Ok(resp) = reqwest::get("http://".to_string() + &self.name).await {
let ip = resp.remote_addr();
if ip.is_some() && cf_ips.check_ip_v4(ip.unwrap().ip().to_string().as_str()) {
result |= check_result::CF_IP;
}
if resp.headers().get("cf-ray").is_some() {
result |= check_result::CF_RAY_HEADER;
}
if resp.headers().get("cf-cache-status").is_some() {
result |= check_result::CF_CACHE_STATUS_HEADER;
}
if resp
.headers()
.get("server")
.unwrap()
.to_str()
.unwrap()
.contains("cloudflare")
{
result |= check_result::CF_SERVER;
}
if self.get_certificate_info().await.is_ok() {
result |= check_result::CF_SSL;
}
} else {
self.is_unreachable = true
}
self.check_result = result;
Ok(())
}
}
impl Domain {
/// Gets domain's certificate info and checks if its issuer is CF.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// let res = domain.get_certificate_info().await.unwrap();
/// assert_eq!(res, true);
/// }
/// ```
pub async fn get_certificate_info(&self) -> Result<bool, Box<dyn Error>> {
let mut root_store = RootCertStore::empty();
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
let config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth();
let domain = &Domain::clear_name_from_proto(&self.name)[..];
let server_name = domain.try_into().unwrap();
let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name)?;
let mut sock = TcpStream::connect((domain, 443))?;
let mut tls = rustls::Stream::new(&mut conn, &mut sock);
tls.write_all(
concat!(
"GET / HTTP/1.1\r\n",
"Host: google.com\r\n",
"Connection: close\r\n",
"Accept-Encoding: identity\r\n",
"\r\n"
)
.as_bytes(),
)?;
let certs = tls.conn.peer_certificates().unwrap();
for cert in certs {
if Domain::get_cert_issuer(cert)
.to_lowercase()
.contains("cloudflare")
{
return Ok(true);
}
}
Ok(false)
}
fn get_cert_issuer(cert: &Certificate) -> String {
let cert = x509_parser::parse_x509_certificate(cert.as_ref()).unwrap();
cert.1.issuer.to_string()
}
}
impl Domain {
/// Checks if domain has CF's SSL.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// domain.verify_domain(cf_ips).await.unwrap();
/// assert_eq!(domain.has_cf_ssl(), true);
/// }
/// ```
pub fn has_cf_ssl(&self) -> bool {
self.check_result & check_result::CF_SSL != 0
}
/// Checks if domain has CF's IP.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// domain.verify_domain(cf_ips).await.unwrap();
/// assert_eq!(domain.has_cf_ip(), true);
/// }
/// ```
pub fn has_cf_ip(&self) -> bool {
self.check_result & check_result::CF_IP != 0
}
/// Checks if domain has CF-Ray header.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// domain.verify_domain(cf_ips).await.unwrap();
/// assert_eq!(domain.has_cf_ray_header(), true);
/// }
/// ```
pub fn has_cf_ray_header(&self) -> bool {
self.check_result & check_result::CF_RAY_HEADER != 0
}
/// Checks if domain has CF-Cache-Status header.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// domain.verify_domain(cf_ips).await.unwrap();
/// assert_eq!(domain.has_cf_cache_status_header(), true);
/// }
/// ```
pub fn has_cf_cache_status_header(&self) -> bool {
self.check_result & check_result::CF_CACHE_STATUS_HEADER != 0
}
/// Checks if domain has cloudflare server header.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// domain.verify_domain(cf_ips).await.unwrap();
/// assert_eq!(domain.has_cf_server_header(), true);
/// }
/// ```
pub fn has_cf_server_header(&self) -> bool {
self.check_result & check_result::CF_SERVER != 0
}
}
impl Domain {
/// Returns domain status.
/// #Example:
/// ```
/// use cfd::{domain::Domain, cf_ips::CFIPs};
/// use std::sync::Arc;
/// #[tokio::main]
/// async fn main(){
/// let target = "http://cloudflare.com".to_string();
/// let cf_ips = Arc::new(CFIPs::load().await.unwrap());
/// let mut domain = Domain::build(target).unwrap();
/// domain.verify_domain(cf_ips).await.unwrap();
/// assert_eq!(domain.get_status(), "CF detected");
/// }
/// ```
pub fn get_status(&self) -> &str {
let status;
if self.is_unreachable {
status = "Unreachable";
} else if self.check_result != 0 {
status = "CF detected";
} else {
status = "CF not detected";
}
status
}
}