Skip to content

Commit b752134

Browse files
WillowSauceRWillowSauceR
authored andcommitted
Add arg "--local-port" for motd
1 parent 2e656d3 commit b752134

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

api.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,12 @@ def decode_unicode(string: str):
8585
return string.encode("utf-8").decode("unicode-escape")
8686

8787

88-
def get_udp_socket(port=random.randint(1024, 65535)):
88+
def get_udp_socket(port=random.randint(1024, 65535), timeout: int=1) -> socket.socket:
8989
if not port:
9090
port = random.randint(1024, 65535)
9191
sockets = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
92-
sockets.settimeout(1)
93-
if sys.platform.startswith('win32'):
94-
sockets.bind(("", port))
92+
sockets.settimeout(timeout)
93+
sockets.bind(("", port))
9594
return sockets
9695

9796

motd.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import argparse
2+
import socket
23
from api import get_udp_socket, log, parse_raw_pkt, MOTD_PKT
34

45

5-
def send_pkt(addr, port, timeout: float=3.0):
6-
udp_skt = get_udp_socket()
7-
udp_skt.settimeout(timeout)
6+
def send_pkt(addr, port, timeout: float=3.0, local_port: int=None):
7+
udp_skt = get_udp_socket(local_port, timeout)
88
udp_skt.sendto(
99
MOTD_PKT,
1010
(addr, port))
@@ -36,16 +36,19 @@ def recv_pkt(sk_send):
3636
help="target server port")
3737
parser.add_argument("-t", "--timeout", default=3.0, type=float,
3838
help="timeout")
39+
parser.add_argument("-lp", "--local-port", default=None, type=int,
40+
help="local port to send motd packet")
3941

4042
args = parser.parse_args()
4143

4244
addr = args.addr
4345
port = args.port
4446
timeout = args.timeout
47+
local_port = args.local_port
4548

4649
try:
47-
send_pkt(addr, port, timeout)
48-
except TimeoutError:
50+
send_pkt(addr, port, timeout, local_port)
51+
except (TimeoutError, socket.timeout):
4952
log(f"Timeout! Server may be offline or blocked motd request.")
5053
except ConnectionResetError:
5154
log(f"Connection Reset! Server may be offline or blocked motd request.")

0 commit comments

Comments
 (0)