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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use curl::easy::{Easy2, List};
use curl::multi::{Easy2Handle, Multi};
use curl::MultiError;
use rayon::prelude::*;
use ssl_expiration::SslExpiration;
use std::io::{Error, ErrorKind};
use std::time::Duration;

use crate::*;


/// Type alias for long type name:
pub type CurlHandler = Result<Easy2Handle<Collector>, MultiError>;


/// Checks trait
pub trait Checks<T> {
    /// Load check from any source
    fn load(name: &str) -> Result<T, Error>;


    /// Execute loaded checks
    fn execute(&self) -> History;


    /// Check SSL certificate expiration using OpenSSL function
    fn check_ssl_expire(domain_name: &str, domain_expectation: DomainExpectation) -> Story {
        SslExpiration::from_domain_name(&domain_name)
            .and_then(|ssl_validator| {
                match domain_expectation {
                    DomainExpectation::ValidExpiryPeriod(expected_days)
                        if ssl_validator.days() < expected_days
                            || ssl_validator.is_expired() =>
                    {
                        Ok(Story::error(Unexpected::TLSDomainExpired(
                            domain_name.to_string(),
                        )))
                    }

                    DomainExpectation::ValidExpiryPeriod(expected_days) => {
                        Ok(Story::success(Expected::TLSCertificateFresh(
                            domain_name.to_string(),
                            ssl_validator.days(),
                            expected_days,
                        )))
                    }
                }
            })
            .unwrap_or_else(|err| {
                Story::error(Unexpected::InternalProtocolProblem(
                    domain_name.to_string(),
                    err.0.to_string(),
                ))
            })
    }


    /// Check domains
    fn check_domains(domains: Option<Domains>) -> History {
        domains.map(|domains| History::new_from(
                        domains
                            .into_par_iter()
                            .flat_map(|defined_check| {
                                let domain_check = defined_check;
                                let domain_name = domain_check.name;
                                let domain_expectations = domain_check.expects;
                                debug!("check_domain::domain_expectations: {}", format!("{:?}", domain_expectations).magenta());

                                // Process Domain expectations using parallel iterator (Rayon):
                                History::new_from(
                                    domain_expectations
                                        .into_par_iter()
                                        .map(|domain_expectation| {
                                            let domain_story = Self::check_ssl_expire(&domain_name, domain_expectation);
                                            match domain_story.clone() {
                                                Story{ success: Some(success_msg), error: None, .. } =>
                                                    info!("Domain TLS-cert validity Story of: SUCCESS: {}", success_msg.to_string().green()),

                                                Story{ success: None, error: Some(error_msg), .. } =>
                                                    error!("Domain TLS-cert validity Story of: FAILURE: {}", error_msg.to_string().red()),

                                                Story{ success: None, error: None, .. } =>
                                                    warn!("Domain TLS-cert validity Story of: WARNING: {}", "Ambiguous Story that lacks both success and error?!".yellow()),

                                                Story{ success: Some(_), error: Some(_), .. } =>
                                                    warn!("Domain TLS-cert validity Story of: WARNING: {}", "Ambiguous Story with success and failure at the same time?!".yellow()),
                                            };
                                            domain_story
                                        })
                                        .collect()
                                ).stories()
                            })
                            .collect()
                        ))
            .unwrap_or_else(History::empty)
    }


    /// Find and extract code validation from validations
    fn find_code_validation(page_expectations: &[PageExpectation]) -> &PageExpectation {
        page_expectations
            .par_iter()
            .find_any(|exp| {
                match exp {
                    PageExpectation::ValidCode(_) => true,
                    _ => false,
                }
            })
            .unwrap_or_else(|| &PageExpectation::ValidCode(CHECK_DEFAULT_SUCCESSFUL_HTTP_CODE))
    }


    /// Find and extract content validation from validations
    fn find_content_validation(page_expectations: &[PageExpectation]) -> &PageExpectation {
        page_expectations
            .par_iter()
            .find_any(|exp| {
                match exp {
                    PageExpectation::ValidContent(_) => true,
                    _ => false,
                }
            })
            .unwrap_or_else(|| &PageExpectation::ValidNoContent)
    }


    /// Find and extract content length validation from validations
    fn find_content_length_validation(
        page_expectations: &[PageExpectation],
    ) -> &PageExpectation {
        page_expectations
            .par_iter()
            .find_any(|exp| {
                match exp {
                    PageExpectation::ValidLength(_) => true,
                    _ => false,
                }
            })
            .unwrap_or_else(|| &PageExpectation::ValidNoLength)
    }


