Sha256: 2c7e2188dd54efca85a0a5fe77e239e1b917f0f0ac7d523540969b81ce38b563

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

#!/usr/bin/env ruby
# 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.unpack("m*")[0].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 "Basic" == type && match_credentials.call(credentials)
        raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
      end
    end
  }
}

proxy = WEBrick::HTTPProxyServer.new(webrick_opts)

trap(:TERM) { proxy.shutdown }
trap(:INT) { proxy.shutdown }

proxy.start

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
faraday-0.8.11 script/proxy-server
faraday-0.8.10 script/proxy-server
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/faraday-0.8.9/script/proxy-server
faraday-0.8.9 script/proxy-server
faraday-0.8.8 script/proxy-server