16
16
* limitations under the License.
17
17
*/
18
18
19
- #![ allow( unused_imports) ]
20
19
mod cli;
21
20
mod lua_api;
22
21
mod network;
@@ -26,14 +25,15 @@ mod payloads;
26
25
mod scan;
27
26
28
27
use cli:: bar:: create_progress;
28
+ use cli:: bar:: { show_msg, MessageLevel } ;
29
29
use cli:: errors:: CliErrors ;
30
+ use futures:: { stream, StreamExt } ;
30
31
use glob:: glob;
31
32
use log:: error;
32
33
use parsing:: files:: filename_to_string;
33
34
use reqwest:: header:: HeaderMap ;
34
35
use std:: path:: PathBuf ;
35
- use futures:: { stream, StreamExt } ;
36
- use std:: sync:: Arc ;
36
+ use std:: sync:: { Arc , Mutex } ;
37
37
38
38
#[ derive( Clone ) ]
39
39
pub struct RequestOpts {
@@ -47,11 +47,12 @@ pub struct Lotus {
47
47
pub script_path : PathBuf ,
48
48
pub output : Option < PathBuf > ,
49
49
pub workers : usize ,
50
- pub script_workers : usize
50
+ pub script_workers : usize ,
51
+ pub stop_after : Arc < Mutex < i32 > > ,
51
52
}
52
53
53
54
impl Lotus {
54
- pub async fn start ( & self , urls : Vec < String > , request_option : RequestOpts ) {
55
+ pub async fn start ( & self , urls : Vec < String > , request_option : RequestOpts , exit_after : i32 ) {
55
56
let loaded_scripts = {
56
57
if self . script_path . is_dir ( ) {
57
58
self . load_scripts ( )
@@ -60,39 +61,60 @@ impl Lotus {
60
61
}
61
62
} ;
62
63
if loaded_scripts. is_err ( ) {
63
- eprintln ! ( "Reading errors" ) ; // TODO
64
+ show_msg ( & format ! ( "Loading scripts error: {}" , loaded_scripts . unwrap_err ( ) ) , MessageLevel :: Error ) ;
64
65
std:: process:: exit ( 1 ) ;
65
66
}
66
67
let bar =
67
68
create_progress ( urls. len ( ) as u64 * loaded_scripts. as_ref ( ) . unwrap ( ) . len ( ) as u64 ) ;
68
- if loaded_scripts. is_err ( ) {
69
- eprintln ! ( "Reading error bruh" ) ; // TODO
69
+ let loaded_scripts = loaded_scripts. unwrap ( ) ;
70
+ if self . output . is_none ( ) {
71
+ show_msg ( "Output argument is missing" , MessageLevel :: Error ) ;
70
72
std:: process:: exit ( 1 ) ;
71
73
}
72
- let loaded_scripts = loaded_scripts. unwrap ( ) ;
73
-
74
74
let lotus_obj = Arc :: new ( scan:: LuaLoader :: new (
75
75
& bar,
76
76
request_option. clone ( ) ,
77
- self . output . as_ref ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ) ) ;
77
+ self . output . as_ref ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
78
+ ) ) ;
78
79
stream:: iter ( urls)
79
80
. map ( move |url| {
80
81
let loaded_scripts = loaded_scripts. clone ( ) ;
81
82
let lotus_loader = Arc :: clone ( & lotus_obj) ;
82
83
stream:: iter ( loaded_scripts. into_iter ( ) )
83
84
. map ( move |( script_out, script_name) | {
84
85
let url = url. clone ( ) ;
85
- log:: debug!( "Running {} script on {} url" , script_name, url) ;
86
86
let lotus_loader = Arc :: clone ( & lotus_loader) ;
87
+ let error_check = {
88
+ if * self . stop_after . lock ( ) . unwrap ( ) == exit_after {
89
+ log:: debug!( "Ignoring scripts" ) ;
90
+ false
91
+ } else {
92
+ log:: debug!( "Running {} script on {} url" , script_name, url) ;
93
+ true
94
+ }
95
+ } ;
87
96
async move {
88
- lotus_loader. run_scan ( url. as_str ( ) , None , & script_out, & script_name) . await
97
+ if error_check == false {
98
+ // Nothing
99
+ } else {
100
+ let run_scan = lotus_loader
101
+ . run_scan ( url. as_str ( ) , None , & script_out, & script_name)
102
+ . await ;
103
+ if run_scan. is_err ( ) {
104
+ log:: error!( "Script is raising error" ) ;
105
+ let mut a = self . stop_after . lock ( ) . unwrap ( ) ;
106
+ log:: debug!( "Errors Counter: {}" , a) ;
107
+ * a += 1 ;
108
+ }
109
+ }
89
110
}
90
111
} )
91
112
. buffer_unordered ( self . script_workers )
92
113
. collect :: < Vec < _ > > ( )
93
114
} )
94
115
. buffer_unordered ( self . workers )
95
- . collect :: < Vec < _ > > ( ) . await ;
116
+ . collect :: < Vec < _ > > ( )
117
+ . await ;
96
118
}
97
119
98
120
fn load_scripts ( & self ) -> Result < Vec < ( String , String ) > , CliErrors > {
0 commit comments