    /// Find and extract address validation from validations
    fn find_address_validation(page_expectations: &[PageExpectation]) -> &PageExpectation {
        page_expectations
            .par_iter()
            .find_any(|exp| {
                match exp {
                    PageExpectation::ValidAddress(_) => true,
                    _ => false,
                }
            })
            .unwrap_or_else(|| &PageExpectation::ValidNoAddress)
    }


    /// Build headers List for Curl
    fn list_of_headers(headers: Option<Vec<String>>) -> List {
        // Build List of HTTP headers
        // ex. header:
        //         list.append("Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==").unwrap();
        let mut list = List::new();
        for header in headers.unwrap_or_default() {
            debug!("Setting Curl header: {}", header.magenta());
            list.append(&header.to_owned()).unwrap_or_default();
        }
        list
    }


    /// Load page check handler
    fn load_handler_for(page_check: &Page, multi: &Multi) -> CurlHandler {
        // Initialize Curl, set URL
        let mut curl = Easy2::new(Collector(Vec::new()));
        curl.url(&page_check.url).unwrap_or_default();
        debug!("Curl URL:: {}", format!("{}", &page_check.url.magenta()));

        // Load Curl request options from check:
        let curl_options = page_check.clone().options.unwrap_or_default();
        debug!(
            "Curl options: {}",
            format!("{}", curl_options.to_string().magenta())
        );

        // Setup Curl configuration based on given options
        if curl_options.follow_redirects.unwrap_or_else(|| true) {
            debug!("Enabled following redirects.");
            curl.follow_location(true).unwrap_or_default();
        } else {
            debug!("Disabled following redirects.");
            curl.follow_location(false).unwrap_or_default();
        }

        if curl_options.verbose.unwrap_or_else(|| false) {
            debug!("Enabling Verbose mode.");
            curl.verbose(true).unwrap_or_default();
        } else {
            debug!("Disabling Verbose mode.");
            curl.verbose(false).unwrap_or_default();
        }

        // Setup Curl configuration based on given options
        match curl_options.method {
            Some(Method::PUT) | Some(Method::POST) => {
                debug!("Curl method: {}", "POST".magenta());
                let post_data = curl_options.post_data.unwrap_or_default();
                curl.get(false).unwrap_or_default();
                curl.post(true).unwrap_or_default();
                curl.post_field_size(post_data.len() as u64)
                    .unwrap_or_default();
            }

            // fallbacks to GET
            Some(_) | None => {
                debug!("Curl method: {}", "GET".magenta());
                curl.put(false).unwrap_or_default();
                curl.post(false).unwrap_or_default();
                curl.get(true).unwrap_or_default();
            }
        };

        // Pass headers and cookies
        curl.http_headers(Self::list_of_headers(curl_options.headers))
            .unwrap_or_default();
        for cookie in curl_options.cookies.unwrap_or_default() {
            debug!("Setting cookie: {}", format!("{}", cookie.magenta()));
            curl.cookie(&cookie).unwrap_or_default();
        }

        // Set agent
        match curl_options.agent {
            Some(new_agent) => {
                debug!("Setting useragent: {}", format!("{}", &new_agent.magenta()));
                curl.useragent(&new_agent).unwrap_or_default()
            }
            None => {
                debug!("Empty useragent");
            }
        }

        // Set connection and request timeout with default fallback to 30s for each
        curl.connect_timeout(Duration::from_secs(
            curl_options
                .connection_timeout
                .unwrap_or_else(|| CHECK_CONNECTION_TIMEOUT),
        ))
        .unwrap_or_default();
        curl.timeout(Duration::from_secs(
            curl_options.timeout.unwrap_or_else(|| CHECK_TIMEOUT),
        ))
        .unwrap_or_default();

        // Verify SSL PEER
        if curl_options.ssl_verify_peer.unwrap_or_else(|| true) {
            debug!("Enabled TLS-PEER verification.");
            curl.ssl_verify_peer(true).unwrap_or_default();
        } else {
            warn!("Disabled TLS-PEER verification!");
            curl.ssl_verify_peer(false).unwrap_or_default();
        }

        // Verify SSL HOST
        if curl_options.ssl_verify_host.unwrap_or_else(|| true) {
            debug!("Enabled TLS-HOST verification.");
            curl.ssl_verify_host(true).unwrap_or_default();
        } else {
            warn!("Disabled TLS-HOST verification!");
            curl.ssl_verify_host(false).unwrap_or_default();
        }

        // Max connections is 10 per check
        curl.max_connects(CHECK_MAX_CONNECTIONS).unwrap_or_default();

        // Max reconnections is 10 per check
        curl.max_redirections(CHECK_MAX_REDIRECTIONS)
            .unwrap_or_default();

        // let handler: CurlHandler = multi.add2(curl);
        multi.add2(curl)
    }


