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
//#[macro_use]
//extern crate log;

use std::time::{Duration, Instant};
use std::collections::HashMap;
use tokio::time::{timeout};

mod uri;

pub use nerve_base::ScanStatus;

/// Result of UriScanner::new
pub type NewUriScannerResult = Result<UriScanner, String>;

/// Structure for uri scan  
/// 
/// Should be constructed using UriScanner::new 
pub struct UriScanner {
    /// Base URI of scan target.  
    base_uri: String,
    /// Word-list of files or directories
    word_list: Vec<String>,
    /// Timeout setting of uri scan.  
    timeout: Duration,
    /// Result of uri scan.  
    scan_result: UriScanResult,
}

/// Result of UriScanner::run_scan  
#[derive(Clone)]
pub struct UriScanResult {
    /// HashMap of responses. 
    /// 
    /// (URI, Status)
    pub responses: HashMap<String, String>,
    /// Time from start to end of scan.  
    pub scan_time: Duration,
    /// Scan job status
    pub scan_status: ScanStatus,
}

impl UriScanner{
    /// Construct new UriScanner  
    pub fn new() -> NewUriScannerResult {
        let ini_scan_result = UriScanResult{
            responses: HashMap::new(),
            scan_time: Duration::from_millis(1),
            scan_status: ScanStatus::Ready,
        };
        let uri_scanner = UriScanner{
            base_uri: String::new(),
            word_list: vec![],
            timeout: Duration::from_millis(30000),
            scan_result: ini_scan_result,
        };
        Ok(uri_scanner)
    }
    /// Set base URI of scan target.  
    pub fn set_base_uri(&mut self, base_uri: String) {
        self.base_uri = base_uri;
    }
    /// Add word(file name or dir name) to word-list
    pub fn add_word(&mut self, word: String) {
        self.word_list.push(word);
    }
    /// Set scan timeout  
    pub fn set_timeout(&mut self, timeout: Duration){
        self.timeout = timeout;
    }
    /// Run scan with current settings. 
    /// 
    /// Results are stored in UriScanner::scan_result
    pub async fn run_scan(&mut self){
        let start_time = Instant::now();
        let res = timeout(self.timeout, uri::scan_uri(&self.base_uri, &self.word_list)).await;
        match res {
            Ok(responses) => {
                self.scan_result.responses = responses;
                self.scan_result.scan_status = ScanStatus::Done;
            },
            Err(_) => {
                self.scan_result.scan_status = ScanStatus::Timeout;
            },
        }
        //self.scan_result.responses = uri::scan_uri(&self.base_uri, &self.word_list).await;
        self.scan_result.scan_time = Instant::now().duration_since(start_time);
    }
    /// Return scan result.
    pub fn get_result(&mut self) -> UriScanResult{
        return self.scan_result.clone();
    }
}