I've attempted the same thing and found there is very little base to
work from. You can take a look at WEBrick's httpproxy.rb but I found
it hard to determine where I would place my "hooks" to reprocess the
content. I've got a partially functional proxy that I wrote from the
ground up, but it has issues displaying certain pages. If you're
interested I can get the code up somewhere that it can be seen.
Hmm... I actually did this last week, and I found some example code on
the web pretty quickly (it was in Japanese, admittedly...). Here's
the simple AdBlock proxy I ran up whilst playing around (it uses the
pierceive adblock list). It returns an empty document for disallowed
addresses, and removes all img tags, just as an example of processing.
It's not meant to be feature-rich or even high-quality code, but it
does most of what you seem to want.
Paul.
#!/usr/bin/env ruby
require 'webrick/httpproxy'
require 'stringio'
require 'zlib'
require 'open-uri'
require 'iconv'
class AdBlocker
def initialize
reload
end
def reload
bl =3D []
File.open('adblock.txt').each_line do |line|
line.strip!
next if (line =3D~ /\[Adblock\]/ || line =3D~ /^!/)
if (%r!^/.*/$! =3D~ line)
bl << Regexp.new(line[1..-1])
else
bl << line
end
end
@block_list =3D bl
end
def blocked?(uri)
@block_list.each { |rx|=20
if (uri.match(rx))=20
return true=20
end
}
return false
end
end
module WEBrick
class RejectingProxyServer < HTTPProxyServer
def service(req, res)
if (@config[
roxyURITest].call(req.unparsed_uri))
super(req, res)
else
blank(req, res)
end
end
def blank(req, res)
res.header['content-type'] =3D 'text/plain'
res.header.delete('content-encoding')
res.body =3D ''
end
end
end
class ProxyServer
#
# Handler that is called by the proxy to process each page
#
def handler(req, res)
#p res.header
# Inflate content if it's gzipped
if ('gzip' =3D=3D res.header['content-encoding'])
res.header.delete('content-encoding')
res.body =3D Zlib::GzipReader.new(StringIO.new(res.body)).read
end
res.body.gsub!(%r!<img[^>]*>!im, '[image]')
end
def uri_allowed(uri)
b =3D @adblocker.blocked?(uri)
#puts("--> URI #{b ? 'blocked' : 'allowed'}: #{uri}")
return !b
end
def initialize
@server =3D WEBrick::RejectingProxyServer.new(
:BindAddress =3D> '0.0.0.0',
ort =3D> 8181,
roxyVia =3D> false,
#
roxyURI =3D> URI.parse('
http://localhost:8118/'),
roxyContentHandler =3D> method
handler),
roxyURITest =3D> method
uri_allowed)
)
@adblocker =3D AdBlocker.new
end
def start
@server.start
end
def stop
@server.shutdown
end
end
#
# Create and start the server
#
ps =3D ProxyServer.new
%w[INT HUP].each { |signal| trap(signal) { ps.stop } }
ps.start