    /// Process Curl page requests using given handler
    fn process_page_handler(
        page_check: &Page,
        handler: CurlHandler,
        multi: &Multi,
    ) -> History {
        let page_expectations = page_check.clone().expects;
        debug!(
            "process_page_handler::page_expectations: {}",
            format!("{:?}", page_expectations).magenta()
        );

        // take control over curl handler, perform validations, produce stories…
        let a_handler = match handler {
            Ok(handle) => handle,
            Err(err) => {
                error!(
                    "Curl-handler FAILURE: URL: {}. Error details: {:?}",
                    page_check.url.cyan(),
                    err.to_string().red()
                );
                return History::empty();
            }
        };
        let handle = a_handler.get_ref();
        let raw_page_content = String::from_utf8_lossy(&handle.0);
        debug!(
            "process_page_handler::raw_page_content: {}",
            format!("{}", raw_page_content).magenta()
        );
        let expected_code = Self::find_code_validation(&page_expectations);
        debug!(
            "process_page_handler::expected_code: {}",
            format!("{}", expected_code).magenta()
        );
        let expected_content = Self::find_content_validation(&page_expectations);
        debug!(
            "process_page_handler::expected_content: {}",
            format!("{}", expected_content).magenta()
        );
        let expected_content_length = Self::find_content_length_validation(&page_expectations);
        debug!(
            "process_page_handler::expected_content_length: {}",
            format!("{}", expected_content_length).magenta()
        );
        let expected_final_address = Self::find_address_validation(&page_expectations);
        debug!(
            "process_page_handler::expected_final_address: {}",
            format!("{}", expected_final_address).magenta()
        );

        // Gather Story from expectations
        let content_story = Self::handle_page_content_expectation(
            &page_check.url,
            &raw_page_content,
            expected_content,
        );
        debug!(
            "process_page_handler::content_story: {}",
            format!("{:?}", content_story).magenta()
        );
        let content_length_story = Self::handle_page_length_expectation(
            &page_check.url,
            &raw_page_content,
            expected_content_length,
        );
        debug!(
            "process_page_handler::content_length_story: {}",
            format!("{:?}", content_length_story).magenta()
        );

        let mut result_handler = match multi.remove2(a_handler) {
            Ok(res_handler) => res_handler,
            Err(err) => {
                error!(
                    "Curl-result-handler FAILURE: URL: {}. Error details: {:?}",
                    page_check.url.cyan(),
                    err.to_string().red()
                );
                return History::empty();
            }
        };
        let result_final_address = result_handler.effective_url().unwrap_or_default();
        let result_final_address_story = Self::handle_page_address_expectation(
            &page_check.url,
            &result_final_address.unwrap_or_default(),
            expected_final_address,
        );
        debug!(
            "process_page_handler::result_final_address_story: {}",
            format!("{:?}", result_final_address_story).magenta()
        );
        let result_handler_story = Self::handle_page_httpcode_expectation(
            &page_check.url,
            result_handler
                .response_code()
                .map_err(|err| Error::new(ErrorKind::Other, err.to_string())),
            expected_code,
        );
        debug!(
            "process_page_handler::handle_page_httpcode_expectation: {}",
            format!("{:?}", result_handler_story).magenta()
        );

        // Collect the history results
        History::new_from(
            [
                History::new(content_story).stories(),
                History::new(content_length_story).stories(),
                History::new(result_handler_story).stories(),
                History::new(result_final_address_story).stories(),
            ]
            .concat(),
        )
    }


    /// Build a Story from a Length PageExpectation
    fn handle_page_content_expectation(
        url: &str,
        raw_page_content: &str,
        expected_content: &PageExpectation,
    ) -> Story {
        match expected_content {
            &PageExpectation::ValidContent(ref content)
                if raw_page_content.contains(content) =>
            {
                Story::success(Expected::Content(url.to_string(), content.to_string()))
            }

            &PageExpectation::ValidContent(ref content) => {
                Story::error(Unexpected::ContentInvalid(
                    url.to_string(),
                    content.to_string(),
                ))
            }

            &PageExpectation::ValidNoContent => {
                Story::success(Expected::EmptyContent(url.to_string()))
            }

            edge_case => {
                Story::error(Unexpected::UnmatchedValidationCase(
                    url.to_string(),
                    edge_case.to_string(),
                ))
            }
        }
    }


