-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
83 lines (64 loc) · 2.02 KB
/
util.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
'''
Contains information about a port to scan.
'''
class Port:
def __init__(self, port, proto='tcp', trace=False):
self.port = port
self.proto = proto
self.trace = trace
def __repr__(self):
return self.__str__()
def __str__(self):
return f'Port({self.port}, {self.proto}, {self.trace})'
def toJson(self):
return json.dumps({'port': self.port, 'proto': self.proto, 'trace': self.trace})
'''
Contains information about the results of a single port.
'''
class PortResults:
def __init__(self, host, port, status, raw):
self.host = host
self.port = port
self.status = status
self.raw = raw
def __repr__(self):
return self.__str__()
def __str__(self):
return f'PortResults({self.port}, {self.status})'
def toJson(self):
return {'port': self.port.toJson(), 'status': self.status, 'raw': self.raw}
def toJsonStr(self):
return json.dumps(self.toJson())
'''
Contains information about the results of a single host.
'''
class HostResults:
def __init__(self, host, portResults=None):
self.host = host
self.portResults = [] if portResults is None else portResults
def __repr__(self):
return self.__str__()
def __str__(self):
return f'HostResults({self.host}, {self.portResults})'
def toJson(self):
return {'host': self.host, 'portResults': list(map(lambda r: r.toJson(), self.portResults))}
def toJsonStr(self):
return json.dumps(self.toJson())
'''
Contains all results.
'''
class AllResults:
def __init__(self, hostResults=None):
self.hostResults = {} if hostResults is None else hostResults
def __getitem__(self, key):
return self.hostResults[key]
def __repr__(self):
return self.__str__()
def __str__(self):
return f'AllResults({len(self.hostResults)})'
def addPortResult(self, portResult):
if portResult.host in self.hostResults:
self.hostResults[portResult.host].portResults.append(portResult)
else:
self.hostResults[portResult.host] = HostResults(portResult.host, [portResult])