Skip to content

Instantly share code, notes, and snippets.

@whym
Created October 28, 2010 05:58
Show Gist options
  • Select an option

  • Save whym/650733 to your computer and use it in GitHub Desktop.

Select an option

Save whym/650733 to your computer and use it in GitHub Desktop.
insert dummy DNS entries for the domains specified in LeechBlock for Firefox
#!/usr/bin/env ruby
# insert dummy DNS entries for the domains specified in LeechBlock for Firefox
# usage: use this as a cron job
USAGE = <<"EOD"
usage: #{$0} [-f leechblock_exported_file]
EOD
require 'optparse'
OPT = Struct.
new(:file, :id, :sentinel, :unreachable, :dnsmasq, :verbose).
new(ENV['HOME']+'/.myconf/leechblock.txt', '1', 'leechblock-sentinel-a.localhost', '192.0.2.1', '/usr/sbin/dnsmasq', false)
OptionParser.new do |opts|
opts.on('--file STR', String) do |v|
OPT.file = v
end
opts.on('--[no-]verbose') do |v|
OPT.verbose = v
end
end.parse!
require 'resolv'
kvs = {}
open(OPT.file) do |io|
io.each_line do |line|
line.strip!
k,v = line.split(/=/)
kvs[k] = v
end
end
duras = kvs['times'+OPT.id]
sites = kvs['sites'+OPT.id].split(/\s+/)
block_enabled = false
msg = Resolv::DNS.new.getresources('localhost.', Resolv::DNS::Resource::IN::TXT)
if msg.length > 0 and msg[0].strings.any?{|x| x == OPT.sentinel} then
block_enabled = true
end
block_written = false
time = Time.new.hour * 100 + Time.new.min
duras.split(/,/).each do |pair|
min,max = pair.split(/\-/).map{|x| x.to_i}
if min <= time and time < max then
block_written = true
end
end
if OPT.verbose then
def system_verbose(*x)
puts x.join(' ')
system *x
end
else
alias :system_verbose :system
end
if block_written and !block_enabled then
args = sites.map do |x|
x.sub!(/^\*/,'')
"--address=/#{x}/#{OPT.unreachable}"
end
system_verbose "pkill dnsmasq && " + "#{OPT.dnsmasq} --txt-record=localhost,#{OPT.sentinel} #{args.join(' ')}"
end
if !block_written and block_enabled then
system_verbose "pkill dnsmasq && #{OPT.dnsmasq}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment