netscan_service/
setting.rs

1use crate::payload::{PayloadBuilder, PayloadInfo};
2use std::{
3    collections::HashMap,
4    net::{IpAddr, Ipv4Addr},
5    time::Duration,
6};
7
8/// Probe setting for service detection
9#[derive(Clone, Debug)]
10pub struct ProbeSetting {
11    /// Destination IP address
12    pub ip_addr: IpAddr,
13    /// Destination Host Name
14    pub hostname: String,
15    /// Target ports for service detection
16    pub ports: Vec<u16>,
17    /// TCP connect (open) timeout
18    pub connect_timeout: Duration,
19    /// TCP read timeout
20    pub read_timeout: Duration,
21    /// SSL/TLS certificate validation when detecting HTTPS services.  
22    ///
23    /// Default value is false, which means validation is enabled.
24    pub accept_invalid_certs: bool,
25    /// Payloads for specified ports.
26    ///
27    /// If not set, default null probe will be used. (No payload, just open TCP connection and read response)
28    pub payload_map: HashMap<u16, PayloadInfo>,
29    /// Concurrent connection limit for service detection
30    pub concurrent_limit: usize,
31}
32
33impl ProbeSetting {
34    /// Create new ProbeSetting
35    pub fn new() -> ProbeSetting {
36        ProbeSetting {
37            ip_addr: IpAddr::V4(Ipv4Addr::LOCALHOST),
38            hostname: String::new(),
39            ports: vec![],
40            connect_timeout: Duration::from_millis(200),
41            read_timeout: Duration::from_secs(5),
42            accept_invalid_certs: false,
43            payload_map: HashMap::new(),
44            concurrent_limit: 10,
45        }
46    }
47    pub fn default(ip_addr: IpAddr, hostname: String, ports: Vec<u16>) -> ProbeSetting {
48        let mut payload_map: HashMap<u16, PayloadInfo> = HashMap::new();
49        let http_head = PayloadBuilder::http_head();
50        let https_head = PayloadBuilder::https_head(hostname.clone());
51        payload_map.insert(80, http_head.clone());
52        payload_map.insert(443, https_head.clone());
53        payload_map.insert(8080, http_head);
54        payload_map.insert(8443, https_head);
55        ProbeSetting {
56            ip_addr: ip_addr,
57            hostname: hostname,
58            ports: ports,
59            connect_timeout: Duration::from_secs(1),
60            read_timeout: Duration::from_secs(5),
61            accept_invalid_certs: false,
62            payload_map: payload_map,
63            concurrent_limit: 10,
64        }
65    }
66    /// Set Destination IP address
67    pub fn with_ip_addr(&mut self, ip_addr: IpAddr) -> &mut Self {
68        self.ip_addr = ip_addr;
69        self
70    }
71    /// Set Destination Host Name. If IP address is not set, it will be resolved from the hostname.
72    pub fn with_hostname(&mut self, hostname: String) -> &mut Self {
73        self.hostname = hostname;
74        if self.ip_addr == IpAddr::V4(Ipv4Addr::LOCALHOST) {
75            match dns_lookup::lookup_host(&self.hostname) {
76                Ok(ips) => {
77                    if ips.len() > 0 {
78                        self.ip_addr = ips.first().unwrap().clone();
79                    }
80                }
81                Err(_) => {}
82            }
83        }
84        self
85    }
86    /// Add target port
87    pub fn add_port(&mut self, port: u16) {
88        self.ports.push(port);
89    }
90    /// Set connect (open) timeout in milliseconds
91    pub fn set_connect_timeout_millis(&mut self, connect_timeout_millis: u64) {
92        self.connect_timeout = Duration::from_millis(connect_timeout_millis);
93    }
94    /// Set TCP read timeout in milliseconds
95    pub fn set_read_timeout_millis(&mut self, read_timeout_millis: u64) {
96        self.read_timeout = Duration::from_millis(read_timeout_millis);
97    }
98}
99
100pub struct NoCertificateVerification {}
101impl rustls::client::ServerCertVerifier for NoCertificateVerification {
102    fn verify_server_cert(
103        &self,
104        _end_entity: &rustls::Certificate,
105        _intermediates: &[rustls::Certificate],
106        _server_name: &rustls::ServerName,
107        _scts: &mut dyn Iterator<Item = &[u8]>,
108        _ocsp: &[u8],
109        _now: std::time::SystemTime,
110    ) -> Result<rustls::client::ServerCertVerified, rustls::Error> {
111        Ok(rustls::client::ServerCertVerified::assertion())
112    }
113}