1
0
Fork 0
mirror of https://github.com/de2tla2f/pymaster.git synced 2024-11-27 13:21:28 +00:00

Compare commits

..

5 commits

Author SHA1 Message Date
ghost 42923b1795 debug timeout event 2024-01-09 04:39:46 +02:00
ghost 6f87258799 remove extra debug event 2024-01-09 04:35:51 +02:00
ghost 65e8183817 add servers cleanup by timeout 2024-01-09 04:10:47 +02:00
ghost 2b6872e04f update yggdrasil address 2024-01-09 03:30:27 +02:00
ghost 886102c008 reduce codebase, remove deprecated constructions 2024-01-09 03:27:45 +02:00
4 changed files with 26 additions and 161 deletions

View file

@ -20,4 +20,4 @@ Master server written in Python. Replacement for HLMaster
### Yggdrasil ### Yggdrasil
* `[201:23b4:991a:634d:8359:4521:5576:15b7]:27010` * `[201:5eb5:f061:678e:7565:6338:c02c:5251]:27010`

View file

@ -5,8 +5,6 @@ class MasterProtocol:
# Server To Master # Server To Master
challengeRequest = 'q' challengeRequest = 'q'
addServer = '0'
removeServer = 'b'
# Master To Client # Master To Client
queryPacketHeader = b'\xff\xff\xff\xff\x66\x0a' queryPacketHeader = b'\xff\xff\xff\xff\x66\x0a'

View file

@ -39,10 +39,6 @@ class PyMaster:
self.client_query(data, addr) self.client_query(data, addr)
case MasterProtocol.challengeRequest: case MasterProtocol.challengeRequest:
self.send_challenge_to_server(data, addr) 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: case other:
logging.debug("Unknown message: {0} from {1}:{2}".format(data, addr[0], addr[1])) logging.debug("Unknown message: {0} from {1}:{2}".format(data, addr[0], addr[1]))
@ -89,85 +85,31 @@ class PyMaster:
except IndexError: except IndexError:
pass pass
if clver is None: # Probably an old vulnerable version
self.fake_info_for_old_versions(gamedir, addr)
return
packet = MasterProtocol.queryPacketHeader packet = MasterProtocol.queryPacketHeader
if key != None: # Required in latest Xash3D version if key != None: # Required in latest Xash3D version
packet += b'\x7F' + pack('<I', key) + b'\x00' packet += b'\x7F' + pack('<I', key) + b'\x00'
for i in self.serverList: for i in self.serverList:
# Cleanup servers by timeout
if time() > i.die: if time() > i.die:
logging.debug("Server removed by timeout")
self.serverList.remove(i) self.serverList.remove(i)
logging.debug("Server removed by timeout: {0}:{1}".format(addr[0], addr[1]))
continue continue
if not i.check:
logging.debug("Invalid request")
continue
if nat != i.nat:
logging.debug("NAT {0} mismatch {1}".format(i.nat, nat))
continue
if gamedir is not None and gamedir != i.gamedir:
logging.debug("Game dir {0} mismatch node settings: {1}".format(i.gamedir, gamedir))
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 # Use pregenerated address string
packet += i.queryAddr packet += i.queryAddr
packet += b"\0\0\0\0\0\0" # Fill last IP:Port with \0 packet += b"\0\0\0\0\0\0" # Fill last IP:Port with \0
self.sock.sendto(packet, addr) 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)
def remove_server_from_list(self, data, addr):
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)
def send_challenge_to_server(self, data, addr): def send_challenge_to_server(self, data, addr):
logging.debug("Challenge Request: from {0}:{1}".format(addr[0], addr[1]))
# At first, remove old server- data from list
# self.removeServerFromList(None, addr)
logging.debug("Challenge request: {0}:{1}".format(addr[0], addr[1]))
# At first, remove old server- data from list
count = 0 count = 0
for i in self.serverList: for i in self.serverList:
if i.addr[0] == addr[0]: if i.addr[0] == addr[0]:
@ -182,7 +124,6 @@ class PyMaster:
challenge = random.randint(0, 2**32 - 1) challenge = random.randint(0, 2**32 - 1)
# Add server to list # Add server to list
logging.debug("Added new server {0}:{1} with challenge {2}".format(addr[0], addr[1], challenge))
self.serverList.append(ServerEntry(addr, challenge)) self.serverList.append(ServerEntry(addr, challenge))
# And send him a challenge # And send him a challenge
@ -190,21 +131,6 @@ class PyMaster:
packet += pack("I", challenge) packet += pack("I", challenge)
self.sock.sendto(packet, addr) 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")
# Find a server with same address
for serverEntry in self.serverList:
if serverEntry.addr == addr:
logging.debug("Skipped same server address: {0}:{1}".format(addr[0], addr[1]))
break
serverEntry.setInfoString(serverInfo)
def spawn_pymaster(verbose, ip, port): def spawn_pymaster(verbose, ip, port):
if verbose: if verbose:
logging.getLogger().addHandler(logging.StreamHandler()) logging.getLogger().addHandler(logging.StreamHandler())

