gsquery.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 == 'quake':
  27. self.query_prompt_string = b'\xff\xff\xff\xffstatus\x00'
  28. elif self.option.engine == 'quakelive':
  29. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  30. elif self.option.engine == 'realvirtuality':
  31. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  32. elif self.option.engine == 'refractor':
  33. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  34. elif self.option.engine == 'source':
  35. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  36. elif self.option.engine == 'spark':
  37. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  38. elif self.option.engine == 'unity3d':
  39. self.query_prompt_string = '\xFF\xFF\xFF\xFFTSource Engine Query\0'
  40. elif self.option.engine == 'unreal':
  41. self.query_prompt_string = b'\x5C\x69\x6E\x66\x6F\x5C'
  42. elif self.option.engine == 'unreal2':
  43. self.query_prompt_string = b'\x79\x00\x00\x00\x00'
  44. self.connected = False
  45. self.response = None
  46. self.sanity_checks()
  47. def fatal_error(self, error_message, error_code=1):
  48. sys.stderr.write('ERROR: ' + str(error_message) + '\n')
  49. sys.exit(error_code)
  50. def exit_success(self, success_message=''):
  51. sys.stdout.write('OK: ' + str(success_message) + '\n')
  52. sys.exit(0)
  53. def responding(self):
  54. # Connect.
  55. connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  56. connection.settimeout(self.server_response_timeout)
  57. try:
  58. self.connected = connection.connect((self.option.address, int(self.option.port)))
  59. except socket.timeout:
  60. self.fatal_error('Request timed out', 1)
  61. except:
  62. self.fatal_error('Unable to connect', 1)
  63. # Send.
  64. connection.send(self.query_prompt_string)
  65. # Receive.
  66. try:
  67. self.response = connection.recv(self.default_buffer_length)
  68. except socket.error:
  69. self.fatal_error('Unable to receive', 2)
  70. connection.close()
  71. # Response.
  72. if self.response is None:
  73. self.fatal_error('No response', 3)
  74. if len(self.response) < 10:
  75. sys.exit('Short response.', 3)
  76. else:
  77. self.exit_success(str(self.response))
  78. def sanity_checks(self):
  79. if not self.option.address:
  80. self.fatal_error('No IPv4 address supplied.', 4)
  81. if not self.option.port:
  82. self.fatal_error('No port supplied.', 4)
  83. if __name__ == '__main__':
  84. parser = optparse.OptionParser(
  85. usage='usage: python %prog [options]',
  86. version='%prog 0.0.1'
  87. )
  88. parser.add_option(
  89. '-a', '--address',
  90. action='store',
  91. dest='address',
  92. default=False,
  93. help='The IPv4 address of the server.'
  94. )
  95. parser.add_option(
  96. '-p', '--port',
  97. action='store',
  98. dest='port',
  99. default=False,
  100. help='The IPv4 port of the server.'
  101. )
  102. parser.add_option(
  103. '-e', '--engine',
  104. action='store',
  105. dest='engine',
  106. default=False,
  107. help='Engine type: avalanche, goldsource, idtech2, idtech3, realvirtuality, quake, quakelive, refractor, spark, source, unity3d, unreal, unreal2.'
  108. )
  109. parser.add_option(
  110. '-v', '--verbose',
  111. action='store_true',
  112. dest='verbose',
  113. default=False,
  114. help='Display verbose output.'
  115. )
  116. parser.add_option(
  117. '-d', '--debug',
  118. action='store_true',
  119. dest='debug',
  120. default=False,
  121. help='Display debugging output.'
  122. )
  123. options, arguments = parser.parse_args()
  124. #
  125. server = GameServer(options, arguments)
  126. server.responding()