Skip to content

Instantly share code, notes, and snippets.

@airways
Created February 19, 2011 18:15
Show Gist options
  • Save airways/835236 to your computer and use it in GitHub Desktop.
Save airways/835236 to your computer and use it in GitHub Desktop.
Inverted the webdelay ruby script written by someone on reddit (http://www.reddit.com/tb/fo276) - everything *except* the listed sites is delayed by 10 seconds
#!/usr/bin/ruby
require 'webrick'
require 'webrick/httpproxy'
require 'getoptlong'
opts = GetoptLong.new(
[ "--help", "-h", GetoptLong::NO_ARGUMENT ],
[ "--file", "-f", GetoptLong::REQUIRED_ARGUMENT ],
[ "--port", "-p", GetoptLong::REQUIRED_ARGUMENT ],
[ "--delay", "-d", GetoptLong::REQUIRED_ARGUMENT ]
)
def showHelp
puts "Act as a transparent proxy that delays certain sites from loading"
puts "\t-h or --help Display this help"
puts "\t-f or --file Specify a file to load sites from - sites NOT in this list will be delayed"
puts "\t-p or --port Specify the port to accept connections on (default 8085)"
puts "\t-d or --delay Specify the number of seconds to sleep when accesing a \"slow\" site (default 10 seconds)"
end
$sites = []
$port = 8085
$delay = 10
$filename = "normalsites.txt"
opts.each do |opt, arg|
case opt
when "--help"
showHelp
exit
when "--port"
$port = arg.to_i
when "--file"
filename = arg
when "--delay"
$delay = arg.to_i
else
puts "Invalid argument - #{opt}"
showHelp
exit
end
end
begin
fdata = File.read($filename)
fdata.split(/\n/).each do |site|
$sites << site
end
rescue
puts "Failed to load file #{$filename} - #{$!}"
end
while site = ARGV.shift
$sites << site
end
if $sites.size ==0
puts "Note: You have not specified any sites to not delay from loading."
end
puts "The following sites will load normally:"
$sites.each do |site|
puts "\t#{site}"
end
$proxy = WEBrick::HTTPProxyServer.new\
:Port => $port,
:ServerType => Thread,
:RequestCallback => Proc.new { |req,res|
puts "[+] Parsing request #{req.request_line}"
delay = true
$sites.each do |site|
if req.request_line.match(site)
delay=false
break
end
end
if delay
puts "\tThis is a \"slow\" site, sleeping for #{$delay} seconds"
sleep $delay
end
}
$proxy.start
trap("INT") {
puts "Shutting down..."
$done = true
}
until $done
# Idle loop
sleep(1)
end
$proxy.shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment