Module: RTunnel::CommandProtocol

Includes:
FrameProtocol
Included in:
RTunnel::Client::ServerConnection, Server::ControlConnection
Defined in:
lib/rtunnel/command_protocol.rb

Instance Method Summary collapse

Methods included from FrameProtocol

#receive_data, #send_frame

Instance Method Details

#incoming_command_hasher=(hasher) ⇒ Object

Sets a cryptographic hasher that will be used to verify incoming commands. Once a hasher is set, all incoming frames without a matching signature will be ignored.



42
43
44
# File 'lib/rtunnel/command_protocol.rb', line 42

def incoming_command_hasher=(hasher)
  @in_command_hasher = hasher
end

#outgoing_command_hasher=(hasher) ⇒ Object

Sets a cryptographic hasher that will be used to sign outgoing commands. Once a hasher is set, all outgoing frames will be signed.



35
36
37
# File 'lib/rtunnel/command_protocol.rb', line 35

def outgoing_command_hasher=(hasher)
  @out_command_hasher = hasher
end

#receive_bad_frame(frame, exception) ⇒ Object

Override to handle frames with corrupted or absent signatures.



47
48
49
# File 'lib/rtunnel/command_protocol.rb', line 47

def receive_bad_frame(frame, exception)
  nil
end

#receive_frame(frame) ⇒ Object

Decodes a frame into an RTunnel command.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rtunnel/command_protocol.rb', line 15

def receive_frame(frame)
  ioframe = StringIO.new frame
  begin
    command = RTunnel::Command.decode ioframe
  rescue Exception => e
    receive_bad_frame frame, e
    return
  end
  if @in_command_hasher
    signature = ioframe.read
    if signature != @in_command_hasher.hash(frame[0...(-signature.length)])
      receive_bad_frame frame, :bad_signature
      return
    end
  end
  receive_command command
end

#send_command(command) ⇒ Object

Sends an encoded RTunnel command as a frame.



5
6
7
8
9
10
11
12
# File 'lib/rtunnel/command_protocol.rb', line 5

def send_command(command)    
  command_str = command.to_encoded_str
  if @out_command_hasher
    send_frame command_str + @out_command_hasher.hash(command_str)
  else
    send_frame command_str
  end
end