View file

@ -4,85 +4,26 @@ from struct import pack
import ipaddress import ipaddress
class ServerEntry: class ServerEntry:
challenge2 = 0
gamedir = 'valve'
protocol = 0
players = 0
maxplayers = 0
bots = 0
gamemap = ''
version = '0'
servtype = 'd'
password = 0
os = 'l'
secure = 0
lan = 0
region = 255
product = ''
nat = 0
key = None
def setInfoString(self, data): def __init__(self, addr, challenge):
infostring = data.replace('\n', '').replace('\r', '').replace('\0', '')
split = infostring.split('\\')
for i in range(0, len(split), 2):
try:
value = split[i + 1]
if( split[i] == 'challenge' ):
self.challenge2 = int(value)
elif( split[i] == 'gamedir' ):
self.gamedir = value.lower() # keep gamedir lowercase
elif( split[i] == 'protocol' ):
self.protocol = int(value)
elif( split[i] == 'players' ):
self.players = int(value)
elif( split[i] == 'max' ):
self.maxplayers = int(value.split('.')[0])
elif( split[i] == 'bots' ):
self.bots = int(value)
elif( split[i] == 'map' ):
self.gamemap = value
elif( split[i] == 'version' ):
self.version = value
elif( split[i] == 'type' ):
self.servtype = value
elif( split[i] == 'password' ):
self.password = value
elif( split[i] == 'os' ):
self.os = value
elif( split[i] == 'secure' ):
self.secure = value
elif( split[i] == 'lan' ):
self.lan = value
elif( split[i] == 'region' ):
self.region = value
elif( split[i] == 'product' ):
self.product = value
elif( split[i] == 'nat' ):
self.nat = int(value)
elif split[i] == 'key':
self.key = int(value, 16)
except IndexError:
pass
self.check = self.challenge == self.challenge2
return self.check
def __init__(self, addr, challenge): # Address
# Address self.addr = addr
self.addr = addr # Shortcuts for generating query
# Shortcuts for generating query self.queryAddr = b''
self.queryAddr = b''
self.queryAddr += ipaddress.ip_address(addr[0]).packed
self.queryAddr += pack('!H', int(addr[1]))
# Random number that server must return if ':' in addr[0]:
self.challenge = challenge self.queryAddr += ipaddress.ip_address(addr[0]).packed
self.sentChallengeAt = time() else:
for i in addr[0].split('.'):
self.queryAddr += pack('!B', int(i))
# This server is not checked self.queryAddr += pack('!H', int(addr[1]))
# So it will not get into queries
self.check = False
# Remove server after this time. # Random number that server must return
# This maybe not instant self.challenge = challenge
self.die = self.sentChallengeAt + 600 self.sentChallengeAt = time()
# Remove server after this time.
# This maybe not instant
self.die = self.sentChallengeAt + 600