Class: BetterCap::Proxy::HTTP::Proxy

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

Overview

Transparent HTTP proxy class.

Instance Method Summary collapse

Constructor Details

#initialize(address, port, is_https) ⇒ Proxy

Initialize the transparent proxy, making it listen on address:port. If is_https is true a HTTPS proxy will be created, otherwise a HTTP one.


22
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
51
52
53
54
55
56
57
# File 'lib/bettercap/proxy/http/proxy.rb', line 22

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

  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

  tmin = System.cpu_count
  tmax = tmin * 16

  @pool = ThreadPool.new( tmin, tmax ) do |client|
    begin
     client_worker client
    rescue Exception => e
      Logger.warn "Client worker error: #{e.message}"
      Logger.exception e
    end
  end
end

Instance Method Details

#startObject

Start this proxy instance.


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bettercap/proxy/http/proxy.rb', line 60

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

    if @is_https
      @sslserver = SSL::Server.new( @socket )
      @server    = @sslserver.io
    else
      @server    = @socket
    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.


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bettercap/proxy/http/proxy.rb', line 79

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

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