2016-01-22 10:15:02 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-01-19 21:32:16 +00:00
|
|
|
import socket
|
|
|
|
import random
|
|
|
|
import sys
|
2016-01-20 23:24:37 +00:00
|
|
|
import traceback
|
|
|
|
import logging
|
2019-10-17 01:04:23 +00:00
|
|
|
import os
|
|
|
|
from optparse import OptionParser
|
2023-12-26 16:58:55 +00:00
|
|
|
from struct import pack
|
2016-01-20 23:24:37 +00:00
|
|
|
from time import time
|
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
from server_entry import ServerEntry
|
2016-01-20 23:24:37 +00:00
|
|
|
from protocol import MasterProtocol
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2023-12-26 16:58:55 +00:00
|
|
|
LOG_FILENAME = "pymaster.log"
|
2021-10-12 09:27:05 +00:00
|
|
|
MAX_SERVERS_FOR_IP = 14
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
|
2023-12-26 16:58:55 +00:00
|
|
|
class PyMaster:
|
|
|
|
def __init__(self, ip, port):
|
|
|
|
self.serverList = []
|
2023-12-26 17:01:26 +00:00
|
|
|
if ':' in ip:
|
|
|
|
self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
|
|
|
else:
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
2023-12-26 16:58:55 +00:00
|
|
|
self.sock.bind((ip, port))
|
|
|
|
|
|
|
|
logging.debug("Welcome to PyMaster!")
|
|
|
|
logging.debug("I ask you again, are you my master? @-@")
|
|
|
|
logging.debug("Running on %s:%d" % (ip, port))
|
|
|
|
|
|
|
|
|
|
|
|
def server_loop(self):
|
|
|
|
data, addr = self.sock.recvfrom(1024)
|
|
|
|
data = data.decode("latin_1")
|
|
|
|
|
|
|
|
match data[0]:
|
|
|
|
case MasterProtocol.clientQuery:
|
|
|
|
self.client_query(data, addr)
|
|
|
|
case MasterProtocol.challengeRequest:
|
|
|
|
self.send_challenge_to_server(data, addr)
|
|
|
|
case MasterProtocol.addServer:
|
|
|
|
self.add_server_to_list(data, addr)
|
|
|
|
case MasterProtocol.removeServer:
|
|
|
|
self.remove_server_from_list(data, addr)
|
|
|
|
case other:
|
|
|
|
logging.debug("Unknown message: {0} from {1}:{2}".format(data, addr[0], addr[1]))
|
|
|
|
|
|
|
|
|
|
|
|
def client_query(self, data, addr):
|
|
|
|
region = data[1] # UNUSED
|
|
|
|
data = data.strip("1" + region)
|
|
|
|
try:
|
|
|
|
query = data.split("\0")
|
|
|
|
except ValueError:
|
|
|
|
logging.debug(traceback.format_exc())
|
|
|
|
return
|
|
|
|
|
|
|
|
queryAddr = query[0] # UNUSED
|
|
|
|
rawFilter = query[1]
|
|
|
|
|
|
|
|
# Remove first \ character
|
|
|
|
rawFilter = rawFilter.strip("\\")
|
|
|
|
split = rawFilter.split("\\")
|
|
|
|
|
|
|
|
# Use NoneType as undefined
|
|
|
|
gamedir = "valve" # halflife, by default
|
|
|
|
clver = None
|
|
|
|
nat = 0
|
2023-12-26 17:07:15 +00:00
|
|
|
key = None
|
2023-12-26 16:58:55 +00:00
|
|
|
|
|
|
|
for i in range(0, len(split), 2):
|
|
|
|
try:
|
2023-12-26 17:07:15 +00:00
|
|
|
value = split[i + 1]
|
2023-12-26 16:58:55 +00:00
|
|
|
if split[i] == "gamedir":
|
2023-12-26 17:07:15 +00:00
|
|
|
gamedir = value.lower() # keep gamedir in lowercase
|
2023-12-26 16:58:55 +00:00
|
|
|
elif split[i] == "nat":
|
2023-12-26 17:07:15 +00:00
|
|
|
nat = int(value)
|
2023-12-26 16:58:55 +00:00
|
|
|
elif split[i] == "clver":
|
2023-12-26 17:07:15 +00:00
|
|
|
clver = value
|
|
|
|
elif split[i] == 'key':
|
|
|
|
key = int(value, 16)
|
2023-12-26 16:58:55 +00:00
|
|
|
else:
|
|
|
|
logging.debug(
|
|
|
|
"Unhandled info string entry: {0}/{1}. Infostring was: {2}".format(
|
2023-12-26 17:07:15 +00:00
|
|
|
split[i], value, split
|
2023-12-26 16:58:55 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if clver is None: # Probably an old vulnerable version
|
|
|
|
self.fake_info_for_old_versions(gamedir, addr)
|
|
|
|
return
|
|
|
|
|
|
|
|
packet = MasterProtocol.queryPacketHeader
|
2023-12-26 17:07:15 +00:00
|
|
|
|
|
|
|
if key != None: # Required in latest Xash3D version
|
|
|
|
packet += b'\x7F' + pack('<I', key) + b'\x00'
|
|
|
|
|
2023-12-26 16:58:55 +00:00
|
|
|
for i in self.serverList:
|
|
|
|
if time() > i.die:
|
2024-01-08 19:10:13 +00:00
|
|
|
logging.debug("Server removed by timeout")
|
2023-12-26 16:58:55 +00:00
|
|
|
self.serverList.remove(i)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not i.check:
|
2024-01-08 19:10:13 +00:00
|
|
|
logging.debug("Invalid request")
|
2023-12-26 16:58:55 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if nat != i.nat:
|
2024-01-08 19:10:13 +00:00
|
|
|
logging.debug("NAT {0} mismatch {1}".format(i.nat, nat))
|
2023-12-26 16:58:55 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if gamedir is not None and gamedir != i.gamedir:
|
2024-01-08 19:10:13 +00:00
|
|
|
logging.debug("Game dir {0} mismatch node settings: {1}".format(i.gamedir, gamedir))
|
2023-12-26 16:58:55 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if nat:
|
|
|
|
reply = "\xff\xff\xff\xffc {0}:{1}".format(addr[0], addr[1])
|
|
|
|
data = reply.encode("latin_1")
|
|
|
|
# Tell server to send info reply
|
|
|
|
self.sock.sendto(data, i.addr)
|
|
|
|
|
|
|
|
# Use pregenerated address string
|
|
|
|
packet += i.queryAddr
|
|
|
|
|
|
|
|
packet += b"\0\0\0\0\0\0" # Fill last IP:Port with \0
|
|
|
|
self.sock.sendto(packet, addr)
|
|
|
|
|
|
|
|
|
|
|
|
def _send_fake_info(sock, warnmsg, gamedir, addr):
|
|
|
|
baseReply = (
|
|
|
|
b"\xff\xff\xff\xffinfo\n\host\\"
|
|
|
|
+ warnmsg.encode("utf-8")
|
|
|
|
+ b"\map\\update\dm\\0\\team\\0\coop\\0\\numcl\\32\maxcl\\32\\gamedir\\"
|
|
|
|
+ gamedir.encode("latin-1")
|
|
|
|
+ b"\\"
|
|
|
|
)
|
|
|
|
sock.sendto(baseReply, addr)
|
|
|
|
|
|
|
|
|
|
|
|
def fake_info_for_old_versions(self, gamedir, addr):
|
|
|
|
error_message = [
|
|
|
|
"This version is not",
|
|
|
|
"supported anymore",
|
|
|
|
"Please update Xash3DFWGS",
|
|
|
|
"From GooglePlay or GitHub",
|
|
|
|
"Эта версия",
|
|
|
|
"устарела",
|
|
|
|
"Обновите Xash3DFWGS c",
|
|
|
|
"GooglePlay или GitHub",
|
|
|
|
]
|
|
|
|
|
|
|
|
for string in error_message:
|
|
|
|
_send_fake_info(self.sock, string, gamedir, addr)
|
|
|
|
|
|
|
|
|
2023-12-26 17:02:26 +00:00
|
|
|
def remove_server_from_list(self, data, addr):
|
2023-12-26 16:58:55 +00:00
|
|
|
for server in self.serverList:
|
|
|
|
if server.addr == addr:
|
|
|
|
logging.debug("Remove Server: from {0}:{1}".format(addr[0], addr[1]))
|
|
|
|
self.serverList.remove(server)
|
|
|
|
|
|
|
|
|
2023-12-26 17:02:26 +00:00
|
|
|
def send_challenge_to_server(self, data, addr):
|
2023-12-26 16:58:55 +00:00
|
|
|
logging.debug("Challenge Request: from {0}:{1}".format(addr[0], addr[1]))
|
|
|
|
# At first, remove old server- data from list
|
|
|
|
# self.removeServerFromList(None, addr)
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
for i in self.serverList:
|
|
|
|
if i.addr[0] == addr[0]:
|
|
|
|
if i.addr[1] == addr[1]:
|
|
|
|
self.serverList.remove(i)
|
|
|
|
else:
|
|
|
|
count += 1
|
|
|
|
if count > MAX_SERVERS_FOR_IP:
|
2024-01-08 19:10:13 +00:00
|
|
|
logging.debug("Reached MAX_SERVERS_FOR_IP: {0}".format(MAX_SERVERS_FOR_IP))
|
2023-12-26 16:58:55 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
challenge = random.randint(0, 2**32 - 1)
|
|
|
|
|
|
|
|
# Add server to list
|
2024-01-08 19:10:13 +00:00
|
|
|
logging.debug("Added new server {0}:{1} with challenge {2}".format(addr[0], addr[1], challenge))
|
2023-12-26 16:58:55 +00:00
|
|
|
self.serverList.append(ServerEntry(addr, challenge))
|
|
|
|
|
|
|
|
# And send him a challenge
|
|
|
|
packet = MasterProtocol.challengePacketHeader
|
|
|
|
packet += pack("I", challenge)
|
|
|
|
self.sock.sendto(packet, addr)
|
|
|
|
|
|
|
|
|
|
|
|
def add_server_to_list(self, data, addr):
|
|
|
|
logging.debug("Add Server: from {0}:{1}".format(addr[0], addr[1]))
|
|
|
|
# Remove the header. Just for better parsing.
|
|
|
|
serverInfo = data.strip("\x30\x0a\x5c")
|
2022-12-07 20:18:59 +00:00
|
|
|
|
2023-12-26 16:58:55 +00:00
|
|
|
# Find a server with same address
|
|
|
|
for serverEntry in self.serverList:
|
|
|
|
if serverEntry.addr == addr:
|
2024-01-08 19:10:13 +00:00
|
|
|
logging.debug("Skipped same server address: {0}:{1}".format(addr[0], addr[1]))
|
2023-12-26 16:58:55 +00:00
|
|
|
break
|
2022-12-07 20:18:59 +00:00
|
|
|
|
2023-12-26 16:58:55 +00:00
|
|
|
serverEntry.setInfoString(serverInfo)
|
2022-12-07 20:18:59 +00:00
|
|
|
|
|
|
|
|
2023-12-26 16:58:55 +00:00
|
|
|
def spawn_pymaster(verbose, ip, port):
|
|
|
|
if verbose:
|
|
|
|
logging.getLogger().addHandler(logging.StreamHandler())
|
|
|
|
logging.getLogger().addHandler(logging.FileHandler(LOG_FILENAME))
|
|
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2023-12-26 16:58:55 +00:00
|
|
|
masterMain = PyMaster(ip, port)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
masterMain.server_loop()
|
|
|
|
except Exception:
|
|
|
|
logging.debug(traceback.format_exc())
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-12-26 16:58:55 +00:00
|
|
|
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option(
|
|
|
|
"-i",
|
|
|
|
"--ip",
|
|
|
|
action="store",
|
|
|
|
dest="ip",
|
|
|
|
default="0.0.0.0",
|
|
|
|
help="ip to listen [default: %default]",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-p",
|
|
|
|
"--port",
|
|
|
|
action="store",
|
|
|
|
dest="port",
|
|
|
|
type="int",
|
|
|
|
default=27010,
|
|
|
|
help="port to listen [default: %default]",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-d",
|
|
|
|
"--daemonize",
|
|
|
|
action="store_true",
|
|
|
|
dest="daemonize",
|
|
|
|
default=False,
|
|
|
|
help="run in background, argument is uid [default: %default]",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-q",
|
|
|
|
"--quiet",
|
|
|
|
action="store_false",
|
|
|
|
dest="verbose",
|
|
|
|
default=True,
|
|
|
|
help="don't print to stdout [default: %default]",
|
|
|
|
)
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.daemonize != 0:
|
|
|
|
from daemon import DaemonContext
|
|
|
|
|
|
|
|
with DaemonContext(
|
|
|
|
stdout=sys.stdout, stderr=sys.stderr, working_directory=os.getcwd()
|
|
|
|
) as context:
|
|
|
|
spawn_pymaster(options.verbose, options.ip, options.port)
|
|
|
|
else:
|
|
|
|
sys.exit(spawn_pymaster(options.verbose, options.ip, options.port))
|