@@ -8,13 +8,15 @@ use anyhow::{Result, anyhow};
88use indicatif:: { ProgressBar , ProgressStyle } ;
99use crate :: types:: ScanResult ;
1010use crate :: service_detection:: detect_service;
11- use crate :: patterns:: { get_protocol, get_mac_vendor, get_rpc_info, get_services_by_port} ;
11+ use std:: sync:: Arc ;
12+ use tokio:: sync:: Semaphore ;
1213
1314const MAX_RETRIES : u32 = 2 ;
14- const RETRY_DELAY : u64 = 500 ;
15+ const RETRY_DELAY : u64 = 100 ;
1516const CHUNK_SIZE : usize = 1000 ;
1617const SUBNET_CHUNK_SIZE : usize = 100 ;
1718const MAX_CONCURRENCY : usize = 1000 ;
19+ const CONNECT_TIMEOUT : u64 = 200 ;
1820
1921pub struct Scanner {
2022 targets : Vec < String > ,
@@ -33,7 +35,7 @@ impl Scanner {
3335 service_detection : bool ,
3436 ) -> Self {
3537 let concurrency = concurrency. min ( MAX_CONCURRENCY ) ;
36- let timeout = timeout. max ( 500 ) ; // Минимальный таймаут 500мс
38+ let timeout = timeout. max ( 500 ) ;
3739
3840 Self {
3941 targets,
@@ -68,35 +70,21 @@ impl Scanner {
6870 }
6971 }
7072
71- async fn try_connect ( addr : SocketAddr , timeout_ms : u64 ) -> Result < Option < TcpStream > > {
72- for retry in 0 ..MAX_RETRIES {
73- match timeout (
74- Duration :: from_millis ( timeout_ms) ,
75- TcpStream :: connect ( addr) ,
76- ) . await {
77- Ok ( Ok ( stream) ) => {
78- stream. set_nodelay ( true ) ?;
79- return Ok ( Some ( stream) ) ;
80- }
81- Ok ( Err ( e) ) => {
82- if retry < MAX_RETRIES - 1 {
83- tokio:: time:: sleep ( Duration :: from_millis ( RETRY_DELAY ) ) . await ;
84- continue ;
85- }
86- return Ok ( None ) ;
87- }
88- Err ( _) if retry < MAX_RETRIES - 1 => {
89- tokio:: time:: sleep ( Duration :: from_millis ( RETRY_DELAY ) ) . await ;
90- continue ;
91- }
92- Err ( _) => return Ok ( None ) ,
73+ async fn try_connect ( addr : SocketAddr ) -> Result < Option < TcpStream > > {
74+ match timeout (
75+ Duration :: from_millis ( CONNECT_TIMEOUT ) ,
76+ TcpStream :: connect ( addr) ,
77+ ) . await {
78+ Ok ( Ok ( stream) ) => {
79+ stream. set_nodelay ( true ) ?;
80+ Ok ( Some ( stream) )
9381 }
82+ _ => Ok ( None ) ,
9483 }
95- Ok ( None )
9684 }
9785
9886 async fn scan_addr ( & self , addr : SocketAddr ) -> Result < Option < ScanResult > > {
99- if let Ok ( Some ( mut stream) ) = Self :: try_connect ( addr, self . timeout ) . await {
87+ if let Ok ( Some ( mut stream) ) = Self :: try_connect ( addr) . await {
10088 let mut service = None ;
10189 let mut raw_response = String :: new ( ) ;
10290
@@ -120,13 +108,18 @@ impl Scanner {
120108
121109 async fn scan_ip_chunk ( & self , ips : & [ IpAddr ] , pb : & ProgressBar ) -> Vec < ScanResult > {
122110 let mut results = Vec :: new ( ) ;
111+ let semaphore = Arc :: new ( Semaphore :: new ( self . concurrency ) ) ;
123112 let mut futures = Vec :: with_capacity ( ips. len ( ) * self . port_range . clone ( ) . count ( ) ) ;
124113
125114 for ip in ips {
126115 for port in self . port_range . clone ( ) {
127116 let addr = SocketAddr :: new ( * ip, port) ;
128117 let scanner = self . clone ( ) ;
118+ let sem = semaphore. clone ( ) ;
119+ let pb = pb. clone ( ) ;
120+
129121 futures. push ( async move {
122+ let _permit = sem. acquire ( ) . await . unwrap ( ) ;
130123 let result = scanner. scan_addr ( addr) . await ;
131124 pb. inc ( 1 ) ;
132125 result
0 commit comments