Class: BetterCap::Proxy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/proxy/proxy.rb

Overview

Transparent proxy class.

Instance Method Summary collapse

Constructor Details

#initialize(address, port, is_https, processor) ⇒ Proxy

Initialize the transparent proxy, making it listen on address:port and use the specified processor routine for each request. If is_https is true a HTTPS proxy will be created, otherwise a HTTP one.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bettercap/proxy/proxy.rb', line 23

def initialize( address, port, is_https, processor )
  @socket      = nil
  @address     = address
  @port        = port
  @is_https    = is_https
  @type        = is_https ? 'HTTPS' : 'HTTP'
  @sslserver   = nil
  @sslcontext  = nil
  @server      = nil
  @main_thread = nil
  @running     = false
  @streamer    = Streamer.new processor
  @local_ips   = []

  begin
    @local_ips = Socket.ip_address_list.collect { |x| x.ip_address }
  rescue
    Logger.warn 'Could not get local ips using Socket module, using Network.get_local_ips method.'

    @local_ips = Network.get_local_ips
  end

  BasicSocket.do_not_reverse_lookup = true

  @pool = ThreadPool.new( 4, 16 ) do |client|
     client_worker client
  end
end

Instance Method Details

#startObject

Start this proxy instance.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bettercap/proxy/proxy.rb', line 53

def start
  begin
    @server = @socket = TCPServer.new( @address, @port )

    if @is_https
      cert = Context.get.certificate

      @sslcontext      = OpenSSL::SSL::SSLContext.new
      @sslcontext.cert = cert[:cert]
      @sslcontext.key  = cert[:key]

      @server = @sslserver = OpenSSL::SSL::SSLServer.new( @socket, @sslcontext )
    end

    @main_thread = Thread.new &method(:server_thread)
  rescue Exception => e
    Logger.error "Error starting #{@type} proxy: #{e.inspect}"
    @socket.close unless @socket.nil?
  end
end

#stopObject

Stop this proxy instance.


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bettercap/proxy/proxy.rb', line 75

def stop
  begin
    Logger.info "Stopping #{@type} proxy ..."

    if @socket and @running
      @running = false
      @socket.close
      @pool.shutdown false
    end
  rescue
  end
end