This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Fix progress and improvements. #101
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
315e437
Use enum for scan status.
jjnicola 6d5f7dc
Remove legacy_target string from the scan_info table.
jjnicola 30635e3
Fix progress calculation for legacy target mode.
jjnicola 269a50c
Check for scan status before trying to deleted.
jjnicola 9e1a982
Solve issues reported by Codacy.
jjnicola 056182f
Some fix.
jjnicola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
import uuid | ||
import multiprocessing | ||
import itertools | ||
from enum import Enum | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
@@ -51,6 +52,14 @@ | |
PORT = 1234 | ||
ADDRESS = "0.0.0.0" | ||
|
||
class ScanStatus(Enum): | ||
"""Scan status. """ | ||
INIT = 0 | ||
RUNNING = 1 | ||
STOPPED = 2 | ||
FINISHED = 3 | ||
|
||
|
||
|
||
class ScanCollection(object): | ||
|
||
|
@@ -103,15 +112,14 @@ def set_progress(self, scan_id, progress): | |
if progress == 100: | ||
self.scans_table[scan_id]['end_time'] = int(time.time()) | ||
|
||
def set_target_progress(self, scan_id, target, progress): | ||
def set_target_progress(self, scan_id, target, host, progress): | ||
""" Sets scan_id scan's progress. """ | ||
if progress > 0 and progress <= 100: | ||
target_process = dict() | ||
target_process = self.scans_table[scan_id]['target_progress'] | ||
target_process[target] = progress | ||
targets = self.scans_table[scan_id]['target_progress'] | ||
targets[target][host] = progress | ||
# Set scan_info's target_progress to propagate progresses | ||
# to parent process. | ||
self.scans_table[scan_id]['target_progress'] = target_process | ||
self.scans_table[scan_id]['target_progress'] = targets | ||
|
||
def set_host_finished(self, scan_id, target, host): | ||
""" Add the host in a list of finished hosts """ | ||
|
@@ -135,26 +143,26 @@ def ids_iterator(self): | |
|
||
return iter(self.scans_table.keys()) | ||
|
||
def create_scan(self, scan_id='', targets='', target_str=None, | ||
options=dict(), vts=''): | ||
def create_scan(self, scan_id='', targets='', options=None, vts=''): | ||
""" Creates a new scan with provided scan information. """ | ||
|
||
if self.data_manager is None: | ||
self.data_manager = multiprocessing.Manager() | ||
if not options: | ||
options = Dict() | ||
scan_info = self.data_manager.dict() | ||
scan_info['results'] = list() | ||
scan_info['finished_hosts'] = dict( | ||
[[target, []] for target, _, _ in targets]) | ||
scan_info['progress'] = 0 | ||
scan_info['target_progress'] = dict( | ||
[[target, 0] for target, _, _ in targets]) | ||
[[target, {}] for target, _, _ in targets]) | ||
scan_info['targets'] = targets | ||
scan_info['legacy_target'] = target_str | ||
scan_info['vts'] = vts | ||
scan_info['options'] = options | ||
scan_info['start_time'] = int(time.time()) | ||
scan_info['end_time'] = "0" | ||
scan_info['status'] = "" | ||
scan_info['status'] = ScanStatus.INIT | ||
if scan_id is None or scan_id == '': | ||
scan_id = str(uuid.uuid4()) | ||
scan_info['scan_id'] = scan_id | ||
|
@@ -185,10 +193,15 @@ def get_progress(self, scan_id): | |
|
||
return self.scans_table[scan_id]['progress'] | ||
|
||
def get_target_progress(self, scan_id): | ||
""" Get a scan's current progress value. """ | ||
def get_target_progress(self, scan_id, target): | ||
""" Get a target's current progress value. | ||
The value is calculated with the progress of each single host | ||
in the target.""" | ||
|
||
return self.scans_table[scan_id]['target_progress'] | ||
total_hosts = len(target_str_to_list(target)) | ||
host_progresses = self.scans_table[scan_id]['target_progress'].get(target) | ||
t_prog = sum(host_progresses.values()) / total_hosts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you check and ensure that total_hosts will never be zero? |
||
return t_prog | ||
|
||
def get_start_time(self, scan_id): | ||
""" Get a scan's start time. """ | ||
|
@@ -202,14 +215,11 @@ def get_end_time(self, scan_id): | |
|
||
def get_target(self, scan_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I would rename the method to |
||
""" Get a scan's target list. """ | ||
if self.scans_table[scan_id]['legacy_target']: | ||
return self.scans_table[scan_id]['legacy_target'] | ||
|
||
target_list = [] | ||
for item in self.scans_table[scan_id]['targets']: | ||
target_list.append(item[0]) | ||
separ = ',' | ||
return separ.join(target_list) | ||
for target, _, _ in self.scans_table[scan_id]['targets']: | ||
target_list.append(target) | ||
return target_list | ||
|
||
def get_ports(self, scan_id, target): | ||
""" Get a scan's ports list. If a target is specified | ||
|
@@ -246,7 +256,7 @@ def id_exists(self, scan_id): | |
def delete_scan(self, scan_id): | ||
""" Delete a scan if fully finished. """ | ||
|
||
if self.get_status(scan_id) == "running": | ||
if self.get_status(scan_id) == ScanStatus.RUNNING: | ||
return False | ||
self.scans_table.pop(scan_id) | ||
if len(self.scans_table) == 0: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.