-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·98 lines (76 loc) · 2.79 KB
/
main.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
from sys import argv
# Currently the only possible value when using the impostor custom server
SERVER_SUFFIX = "-Master-1"
def gen_stream(region_name: str, ip_address: str = "127.0.0.1", port: int = 22023) -> bytearray:
"""
Generates the bytearray with the specified values.
Port should currently always be 22023 for it to work
"""
server_name = region_name + SERVER_SUFFIX
# Make sure all variables are within the correct length range
if len(server_name) > 0xff:
raise ValueError("Region name too long")
if len(ip_address) > 0xff:
raise ValueError("IP-address too long")
if port > 0x7fff:
raise ValueError("Port too high")
# Append region name
data = bytearray(len(region_name).to_bytes(5, "big"))
data += region_name.encode("ascii")
# Append ip address string
data.append(len(ip_address))
data += ip_address.encode("ascii")
data.extend(0x1.to_bytes(4, "little"))
# Append server name
data.append(len(server_name))
data += server_name.encode("ascii")
# Append ip address in byte form
ip_address_bytes = bytearray()
for value in ip_address.split("."):
ip_address_bytes.append(int(value))
data += ip_address_bytes
# Append port
data.extend(port.to_bytes(2, "little"))
data.extend(0x0.to_bytes(4, "big"))
return data
def write_file(region_name: str, ip_address: str = "127.0.0.1", file_name: str = "regionInfo.dat", port: int = 22023, *, log_bytes=False):
"""
Creates and writes the among us regionInfo.dat file.
Port should currently always be 22023 for it to work
Example:
```py
try:
write_file("Example server", "127.0.0.1", port=12345))
print("Success!")
except Exception as error:
print(error)
```
"""
# Generate the file and save to the specified file name
data = gen_stream(region_name, ip_address, port)
with open(file_name, "wb") as file:
file.write(data)
# Log the bytes to the console if specified
if log_bytes:
print("Writing", content_from_stream(data), "to", file_name)
def content_from_stream(stream: bytearray) -> str:
"""
Create a string with the bytes in 'stream'
"""
hex_array = []
for byte in stream:
hex_array.append(hex(byte))
return f"{{{', '.join(hex_array)}}}"
if __name__ == "__main__":
if len(argv) < 2:
print('Usage: python main.py <name> [ip-address] [file-name] [port]')
print('Example: python main.py "Server name" 127.0.0.1')
# The standard exit code for usage errors
exit(64)
print(f"Creating file using arguments '{', '.join(argv[1:])}'")
try:
write_file(*argv[1:], log_bytes=True)
except Exception as error:
print("Error:", error)
exit(1)