Skip to content

Commit

Permalink
refactor: feat: fully fletged Cartesian product of digits
Browse files Browse the repository at this point in the history
  • Loading branch information
storenth committed Jan 24, 2024
1 parent fff237e commit b9e2d04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/pnk/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.1"
__version__ = "1.0.0"
2 changes: 1 addition & 1 deletion src/pnk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setup_argparse():
# optional argument
group = parser.add_mutually_exclusive_group()
group.add_argument('-i', '--increment', action='store_true', help='increment any one or two digits on subdomains')
group.add_argument('-c', '--cartesian', action='store_true', help='increment digits on subdomains creating Cartesian product')
group.add_argument('-c', '--cartesian', action='store_true', help='increment digits on subdomains creating their Cartesian product')
# positional argument
parser.add_argument(
'file',
Expand Down
41 changes: 39 additions & 2 deletions src/pnk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import itertools
import pathlib
import re
from collections import deque
from urllib.parse import urlparse

from pnk.helpers import logger

log = logger.get_logger()
Expand Down Expand Up @@ -82,6 +84,14 @@ def incrmt(self, subdomain):
log.debug("No digits found!")
yield subdomain

def join_product_tuples(self, tuples):
_len = len(tuples)
log.debug(_len)
log.debug(tuples)
if _len == 1:
return "".join(*tuples)
return "".join(tuples[0]) + "." + self.join_product_tuples(tuples[1:])

def run(self):
"""Compose functions on files with hostname lines"""
for lines in self.file:
Expand All @@ -100,8 +110,35 @@ def run(self):
print(".".join(_s) + "." + d)
_s[index] = j
if self.args.cartesian:
for p in itertools.product(*map(self.incrmt, s)):
print(".".join(p) + "." + d)
_deque = deque()
subs_list = []
pattern = re.compile('((?<!\d)\d{1,2}(?!\d))')
for index, _s in enumerate(s):
log.debug(_s)
_list = pattern.split(_s)
if len(_list) == 1:
log.debug(_list)
_deque.append(_list)
else:
for x in _list:
try:
if type(int(x)) == int:
_deque.append([str(i).zfill(len(x)) for i in range(10 if len(x) < 2 else 100)])
except ValueError:
# filter out empty str
if x != "":
_deque.append([x])

subs_list.append(itertools.product(*_deque))
log.debug(_deque)
log.debug(subs_list)
_deque.clear()

for p in itertools.product(*subs_list):
log.debug(p)
log.debug(self.join_product_tuples(p))
print(self.join_product_tuples(p) + "." + d)

for p in self.pnk(s):
print(".".join(p) + "." + d)

Expand Down

0 comments on commit b9e2d04

Please sign in to comment.