gsquery.py 4.8 KB

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