|
|
@ -1,59 +1,81 @@ |
|
|
|
|
|
|
|
# Basic networking |
|
|
|
import socket |
|
|
|
import socket |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Challenge generator |
|
|
|
import random |
|
|
|
import random |
|
|
|
import struct |
|
|
|
|
|
|
|
|
|
|
|
# System important... things |
|
|
|
import sys |
|
|
|
import sys |
|
|
|
|
|
|
|
import traceback |
|
|
|
|
|
|
|
import logging |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Network packet creating |
|
|
|
|
|
|
|
from struct import pack |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Server time control |
|
|
|
|
|
|
|
from time import time |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ServerEntry class module |
|
|
|
from server_entry import ServerEntry |
|
|
|
from server_entry import ServerEntry |
|
|
|
from util import getAttrOrNone |
|
|
|
# Protocol class |
|
|
|
|
|
|
|
from protocol import MasterProtocol |
|
|
|
|
|
|
|
|
|
|
|
UDP_IP = "127.0.0.1" |
|
|
|
UDP_IP = "127.0.0.1" |
|
|
|
UDP_PORT = 27010 |
|
|
|
UDP_PORT = 27010 |
|
|
|
|
|
|
|
LOG_FILENAME = 'pymaster.log' |
|
|
|
sock = [] |
|
|
|
logging.basicConfig( filename = LOG_FILENAME, level = logging.DEBUG ) |
|
|
|
|
|
|
|
|
|
|
|
def logPrint( msg ): |
|
|
|
def logPrint( msg ): |
|
|
|
if DEBUG: |
|
|
|
logging.debug( msg ) |
|
|
|
print( msg ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PyMaster: |
|
|
|
class PyMaster: |
|
|
|
self.serverList = [] |
|
|
|
serverList = [] |
|
|
|
self.sock = [] |
|
|
|
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
def __init__(self): |
|
|
|
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) |
|
|
|
self.sock.bind( (UDP_IP, UDP_PORT) ) |
|
|
|
sock.bind( (UDP_IP, UDP_PORT) ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logprint("Welcome to PyMaster!") |
|
|
|
logPrint("Welcome to PyMaster!") |
|
|
|
logprint("I ask you again, are you my master?") |
|
|
|
logPrint("I ask you again, are you my master?") |
|
|
|
logprint("Running on %s:%i" % UDP_IP, UDP_PORT) |
|
|
|
logPrint("Running on {0}:{1}".format( UDP_IP, UDP_PORT)) |
|
|
|
|
|
|
|
|
|
|
|
def serverLoop(self) |
|
|
|
def serverLoop(self): |
|
|
|
data, addr = sock.recvfrom(1024)4 |
|
|
|
data, addr = self.sock.recvfrom(1024) |
|
|
|
logprint("received message: %s" % data) |
|
|
|
data = data.decode('latin_1') |
|
|
|
|
|
|
|
|
|
|
|
if data[0] == '1': |
|
|
|
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] == 'q' |
|
|
|
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] == '0' |
|
|
|
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] == 'b' |
|
|
|
elif( data[0] == MasterProtocol.removeServer ): |
|
|
|
|
|
|
|
logPrint("Remove Server: from {0}:{1}".format(addr[0], addr[1])) |
|
|
|
self.removeServerFromList(data, addr); |
|
|
|
self.removeServerFromList(data, addr); |
|
|
|
else |
|
|
|
elif( data[0] == MasterProtocol.statusRequest ): |
|
|
|
logprint("Unknown message: %s from %s" % data, addr) |
|
|
|
logPrint("Status Request: from {0}:{1}".format(addr[0], addr[1])) |
|
|
|
|
|
|
|
self.sendStatus(data, addr); |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
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') |
|
|
|
data = data.strip('1\xff') |
|
|
|
|
|
|
|
|
|
|
|
region = data[0] |
|
|
|
region = data[0] |
|
|
|
queryAddr, rawFilter = data.split('\0') |
|
|
|
try: |
|
|
|
|
|
|
|
queryAddr, rawFilter = data.split('\0') |
|
|
|
|
|
|
|
except ValueError: |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
rawFilter = rawFilter.split('\\') |
|
|
|
rawFilter = rawFilter.strip('\\') |
|
|
|
queryFilter = namedtuple('QueryFilter', rawFilter[0::2])._make(rawFilter[1::2]) |
|
|
|
split = rawFilter.split('\\') |
|
|
|
|
|
|
|
|
|
|
|
#nor = getAttrOrNone(queryFilter, 'nor') |
|
|
|
#nor = getAttrOrNone(queryFilter, 'nor') |
|
|
|
#nand = getAttrOrNone(queryFilter, 'nand') |
|
|
|
#nand = getAttrOrNone(queryFilter, 'nand') |
|
|
|
#dedicated = getAttrOrNone(queryFilter, 'dedicated') |
|
|
|
#dedicated = getAttrOrNone(queryFilter, 'dedicated') |
|
|
|
#gamedir = getAttrOrNone(queryFilter, 'gamedir') |
|
|
|
|
|
|
|
#gamemap = getAttrOrNone(queryFilter, 'map') |
|
|
|
#gamemap = getAttrOrNone(queryFilter, 'map') |
|
|
|
#linux = getAttrOrNone(queryFilter, 'linux') |
|
|
|
#linux = getAttrOrNone(queryFilter, 'linux') |
|
|
|
#empty = getAttrOrNone(queryFilter, 'empty') |
|
|
|
#empty = getAttrOrNone(queryFilter, 'empty') |
|
|
@ -64,21 +86,51 @@ class PyMaster: |
|
|
|
#name = getAttrOrNone(queryFilter, 'name') |
|
|
|
#name = getAttrOrNone(queryFilter, 'name') |
|
|
|
#version = getAttrOrNone(queryFilter, 'version_match') |
|
|
|
#version = getAttrOrNone(queryFilter, 'version_match') |
|
|
|
#gameaddr = getAttrOrNone(queryFilter, 'gameaddr') |
|
|
|
#gameaddr = getAttrOrNone(queryFilter, 'gameaddr') |
|
|
|
secure = getAttrOrNone(queryFilter, 'secure') |
|
|
|
#secure = getAttrOrNone(queryFilter, 'secure') |
|
|
|
|
|
|
|
|
|
|
|
packet = '\xff\xff\xff\xff\x66\x0a' |
|
|
|
# Use NoneType as undefined |
|
|
|
|
|
|
|
gamedir = None |
|
|
|
|
|
|
|
gamemap = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range( 0, len(split), 2 ): |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
key = split[i + 1] |
|
|
|
|
|
|
|
if( split[i] == 'gamedir' ): |
|
|
|
|
|
|
|
gamedir = key |
|
|
|
|
|
|
|
elif( split[i] == 'map' ): |
|
|
|
|
|
|
|
gamemap = key |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
logPrint('Unhandled info string entry: {0}/{1}'.format(split[i], key)) |
|
|
|
|
|
|
|
except IndexError: |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
packet = MasterProtocol.queryPacketHeader |
|
|
|
for i in self.serverList: |
|
|
|
for i in self.serverList: |
|
|
|
if( !i.check ): |
|
|
|
if( time() > i.die ): |
|
|
|
|
|
|
|
self.serverList.remove(i) |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( not i.check ): |
|
|
|
continue |
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
if( gamedir != None ): |
|
|
|
if( gamedir != None ): |
|
|
|
if( gamedir != i.serverInfo.gamedir): |
|
|
|
if( gamedir != i.gamedir): |
|
|
|
continue |
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
# Use pregenerated address string |
|
|
|
# Use pregenerated address string |
|
|
|
packet += self.queryAddr |
|
|
|
packet += i.queryAddr |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.sock.sendto(packet, addr) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def removeServerFromList(self, data, addr): |
|
|
|
|
|
|
|
for i in self.serverList: |
|
|
|
|
|
|
|
if (i.addr == addr): |
|
|
|
|
|
|
|
self.serverList.remove(i) |
|
|
|
|
|
|
|
|
|
|
|
def sendChallengeToServer(self, data, addr): |
|
|
|
def sendChallengeToServer(self, data, addr): |
|
|
|
|
|
|
|
# At first, remove old server data from list |
|
|
|
|
|
|
|
self.removeServerFromList(None, addr) |
|
|
|
|
|
|
|
|
|
|
|
# Generate a 32 bit challenge number |
|
|
|
# Generate a 32 bit challenge number |
|
|
|
challenge = random.randint(0, 2**32-1) |
|
|
|
challenge = random.randint(0, 2**32-1) |
|
|
|
|
|
|
|
|
|
|
@ -86,23 +138,30 @@ class PyMaster: |
|
|
|
self.serverList.append(ServerEntry(addr, challenge)) |
|
|
|
self.serverList.append(ServerEntry(addr, challenge)) |
|
|
|
|
|
|
|
|
|
|
|
# And send him a challenge |
|
|
|
# And send him a challenge |
|
|
|
packet = '\xff\xff\xff\xff\x73\x0a' |
|
|
|
packet = MasterProtocol.challengePacketHeader |
|
|
|
packet += struct.pack('I', challenge) |
|
|
|
packet += pack('I', challenge) |
|
|
|
socket.sendto(packet, addr) |
|
|
|
self.sock.sendto(packet, addr) |
|
|
|
|
|
|
|
|
|
|
|
def addServerToList(self, data, addr): |
|
|
|
def addServerToList(self, data, addr): |
|
|
|
# Remove the header |
|
|
|
# Remove the header. Just for better parsing. |
|
|
|
serverInfo = data.strip('\x30\x0a\x5c') |
|
|
|
serverInfo = data.strip('\x30\x0a\x5c') |
|
|
|
|
|
|
|
|
|
|
|
# Find a server with |
|
|
|
# Find a server with same address |
|
|
|
for serverEntry in self.serverList: |
|
|
|
for serverEntry in self.serverList: |
|
|
|
if (serverEntry.addr == addr): |
|
|
|
if( serverEntry.addr == addr ): |
|
|
|
serverEntry.setInfoString(serverInfo) |
|
|
|
serverEntry.setInfoString( serverInfo ) |
|
|
|
|
|
|
|
|
|
|
|
def removeServerFromList(self, data, addr): |
|
|
|
def sendStatus( self, data, addr ): |
|
|
|
|
|
|
|
count = len(self.serverList) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
packet = b'Server\t\t\tGame\tMap\tPlayers\tVersion\tChallenge\tCheck\n' |
|
|
|
for i in self.serverList: |
|
|
|
for i in self.serverList: |
|
|
|
if (i.addr == addr): |
|
|
|
line = '{0}:{1}\t{2}\t{3}\t{4}/{5}\t{6}\n'.format(i.addr[0], i.addr[1], |
|
|
|
self.serverList.remove(i) |
|
|
|
i.gamedir, i.gamemap, i.players, |
|
|
|
|
|
|
|
i.maxplayers, i.version, i.challenge, i.check) |
|
|
|
|
|
|
|
packet += line.encode('latin_1') |
|
|
|
|
|
|
|
self.sock.sendto(packet, addr) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main( argv = None ): |
|
|
|
def main( argv = None ): |
|
|
|
if argv is None: |
|
|
|
if argv is None: |
|
|
@ -110,7 +169,11 @@ def main( argv = None ): |
|
|
|
|
|
|
|
|
|
|
|
masterMain = PyMaster() |
|
|
|
masterMain = PyMaster() |
|
|
|
while True: |
|
|
|
while True: |
|
|
|
masterMain.serverLoop() |
|
|
|
try: |
|
|
|
|
|
|
|
masterMain.serverLoop() |
|
|
|
|
|
|
|
except Exception: |
|
|
|
|
|
|
|
logging.exception() |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
if __name__ == "__main__": |
|
|
|
sys.exit( main( ) ) |
|
|
|
sys.exit( main( ) ) |