Class: Ferrum::Proxy
- Inherits:
-
Object
- Object
- Ferrum::Proxy
- Defined in:
- lib/ferrum/proxy.rb
Overview
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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.start(**args) ⇒ Proxy
Builds a new proxy server and starts it.
Instance Method Summary collapse
-
#initialize(host: "127.0.0.1", port: 0, user: nil, password: nil) ⇒ Proxy
constructor
A new instance of Proxy.
-
#rotate(host:, port:, user: nil, password: nil) ⇒ void
Changes the upstream proxy the server forwards connections to.
-
#start ⇒ void
Starts the WEBrick proxy server.
-
#stop ⇒ void
Stops the proxy server and removes the htpasswd file.
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
36 37 38 |
# File 'lib/ferrum/proxy.rb', line 36 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
36 37 38 |
# File 'lib/ferrum/proxy.rb', line 36 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
36 37 38 |
# File 'lib/ferrum/proxy.rb', line 36 def port @port end |
#user ⇒ Object (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.
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.
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 |
#start ⇒ void
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 = { 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)) .merge!(ProxyAuthProc: authenticator.method(:authenticate).to_proc) end @server = HTTPProxyServer.new(**) @server.start at_exit { stop } @port = @server.config[:Port] end |
#stop ⇒ void
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 |