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
|
2016-01-20 23:24:37 +00:00
|
|
|
from struct import pack
|
|
|
|
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
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
LOG_FILENAME = 'pymaster.log'
|
2016-01-19 21:32:16 +00:00
|
|
|
|
|
|
|
def logPrint( msg ):
|
2016-01-20 23:24:37 +00:00
|
|
|
logging.debug( msg )
|
2016-01-19 21:32:16 +00:00
|
|
|
|
|
|
|
class PyMaster:
|
2019-10-17 01:04:23 +00:00
|
|
|
def __init__(self, ip, port):
|
|
|
|
self.serverList = []
|
|
|
|
self.sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
|
|
|
|
self.sock.bind( (ip, port) )
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
logPrint("Welcome to PyMaster!")
|
|
|
|
logPrint("I ask you again, are you my master?")
|
2019-10-17 01:04:23 +00:00
|
|
|
logPrint("Running on %s:%d" % (ip, port))
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
def serverLoop(self):
|
|
|
|
data, addr = self.sock.recvfrom(1024)
|
|
|
|
data = data.decode('latin_1')
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
if( data[0] == MasterProtocol.clientQuery ):
|
2016-01-22 10:15:02 +00:00
|
|
|
self.clientQuery(data, addr)
|
2016-01-20 23:24:37 +00:00
|
|
|
elif( data[0] == MasterProtocol.challengeRequest ):
|
2016-01-22 10:15:02 +00:00
|
|
|
self.sendChallengeToServer(data, addr)
|
2016-01-20 23:24:37 +00:00
|
|
|
elif( data[0] == MasterProtocol.addServer ):
|
2016-01-22 10:15:02 +00:00
|
|
|
self.addServerToList(data, addr)
|
2016-01-20 23:24:37 +00:00
|
|
|
elif( data[0] == MasterProtocol.removeServer ):
|
2016-01-22 10:15:02 +00:00
|
|
|
self.removeServerFromList(data, addr)
|
2016-01-20 23:24:37 +00:00
|
|
|
else:
|
|
|
|
logPrint("Unknown message: {0} from {1}:{2}".format(data, addr[0], addr[1]))
|
2016-01-19 21:32:16 +00:00
|
|
|
|
|
|
|
def clientQuery(self, data, addr):
|
2016-01-22 10:15:02 +00:00
|
|
|
region = data[1] # UNUSED
|
|
|
|
data = data.strip('1' + region)
|
2016-01-20 23:24:37 +00:00
|
|
|
try:
|
2016-01-22 10:15:02 +00:00
|
|
|
query = data.split('\0')
|
2016-01-20 23:24:37 +00:00
|
|
|
except ValueError:
|
2016-01-22 10:15:02 +00:00
|
|
|
logPrint(traceback.format_exc())
|
2016-01-20 23:24:37 +00:00
|
|
|
return
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-22 10:15:02 +00:00
|
|
|
queryAddr = query[0] # UNUSED
|
|
|
|
rawFilter = query[1]
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-22 10:15:02 +00:00
|
|
|
# Remove first \ character
|
2016-01-20 23:24:37 +00:00
|
|
|
rawFilter = rawFilter.strip('\\')
|
|
|
|
split = rawFilter.split('\\')
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
# Use NoneType as undefined
|
2018-07-30 18:59:29 +00:00
|
|
|
gamedir = 'valve' # halflife, by default
|
2018-07-26 20:53:30 +00:00
|
|
|
clver = None
|
2017-03-23 21:30:03 +00:00
|
|
|
nat = 0
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
for i in range( 0, len(split), 2 ):
|
|
|
|
try:
|
|
|
|
key = split[i + 1]
|
|
|
|
if( split[i] == 'gamedir' ):
|
2018-12-30 11:43:52 +00:00
|
|
|
gamedir = key.lower() # keep gamedir in lowercase
|
2017-03-23 21:30:03 +00:00
|
|
|
elif( split[i] == 'nat' ):
|
|
|
|
nat = int(key)
|
2018-07-26 20:53:30 +00:00
|
|
|
elif( split[i] == 'clver' ):
|
|
|
|
clver = key
|
2016-01-20 23:24:37 +00:00
|
|
|
else:
|
2017-06-10 19:11:51 +00:00
|
|
|
logPrint('Unhandled info string entry: {0}/{1}. Infostring was: {2}'.format(split[i], key, split))
|
2016-01-20 23:24:37 +00:00
|
|
|
except IndexError:
|
|
|
|
pass
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2018-07-26 20:53:30 +00:00
|
|
|
if( clver == None ): # Probably an old vulnerable version
|
2018-07-26 21:25:26 +00:00
|
|
|
self.fakeInfoForOldVersions( gamedir, addr )
|
2018-07-26 20:53:30 +00:00
|
|
|
return
|
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
packet = MasterProtocol.queryPacketHeader
|
2016-01-19 21:32:16 +00:00
|
|
|
for i in self.serverList:
|
2016-01-20 23:24:37 +00:00
|
|
|
if( time() > i.die ):
|
|
|
|
self.serverList.remove(i)
|
|
|
|
continue
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
if( not i.check ):
|
2016-01-19 21:32:16 +00:00
|
|
|
continue
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2017-03-23 21:30:03 +00:00
|
|
|
if( nat != i.nat ):
|
|
|
|
continue
|
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
if( gamedir != None ):
|
2017-03-23 21:30:03 +00:00
|
|
|
if( gamedir != i.gamedir ):
|
2016-01-19 21:32:16 +00:00
|
|
|
continue
|
2017-03-23 21:30:03 +00:00
|
|
|
|
|
|
|
if( nat ):
|
2017-06-10 19:04:53 +00:00
|
|
|
reply = '\xff\xff\xff\xffc {0}:{1}'.format( addr[0], addr[1] )
|
|
|
|
data = reply.encode( 'latin_1' )
|
2017-03-23 21:30:03 +00:00
|
|
|
# Tell server to send info reply
|
2017-06-10 19:04:53 +00:00
|
|
|
self.sock.sendto( data, i.addr )
|
2017-03-23 21:30:03 +00:00
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
# Use pregenerated address string
|
2016-01-20 23:24:37 +00:00
|
|
|
packet += i.queryAddr
|
2016-01-22 10:54:23 +00:00
|
|
|
packet += b'\0\0\0\0\0\0' # Fill last IP:Port with \0
|
2016-01-20 23:24:37 +00:00
|
|
|
self.sock.sendto(packet, addr)
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2018-07-26 20:53:30 +00:00
|
|
|
def fakeInfoForOldVersions(self, gamedir, addr):
|
2018-07-26 21:03:07 +00:00
|
|
|
def sendFakeInfo(sock, warnmsg, gamedir, addr):
|
2018-07-26 21:25:26 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
sendFakeInfo(self.sock, "This version is not", gamedir, addr)
|
|
|
|
sendFakeInfo(self.sock, "supported anymore", gamedir, addr)
|
|
|
|
sendFakeInfo(self.sock, "Please update Xash3DFWGS", gamedir, addr)
|
|
|
|
sendFakeInfo(self.sock, "From GooglePlay or GitHub", gamedir, addr)
|
|
|
|
sendFakeInfo(self.sock, "Эта версия", gamedir, addr)
|
|
|
|
sendFakeInfo(self.sock, "устарела", gamedir, addr)
|
|
|
|
sendFakeInfo(self.sock, "Обновите Xash3DFWGS c", gamedir, addr)
|
|
|
|
sendFakeInfo(self.sock, "GooglePlay или GitHub", gamedir, addr)
|
2018-07-26 20:53:30 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
def removeServerFromList(self, data, addr):
|
|
|
|
for i in self.serverList:
|
|
|
|
if (i.addr == addr):
|
2016-01-30 17:40:48 +00:00
|
|
|
logPrint("Remove Server: from {0}:{1}".format(addr[0], addr[1]))
|
2016-01-20 23:24:37 +00:00
|
|
|
self.serverList.remove(i)
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
def sendChallengeToServer(self, data, addr):
|
2016-01-22 10:15:02 +00:00
|
|
|
logPrint("Challenge Request: from {0}:{1}".format(addr[0], addr[1]))
|
|
|
|
# At first, remove old server- data from list
|
2016-01-30 17:40:48 +00:00
|
|
|
#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 > 7 ):
|
|
|
|
return
|
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
# Generate a 32 bit challenge number
|
|
|
|
challenge = random.randint(0, 2**32-1)
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
# Add server to list
|
|
|
|
self.serverList.append(ServerEntry(addr, challenge))
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
# And send him a challenge
|
2016-01-20 23:24:37 +00:00
|
|
|
packet = MasterProtocol.challengePacketHeader
|
|
|
|
packet += pack('I', challenge)
|
|
|
|
self.sock.sendto(packet, addr)
|
2016-01-19 21:32:16 +00:00
|
|
|
|
|
|
|
def addServerToList(self, data, addr):
|
2016-01-22 10:15:02 +00:00
|
|
|
logPrint("Add Server: from {0}:{1}".format(addr[0], addr[1]))
|
2016-01-20 23:24:37 +00:00
|
|
|
# Remove the header. Just for better parsing.
|
2016-01-19 21:32:16 +00:00
|
|
|
serverInfo = data.strip('\x30\x0a\x5c')
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
# Find a server with same address
|
2016-01-19 21:32:16 +00:00
|
|
|
for serverEntry in self.serverList:
|
2016-01-20 23:24:37 +00:00
|
|
|
if( serverEntry.addr == addr ):
|
2016-01-22 10:15:02 +00:00
|
|
|
break
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-22 10:15:02 +00:00
|
|
|
serverEntry.setInfoString( serverInfo )
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2019-10-17 01:04:23 +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-30 17:40:48 +00:00
|
|
|
|
2019-10-17 01:04:23 +00:00
|
|
|
masterMain = PyMaster(ip, port)
|
2016-01-30 17:40:48 +00:00
|
|
|
while True:
|
2016-01-20 23:24:37 +00:00
|
|
|
try:
|
|
|
|
masterMain.serverLoop()
|
|
|
|
except Exception:
|
2016-01-22 10:15:02 +00:00
|
|
|
logPrint(traceback.format_exc())
|
2016-01-20 23:24:37 +00:00
|
|
|
pass
|
2016-01-19 21:32:16 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-10-17 01:04:23 +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 pidfile, 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))
|