Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/quake/message.rb
Views: 11704
# -*- coding: binary -*-12module Rex3module Proto4##5#6# Quake 3 protocol, taken from ftp://ftp.idsoftware.com/idstuff/quake3/docs/server.txt7#8##9module Quake::Message10HEADER = 0xFFFFFFFF1112def decode_message(message)13# minimum size is header (4) + <command> + <stuff>14return if message.length < 715header = message.unpack('N')[0]16return if header != HEADER17message[4, message.length]18end1920def encode_message(payload)21[HEADER].pack('N') + payload22end2324def getstatus25encode_message('getstatus')26end2728def getinfo29encode_message('getinfo')30end3132def decode_infostring(infostring)33# decode an "infostring", which is just a (supposedly) quoted string of tokens separated34# by backslashes, generally terminated with a newline35token_re = /([^\\]+)\\([^\\]+)/36return nil unless infostring =~ token_re37# remove possibly present leading/trailing double quote38infostring.gsub!(/(?:^"|"$)/, '')39# remove the trailing \n, if present40infostring.gsub!(/\n$/, '')41# split on backslashes and group into key value pairs42infohash = {}43infostring.scan(token_re).each do |kv|44infohash[kv.first] = kv.last45end46infohash47end4849def decode_response(message, type)50resp = decode_message(message)51if /^print\n(?<error>.*)\n?/m =~ resp52# XXX: is there a better exception to throw here?53fail ::ArgumentError, "#{type} error: #{error}"54# why doesn't this work?55# elsif /^#{type}Response\n(?<infostring>.*)/m =~ resp56elsif resp =~ /^#{type}Response\n(.*)/m57decode_infostring(Regexp.last_match(1))58else59nil60end61end6263def decode_status(message)64decode_response(message, 'status')65end6667def decode_info(message)68decode_response(message, 'info')69end70end71end72end737475