module Nova::Starbound::Protocol::Messages

Handles messages.

Public Instance Methods

check_versions(other_packet) click to toggle source

Checks the versions of the remote and us. If the major versions don’t match, raise an error.

@raise [IncompatibleRemoteError] if the major versions don’t

match.

@return [void]

# File lib/nova/starbound/protocol/messages.rb, line 14
def check_versions(other_packet)
  other_packet.expect(:protocol_version)
  our_major = Nova::VERSION.split('.').first
  their_major = other_packet.body.split('.').first

  if our_major != their_major
    raise IncompatibleRemoteError,
      "Major versions do not match (our: #{our_major}, " +
      "theirs: #{their_major})"
  end
end
handle_encryption() click to toggle source

Handles setting up encryption with the server. Sends the server the list of options we have, and waits for a response. The first 4 bytes of the public key correspond to the index of the encryptor, and the rest is the actual public key. It then sends our public key back, but doesn’t wait for a response.

@return [void]

# File lib/nova/starbound/protocol/messages.rb, line 49
def handle_encryption
  sent = send :encryption_options,
    Encryptor.sorted_encryptors.map(&:encryptor_name).join("\n")

  response = response_to sent
  response.expect(:public_key)

  encryptor = matching_encryptor *response.body.split("\n", 2)

  respond_to response, :public_key, encryptor.public_key
  self.encryption_provider = encryptor
end
handle_server_encryption() click to toggle source

Handles the server encryption. Reads a packet, splits the body by new lines, and searches our encryptors for a matching encryptor. Initializes it, and sends back data to the client containing which encryptor was used and our public key; waits for a response containing the client’s public key, and then sets the encryption provider.

@return [void]

# File lib/nova/starbound/protocol/messages.rb, line 70
def handle_server_encryption
  enc_options = read
  enc_options.expect(:encryption_options)
  lines = enc_options.body.split("\n")

  encs = Encryptor.sorted_encryptors.select do |e|
    lines.include?(e.encryptor_name)
  end

  preferred = encs.first
  index = lines.index(preferred.encryptor_name)
  encryptor = preferred.new
  encryptor.private_key!

  data = preferred.encryptor_name + "\n"

  out = respond_to enc_options, :public_key, data +
    encryptor.public_key

  pub_key = response_to out
  pub_key.expect(:public_key)

  encryptor.other_public_key = pub_key.body

  self.encryption_provider = encryptor
end
wait_for_protocol_version() click to toggle source

Waits for the remote to send their protocol version, and then checks their version against ours, making sure the versions match.

@raise [IncompatibleRemoteError] if the major versions don’t

match.

@return [void]

# File lib/nova/starbound/protocol/messages.rb, line 33
def wait_for_protocol_version
  pack = read

  check_versions(pack)

  respond_to pack, :protocol_version, Nova::VERSION
end

Private Instance Methods

matching_encryptor(name, body) click to toggle source

Handles selecting and setting up the encryption for this protocol, given the name from the remote.

@return [Encryptor]

# File lib/nova/starbound/protocol/messages.rb, line 103
def matching_encryptor(name, body)
  enc = Encryptor.encryptors.select { |e|
    e.encryptor_name == name
  }.first.new
  enc.private_key!
  enc.other_public_key = body

  enc
end