Skip to content

Instantly share code, notes, and snippets.

@fabjan
Forked from banister/delay refactored.rb
Created February 19, 2011 07:54
Show Gist options
  • Save fabjan/834912 to your computer and use it in GitHub Desktop.
Save fabjan/834912 to your computer and use it in GitHub Desktop.
require 'webrick'
require 'webrick/httpproxy'
require 'optparse'
sites = []
port = 8080
delay = 5
OptionParser.new do |opts|
opts.banner = "Act as a transparent proxy that delays certain sites from loading"
opts.on("-p", "--port PORT", "Specify the port to accept connections on (default 8080)") do |port|
port = port.to_i
end
opts.on("-d", "--delay DELAY", "Specify the number of seconds to sleep when accesing a" \
" 'slow' site (default 5 seconds)") do |delay|
delay = delay.to_i
end
opts.on("-f", "--file FILE", "Specify a file to load sites from") do |file|
sites = File.readlines(file)
end
opts.on_tail("-h", "--help", "This message.") do
puts opts
exit
end
end.order! do |v|
sites << v
end
if sites.empty?
puts "You have not specified any sites to delay from loading. There is no purpose to proxy transparently."
exit
end
puts "Delaying the following sites from loading:"
sites.each do |site|
puts "\t#{site}"
end
proxy = WEBrick::HTTPProxyServer.new\
:Port => port,
:ServerType => Thread,
:RequestCallback => proc { |req,res|
puts "[+] Parsing request #{req.request_line}"
if (sites.any? {|site| req.request_line.match(site)})
puts "\tThis is a 'slow' site, sleeping for #{delay} seconds"
sleep delay
end
}
done = false
proxy.start
trap("INT") {
puts "Shutting down..."
done = true
}
sleep(1) until done
proxy.shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment