File: proxy-server

package info (click to toggle)
ruby-faraday 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 508 kB
  • ctags: 836
  • sloc: ruby: 4,882; sh: 138; makefile: 5
file content (42 lines) | stat: -rwxr-xr-x 1,133 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/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)
        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