netscan_service/
setting.rs1use crate::payload::{PayloadBuilder, PayloadInfo};
2use std::{
3 collections::HashMap,
4 net::{IpAddr, Ipv4Addr},
5 time::Duration,
6};
7
8#[derive(Clone, Debug)]
10pub struct ProbeSetting {
11 pub ip_addr: IpAddr,
13 pub hostname: String,
15 pub ports: Vec<u16>,
17 pub connect_timeout: Duration,
19 pub read_timeout: Duration,
21 pub accept_invalid_certs: bool,
25 pub payload_map: HashMap<u16, PayloadInfo>,
29 pub concurrent_limit: usize,
31}
32
33impl ProbeSetting {
34 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 pub fn with_ip_addr(&mut self, ip_addr: IpAddr) -> &mut Self {
68 self.ip_addr = ip_addr;
69 self
70 }
71 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 pub fn add_port(&mut self, port: u16) {
88 self.ports.push(port);
89 }
90 pub fn set_connect_timeout_millis(&mut self, connect_timeout_millis: u64) {
92 self.connect_timeout = Duration::from_millis(connect_timeout_millis);
93 }
94 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}