gsquery.py 3.6 KB

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