Guest User

detectclient.py

a guest
Apr 13th, 2014
389
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. detectclient.py
  3.  
  4. Author: noway421
  5.  
  6. """
  7.  
  8. from pyspades.constants import *
  9. from pyspades.loaders import Loader
  10. from pyspades.bytes import ByteReader, ByteWriter
  11. from twisted.internet.reactor import callLater
  12.  
  13. from collections import namedtuple
  14. import itertools
  15.  
  16. id_iter = itertools.count(31)  # http://github.com/yvt/openspades/commit/aa62a0
  17.  
  18.  
  19. class HandShakeInit(Loader):
  20.     id = id_iter.next()
  21.  
  22.     def read(self, reader):
  23.         pass
  24.  
  25.     def write(self, writer):
  26.         writer.writeByte(self.id, True)
  27.         writer.writeInt(42, True)
  28.  
  29.  
  30. class HandShakeReturn(Loader):
  31.     id = id_iter.next()
  32.  
  33.     challenge_passed = -1
  34.  
  35.     def read(self, reader):
  36.         tmp = reader.readInt(True)
  37.         if tmp == 42:
  38.             self.challenge_passed = 1
  39.         else:
  40.             self.challenge_passed = 0
  41.  
  42.     def write(self, writer):
  43.         writer.writeByte(self.id, True)
  44.  
  45.  
  46. class VersionGet(Loader):
  47.     id = id_iter.next()
  48.  
  49.     def read(self, reader):
  50.         pass
  51.  
  52.     def write(self, writer):
  53.         writer.writeByte(self.id, True)
  54.  
  55.  
  56. class VersionSend(Loader):
  57.     id = id_iter.next()
  58.  
  59.     client = ord('-')
  60.     version_major = -1
  61.     version_minor = -1
  62.     version_revision = -1
  63.     version_info = 'None'
  64.  
  65.     def read(self, reader):
  66.         self.client = reader.readByte(True)
  67.         self.version_major = reader.readByte(True)
  68.         self.version_minor = reader.readByte(True)
  69.         self.version_revision = reader.readByte(True)
  70.         self.version_info = reader.readString()
  71.  
  72.     def write(self, writer):
  73.         writer.writeByte(self.id, True)
  74.  
  75.  
  76. def apply_script(protocol, connection, config):
  77.  
  78.     class DetectclientConnection(connection):
  79.         def loader_received(self, loader):
  80.             if self.player_id is not None:
  81.                 if self.hp:  # atleast player spawned
  82.                     data = ByteReader(loader.data)
  83.                     packet_id = data.readByte(True)
  84.                     if packet_id == HandShakeReturn.id:
  85.                         handshake_return = HandShakeReturn()
  86.                         handshake_return.read(data)
  87.                         self.on_handshake_answer()
  88.                         return None
  89.                     elif packet_id == VersionSend.id:
  90.                         version_send = VersionSend()
  91.                         version_send.read(data)
  92.                         self.on_version_answer(version_send)
  93.                         return None
  94.             return connection.loader_received(self, loader)
  95.  
  96.         def on_spawn(self, pos):
  97.             self.protocol.send_contained(HandShakeInit(), save=True)
  98.             self.handshake_timer = callLater(1.4, self.handshake_timeout)
  99.             return connection.on_spawn(self, pos)
  100.  
  101.         def handshake_timeout(self):
  102.             # just assume it's vanilla
  103.             info = namedtuple(
  104.                 'Info',
  105.                 'client, version_major, version_minor, ' +
  106.                 'version_revision, version_info')
  107.             info.client = ord('a')
  108.             info.version_major = 0
  109.             info.version_minor = 75
  110.             info.version_revision = 0
  111.             info.version_info = 'Windows'
  112.             self.on_version_get(info)
  113.  
  114.         def on_handshake_answer(self):
  115.             if self.handshake_timer.active():
  116.                 self.handshake_timer.cancel()
  117.             self.protocol.send_contained(VersionGet(), save=True)
  118.  
  119.         def on_version_answer(self, info):
  120.             self.on_version_get(info)
  121.  
  122.         def on_version_get(self, info):
  123.             self.send_chat(
  124.                 'You are running ' +
  125.                 ('Ace of Spades' if chr(info.client) == 'a' else
  126.                     'OpenSpades' if chr(info.client) == 'o' else
  127.                     'Unknown') + ' v' +
  128.                 str(info.version_major) + '.' +
  129.                 str(info.version_minor) + '.' +
  130.                 str(info.version_revision) + ' on ' +
  131.                 str(info.version_info))
  132.  
  133.     class DetectclientProtocol(protocol):
  134.         def __init__(self, *arg, **kw):
  135.             return protocol.__init__(self, *arg, **kw)
  136.  
  137.     return DetectclientProtocol, DetectclientConnection
RAW Paste Data