Class: Ferrum::Proxy

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

Overview

Note:

Requires the webrick gem, which isn't a hard dependency of Ferrum; add it to your Gemfile to use this class.

A local WEBrick-based proxy server, useful for injecting HTTP Basic-Auth credentials into requests or forwarding to (and rotating between) upstream proxies, cases the browser's own proxy support can't handle directly.

Defined Under Namespace

Classes: HTTPProxyServer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: "127.0.0.1", port: 0, user: nil, password: nil) ⇒ Proxy

Returns a new instance of Proxy.



38
39
40
41
42
43
44
# File 'lib/ferrum/proxy.rb', line 38

def initialize(host: "127.0.0.1", port: 0, user: nil, password: nil)
  @file = nil
  @host = host
  @port = port
  @user = user
  @password = password
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



36
37
38
# File 'lib/ferrum/proxy.rb', line 36

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



36
37
38
# File 'lib/ferrum/proxy.rb', line 36

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



36
37
38
# File 'lib/ferrum/proxy.rb', line 36

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



36
37
38
# File 'lib/ferrum/proxy.rb', line 36

def user
  @user
end

Class Method Details

.start(**args) ⇒ Proxy

Builds a new proxy server and starts it.

Parameters:

  • args (Hash)

    Keyword arguments forwarded to #initialize.

Returns:



32
33
34
# File 'lib/ferrum/proxy.rb', line 32

def self.start(**args)
  new(**args).tap(&:start)
end

Instance Method Details

#rotate(host:, port:, user: nil, password: nil) ⇒ void

This method returns an undefined value.

Changes the upstream proxy the server forwards connections to.

Parameters:

  • host (String)

    Address of the upstream proxy.

  • port (Integer)

    Port of the upstream proxy.

  • user (String, nil) (defaults to: nil)

    Username for upstream proxy authentication.

  • password (String, nil) (defaults to: nil)

    Password for upstream proxy authentication.



93
94
95
96
97
# File 'lib/ferrum/proxy.rb', line 93

def rotate(host:, port:, user: nil, password: nil)
  credentials = "#{user}:#{password}@" if user && password
  proxy_uri = "schema://#{credentials}#{host}:#{port}"
  @server.config[:ProxyURI] = URI.parse(proxy_uri)
end

#startvoid

This method returns an undefined value.

Starts the WEBrick proxy server.



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

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

#stopvoid

This method returns an undefined value.

Stops the proxy server and removes the htpasswd file.



104
105
106
107
# File 'lib/ferrum/proxy.rb', line 104

def stop
  @file&.close(true)
  @server.shutdown
end