Skip to content

Commit 2aa1c32

Browse files
committed
refactor(host_scanner): enhance CIDR input handling and add file reading support
refactor(validators): improve CIDR validation logic for better error handling
1 parent 823b876 commit 2aa1c32

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

bugscanx/modules/scanners/host_scanner.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
from bugscanx.utils.common import get_input, get_confirm, is_cidr
4+
from bugscanx.utils.cidr import read_cidrs_from_file
45

56

67
def get_cidr_ranges_from_input(cidr_input):
@@ -30,7 +31,11 @@ def get_common_inputs(input_source):
3031
def get_host_input():
3132
filename = get_input("Enter filename", "file", mandatory=False)
3233
if not filename:
33-
cidr = get_input("Enter CIDR range(s)", validators=[is_cidr])
34+
cidr = get_input("Enter CIDR range(s)", validators=[is_cidr], mandatory=False)
35+
if not cidr:
36+
cidr_file = get_input(
37+
"Enter CIDR file", "file")
38+
cidr = read_cidrs_from_file(cidr_file) if cidr_file else None
3439
return None, cidr
3540
return filename, None
3641

@@ -54,7 +59,10 @@ def get_input_direct(no302=False):
5459
)
5560

5661
if cidr:
57-
cidr_ranges = get_cidr_ranges_from_input(cidr)
62+
try:
63+
cidr_ranges = get_cidr_ranges_from_input(cidr)
64+
except AttributeError:
65+
cidr_ranges = cidr
5866
from .scanners.direct import CIDRDirectScanner
5967
scanner = CIDRDirectScanner(
6068
method_list=method_list,
@@ -93,7 +101,10 @@ def get_input_proxy():
93101
output, threads = get_common_inputs(filename or cidr)
94102

95103
if cidr:
96-
cidr_ranges = get_cidr_ranges_from_input(cidr)
104+
try:
105+
cidr_ranges = get_cidr_ranges_from_input(cidr)
106+
except AttributeError:
107+
cidr_ranges = cidr
97108
from .scanners.proxy_check import CIDRProxyScanner
98109
scanner = CIDRProxyScanner(
99110
cidr_ranges=cidr_ranges,
@@ -144,7 +155,10 @@ def get_input_proxy2():
144155
proxy_password = get_input("Enter proxy password")
145156

146157
if cidr:
147-
cidr_ranges = get_cidr_ranges_from_input(cidr)
158+
try:
159+
cidr_ranges = get_cidr_ranges_from_input(cidr)
160+
except AttributeError:
161+
cidr_ranges = cidr
148162
from .scanners.proxy_request import CIDRProxy2Scanner
149163
scanner = CIDRProxy2Scanner(
150164
method_list=method_list,
@@ -172,7 +186,10 @@ def get_input_ssl():
172186
output, threads = get_common_inputs(filename or cidr)
173187

174188
if cidr:
175-
cidr_ranges = get_cidr_ranges_from_input(cidr)
189+
try:
190+
cidr_ranges = get_cidr_ranges_from_input(cidr)
191+
except AttributeError:
192+
cidr_ranges = cidr
176193
from .scanners.ssl import CIDRSSLScanner
177194
scanner = CIDRSSLScanner(
178195
cidr_ranges=cidr_ranges,
@@ -197,7 +214,10 @@ def get_input_ping():
197214
output, threads = get_common_inputs(filename or cidr)
198215

199216
if cidr:
200-
cidr_ranges = get_cidr_ranges_from_input(cidr)
217+
try:
218+
cidr_ranges = get_cidr_ranges_from_input(cidr)
219+
except AttributeError:
220+
cidr_ranges = cidr
201221
from .scanners.ping import CIDRPingScanner
202222
scanner = CIDRPingScanner(
203223
port_list=port_list,

bugscanx/utils/cidr.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22
from rich import print
33

44

5-
def get_hosts_from_cidr(cidr_input):
6-
hosts = []
7-
cidr_ranges = [c.strip() for c in cidr_input.split(',')]
8-
9-
for cidr in cidr_ranges:
10-
try:
11-
network = ipaddress.ip_network(cidr, strict=False)
12-
hosts.extend([str(ip) for ip in network.hosts()])
13-
except ValueError:
14-
continue
15-
return hosts
16-
17-
185
def read_cidrs_from_file(filepath):
196
valid_cidrs = []
207
try:
@@ -29,9 +16,6 @@ def read_cidrs_from_file(filepath):
2916
except ValueError:
3017
pass
3118

32-
if valid_cidrs:
33-
print(f"[green] Successfully loaded {len(valid_cidrs)} valid CIDR ranges[/green]")
34-
3519
return valid_cidrs
3620
except Exception as e:
3721
print(f"[bold red]Error reading file: {e}[/bold red]")

bugscanx/utils/validators.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ def is_cidr(text):
4545
if not text.strip():
4646
return "CIDR input cannot be empty"
4747

48-
try:
49-
ipaddress.ip_network(text.strip(), strict=False)
50-
return True
51-
except ValueError:
52-
return f"Invalid CIDR notation: {text.strip()}"
48+
cidrs = [cidr.strip() for cidr in text.split(',')]
49+
50+
for cidr in cidrs:
51+
if not cidr:
52+
continue
53+
try:
54+
ipaddress.ip_network(cidr, strict=False)
55+
except ValueError:
56+
return f"Invalid CIDR notation: {cidr}"
57+
58+
return True
5359

5460

5561
def is_digit(text, allow_comma=True):

0 commit comments

Comments
 (0)