    /// Build a Story from a Length PageExpectation
    fn handle_page_length_expectation(
        url: &str,
        raw_page_content: &str,
        expected_content_length: &PageExpectation,
    ) -> Story {
        match expected_content_length {
            &PageExpectation::ValidLength(ref requested_length)
                if raw_page_content.len() >= *requested_length =>
            {
                Story::success(Expected::ContentLength(url.to_string(), *requested_length))
            }

            &PageExpectation::ValidLength(ref requested_length) => {
                Story::error(Unexpected::ContentLengthInvalid(
                    url.to_string(),
                    raw_page_content.len(),
                    *requested_length,
                ))
            }

            &PageExpectation::ValidNoLength => {
                Story::success(Expected::NoContentLength(url.to_string()))
            }

            edge_case => {
                Story::error(Unexpected::UnmatchedValidationCase(
                    url.to_string(),
                    edge_case.to_string(),
                ))
            }
        }
    }


    /// Build a Story from a Address PageExpectation
    fn handle_page_address_expectation(
        url: &str,
        address: &str,
        expected_address: &PageExpectation,
    ) -> Story {
        match expected_address {
            &PageExpectation::ValidAddress(ref an_address) if address.contains(an_address) => {
                Story::success(Expected::Address(url.to_string(), address.to_string()))
            }

            &PageExpectation::ValidAddress(ref an_address) => {
                Story::error(Unexpected::AddressInvalid(
                    url.to_string(),
                    address.to_string(),
                    an_address.to_string(),
                ))
            }

            &PageExpectation::ValidNoAddress => {
                Story::success(Expected::Address(url.to_string(), url.to_string()))
            }

            edge_case => {
                Story::error(Unexpected::UnmatchedValidationCase(
                    url.to_string(),
                    edge_case.to_string(),
                ))
            }
        }
    }


    /// Build a Story from a HttpCode PageExpectation
    fn handle_page_httpcode_expectation(
        url: &str,
        response_code: Result<u32, Error>,
        expected_code: &PageExpectation,
    ) -> Story {
        match response_code {
            Ok(responded_code) => {
                match expected_code {
                    &PageExpectation::ValidCode(the_code) if responded_code == the_code => {
                        Story::success(Expected::HttpCode(url.to_string(), the_code))
                    }

                    &PageExpectation::ValidCode(the_code) => {
                        Story::error(Unexpected::HttpCodeInvalid(
                            url.to_string(),
                            responded_code,
                            the_code,
                        ))
                    }

                    edge_case => {
                        Story::error(Unexpected::UnmatchedValidationCase(
                            url.to_string(),
                            edge_case.to_string(),
                        ))
                    }
                }
            }

            Err(err) => {
                Story::error(Unexpected::URLConnectionProblem(
                    url.to_string(),
                    err.to_string(),
                ))
            }
        }
    }


    /// Check pages
    fn check_pages(pages: Option<Pages>) -> History {
        let mut multi = Multi::new();
        multi.pipelining(true, true).unwrap_or_default();
        pages
            .and_then(|pages| {
                // collect tuple of page-checks and Curl handler:
                let process_handlers: Vec<_> // : Vec<(Page, CurlHandler)>
                    = pages
                        .iter()
                        .map(|check| (check, Self::load_handler_for(&check, &multi)))
                        .collect();

                // perform all checks at once:
                while multi
                        .perform()
                        .unwrap_or_default() > 0 {
                    multi
                        .wait(&mut [], Duration::from_secs(1))
                        .unwrap_or_default();
                }

                // Collect History of results:
                Some(
                     History::new_from(
                        process_handlers
                            .into_iter()
                            .flat_map(|(check, handler)|
                                Self::process_page_handler(&check, handler, &multi)
                                    .stories()
                                    .into_par_iter()
                                    .map(|new_story| {
                                        match new_story.clone() {
                                            Story{ success: Some(success_msg), error: None, .. } =>
                                                info!("Web-page Story of: SUCCESS: {}", success_msg.to_string().green()),

                                            Story{ success: None, error: Some(error_msg), .. } =>
                                                error!("Web-page Story of: FAILURE: {}", error_msg.to_string().red()),

                                            Story{ success: None, error: None, .. } =>
                                                warn!("Web-page Story of: WARNING: {}", "Ambiguous Story that lacks both success and error?!".yellow()),

                                            Story{ success: Some(_), error: Some(_), .. } =>
                                                warn!("Web-page Story of: WARNING: {}", "Ambiguous Story with success and failure at the same time?!".yellow()),
                                        };
                                        new_story
                                    })
                                    .collect::<Stories>()
                            )
                            .collect()
                    )
                )
            })
            .unwrap_or_else(History::empty)
    }
}