Skip to content

Commit ad748ef

Browse files
committed
fix: simplified the extraports structs
1 parent e0b43f6 commit ad748ef

File tree

8 files changed

+53
-109
lines changed

8 files changed

+53
-109
lines changed

.pre-commit-config.yaml

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
exclude: ^(test/|.tox/|docs)
22
repos:
3-
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v2.3.0
5-
hooks:
6-
- id: check-yaml
7-
- id: end-of-file-fixer
8-
- id: trailing-whitespace
9-
- repo: https://github.com/psf/black
10-
rev: 24.3.0
11-
hooks:
12-
- id: black
13-
args: [--line-length=79]
14-
files: ^libnmap
15-
- repo: https://github.com/pre-commit/mirrors-isort
16-
rev: v5.6.4
17-
hooks:
18-
- id: isort
19-
args: [--multi-line=3, --line-length=79, --profile=black]
20-
- repo: https://gitlab.com/pycqa/flake8
21-
rev: 3.8.4
22-
hooks:
23-
- id: flake8
24-
exclude: ^libnmap/(test/|docs/|examples/)
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v2.3.0
5+
hooks:
6+
- id: check-yaml
7+
- id: end-of-file-fixer
8+
- id: trailing-whitespace
9+
- repo: https://github.com/psf/black
10+
rev: 24.3.0
11+
hooks:
12+
- id: black
13+
args: [--line-length=79]
14+
files: ^libnmap
15+
- repo: https://github.com/pre-commit/mirrors-isort
16+
rev: v5.6.4
17+
hooks:
18+
- id: isort
19+
args: [--multi-line=3, --line-length=79, --profile=black]
20+
- repo: https://gitlab.com/pycqa/flake8
21+
rev: 3.8.4
22+
hooks:
23+
- id: flake8
24+
exclude: ^libnmap/(test/|docs/|examples/)
25+
- repo: local
26+
hooks:
27+
- id: pytest-check
28+
name: pytest-check
29+
stages: [pre-commit]
30+
types: [python]
31+
entry: pytest --cov=libnmap/ --ignore=libnmap/test/test_backend_plugin_factory.py
32+
language: system
33+
pass_filenames: false
34+
always_run: true

libnmap/objects/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
from libnmap.objects.host import NmapHost
44
from libnmap.objects.report import NmapReport
5-
from libnmap.objects.service import NmapExtraPort, NmapService
5+
from libnmap.objects.service import NmapService
66

7-
__all__ = ["NmapReport", "NmapHost", "NmapService", "NmapExtraPort"]
7+
__all__ = ["NmapReport", "NmapHost", "NmapService"]

libnmap/objects/host.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,10 @@ def id(self):
465465
@property
466466
def extraports(self):
467467
"""
468-
Returns a list of NmapExtraPort objects
468+
Returns a list of extraport dict with
469+
with struct { count: "123", state: "filtered, extrareasons: [{}] }
469470
470-
:return: list of NmapExtraPort objects
471+
:return: list of extraport dict
471472
"""
472473
return self._extraports
473474

libnmap/objects/service.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -366,52 +366,3 @@ def diff(self, other):
366366
:return: NmapDiff object
367367
"""
368368
return NmapDiff(self, other)
369-
370-
371-
class NmapExtraPort(object):
372-
"""
373-
NmapExtraPort is an object which documents unlisted ports/services
374-
which are possibly closed, filtered, ignored,...
375-
"""
376-
377-
def __init__(self, xdict):
378-
"""
379-
Constructor
380-
:param xdict: python dict containing the following structure:
381-
{
382-
'state': <str>,
383-
'count': <int>,
384-
'reasons': [{'reason': <str>, 'count' <int>}]
385-
}
386-
"""
387-
self._count = xdict.get("count", 0)
388-
self._state = xdict.get("state", "unknown")
389-
self._reasons = xdict.get("reasons", [])
390-
391-
@property
392-
def extra_count(self):
393-
"""
394-
Accessor for the number of extraports
395-
396-
:return: int
397-
"""
398-
return int(self._count)
399-
400-
@property
401-
def extra_state(self):
402-
"""
403-
Accessor for the state of extraports listed
404-
405-
:return: string
406-
"""
407-
return self._state
408-
409-
@property
410-
def extra_reasons(self):
411-
"""
412-
Return the first reason available for the extraport listed.
413-
414-
:return: dict, empty if no extraports reason available
415-
"""
416-
417-
return self._reasons

libnmap/parser.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from xml.etree.ElementTree import iselement as et_iselement
1313

14-
from libnmap.objects import NmapExtraPort, NmapHost, NmapReport, NmapService
14+
from libnmap.objects import NmapHost, NmapReport, NmapService
1515

1616

1717
class NmapParser(object):
@@ -469,9 +469,9 @@ def __parse_extraports(cls, extraports_data):
469469
:param extraports_data: XML data for extraports
470470
:type extraports_data: xml.ElementTree.Element or a string
471471
472-
:return: python dict with following keys: state, count, reason
472+
:return: python dict with following keys: state, count, reasons
473473
"""
474-
rdict = {"state": "", "count": "", "reasons": []}
474+
rdict = {"state": "", "count": "", "extrareasons": []}
475475
xelement = cls.__format_element(extraports_data)
476476
extraports_dict = cls.__format_attributes(xelement)
477477

