Skip to content

Commit

Permalink
Remove usages of Scanner.queue_scan()
Browse files Browse the repository at this point in the history
  • Loading branch information
nabla-c0d3 committed Mar 27, 2021
1 parent a071c95 commit dea5500
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
12 changes: 7 additions & 5 deletions api_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ def main() -> None:
scanner = Scanner()

# Then queue some scan commands for each server
for server_info in servers_to_scan:
server_scan_req = ServerScanRequest(
server_info=server_info, scan_commands={ScanCommand.CERTIFICATE_INFO, ScanCommand.SSL_2_0_CIPHER_SUITES},
all_server_scans = [
ServerScanRequest(
server_info=server_info, scan_commands={ScanCommand.CERTIFICATE_INFO, ScanCommand.SSL_2_0_CIPHER_SUITES}
)
scanner.queue_scan(server_scan_req)
for server_info in servers_to_scan
]
scanner.start_scans(all_server_scans)

# Then retrieve the result of the scan commands for each server
for server_scan_result in scanner.get_results():
Expand Down Expand Up @@ -90,7 +92,7 @@ def basic_example() -> None:
server_scan_req = ServerScanRequest(
server_info=server_info, scan_commands={ScanCommand.CERTIFICATE_INFO, ScanCommand.SSL_2_0_CIPHER_SUITES},
)
scanner.queue_scan(server_scan_req)
scanner.start_scans([server_scan_req])

# Then retrieve the results
for server_scan_result in scanner.get_results():
Expand Down
42 changes: 16 additions & 26 deletions sslyze/__main__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import sys
from concurrent.futures import as_completed
from concurrent.futures.thread import ThreadPoolExecutor
from typing import Any, Optional

from sslyze.cli.output_hub import OutputHub
from sslyze.__version__ import __version__
from sslyze.cli.command_line_parser import CommandLineParsingError, CommandLineParser
import signal
from time import time

from sslyze.errors import ConnectionToServerFailed
from sslyze.scanner import Scanner, ServerScanRequest
from sslyze.server_connectivity import ServerConnectivityTester

global_scanner: Optional[Scanner] = None


def sigint_handler(signum: int, frame: Any) -> None:
print("Scan interrupted... shutting down.")
sys.exit()


def main() -> None:
global global_scanner

# Handle SIGINT to terminate processes
signal.signal(signal.SIGINT, sigint_handler)
start_time = time()

# Create the command line parser and the list of available options
sslyze_parser = CommandLineParser(__version__)
try:
# Parse the supplied command line
parsed_command_line = sslyze_parser.parse_command_line()
except CommandLineParsingError as e:
print(e.get_error_msg())
Expand All @@ -39,13 +26,9 @@ def main() -> None:
output_hub = OutputHub()
output_hub.command_line_parsed(parsed_command_line)

global_scanner = Scanner(
per_server_concurrent_connections_limit=parsed_command_line.per_server_concurrent_connections_limit,
concurrent_server_scans_limit=parsed_command_line.concurrent_server_scans_limit,
)

# Figure out which hosts are up and fill the task queue with work to do
# Figure out which servers are reachable
connectivity_tester = ServerConnectivityTester()
all_server_scan_requests = []
with ThreadPoolExecutor(max_workers=10) as thread_pool:
futures = [
thread_pool.submit(connectivity_tester.perform, server_location, network_config)
Expand All @@ -56,22 +39,29 @@ def main() -> None:
server_connectivity_info = completed_future.result()
output_hub.server_connectivity_test_succeeded(server_connectivity_info)

# Send scan commands for this server to the scanner
# Server is only; add it to the list of servers to scan
scan_request = ServerScanRequest(
server_info=server_connectivity_info,
scan_commands=parsed_command_line.scan_commands,
scan_commands_extra_arguments=parsed_command_line.scan_commands_extra_arguments,
)
global_scanner.queue_scan(scan_request)
all_server_scan_requests.append(scan_request)

except ConnectionToServerFailed as e:
output_hub.server_connectivity_test_failed(e)

# For the servers that are reachable, start the scans
output_hub.scans_started()

# Process the results as they come
for scan_result in global_scanner.get_results():
output_hub.server_scan_completed(scan_result)
if all_server_scan_requests:
sslyze_scanner = Scanner(
per_server_concurrent_connections_limit=parsed_command_line.per_server_concurrent_connections_limit,
concurrent_server_scans_limit=parsed_command_line.concurrent_server_scans_limit,
)
sslyze_scanner.start_scans(all_server_scan_requests)

# Process the results as they come
for scan_result in sslyze_scanner.get_results():
output_hub.server_scan_completed(scan_result)

# All done
exec_time = time() - start_time
Expand Down
3 changes: 3 additions & 0 deletions sslyze/scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def start_scans(self, server_scan_requests: List[ServerScanRequest]) -> None:
if self._are_server_scans_ongoing:
raise ValueError("Already submitted scan requests")

if not server_scan_requests:
raise ValueError("Submitted emtpy list of server_scan_requests")

self._producer_thread = ProducerThread(
concurrent_server_scans_count=self._concurrent_server_scans_count,
per_server_concurrent_connections_count=self._per_server_concurrent_connections_count,
Expand Down
2 changes: 1 addition & 1 deletion tests/web_servers/scan_localhost.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def main(server_software_running_on_localhost: WebServerSoftwareEnum) -> None:
server_scan_req = ServerScanRequest(
server_info=server_info, scan_commands=ScanCommandsRepository.get_all_scan_commands(),
)
scanner.queue_scan(server_scan_req)
scanner.start_scans([server_scan_req])

# Retrieve the result
for server_scan_result in scanner.get_results():
Expand Down

0 comments on commit dea5500

Please sign in to comment.