2019-01-27 20:28:58 +00:00
|
|
|
from network_scan.CoreModel import CoreModel
|
|
|
|
from address_generation.Parser import Parser
|
2019-02-03 09:13:10 +00:00
|
|
|
from threading import RLock
|
2019-01-21 15:22:17 +00:00
|
|
|
import datetime
|
2019-02-03 09:13:10 +00:00
|
|
|
from PyQt5.Qt import QThread, pyqtSignal
|
|
|
|
from PyQt5.QtCore import QObject, pyqtSlot
|
2019-01-27 20:28:58 +00:00
|
|
|
from address_generation.IpGenerator import IpGenerator
|
2019-01-28 18:57:07 +00:00
|
|
|
from storage.JSONStorage import JSONStorage
|
2019-01-20 16:59:47 +00:00
|
|
|
|
2019-01-31 18:19:44 +00:00
|
|
|
|
2019-01-20 16:59:47 +00:00
|
|
|
class MainPresenter:
|
|
|
|
def __init__(self, ui):
|
|
|
|
self.ui = ui
|
|
|
|
self.threads = []
|
|
|
|
self.isScanEnabled = False
|
2019-01-28 18:57:07 +00:00
|
|
|
self.parser = Parser()
|
|
|
|
#needed config to specify path
|
|
|
|
self.storage = JSONStorage("results.json")
|
2019-02-03 09:13:10 +00:00
|
|
|
self.exit_lock = RLock()
|
2019-01-20 16:59:47 +00:00
|
|
|
|
|
|
|
def startScan(self, ipRanges, portsStr, threadNumber, timeout):
|
2019-02-03 09:13:10 +00:00
|
|
|
timeout = 3 if not timeout else int(timeout)
|
2019-01-27 20:28:58 +00:00
|
|
|
addresses = self.parser.parse_address_field(ipRanges)
|
|
|
|
ports = self.parser.parse_port_field(portsStr)
|
|
|
|
self.ip_generator = IpGenerator(addresses,ports)
|
2019-02-03 09:13:10 +00:00
|
|
|
self.scanner = CoreModel(timeout)
|
|
|
|
threadNumber = int(threadNumber)
|
|
|
|
for i in range(threadNumber):
|
|
|
|
scan_worker = ScanWorker(
|
|
|
|
self.ip_generator,
|
|
|
|
self.scanner,
|
|
|
|
self.storage
|
|
|
|
)
|
|
|
|
scan_thread = QThread()
|
|
|
|
scan_worker.log_signal.connect(self.log_text)
|
|
|
|
scan_worker.moveToThread(scan_thread)
|
|
|
|
scan_worker.exit_signal.connect(scan_thread.exit)
|
|
|
|
scan_worker.exit_signal.connect(self.on_worker_exit)
|
|
|
|
scan_thread.started.connect(scan_worker.work)
|
|
|
|
self.threads.append((scan_worker, scan_thread))
|
|
|
|
self.changeThreadLabel(threadNumber)
|
2019-01-20 16:59:47 +00:00
|
|
|
for thread in self.threads:
|
2019-02-03 09:13:10 +00:00
|
|
|
scan_worker, scan_thread = thread
|
|
|
|
scan_thread.start()
|
|
|
|
|
|
|
|
def changeThreadLabel(self,number_of_threads):
|
|
|
|
self.number_of_threads = number_of_threads
|
|
|
|
self.ui.currentThreadsLabel.setText(str(number_of_threads))
|
2019-01-20 16:59:47 +00:00
|
|
|
|
2019-02-03 09:13:10 +00:00
|
|
|
def on_worker_exit(self):
|
|
|
|
print("on_worker_exit called")
|
|
|
|
self.changeThreadLabel(self.number_of_threads - 1)
|
|
|
|
with self.exit_lock:
|
|
|
|
for num, thread in enumerate(self.threads):
|
|
|
|
scan_worker, scan_thread = thread
|
|
|
|
if not scan_worker.isRunning:
|
|
|
|
self.threads.pop(num)
|
|
|
|
break
|
|
|
|
if self.number_of_threads == 0:
|
2019-01-31 18:19:44 +00:00
|
|
|
self.on_end_scanning()
|
|
|
|
def on_end_scanning(self):
|
|
|
|
self.isScanEnabled = False
|
|
|
|
self.ui.startButton.setText("Start")
|
|
|
|
self.storage.save()
|
|
|
|
|
2019-01-20 16:59:47 +00:00
|
|
|
def stopScan(self):
|
2019-02-03 09:13:10 +00:00
|
|
|
while self.threads:
|
|
|
|
scan_worker, scan_thread = self.threads[0]
|
|
|
|
if scan_worker.isRunning:
|
|
|
|
scan_worker.stop()
|
2019-01-31 18:19:44 +00:00
|
|
|
|
2019-02-03 09:13:10 +00:00
|
|
|
def log_text(self, string):
|
2019-01-21 15:22:17 +00:00
|
|
|
self.ui.dataText.append("[" + str(datetime.datetime.now()) + "] " + str(string))
|
2019-01-20 16:59:47 +00:00
|
|
|
|
|
|
|
def setCurrentThreadsLabel(self, threadNumber):
|
|
|
|
self.ui.currentThreadsLabel.setText(str(threadNumber))
|
|
|
|
|
|
|
|
|
2019-02-03 09:13:10 +00:00
|
|
|
class ScanWorker(QObject):
|
|
|
|
|
2019-01-20 16:59:47 +00:00
|
|
|
|
2019-02-03 09:13:10 +00:00
|
|
|
log_signal = pyqtSignal(str)
|
2019-01-31 18:19:44 +00:00
|
|
|
exit_signal = pyqtSignal()
|
2019-01-20 16:59:47 +00:00
|
|
|
|
2019-02-03 09:13:10 +00:00
|
|
|
def __init__(self, ip_generator, scanner, storage, **kwargs):
|
|
|
|
super().__init__()
|
2019-01-27 20:28:58 +00:00
|
|
|
self.ip_generator = ip_generator
|
2019-02-03 09:13:10 +00:00
|
|
|
self.storage = storage
|
|
|
|
self.scanner = scanner
|
2019-01-28 18:57:07 +00:00
|
|
|
self.previous_address = None
|
2019-02-03 09:13:10 +00:00
|
|
|
self.isRunning = True
|
2019-01-20 16:59:47 +00:00
|
|
|
|
2019-02-03 09:13:10 +00:00
|
|
|
@pyqtSlot()
|
|
|
|
def work(self):
|
|
|
|
while self.isRunning:
|
2019-01-28 18:57:07 +00:00
|
|
|
scan_address = self.ip_generator.get_next_address(self.previous_address)
|
2019-01-27 20:28:58 +00:00
|
|
|
if not scan_address:
|
|
|
|
break
|
2019-01-28 18:57:07 +00:00
|
|
|
self.previous_address = scan_address
|
2019-02-03 09:13:10 +00:00
|
|
|
scan_result = self.scanner.scan_address(scan_address)
|
|
|
|
self.storage.put_responce(scan_address, scan_result)
|
2019-01-31 18:19:44 +00:00
|
|
|
if scan_result == 0:
|
2019-02-03 09:13:10 +00:00
|
|
|
self.log_signal.emit('%s has open port: %s' % scan_address)
|
2019-01-20 16:59:47 +00:00
|
|
|
else:
|
2019-02-03 09:13:10 +00:00
|
|
|
self.log_signal.emit('%s has closed port: %s' % scan_address)
|
|
|
|
def stop(self):
|
|
|
|
print("stop called")
|
|
|
|
self.isRunning = False
|
2019-01-31 18:19:44 +00:00
|
|
|
self.exit_signal.emit()
|