Skip to content

Commit 7f1ca10

Browse files
committed
refactor(scanner): reorganize files and merge logger into multithreading
1 parent 721ced5 commit 7f1ca10

File tree

5 files changed

+56
-78
lines changed

5 files changed

+56
-78
lines changed

bugscanx/modules/scanners/port_scanner.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import socket
22
from concurrent.futures import ThreadPoolExecutor, as_completed
3-
43
from rich.console import Console
54
from rich.progress import (
65
BarColumn,
76
Progress,
87
TextColumn,
98
TimeRemainingColumn,
109
)
11-
1210
from bugscanx.utils.prompts import get_input
1311

1412
console = Console()

bugscanx/modules/scanners/scanners/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import threading
33
from datetime import datetime
44

5-
from .concurrency.multithread import MultiThread
5+
from .multithread import MultiThread
66

77

88
class BaseScanner(MultiThread):

bugscanx/modules/scanners/scanners/concurrency/logger.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

bugscanx/modules/scanners/scanners/direct.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
class DirectScannerBase(BaseScanner):
1212
requests = requests
1313
DEFAULT_TIMEOUT = 3
14-
DEFAULT_RETRY = 1
1514

1615
def __init__(
1716
self,
@@ -30,25 +29,16 @@ def __init__(
3029
def request(self, method, url, **kwargs):
3130
method = method.upper()
3231
kwargs['timeout'] = self.timeout
33-
max_attempts = self.DEFAULT_RETRY
34-
35-
for attempt in range(max_attempts):
36-
self.progress(method, url)
37-
try:
38-
return self.requests.request(method, url, **kwargs)
39-
except (
40-
requests.exceptions.ConnectionError,
41-
requests.exceptions.ReadTimeout,
42-
requests.exceptions.Timeout,
43-
requests.exceptions.RequestException
44-
) as e:
45-
wait_time = (1 if isinstance(e, requests.exceptions.ConnectionError)
46-
else 3)
47-
for _ in self.sleep(wait_time):
48-
self.progress(method, url)
49-
if attempt == max_attempts - 1:
50-
return None
51-
return None
32+
self.progress(method, url)
33+
try:
34+
return self.requests.request(method, url, **kwargs)
35+
except (
36+
requests.exceptions.ConnectionError,
37+
requests.exceptions.ReadTimeout,
38+
requests.exceptions.Timeout,
39+
requests.exceptions.RequestException
40+
):
41+
return None
5242

5343
def task(self, payload):
5444
method = payload['method']

bugscanx/modules/scanners/scanners/concurrency/multithread.py renamed to bugscanx/modules/scanners/scanners/multithread.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
1-
import time
1+
import os
2+
import sys
23
from abc import ABC, abstractmethod
34
from queue import Queue, Empty
45
from threading import Thread, RLock
56

6-
from .logger import Logger, CursorManager
7+
8+
class Logger:
9+
COLORS = {
10+
'ORANGE': '\033[33m',
11+
'MAGENTA': '\033[35m',
12+
'CYAN': '\033[36m',
13+
'LGRAY': '\033[37m',
14+
'GRAY': '\033[90m',
15+
'RED': '\033[91m',
16+
'GREEN': '\033[92m',
17+
'YELLOW': '\033[93m',
18+
'BLUE': '\033[94m',
19+
}
20+
RESET = '\033[0m'
21+
CLEAR_LINE = '\033[2K'
22+
23+
def __init__(self):
24+
self._lock = RLock()
25+
26+
@classmethod
27+
def colorize(cls, text, color):
28+
return f"{cls.COLORS.get(color, '')}{text}{cls.RESET}"
29+
30+
def replace(self, message):
31+
cols = os.get_terminal_size().columns
32+
msg = f"{message[:cols - 3]}..." if len(message) > cols else message
33+
with self._lock:
34+
sys.stdout.write(f'{self.CLEAR_LINE}{msg}{self.RESET}\r')
35+
sys.stdout.flush()
36+
37+
def log(self, message):
38+
with self._lock:
39+
sys.stderr.write(f'\r{self.CLEAR_LINE}{message}{self.RESET}\n')
40+
sys.stderr.flush()
41+
42+
43+
class CursorManager:
44+
def __enter__(self):
45+
print('\033[?25l', end='', flush=True)
46+
return self
47+
48+
def __exit__(self, exc_type, exc_val, exc_tb):
49+
print('\033[?25h', end='', flush=True)
750

851

952
class MultiThread(ABC):
@@ -76,12 +119,6 @@ def progress(self, *extra):
76119
] + [str(x) for x in extra if x]
77120
self.logger.replace(" - ".join(parts))
78121

79-
def sleep(self, seconds):
80-
while seconds > 0:
81-
yield seconds
82-
time.sleep(1)
83-
seconds -= 1
84-
85122
@abstractmethod
86123
def generate_tasks(self): pass
87124

0 commit comments

Comments
 (0)