@@ -40,7 +40,6 @@ def main():
40
40
target_list = list (set (target_list )) # Remove possible duplicates
41
41
42
42
if args .url :
43
-
44
43
try :
45
44
res = scan_target (args .url , args )
46
45
except SecurityHeadersException as e :
@@ -55,20 +54,18 @@ def main():
55
54
56
55
57
56
def async_scan_done (scan ):
58
- try :
59
- res , args = scan .result ()
57
+ res , args = scan .result ()
58
+ if 'error' in res :
59
+ print (f"Scanning target { res ['target' ]} ..." )
60
+ print (f"Error: { res ['error' ]} \n " )
61
+ else :
60
62
print (cmd_utils .output_text (res ['target' ], res ['headers' ], res ['https' ], args .no_color , args .verbose ))
61
- except SecurityHeadersException as e :
62
- print (e , file = sys .stderr )
63
63
64
64
65
65
def scan_target (url , args ):
66
- try :
67
- header_check = SecurityHeaders (url , args .max_redirects , args .insecure )
68
- header_check .fetch_headers ()
69
- headers = header_check .check_headers ()
70
- except SecurityHeadersException as e :
71
- raise e
66
+ header_check = SecurityHeaders (url , args .max_redirects , args .insecure )
67
+ header_check .fetch_headers ()
68
+ headers = header_check .check_headers ()
72
69
73
70
if not headers :
74
71
raise FailedToFetchHeaders ("Failed to fetch headers" )
@@ -78,8 +75,11 @@ def scan_target(url, args):
78
75
79
76
80
77
def scan_target_wrapper (url , args ):
81
- # A bit of a dirty hack to pass args to the done callback
82
- return scan_target (url , args ), args
78
+ try :
79
+ # Return the args also for the callback function
80
+ return scan_target (url , args ), args
81
+ except SecurityHeadersException as e :
82
+ return {'target' : url , 'error' : str (e )}, args
83
83
84
84
85
85
async def scan_multiple_targets (args ):
@@ -89,22 +89,23 @@ async def scan_multiple_targets(args):
89
89
loop = asyncio .get_event_loop ()
90
90
tasks = []
91
91
for target in targets :
92
- if args .json :
93
- task = loop .run_in_executor (None , scan_target , target , args )
94
- else :
95
- task = loop .run_in_executor (None , scan_target_wrapper , target , args )
92
+ task = loop .run_in_executor (None , scan_target_wrapper , target , args )
93
+ if not args .json :
94
+ # Output result of each scan immediately
96
95
task .add_done_callback (async_scan_done )
97
96
tasks .append (task )
98
97
99
98
res = []
100
99
for task in tasks :
101
100
await task
102
101
102
+ # When json output, aggregate the results and output the json dump at the end
103
103
if args .json :
104
104
for t in tasks :
105
- res .append (t .result ())
105
+ val , _args = t .result ()
106
+ res .append (val )
106
107
107
- print (str (res ))
108
+ print (json . dumps (res , indent = 2 ))
108
109
109
110
if __name__ == "__main__" :
110
111
main ()
0 commit comments