gsquery.py 4.6 KB

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