forked from BeaconMCDev/BeaconMC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
74 lines (57 loc) · 1.67 KB
/
test2.py
File metadata and controls
74 lines (57 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import socket
import struct
import json
def unpack_varint(s):
d = 0
for i in range(5):
b = ord(s.recv(1))
d |= (b & 0x7F) << 7*i
if not b & 0x80:
break
return d
def pack_varint(d: int):
o = b""
while True:
b = d & 0x7F
d >>= 7
o += struct.pack("B", b | (0x80 if d > 0 else 0))
if d == 0:
break
return o
def pack_data(d):
h = pack_varint(len(d))
if type(d) == str:
d = bytes(d, "utf-8")
return h + d
def pack_port(i):
return struct.pack('>H', i)
def get_info(host='localhost', port=25565):
# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
# Send handshake + status request
s.send(pack_data(b"\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + b"\x01"))
s.send(pack_data("\x00"))
# Read response
print(f"Packet lenth : {unpack_varint(s)}") # Packet length
print(f"Packet ID : {unpack_varint(s)}") # Packet ID
l = unpack_varint(s) # String length
print(f"String lenth : {l}")
d = b""
while len(d) < l:
d += s.recv(1024)
# Close our socket
s.close()
# Load json and return
print(d)
return json.loads(d.decode('utf-8'))
def get_info2(host='localhost', port=25565):
# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
# Send handshake + status request
s.send(pack_data(b"\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + b"\x01"))
s.send(pack_data("\x00"))
a = s.recv(10000)
return f"{len(a)} --> {a}"
print(get_info(host="localhost", port=25565))