query_gsquery.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # query_gsquery.py
  4. # Author: Anonymous & Daniel Gibbs
  5. # Website: https://linuxgsm.com
  6. # Description: Allows querying of various game servers.
  7. import optparse
  8. import socket
  9. import sys
  10. class gsquery:
  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. sourcequery=['protocol-valve','avalanche3.0','barotrauma','madness','quakelive','realvirtuality','refractor','source','goldsource','spark','starbound','unity3d','unreal4','wurm']
  19. idtech2query=['protocol-quake3','idtech2','quake','iw2.0']
  20. idtech3query=['protocol-quake3','iw3.0','ioquake3','qfusion']
  21. minecraftquery=['minecraft','lwjgl2']
  22. jc2mpquery=['jc2mp']
  23. mumblequery=['mumbleping']
  24. twquery=['teeworlds']
  25. unrealquery=['protocol-gamespy1','unreal']
  26. unreal2query=['protocol-unreal2','unreal2']
  27. if self.option.engine in sourcequery:
  28. self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
  29. elif self.option.engine in idtech2query:
  30. self.query_prompt_string = b'\xff\xff\xff\xffstatus\x00'
  31. elif self.option.engine in idtech3query:
  32. self.query_prompt_string = b'\xff\xff\xff\xffgetstatus'
  33. elif self.option.engine in minecraftquery:
  34. self.query_prompt_string = b'\xFE\xFD\x09\x3d\x54\x1f\x93'
  35. elif self.option.engine in jc2mpquery:
  36. self.query_prompt_string = b'\xFE\xFD\x09\x10\x20\x30\x40'
  37. elif self.option.engine in mumblequery:
  38. self.query_prompt_string = b'\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08'
  39. elif self.option.engine in unrealquery:
  40. self.query_prompt_string = b'\x5C\x69\x6E\x66\x6F\x5C'
  41. elif self.option.engine in unreal2query:
  42. self.query_prompt_string = b'\x79\x00\x00\x00\x00'
  43. elif self.option.engine in twquery:
  44. self.query_prompt_string = b"\x04\x00\x00\xff\xff\xff\xff\x05" + bytearray(511)
  45. self.connected = False
  46. self.response = None
  47. self.sanity_checks()
  48. def fatal_error(self, error_message, error_code=1):
  49. sys.stderr.write('ERROR: ' + str(error_message) + '\n')
  50. sys.exit(error_code)
  51. def exit_success(self, success_message=''):
  52. sys.stdout.write('OK: ' + str(success_message) + '\n')
  53. sys.exit(0)
  54. def responding(self):
  55. # Connect.
  56. connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  57. connection.settimeout(self.server_response_timeout)
  58. try:
  59. self.connected = connection.connect((self.option.address, int(self.option.port)))
  60. except socket.timeout:
  61. self.fatal_error('Request timed out', 1)
  62. except:
  63. self.fatal_error('Unable to connect', 1)
  64. # Send.
  65. connection.send(self.query_prompt_string)
  66. # Receive.
  67. try:
  68. self.response = connection.recv(self.default_buffer_length)
  69. except socket.error:
  70. self.fatal_error('Unable to receive', 2)
  71. connection.close()
  72. # Response.
  73. if self.response is None:
  74. self.fatal_error('No response', 3)
  75. if len(self.response) < 10:
  76. sys.exit('Short response.', 3)
  77. else:
  78. self.exit_success(str(self.response))
  79. def sanity_checks(self):
  80. if not self.option.address:
  81. self.fatal_error('No IPv4 address supplied.', 4)
  82. if not self.option.port:
  83. self.fatal_error('No port supplied.', 4)
  84. if __name__ == '__main__':
  85. parser = optparse.OptionParser(
  86. usage='usage: python3 %prog [options]',
  87. version='%prog 0.0.1'
  88. )
  89. parser.add_option(
  90. '-a', '--address',
  91. action='store',
  92. dest='address',
  93. default=False,
  94. help='The IPv4 address of the server.'
  95. )
  96. parser.add_option(
  97. '-p', '--port',
  98. action='store',
  99. dest='port',
  100. default=False,
  101. help='The IPv4 port of the server.'
  102. )
  103. parser.add_option(
  104. '-e', '--engine',
  105. action='store',
  106. dest='engine',
  107. default=False,
  108. help='Engine type: protocol-valve protocol-quake3 protocol-quake3 protocol-gamespy1 protocol-unreal2 minecraft jc2mp mumbleping teeworlds'
  109. )
  110. parser.add_option(
  111. '-v', '--verbose',
  112. action='store_true',
  113. dest='verbose',
  114. default=False,
  115. help='Display verbose output.'
  116. )
  117. parser.add_option(
  118. '-d', '--debug',
  119. action='store_true',
  120. dest='debug',
  121. default=False,
  122. help='Display debugging output.'
  123. )
  124. options, arguments = parser.parse_args()
  125. #
  126. server = gsquery(options, arguments)
  127. server.responding()