Skip to content

Commit 7b1b72c

Browse files
committed
refactor: Improve code readability and structure
1 parent 4a46905 commit 7b1b72c

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

.pylintrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
[MASTER]
22
py-version = 3.8
3-
disable = invalid-name, line-too-long, too-many-locals, too-many-arguments, too-many-return-statements, too-many-positional-arguments
3+
disable =
4+
invalid-name,
5+
line-too-long,
6+
too-many-arguments,
7+
too-many-branches,
8+
too-many-locals,
9+
too-many-return-statements,
10+
too-many-statements

Jiyu_udp_attack/__main__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# pylint: disable=all
2-
31
"""
42
Jiyu Attack Script
53
@@ -46,6 +44,10 @@
4644

4745

4846
def main_parser():
47+
"""
48+
Main parser for the Jiyu attack script.
49+
This function sets up the command-line argument parser with various options for network configuration and attack actions
50+
"""
4951
parser = argparse.ArgumentParser(
5052
prog="Jiyu_udp_attack",
5153
description="Jiyu Attack Script\n\n"
@@ -320,7 +322,8 @@ def setting_parser():
320322
return parser
321323

322324

323-
if __name__ == "__main__":
325+
def main():
326+
"""Main function to parse arguments and execute the attack."""
324327
logger: List[Any] = []
325328

326329
parser = main_parser()
@@ -353,7 +356,9 @@ def setting_parser():
353356
program, args_list = args_list
354357
else:
355358
parser.error("Invalid execute arguments: expected [program] or [program, args_list]")
356-
payload = pkg_execute(program, args_list, "normal" if mode is None else mode)
359+
payload = pkg_execute(
360+
program, args_list, "normal" if mode is None else mode # pylint: disable=E0601, E0606
361+
)
357362
elif args.shutdown is not None:
358363
if len(args.shutdown) == 0:
359364
payload = pkg_shutdown()
@@ -387,7 +392,7 @@ def setting_parser():
387392
elif args.setting is not None:
388393
parser2 = setting_parser()
389394
setting_args = parser2.parse_args(shlex.split(args.setting))
390-
payload = pkg_setting(**dict(setting_args._get_kwargs()))
395+
payload = pkg_setting(**dict(setting_args._get_kwargs())) # pylint: disable=protected-access
391396
logger.append(setting_args) # Store parsed settings for debugging
392397
elif args.hex:
393398
payload = binascii.unhexlify(args.hex.replace(" ", ""))
@@ -408,9 +413,19 @@ def setting_parser():
408413
parser.error("Target IP address cannot be empty. Use -h for help.")
409414

410415
for target in targets:
411-
broadcast_packet(teacher_ip, teacher_port, target, port, payload, ip_id=args.ip_id)
416+
broadcast_packet(
417+
teacher_ip, teacher_port, target, port, payload, ip_id=args.ip_id # pylint: disable=E0601, E0606
418+
)
412419
logger.append(f"Sent packet with a length of {len(payload)} to {target}:{port}")
413420

414421
print(*logger, sep="\n\n")
415-
except Exception as e:
422+
except Exception as e: # pylint: disable=broad-except
416423
parser.error(f"({e.__class__.__name__}) {e}")
424+
425+
426+
if __name__ == "__main__":
427+
main()
428+
else:
429+
print("This script is intended to be run as a standalone program, not imported as a module.")
430+
print("Please run it directly using 'python Jiyu_udp_attack/__main__.py' or similar command.")
431+
raise ImportError("This script is not designed to be imported as a module.")

Jiyu_udp_attack/arg_display.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __call__(
5252
setattr(namespace, self.dest, (mode, values))
5353

5454
def format_usage(self) -> str:
55+
"""Format the usage string for this action."""
5556
return " | ".join(self.option_strings)
5657

5758

Jiyu_udp_attack/ip_analyze.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
This module provides functions to analyze IP addresses and ranges, converting them into a list of valid IP addresses.
33
"""
44

5-
65
from typing import List, Tuple
76

87

@@ -27,8 +26,7 @@ def ip_to_tuple(ip: str) -> Tuple[int, int, int, int]:
2726
return ip_tuple
2827

2928

30-
# pylint: disable=too-many-branches
31-
def ip_analyze(ip: str) -> List[str]:
29+
def ip_analyze(ip: str) -> List[str]: # pylint: disable=too-many-branches
3230
"""
3331
Analyzes an IP address or range and returns a list of valid IP addresses.
3432
@@ -60,10 +58,10 @@ def ip_analyze(ip: str) -> List[str]:
6058
if len(ip_range_tuple) != 4:
6159
raise ValueError(f"Invalid IP address range format: {ip}")
6260
ip_count = 1
63-
ip_range: list[tuple[int, int]] = []
61+
ip_range: List[Tuple[int, int]] = []
6462
for i in ip_range_tuple:
6563
rg = i.split("-")
66-
if len(rg) ==1:
64+
if len(rg) == 1:
6765
rg.append(rg[0])
6866
if len(rg) != 2:
6967
raise ValueError(f"Invalid IP address range format: {ip}")

0 commit comments

Comments
 (0)