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
|
2022-12-07 20:18:59 +00:00
|
|
|
from struct import pack, unpack
|
2016-01-20 23:24:37 +00:00
|
|
|
from time import time
|
2022-12-07 20:18:59 +00:00
|
|
|
from ipaddress import ip_address, ip_network
|
2016-01-20 23:24:37 +00:00
|
|
|
|
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
|
2022-12-07 20:18:59 +00:00
|
|
|
import ipfilter
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
LOG_FILENAME = 'pymaster.log'
|
2021-10-12 09:27:05 +00:00
|
|
|
MAX_SERVERS_FOR_IP = 14
|
2022-12-07 20:18:59 +00:00
|
|
|
CHALLENGE_SEND_PERIOD = 10
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
def log(msg):
|
|
|
|
logging.debug(msg)
|
|
|
|
|
|
|
|
class RateLimitItem:
|
|
|
|
def __init__(self, resetAt):
|
|
|
|
self.reset(resetAt)
|
|
|
|
|
|
|
|
def reset(self, resetAt):
|
|
|
|
self.resetAt = resetAt
|
|
|
|
self.logs = self.calls = 0
|
|
|
|
|
|
|
|
def inc(self):
|
|
|
|
self.calls = self.calls + 1
|
|
|
|
self.logs = self.logs + 1
|
|
|
|
|
|
|
|
def shouldReset(self, curtime):
|
|
|
|
return curtime > self.resetAt
|
|
|
|
|
|
|
|
class IPRateLimit:
|
|
|
|
def __init__(self, type, period, maxcalls):
|
|
|
|
self.type = type
|
|
|
|
self.period = period
|
|
|
|
self.maxcalls = maxcalls
|
|
|
|
self.maxlogs = maxcalls + 2
|
|
|
|
self.ips = {}
|
|
|
|
|
|
|
|
def ratelimit(self, ip):
|
|
|
|
curtime = time()
|
|
|
|
|
|
|
|
if ip not in self.ips:
|
|
|
|
self.ips[ip] = RateLimitItem(curtime + self.period)
|
|
|
|
elif self.ips[ip].shouldReset(curtime):
|
|
|
|
self.ips[ip].reset(curtime + self.period)
|
|
|
|
|
|
|
|
self.ips[ip].inc()
|
|
|
|
|
|
|
|
if self.ips[ip].calls > self.maxcalls:
|
|
|
|
if self.ips[ip].logs < self.maxlogs:
|
|
|
|
log('Ratelimited %s %s' % (self.type, ip))
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
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 = []
|
2022-12-07 20:18:59 +00:00
|
|
|
self.serverRL = IPRateLimit('server', 60, 30)
|
|
|
|
self.clientRL = IPRateLimit('client', 60, 120)
|
|
|
|
self.ipfilterRL = IPRateLimit('filterlog', 60, 10)
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
self.sock.bind((ip, port))
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
log("Welcome to PyMaster!")
|
|
|
|
log("I ask you again, are you my master?")
|
|
|
|
log("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)
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if ip_address(addr[0]) in ipfilter.ipfilter:
|
|
|
|
if not self.ipfilterRL.ratelimit(addr[0]):
|
|
|
|
log('Filter: %s:%d' % (addr[0], addr[1]))
|
|
|
|
return
|
2016-01-19 21:32:16 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if len(data) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
# only client stuff
|
|
|
|
if data.startswith(MasterProtocol.clientQuery):
|
|
|
|
if not self.clientRL.ratelimit(addr[0]):
|
|
|
|
self.clientQuery(data, addr)
|
2016-01-20 23:24:37 +00:00
|
|
|
return
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
# only server stuff
|
|
|
|
if not self.serverRL.ratelimit(addr[0]):
|
|
|
|
if data.startswith(MasterProtocol.challengeRequest):
|
|
|
|
self.sendChallengeToServer(data, addr)
|
|
|
|
elif data.startswith(MasterProtocol.addServer):
|
|
|
|
self.addServerToList(data, addr)
|
|
|
|
elif data.startswith(MasterProtocol.removeServer):
|
|
|
|
self.removeServerFromList(data, addr)
|
|
|
|
else:
|
|
|
|
log('Unknown message: %s from %s:%d' % (str(data), addr[0], addr[1]))
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
def clientQuery(self, data, addr):
|
|
|
|
data = data.decode('latin_1')
|
|
|
|
data = data.strip('1' + data[1])
|
|
|
|
info = data.split('\0')[1].strip('\\')
|
|
|
|
split = info.split('\\')
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
protocol = None
|
|
|
|
gamedir = 'valve'
|
|
|
|
clver = None
|
|
|
|
nat = 0
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
for i in range(0, len(split), 2):
|
2016-01-20 23:24:37 +00:00
|
|
|
try:
|
2022-12-07 20:18:59 +00:00
|
|
|
k = split[i]
|
|
|
|
v = split[i + 1]
|
|
|
|
if k == 'gamedir':
|
|
|
|
gamedir = v.lower() # keep gamedir in lowercase
|
|
|
|
elif k == 'nat':
|
|
|
|
nat = int(v)
|
|
|
|
elif k == 'clver':
|
|
|
|
clver = v
|
|
|
|
elif k == 'protocol':
|
|
|
|
protocol = int(v)
|
|
|
|
# somebody is playing :)
|
|
|
|
elif k == 'thisismypcid' or k == 'heydevelopersifyoureadthis':
|
|
|
|
self.fakeInfoForOldVersions(gamedir, addr)
|
|
|
|
return
|
2016-01-20 23:24:37 +00:00
|
|
|
else:
|
2022-12-07 20:18:59 +00:00
|
|
|
log('Client Query: %s:%d, invalid infostring=%s' % (addr[0], addr[1], rawFilter))
|
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
|
2022-12-07 20:18:59 +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:
|
2022-12-07 20:18:59 +00:00
|
|
|
if time() > i.die:
|
2016-01-20 23:24:37 +00:00
|
|
|
self.serverList.remove(i)
|
|
|
|
continue
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if not i.check:
|
2016-01-19 21:32:16 +00:00
|
|
|
continue
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if nat != i.nat or gamedir != i.gamedir:
|
2017-03-23 21:30:03 +00:00
|
|
|
continue
|
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if protocol != None and protocol != i.protocol:
|
|
|
|
continue
|
2017-03-23 21:30:03 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if nat:
|
2017-03-23 21:30:03 +00:00
|
|
|
# Tell server to send info reply
|
2022-12-07 20:18:59 +00:00
|
|
|
data = ('\xff\xff\xff\xffc %s:%d' % (addr[0], addr[1])).encode('latin_1')
|
|
|
|
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):
|
2022-12-07 20:18:59 +00:00
|
|
|
pass
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-19 21:32:16 +00:00
|
|
|
def sendChallengeToServer(self, data, addr):
|
2016-01-30 17:40:48 +00:00
|
|
|
count = 0
|
2022-12-07 20:18:59 +00:00
|
|
|
s = None
|
2016-01-30 17:40:48 +00:00
|
|
|
for i in self.serverList:
|
2022-12-07 20:18:59 +00:00
|
|
|
if addr[0] != i.addr[0]:
|
|
|
|
continue
|
|
|
|
if addr[1] == i.addr[1]:
|
|
|
|
s = i
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
count += 1
|
|
|
|
if count > MAX_SERVERS_FOR_IP:
|
2016-01-30 17:40:48 +00:00
|
|
|
return
|
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
challenge2 = None
|
|
|
|
if len(data) == 6:
|
|
|
|
# little endian challenge
|
|
|
|
challenge2 = unpack('<I', data[2:])[0]
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if not s:
|
|
|
|
challenge = random.randint(0, 2**32-1) & 0xffffffff #hash(addr[0]) + hash(addr[1]) + hash(time())
|
|
|
|
s = ServerEntry(addr, challenge)
|
|
|
|
self.serverList.append(s)
|
|
|
|
elif s.sentChallengeAt + 5 > time():
|
|
|
|
return
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
packet = MasterProtocol.challengePacketHeader
|
2022-12-07 20:18:59 +00:00
|
|
|
packet += pack('I', s.challenge)
|
|
|
|
|
|
|
|
# send server-to-master challenge back
|
|
|
|
if challenge2 is not None:
|
|
|
|
packet += pack('I', challenge2)
|
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
self.sock.sendto(packet, addr)
|
2016-01-19 21:32:16 +00:00
|
|
|
|
|
|
|
def addServerToList(self, data, addr):
|
2016-01-20 23:24:37 +00:00
|
|
|
# Remove the header. Just for better parsing.
|
2022-12-07 20:18:59 +00:00
|
|
|
info = data.strip(b'\x30\x0a\x5c').decode('latin_1')
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2016-01-20 23:24:37 +00:00
|
|
|
# Find a server with same address
|
2022-12-07 20:18:59 +00:00
|
|
|
s = None
|
|
|
|
for s in self.serverList:
|
|
|
|
if s.addr == addr:
|
2016-01-22 10:15:02 +00:00
|
|
|
break
|
2022-12-07 20:18:59 +00:00
|
|
|
if not s:
|
|
|
|
log('Server skipped challenge request: %s:%d' % (addr[0], addr[1]))
|
|
|
|
return
|
2016-01-30 17:40:48 +00:00
|
|
|
|
2022-12-07 20:18:59 +00:00
|
|
|
if s.setInfoString( info ):
|
|
|
|
log('Add server: %s:%d, game=%s/%s, protocol=%d, players=%d/%d/%d, version=%s' % (addr[0], addr[1], s.gamemap, s.gamedir, s.protocol, s.players, s.bots, s.maxplayers, s.version))
|
|
|
|
else:
|
|
|
|
log('Failed challenge from %s:%d: %d must be %d' % (addr[0], addr[1], s.challenge, s.challenge2))
|
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())
|
2022-12-07 20:18:59 +00:00
|
|
|
# logging.getLogger().addHandler(logging.FileHandler(LOG_FILENAME))
|
2019-10-17 01:04:23 +00:00
|
|
|
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:
|
2022-12-07 20:18:59 +00:00
|
|
|
log(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))
|