#!ruby require 'webrick' require 'webrick/httpproxy' require 'uri' require 'yaml' require "pp" require "pathname" $stdout.sync = true begin @config = YAML.load_file("proxy-config.yaml") rescue Errno::ENOENT @config = { "server" => { }, } end pp @config class ArrogationProxyServer < WEBrick::HTTPProxyServer def proxy_service(req, res) super unless arrogation(req, res) rescue Exception => e puts $@ end private def arrogation(req, res) req.header.delete("referer") dir = Pathname.new(@config[:FilterDir]) content = nil @config[:Rules].each do |path| path = dir + eval("%Q(#{path})") if path.file? puts "Hit Arrogation: #{path}" puts "Rewrited: <= #{req.path_info}" content = path.read break end end if content && content !~ /\A\s*\Z/ mime_types = WEBrick::HTTPUtils::DefaultMimeTypes.update(@config[:MimeTypes]) res.header["Content-Type"] = WEBrick::HTTPUtils.mime_type(req.path_info, mime_types) res.body = content true else false end end end server_config = { :Port => 5432, :ProxyVia => false, :Logger => WEBrick::Log.new(nil, 0), :AccessLog => WEBrick::Log.new(nil, 0), :FilterDir => "files", :Rules => [ "\#{req.host}\#{req.path_info}", "\#{req.host}/\#{File.basename(req.path_info)}", "\#{File.basename(req.path_info)}" ] }.update(@config["server"]) srv = ArrogationProxyServer.new(server_config) trap (:INT) { srv.shutdown } srv.start