aboutsummaryrefslogtreecommitdiff
path: root/lib/results.py
blob: 1565c4b106cf740232042b6d9d11bb8b16a1585c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from threading import Lock
from lib.util import Colored
from lib.scanner import PortState
from ch1p import telegram_notify


class Results:
    def __init__(self):
        self.warnings = []
        self.mutex = Lock()

    def add(self, worker):
        host = worker.get_host()
        with self.mutex:
            if not worker.done:
                print(f'{Colored.RED}{worker.name}: scanning failed{Colored.END}')
                return

            if worker.name != host:
                print(f'{worker.name} ({host}):')
            else:
                print(f'{host}:')

            opened = []
            results = worker.get_results()
            for port, state in results:
                if state != PortState.OPEN:
                    continue

                opened.append(port)
                if not worker.is_expected(port):
                    self.warnings.append(f'<b>{worker.name}</b> ({host}): port {port} is open')
                    print(f'    {Colored.RED}{port} opened{Colored.END}')
                else:
                    print(f'    {Colored.GREEN}{port} opened{Colored.END}')

            if worker.opened:
                for port in worker.opened:
                    if port not in opened:
                        self.warnings.append(
                            f'<b>{worker.name}</b> ({host}): port {port} is NOT open')
                        print(f'    {Colored.RED}{port} not opened{Colored.END}')
            print()

    def has_warnings(self):
        return len(self.warnings) > 0

    def notify(self, chat_id=None, token=None):
        text = '<b>❗️Attention!</b>\n\n'
        text += '\n'.join(self.warnings)

        telegram_notify(text, parse_mode='html', chat_id=chat_id, token=token)