server.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. class Server:
  2. def __init__(self):
  3. self.challenge = 0
  4. self.addr = ("0.0.0.0", 0)
  5. self.port = 25600
  6. self.prejoinactions = 0
  7. self.product = ""
  8. self.params = {}
  9. self.paramsallowed = "port;players;maxplayers;level;gametype;version"
  10. self.visible = False
  11. self.lastheartbeat = time.time()
  12. def join(self):
  13. self.challenge = random.randint(0x00000000, 0x7FFFFFFF)
  14. self.port = self.addr[1] - 1
  15. sendto("\1" + struct.pack("i", self.challenge), addr)
  16. def challengeresponse(self, data):
  17. params = re.findall(";(\w+);([^;]*)", data)
  18. valid = False
  19. for param in params:
  20. key = param[0]
  21. val = param[1]
  22. if(key == "challenge"):
  23. if(val == str(self.challenge)):
  24. valid = True
  25. continue
  26. if(key == "product"):
  27. self.product = val
  28. continue
  29. try:
  30. self.paramsallowed.split(";").index(key)
  31. self.params[key] = val
  32. except:
  33. pass
  34. if(not self.visible):
  35. if(valid):
  36. self.visible = True
  37. else:
  38. self.lastheartbeat = time.time()
  39. def statechanged(self, data):
  40. print("state changed: " + data)