class Mallory::Connection

Public Class Methods

new(request_builder, proxy_builder, logger, certificate_manager) click to toggle source
# File lib/mallory/connection.rb, line 8
def initialize(request_builder, proxy_builder, logger, certificate_manager)
  @logger = logger
  @request_builder = request_builder
  @proxy_builder = proxy_builder
  @certificate_manager = certificate_manager
  @start = Time.now
  @secure = false
  @proto = "http"
end

Public Instance Methods

error() click to toggle source
# File lib/mallory/connection.rb, line 31
def error
  @logger.info "Failure in #{Time.now-@start}s"
  send_data "HTTP/1.1 500 Internal Server Error\nContent-Type: text/html\nConnection: close\n\n"
  close_connection_after_writing
end
post_init() click to toggle source
# File lib/mallory/connection.rb, line 23
def post_init # EM::Connection
  @logger.debug "Start connection"
end
receive_data(data) click to toggle source
# File lib/mallory/connection.rb, line 37
def receive_data(data) # EM::Connection
  begin
  request = @request_builder.build(data)
  rescue
    error
    return
  end
  if not @secure and request.method.eql?('connect')
    #TEMPORARY FIXME
    cc = @certificate_manager.get(request.host)
    ca = File.read("./keys/ca.crt")
    private_key_file = Tempfile.new('private_key_file')
    private_key_file.write (cc.key)
    private_key_file.close()
    cert_chain_file = Tempfile.new('cert_chain_file')
    cert_chain_file.write (cc.cert + ca)
    cert_chain_file.close()
    send_data "HTTP/1.0 200 Connection established\r\n\r\n"
    start_tls :private_key_file => private_key_file.path, :cert_chain_file => cert_chain_file.path, :verify_peer => true
    return true
  end
  proxy = @proxy_builder.build
  proxy.callback {
    send_data proxy.response
    close_connection_after_writing
  }
  proxy.errback {
    error
  }
  request.protocol = 'https' if @secure
  proxy.perform(request)
end
ssl_handshake_completed() click to toggle source
# File lib/mallory/connection.rb, line 18
def ssl_handshake_completed # EM::Connection
  @logger.debug "Secure connection intercepted"
  @secure = true
end
unbind(reason=nil) click to toggle source
# File lib/mallory/connection.rb, line 27
def unbind(reason=nil) # EM::Connection
  @logger.debug "Close connection #{reason}"
end