faraday/script/proxy-server
Alexander Popov c26df87b86 Update RuboCop
Require its specific version in `Gemfile`.

Fix it's installation in CI.

Enable new cops.

Resolve new offenses.

Drop support of Ruby 2.3
(it's not supported by RuboCop and by MRI:https://www.ruby-lang.org/en/news/2019/03/31/support-of-ruby-2-3-has-ended/)

Deprecate `Faraday::Request#method`, replace it with `#http_method`.
2020-04-19 17:44:07 +02:00

46 lines
1.2 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# frozen_string_literal: true
# Usage: script/proxy-server [-p PORT] [-u USER:PASSWORD]
require 'webrick'
require 'webrick/httpproxy'
port = 4001
if (found = ARGV.index('-p'))
port = ARGV[found + 1].to_i
end
if (found = ARGV.index('-u'))
username, password = ARGV[found + 1].split(':', 2)
end
match_credentials = lambda { |credentials|
got_username, got_password = credentials.to_s.unpack1('m*').split(':', 2)
got_username == username && got_password == password
}
log_io = $stdout
log_io.sync = true
webrick_opts = {
Port: port, Logger: WEBrick::Log.new(log_io),
AccessLog: [[log_io, '[%{X-Faraday-Adapter}i] %m %U -> %s %b']],
ProxyAuthProc: lambda { |req, res|
if username
type, credentials = req.header['proxy-authorization']
.first.to_s.split(/\s+/, 2)
unless type == 'Basic' && match_credentials.call(credentials)
res['proxy-authenticate'] = %(Basic realm="testing")
raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
end
end
}
}
proxy = WEBrick::HTTPProxyServer.new(webrick_opts)
trap(:TERM) { proxy.shutdown }
trap(:INT) { proxy.shutdown }
proxy.start