@@ -480,9 +480,8 @@ def __parse_extraports(cls, extraports_data):
480480
for xelt in xelement:
481481
if xelt.tag == "extrareasons":
482482
extrareasons_dict = cls.__format_attributes(xelt)
483-
rdict["reasons"].append(extrareasons_dict)
484-
robj = NmapExtraPort(rdict)
485-
return robj
483+
rdict["extrareasons"].append(extrareasons_dict)
484+
return rdict
486485

487486
@classmethod
488487
def __parse_script_table(cls, script_table):

libnmap/test/files/2_hosts.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

libnmap/test/test_extraports.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
class TestExtraPorts(unittest.TestCase):
1111
def setUp(self):
1212
fdir = os.path.dirname(os.path.realpath(__file__))
13-
_extrareason = [
13+
_extrareasons = [
1414
{"reason": "filtered", "count": "3"},
1515
{"reason": "resets", "count": "7"},
1616
]
1717
self.flist = [
1818
{
1919
"path": "%s/%s" % (fdir, "files/extra_ports.xml"),
20-
"extrareason": _extrareason,
20+
"extrareasons": _extrareasons,
2121
}
2222
]
2323

@@ -26,12 +26,14 @@ def test_extraports(self):
2626
rep1 = NmapParser.parse_fromfile(fentry["path"])
2727
ep_list = rep1.hosts[0].extraports
2828
self.assertEqual(len(ep_list), 2)
29-
self.assertEqual(ep_list[0].extra_count, 65509)
30-
self.assertEqual(ep_list[0].extra_state, "closed")
31-
self.assertEqual(len(ep_list[0].extra_reasons), 1)
32-
self.assertEqual(ep_list[1].extra_count, 10)
33-
self.assertEqual(len(ep_list[1].extra_reasons), 2)
34-
self.assertEqual(ep_list[1].extra_reasons, fentry["extrareason"])
29+
self.assertEqual(ep_list[0]["count"], "65509")
30+
self.assertEqual(ep_list[0]["state"], "closed")
31+
self.assertEqual(len(ep_list[0]["extrareasons"]), 1)
32+
self.assertEqual(ep_list[1]["count"], "10")
33+
self.assertEqual(len(ep_list[1]["extrareasons"]), 2)
34+
self.assertEqual(
35+
ep_list[1]["extrareasons"], fentry["extrareasons"]
36+
)
3537

3638

3739
if __name__ == "__main__":

libnmap/test/test_host.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,6 @@ def test_host_api(self):
181181
self.assertEqual(len(h2.get_open_ports()), 3)
182182
self.assertEqual(h2.get_service(22, "tcp").state, "open")
183183

184-
def test_extra_ports(self):
185-
h1 = NmapParser.parse(host1)
186-
h2 = NmapParser.parse(host2)
187-
188-
self.assertEqual(
189-
h1.extraports_state["state"],
190-
{"count": "995", "state": "WILLY_WONCKA"},
191-
)
192-
self.assertEqual(
193-
h1.extraports_reasons, [{"reason": "conn-refused", "count": "995"}]
194-
)
195-
196-
self.assertEqual(
197-
h2.extraports_state["state"], {"count": "995", "state": "closed"}
198-
)
199-
self.assertEqual(
200-
h2.extraports_reasons, [{"reason": "conn-refused", "count": "995"}]
201-
)
202-
203184
def test_diff_host(self):
204185
h1 = NmapParser.parse(host1)
205186
h2 = NmapParser.parse(host2)

0 commit comments

Comments
 (0)