Sha256: 25f216ae2de7e461fca1d905494cf7ea1ac36301aca71e409dd9a79b07894a1c

Contents?: true

Size: 1.97 KB

Versions: 10

Compression:

Stored size: 1.97 KB

Contents

# The message encoder handles reading/writing the message to/from the socket.
# This includes encrypting and formatting.
module Volt
  module MessageBus
    class MessageEncoder
      attr_reader :encrypted
      def initialize
        # rbnacl is not supported on windows.
        windows = Gem.win_platform?

        if windows
          Volt.logger.warn('Currently Message Bus encryption is not supported on windows.')
        end

        # Message bus is encrypted by default
        disable = Volt.config.message_bus.try(:disable_encryption)
        @encrypted = !windows && (disable != true)

        if @encrypted
          # Setup a RbNaCl simple box for handling encryption
          require 'base64'
          begin
            require 'rbnacl/libsodium'
          rescue LoadError => e
          # Ignore, incase they have libsodium installed locally
          end

          begin
            require 'rbnacl'
          rescue LoadError => e
            Volt.logger.error('Volt requires the rbnacl gem to enable encryption on the message bus.  Add it to the gemfile (and rbnacl-sodium if you don\'t have libsodium installed locally')
            raise e
          end

          # use the first 32 chars of the app secret for the encryption key.
          key = Base64.decode64(Volt.config.app_secret)[0..31]

          @encrypt_box = RbNaCl::SimpleBox.from_secret_key(key)
        end
      end

      def encrypt(message)
        if @encrypted
          @encrypt_box.encrypt(message)
        else
          message
        end
      end

      def decrypt(message)
        if @encrypted
          @encrypt_box.decrypt(message)
        else
          message
        end
      end

      def send_message(io, message)
        Marshal.dump(encrypt(message), io)
      end

      def receive_message(io)
        begin
          decrypt(Marshal.load(io))
        rescue EOFError => e
          # We get EOFError when the connection closes, return nil
          nil
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
volt-0.9.4 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.4.pre5 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.4.pre3 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.4.pre2 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.4.pre1 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.3 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.3.pre6 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.3.pre5 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.3.pre4 lib/volt/server/message_bus/message_encoder.rb
volt-0.9.3.pre3 lib/volt/server/message_bus/message_encoder.rb