Browse Source

Fix master to client answers, advanced logging, interpreter header for *nix systems

pull/4/head
a1batross 9 years ago
parent
commit
cbc9fc0883
  1. 62
      pymaster.py
  2. 5
      server_entry.py

62
pymaster.py

@ -1,3 +1,4 @@
#!/usr/bin/env python3
# Basic networking # Basic networking
import socket import socket
@ -20,10 +21,12 @@ from server_entry import ServerEntry
# Protocol class # Protocol class
from protocol import MasterProtocol from protocol import MasterProtocol
UDP_IP = "127.0.0.1" UDP_IP = "0.0.0.0"
UDP_PORT = 27010 UDP_PORT = 27010
LOG_FILENAME = 'pymaster.log' LOG_FILENAME = 'pymaster.log'
logging.basicConfig( filename = LOG_FILENAME, level = logging.DEBUG ) logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().addHandler(logging.FileHandler(LOG_FILENAME))
logging.getLogger().setLevel(logging.DEBUG)
def logPrint( msg ): def logPrint( msg ):
logging.debug( msg ) logging.debug( msg )
@ -44,53 +47,39 @@ class PyMaster:
data = data.decode('latin_1') data = data.decode('latin_1')
if( data[0] == MasterProtocol.clientQuery ): if( data[0] == MasterProtocol.clientQuery ):
logPrint("Client Query: from {0}:{1}".format(addr[0], addr[1])) self.clientQuery(data, addr)
self.clientQuery(data, addr);
elif( data[0] == MasterProtocol.challengeRequest ): elif( data[0] == MasterProtocol.challengeRequest ):
logPrint("Challenge Request: from {0}:{1}".format(addr[0], addr[1])) self.sendChallengeToServer(data, addr)
self.sendChallengeToServer(data, addr);
elif( data[0] == MasterProtocol.addServer ): elif( data[0] == MasterProtocol.addServer ):
logPrint("Add Server: from {0}:{1}".format(addr[0], addr[1])) self.addServerToList(data, addr)
self.addServerToList(data, addr);
elif( data[0] == MasterProtocol.removeServer ): elif( data[0] == MasterProtocol.removeServer ):
logPrint("Remove Server: from {0}:{1}".format(addr[0], addr[1])) self.removeServerFromList(data, addr)
self.removeServerFromList(data, addr);
elif( data[0] == MasterProtocol.statusRequest ): elif( data[0] == MasterProtocol.statusRequest ):
logPrint("Status Request: from {0}:{1}".format(addr[0], addr[1])) self.sendStatus(data, addr)
self.sendStatus(data, addr);
else: else:
logPrint("Unknown message: {0} from {1}:{2}".format(data, addr[0], addr[1])) logPrint("Unknown message: {0} from {1}:{2}".format(data, addr[0], addr[1]))
def clientQuery(self, data, addr): def clientQuery(self, data, addr):
data = data.strip('1\xff') logPrint("Client Query: from {0}:{1}".format(addr[0], addr[1]))
region = data[0] region = data[1] # UNUSED
data = data.strip('1' + region)
try: try:
queryAddr, rawFilter = data.split('\0') query = data.split('\0')
except ValueError: except ValueError:
logPrint(traceback.format_exc())
return return
queryAddr = query[0] # UNUSED
rawFilter = query[1]
# Remove first \ character
rawFilter = rawFilter.strip('\\') rawFilter = rawFilter.strip('\\')
split = rawFilter.split('\\') split = rawFilter.split('\\')
#nor = getAttrOrNone(queryFilter, 'nor')
#nand = getAttrOrNone(queryFilter, 'nand')
#dedicated = getAttrOrNone(queryFilter, 'dedicated')
#gamemap = getAttrOrNone(queryFilter, 'map')
#linux = getAttrOrNone(queryFilter, 'linux')
#empty = getAttrOrNone(queryFilter, 'empty')
#full = getAttrOrNone(queryFilter, 'full')
#proxy = getAttrOrNone(queryFilter, 'proxy')
#noplayers = getAttrOrNone(queryFilter, 'noplayers')
#white = getAttrOrNone(queryFilter, 'white')
#name = getAttrOrNone(queryFilter, 'name')
#version = getAttrOrNone(queryFilter, 'version_match')
#gameaddr = getAttrOrNone(queryFilter, 'gameaddr')
#secure = getAttrOrNone(queryFilter, 'secure')
# Use NoneType as undefined # Use NoneType as undefined
gamedir = None gamedir = None
gamemap = None gamemap = None # UNUSED: until Xash3D will not support full filter
for i in range( 0, len(split), 2 ): for i in range( 0, len(split), 2 ):
try: try:
@ -119,16 +108,19 @@ class PyMaster:
# Use pregenerated address string # Use pregenerated address string
packet += i.queryAddr packet += i.queryAddr
logPrint('Append server to answer: {0}:{1}'.format(i.addr[0], i.addr[1]))
self.sock.sendto(packet, addr) self.sock.sendto(packet, addr)
def removeServerFromList(self, data, addr): def removeServerFromList(self, data, addr):
logPrint("Remove Server: from {0}:{1}".format(addr[0], addr[1]))
for i in self.serverList: for i in self.serverList:
if (i.addr == addr): if (i.addr == addr):
self.serverList.remove(i) self.serverList.remove(i)
def sendChallengeToServer(self, data, addr): def sendChallengeToServer(self, data, addr):
# At first, remove old server data from list logPrint("Challenge Request: from {0}:{1}".format(addr[0], addr[1]))
# At first, remove old server- data from list
self.removeServerFromList(None, addr) self.removeServerFromList(None, addr)
# Generate a 32 bit challenge number # Generate a 32 bit challenge number
@ -143,15 +135,19 @@ class PyMaster:
self.sock.sendto(packet, addr) self.sock.sendto(packet, addr)
def addServerToList(self, data, addr): def addServerToList(self, data, addr):
logPrint("Add Server: from {0}:{1}".format(addr[0], addr[1]))
# Remove the header. Just for better parsing. # Remove the header. Just for better parsing.
serverInfo = data.strip('\x30\x0a\x5c') serverInfo = data.strip('\x30\x0a\x5c')
# Find a server with same address # Find a server with same address
for serverEntry in self.serverList: for serverEntry in self.serverList:
if( serverEntry.addr == addr ): if( serverEntry.addr == addr ):
break
serverEntry.setInfoString( serverInfo ) serverEntry.setInfoString( serverInfo )
def sendStatus( self, data, addr ): def sendStatus( self, data, addr ):
logPrint("Status Request: from {0}:{1}".format(addr[0], addr[1]))
count = len(self.serverList) count = len(self.serverList)
packet = b'Server\t\t\tGame\tMap\tPlayers\tVersion\tChallenge\tCheck\n' packet = b'Server\t\t\tGame\tMap\tPlayers\tVersion\tChallenge\tCheck\n'
@ -172,7 +168,7 @@ def main( argv = None ):
try: try:
masterMain.serverLoop() masterMain.serverLoop()
except Exception: except Exception:
logging.exception() logPrint(traceback.format_exc())
pass pass
if __name__ == "__main__": if __name__ == "__main__":

5
server_entry.py

@ -21,7 +21,6 @@ class ServerEntry:
def setInfoString(self, data): def setInfoString(self, data):
infostring = data.replace('\n', '').replace('\r', '').replace('\0', '') infostring = data.replace('\n', '').replace('\r', '').replace('\0', '')
split = infostring.split('\\') split = infostring.split('\\')
logPrint( split )
for i in range(0, len(split), 2): for i in range(0, len(split), 2):
try: try:
key = split[i + 1] key = split[i + 1]
@ -66,8 +65,8 @@ class ServerEntry:
# Shortcuts for generating query # Shortcuts for generating query
self.queryAddr = b'' self.queryAddr = b''
for i in addr[0].split('.'): for i in addr[0].split('.'):
self.queryAddr += pack('B', int(i)) self.queryAddr += pack('!B', int(i))
self.queryAddr += pack('H', addr[1]) self.queryAddr += pack('!H', int(addr[1]))
# Random number that server must return # Random number that server must return
self.challenge = challenge self.challenge = challenge

Loading…
Cancel
Save