gsquery.py 3.9 KB

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