More documentation
This commit is contained in:
parent
7f5e5ef4b2
commit
026b518840
@ -1,34 +1,62 @@
|
|||||||
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import threading
|
import threading
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
SERVERS = [{"ip": "136.143.97.184", "port": 19711, "last":datetime.datetime.now()},
|
# List of servers always up
|
||||||
{"ip": "73.181.147.35", "port": 19711, "last": datetime.datetime.now()},
|
DEDICATED_SERVERS = [{"ip": "136.143.97.184", "port": 19711, "last": datetime.datetime.now()},
|
||||||
{"ip": "artolsheim.hipstercat.fr", "port": 19711, "last": datetime.datetime.now()}]
|
{"ip": "73.181.147.35", "port": 19711, "last": datetime.datetime.now()},
|
||||||
|
{"ip": "artolsheim.hipstercat.fr", "port": 19711, "last": datetime.datetime.now()}]
|
||||||
|
|
||||||
|
SERVERS = DEDICATED_SERVERS.copy()
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logger(name):
|
||||||
|
# setup logger
|
||||||
|
_ch = logging.StreamHandler()
|
||||||
|
_ch.setLevel("DEBUG")
|
||||||
|
_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
|
_ch.setFormatter(_formatter)
|
||||||
|
logger = logging.getLogger(name)
|
||||||
|
logger.addHandler(_ch)
|
||||||
|
logger.setLevel("DEBUG")
|
||||||
|
logger.debug("Logger initialized")
|
||||||
|
return logger
|
||||||
|
|
||||||
|
|
||||||
|
logger = setup_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
listen_ip = "0.0.0.0"
|
listen_ip = "0.0.0.0"
|
||||||
|
|
||||||
|
# register socket. dedicated servers send packets to this socket to register.
|
||||||
registersock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
registersock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
registersock.bind((listen_ip, 27900))
|
registersock.bind((listen_ip, 27900))
|
||||||
print("Listening on", listen_ip, 27900)
|
logger.info("Register server listening on %s UDP %s", listen_ip, 27900)
|
||||||
|
|
||||||
|
# query socket. clients use this socket to query available servers.
|
||||||
querysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
querysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
querysock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
querysock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
querysock.bind((listen_ip, 28900))
|
querysock.bind((listen_ip, 28900))
|
||||||
print("Listening on", listen_ip, 28900)
|
print("Query server listening on %s TCP %s", listen_ip, 28900)
|
||||||
querysock.listen(5)
|
querysock.listen(5)
|
||||||
|
|
||||||
|
# everything is threaded.
|
||||||
registerthread = threading.Thread(target=registerloop, args=(registersock,))
|
registerthread = threading.Thread(target=registerloop, args=(registersock,))
|
||||||
querythread = threading.Thread(target=queryloop, args=(querysock,))
|
querythread = threading.Thread(target=queryloop, args=(querysock,))
|
||||||
|
|
||||||
|
# clean thread is used to clean older server that haven't updated in a while
|
||||||
cleanthread = threading.Thread(target=cleanloop)
|
cleanthread = threading.Thread(target=cleanloop)
|
||||||
|
|
||||||
|
# start everything
|
||||||
querythread.start()
|
querythread.start()
|
||||||
registerthread.start()
|
registerthread.start()
|
||||||
cleanthread.start()
|
cleanthread.start()
|
||||||
|
|
||||||
|
# wait until everything is finished (which will never happen because they are all running in a loop)
|
||||||
querythread.join()
|
querythread.join()
|
||||||
registerthread.join()
|
registerthread.join()
|
||||||
cleanthread.join()
|
cleanthread.join()
|
||||||
@ -36,8 +64,11 @@ def main():
|
|||||||
|
|
||||||
def queryloop(querysock):
|
def queryloop(querysock):
|
||||||
while True:
|
while True:
|
||||||
|
# new query incoming from a client
|
||||||
(clientsocket, (_, _)) = querysock.accept()
|
(clientsocket, (_, _)) = querysock.accept()
|
||||||
print("A new client has come")
|
logger.info("Query from %s:%s", clientsocket[0], clientsocket[1])
|
||||||
|
|
||||||
|
# forge the response
|
||||||
b = b''
|
b = b''
|
||||||
for i in range(len(SERVERS)):
|
for i in range(len(SERVERS)):
|
||||||
ip = SERVERS[i]["ip"]
|
ip = SERVERS[i]["ip"]
|
||||||
@ -47,42 +78,57 @@ def queryloop(querysock):
|
|||||||
b += b'\xac'
|
b += b'\xac'
|
||||||
b += str(SERVERS[i]["port"]).encode("ascii")
|
b += str(SERVERS[i]["port"]).encode("ascii")
|
||||||
b += b'\x00'
|
b += b'\x00'
|
||||||
print("Sending back", b)
|
logger.info("Sending back %s", b.decode('utf8'))
|
||||||
|
|
||||||
|
# send it
|
||||||
clientsocket.send(b)
|
clientsocket.send(b)
|
||||||
|
|
||||||
|
# close the socket
|
||||||
|
clientsocket.close()
|
||||||
|
|
||||||
|
|
||||||
def registerloop(registersock):
|
def registerloop(registersock):
|
||||||
while True:
|
while True:
|
||||||
|
# new register incoming from a server
|
||||||
data, (ip, port) = registersock.recvfrom(1024)
|
data, (ip, port) = registersock.recvfrom(1024)
|
||||||
try:
|
try:
|
||||||
|
# first byte is some kind of opcode, so we leave it. the rest is the game port
|
||||||
gameport = data[1:].decode("ascii")
|
gameport = data[1:].decode("ascii")
|
||||||
if int(gameport):
|
if int(gameport):
|
||||||
|
# game port sent. now we try to reach the server to avoid registering of servers unreachable.
|
||||||
tempsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
tempsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
tempsocket.settimeout(5)
|
tempsocket.settimeout(5)
|
||||||
|
|
||||||
|
# we try to send a "status" packet to the server.
|
||||||
tempsocket.sendto("\\status\\".encode("ascii"), (ip, port))
|
tempsocket.sendto("\\status\\".encode("ascii"), (ip, port))
|
||||||
tempsocket.recvfrom(1024)
|
tempsocket.recvfrom(1024)
|
||||||
|
|
||||||
|
# server will be None if it has not been seen before, or something else if it has been seen
|
||||||
server = next((x for x in SERVERS if x["ip"] == ip and x["port"] == gameport), None)
|
server = next((x for x in SERVERS if x["ip"] == ip and x["port"] == gameport), None)
|
||||||
if not server:
|
if not server:
|
||||||
SERVERS.append({"ip": ip, "port": gameport, "last": datetime.datetime.now()})
|
SERVERS.append({"ip": ip, "port": gameport, "last": datetime.datetime.now()})
|
||||||
print("New server: %s:%s" % (ip, gameport))
|
logger.info("New server: %s:%s" % (ip, gameport))
|
||||||
else:
|
else:
|
||||||
server["last"] = datetime.datetime.now()
|
server["last"] = datetime.datetime.now()
|
||||||
print("Keepalive from: %s:%s" % (ip, gameport))
|
logger.info("Keepalive from: %s:%s" % (ip, gameport))
|
||||||
tempsocket.close()
|
tempsocket.close()
|
||||||
else:
|
else:
|
||||||
|
# server sent shit, discard it
|
||||||
print("Fuck it, wasn't int: ", data)
|
print("Fuck it, wasn't int: ", data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# something wrong happened but we don't know what
|
||||||
print("Exception")
|
print("Exception")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
def cleanloop():
|
def cleanloop():
|
||||||
|
# clean loop removes servers that haven't registered or sent a packet in the last 2 minutes
|
||||||
|
# but don't remove known dedicated servers
|
||||||
while True:
|
while True:
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
for server in SERVERS:
|
for server in SERVERS:
|
||||||
if now > server["last"] + datetime.timedelta(minutes=2):
|
if now > server["last"] + datetime.timedelta(minutes=2) and server not in DEDICATED_SERVERS:
|
||||||
if server["ip"] == "136.143.97.184" or server["ip"] == "73.181.147.35" or server["ip"] == "artolsheim.hipstercat.fr":
|
logger.info("Deleting %s:%s" % (server["ip"], server["port"]))
|
||||||
# do not remove usual dedicated servers
|
|
||||||
continue
|
|
||||||
print("Deleting %s:%s" % (server["ip"], server["port"]))
|
|
||||||
try:
|
try:
|
||||||
SERVERS.remove(server)
|
SERVERS.remove(server)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
Loading…
Reference in New Issue
Block a user