2013-04-08 19:46:42 +02:00
|
|
|
module NSCA
|
|
|
|
class Server
|
2013-04-08 20:38:11 +02:00
|
|
|
include Enumerable
|
|
|
|
|
2015-07-03 15:26:42 +02:00
|
|
|
attr_reader :iv_key, :server, :packet_version, :password, :xor_iv_key, :xor_password
|
2013-04-08 19:46:42 +02:00
|
|
|
def initialize *args
|
|
|
|
opts = {}
|
|
|
|
opts = args.pop.dup if args.last.is_a? Hash
|
|
|
|
opts[:host] ||= opts[:hostname]
|
2015-07-03 15:26:42 +02:00
|
|
|
opts[:serv] ||= opts[:server]
|
2013-04-08 19:46:42 +02:00
|
|
|
opts[:pass] ||= opts[:password]
|
|
|
|
|
|
|
|
case args[0]
|
|
|
|
when Integer
|
|
|
|
opts[:port] = args[0]
|
|
|
|
opts[:host] ||= args[1]
|
|
|
|
when IO
|
2015-07-03 15:26:42 +02:00
|
|
|
opts[:serv] = args[0]
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@packet_version = opts[:packet_version] || PacketV3
|
|
|
|
@iv_key = (opts[:iv_key] || SecureRandom.random_bytes( 128)).to_s
|
|
|
|
raise ArgumentError, "Key must be 128 bytes long" unless 128 == @iv_key.length
|
|
|
|
@password = opts[:pass].to_s
|
|
|
|
@server = if opts[:serv].is_a?( TCPServer) or opts[:serv].is_a?( UNIXServer)
|
|
|
|
opts[:serv]
|
|
|
|
elsif opts[:port].is_a? Integer
|
2015-07-03 15:26:42 +02:00
|
|
|
TCPServer.new *[opts[:host], opts[:port]].compact
|
2013-04-08 19:46:42 +02:00
|
|
|
else
|
|
|
|
raise ArgumentError, "Server or port-number expected"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def accept() Connection.new @server.accept, self end
|
|
|
|
def close() @server.close end
|
|
|
|
|
2013-04-08 20:38:11 +02:00
|
|
|
def each &block
|
|
|
|
return Enumerator.new( self) unless block_given?
|
|
|
|
while conn = accept
|
|
|
|
yield conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-08 19:46:42 +02:00
|
|
|
class Connection
|
2013-04-08 20:38:11 +02:00
|
|
|
include Enumerable
|
|
|
|
|
2013-04-08 19:46:42 +02:00
|
|
|
def initialize socket, server
|
|
|
|
@socket, @server = socket, server
|
|
|
|
@iv_key, @password = server.iv_key, server.password
|
|
|
|
@packet_version = server.packet_version
|
|
|
|
@packet_length = @packet_version::PACK_LENGTH
|
|
|
|
@socket.write [@iv_key, Time.now.to_i].pack( 'a* L>')
|
2015-07-03 15:26:42 +02:00
|
|
|
@xor_password = NSCA::Helper.xor_stream @password
|
|
|
|
@xor_iv_key = NSCA::Helper.xor_stream @iv_key
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch
|
2015-07-03 15:26:42 +02:00
|
|
|
iv_key = NSCA::Helper.xor_stream @iv_key
|
|
|
|
password = NSCA::Helper.xor_stream @password
|
|
|
|
packet_version = iv_key[ password[ read PacketV3::PACKET_VERSION]]
|
|
|
|
v = packet_version.unpack( 's>').first
|
|
|
|
case v
|
|
|
|
when 3
|
|
|
|
data = packet_version + iv_key[ password[ read( PacketV3::PACK_LENGTH - PacketV3::PACKET_VERSION)]]
|
|
|
|
begin
|
|
|
|
return PacketV3.parse( data)
|
|
|
|
rescue NSCA::Packet::CSC32CheckFailed
|
|
|
|
x = read( PacketV3__2_9::PACK_LENGTH - data.length)
|
|
|
|
raise if x.nil?
|
|
|
|
return PacketV3__2_9.parse( data + iv_key[ password[ x]])
|
|
|
|
end
|
|
|
|
else raise "Unknown Version #{v.inspect}"
|
|
|
|
end
|
2013-04-08 19:46:42 +02:00
|
|
|
end
|
|
|
|
|
2013-04-08 20:38:11 +02:00
|
|
|
def each &block
|
|
|
|
return Enumerator.new( self) unless block_given?
|
2015-07-03 15:26:42 +02:00
|
|
|
yield fetch until eof?
|
2013-04-08 20:38:11 +02:00
|
|
|
end
|
|
|
|
|
2013-04-08 19:46:42 +02:00
|
|
|
def eof?() @socket.eof? end
|
2015-07-03 15:26:42 +02:00
|
|
|
def read( len = nil) @socket.read( len || @packet_length) end
|
2013-04-08 19:46:42 +02:00
|
|
|
def close() @socket.close end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|