1616
1717use std:: fmt;
1818
19+ #[ cfg( feature = "serialize" ) ]
20+ use serde:: ser:: { Serialize , SerializeStruct , Serializer } ;
21+ #[ cfg( feature = "serialize" ) ]
22+ use serde_json;
23+
1924use std:: net:: IpAddr ;
2025use std:: net:: SocketAddr ;
2126use std:: net:: ToSocketAddrs ;
@@ -104,6 +109,27 @@ impl fmt::Display for QScanError {
104109 }
105110}
106111
112+ #[ cfg( feature = "serialize" ) ]
113+ impl Serialize for QScanTcpConnectResult {
114+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
115+ where
116+ S : Serializer ,
117+ {
118+ let mut s = serializer. serialize_struct ( "QScanTcpConnectResult" , 3 ) ?;
119+ s. serialize_field ( "IP" , & self . target . ip ( ) ) ?;
120+ s. serialize_field ( "port" , & self . target . port ( ) ) ?;
121+ match self . state {
122+ QScanTcpConnectState :: Open => {
123+ s. serialize_field ( "state" , "OPEN" ) ?;
124+ }
125+ QScanTcpConnectState :: Close => {
126+ s. serialize_field ( "state" , "CLOSED" ) ?;
127+ }
128+ }
129+ s. end ( )
130+ }
131+ }
132+
107133/// Defaults
108134const SCAN_TYPE : QScanType = QScanType :: TcpConnect ;
109135const PRINT_MODE : QSPrintMode = QSPrintMode :: NonRealTime ;
@@ -283,6 +309,11 @@ impl QScanner {
283309 . collect :: < Vec < u16 > > ( ) ;
284310 }
285311
312+ #[ cfg( feature = "serialize" ) ]
313+ pub fn get_last_results_as_json_string ( & self ) -> serde_json:: Result < String > {
314+ serde_json:: to_string ( & self . last_results )
315+ }
316+
286317 /// Async TCP connect scan
287318 ///
288319 /// # Return
@@ -298,8 +329,8 @@ impl QScanner {
298329 /// let res = Runtime::new().unwrap().block_on(scanner.scan_tcp_connect());
299330 /// ```
300331 ///
301- pub async fn scan_tcp_connect ( & self ) -> Vec < QScanTcpConnectResult > {
302- let mut open_soc : Vec < QScanTcpConnectResult > = Vec :: new ( ) ;
332+ pub async fn scan_tcp_connect ( & mut self ) -> & Vec < QScanTcpConnectResult > {
333+ let mut sock_res : Vec < QScanTcpConnectResult > = Vec :: new ( ) ;
303334 let mut sock_it: sockiter:: SockIter = sockiter:: SockIter :: new ( & self . ips , & self . ports ) ;
304335 let mut ftrs = FuturesUnordered :: new ( ) ;
305336
@@ -328,7 +359,7 @@ impl QScanner {
328359 _ => { }
329360 }
330361
331- open_soc . push ( QScanTcpConnectResult {
362+ sock_res . push ( QScanTcpConnectResult {
332363 target : socket,
333364 state : QScanTcpConnectState :: Open ,
334365 } ) ;
@@ -338,15 +369,17 @@ impl QScanner {
338369 println ! ( "{}:{}:CLOSED" , error. sock. ip( ) , error. sock. port( ) ) ;
339370 }
340371
341- open_soc . push ( QScanTcpConnectResult {
372+ sock_res . push ( QScanTcpConnectResult {
342373 target : error. sock ,
343374 state : QScanTcpConnectState :: Close ,
344375 } ) ;
345376 }
346377 }
347378 }
348379
349- open_soc
380+ drop ( ftrs) ;
381+ self . last_results = Some ( sock_res) ;
382+ self . last_results . as_ref ( ) . unwrap ( )
350383 }
351384
352385 async fn scan_socket_tcp_connect ( & self , socket : SocketAddr ) -> Result < SocketAddr , QScanError > {
0 commit comments