gsquery.py 3.8 KB

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