Method: Ferrum::Proxy#start

Defined in:
lib/ferrum/proxy.rb

#startObject



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

def start
  options = {
    ProxyURI: nil, ServerType: Thread,
    Logger: Logger.new(IO::NULL), AccessLog: [],
    BindAddress: host, Port: port
  }

  if user && password
    @file = Tempfile.new("htpasswd")
    htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@file.path)
    htpasswd.set_passwd "Proxy Realm", user, password
    htpasswd.flush
    authenticator = WEBrick::HTTPAuth::ProxyBasicAuth.new(Realm: "Proxy Realm",
                                                          UserDB: htpasswd,
                                                          Logger: Logger.new(IO::NULL))
    options.merge!(ProxyAuthProc: authenticator.method(:authenticate).to_proc)
  end

  @server = HTTPProxyServer.new(**options)
  @server.start
  at_exit { stop }

  @port = @server.config[:Port]
end