gsquery.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Game Server Query
  4. # Author: Anonymous & Daniel Gibbs
  5. # Website: https://gameservermanagers.com
  6. # Description: Handles querying of .
  7. import optparse
  8. import socket
  9. import sys
  10. class GameServer:
  11. def __init__(self, options, arguments):
  12. self.option = options
  13. self.argument = arguments
  14. #
  15. self.server_response_timeout = 5
  16. self.default_buffer_length = 1024
  17. #
  18. if self.option.engine == 'avalanche':
  19. self.query_prompt_string = b'\xFE\xFD\x09\x10\x20\x30\x40'
  20. elif self.option.engine == 'goldsource':
  21. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  22. elif self.option.engine == 'idtech2':
  23. self.query_prompt_string = b'\xff\xff\xff\xffstatus\x00'
  24. elif self.option.engine == 'idtech3':
  25. self.query_prompt_string = b'\xff\xff\xff\xffgetstatus'
  26. elif self.option.engine == 'iw2.0':
  27. self.query_prompt_string = b'\xff\xff\xff\xffgetstatus'
  28. elif self.option.engine == 'iw3.0':
  29. self.query_prompt_string = b'\xff\xff\xff\xffgetstatus'
  30. elif self.option.engine == 'quake':
  31. self.query_prompt_string = b'\xff\xff\xff\xffstatus\x00'
  32. elif self.option.engine == 'quakelive':
  33. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  34. elif self.option.engine == 'realvirtuality':
  35. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  36. elif self.option.engine == 'refractor':
  37. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  38. elif self.option.engine == 'source':
  39. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  40. elif self.option.engine == 'spark':
  41. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  42. elif self.option.engine == 'unity3d':
  43. self.query_prompt_string = '\xFF\xFF\xFF\xFFTSource Engine Query\0'
  44. elif self.option.engine == 'unreal':
  45. self.query_prompt_string = b'\x5C\x69\x6E\x66\x6F\x5C'
  46. elif self.option.engine == 'unreal2':
  47. self.query_prompt_string = b'\x79\x00\x00\x00\x00'
  48. self.connected = False
  49. self.response = None
  50. self.sanity_checks()
  51. def fatal_error(self, error_message, error_code=1):
  52. sys.stderr.write('ERROR: ' + str(error_message) + '\n')
  53. sys.exit(error_code)
  54. def exit_success(self, success_message=''):
  55. sys.stdout.write('OK: ' + str(success_message) + '\n')
  56. sys.exit(0)
  57. def responding(self):
  58. # Connect.
  59. connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  60. connection.settimeout(self.server_response_timeout)
  61. try:
  62. self.connected = connection.connect((self.option.address, int(self.option.port)))
  63. except socket.timeout:
  64. self.fatal_error('Request timed out', 1)
  65. except:
  66. self.fatal_error('Unable to connect', 1)
  67. # Send.
  68. connection.send(self.query_prompt_string)
  69. # Receive.
  70. try:
  71. self.response = connection.recv(self.default_buffer_length)
  72. except socket.error:
  73. self.fatal_error('Unable to receive', 2)
  74. connection.close()
  75. # Response.
  76. if self.response is None:
  77. self.fatal_error('No response', 3)
  78. if len(self.response) < 10:
  79. sys.exit('Short response.', 3)
  80. else:
  81. self.exit_success(str(self.response))
  82. def sanity_checks(self):
  83. if not self.option.address:
  84. self.fatal_error('No IPv4 address supplied.', 4)
  85. if not self.option.port:
  86. self.fatal_error('No port supplied.', 4)
  87. if __name__ == '__main__':
  88. parser = optparse.OptionParser(
  89. usage='usage: python %prog [options]',
  90. version='%prog 0.0.1'
  91. )
  92. parser.add_option(
  93. '-a', '--address',
  94. action='store',
  95. dest='address',
  96. default=False,
  97. help='The IPv4 address of the server.'
  98. )
  99. parser.add_option(
  100. '-p', '--port',
  101. action='store',
  102. dest='port',
  103. default=False,
  104. help='The IPv4 port of the server.'
  105. )
  106. parser.add_option(
  107. '-e', '--engine',
  108. action='store',
  109. dest='engine',
  110. default=False,
  111. help='Engine type: avalanche, goldsource, idtech2, idtech3, iw2.0, iw3.0, realvirtuality, quake, quakelive, refractor, spark, source, unity3d, unreal, unreal2.'
  112. )
  113. parser.add_option(
  114. '-v', '--verbose',
  115. action='store_true',
  116. dest='verbose',
  117. default=False,
  118. help='Display verbose output.'
  119. )
  120. parser.add_option(
  121. '-d', '--debug',
  122. action='store_true',
  123. dest='debug',
  124. default=False,
  125. help='Display debugging output.'
  126. )
  127. options, arguments = parser.parse_args()
  128. #
  129. server = GameServer(options, arguments)
  130. server.responding()