Skip to content

Instantly share code, notes, and snippets.

@jgn
Created April 27, 2018 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgn/23978efe0256f19aa7fed9c23942c011 to your computer and use it in GitHub Desktop.
Save jgn/23978efe0256f19aa7fed9c23942c011 to your computer and use it in GitHub Desktop.
Block via OpenDNS
require 'mechanize'
require 'set'
require 'pry'
ACCOUNT = ENV['OPENDNS_ACCOUNT']
USERNAME = ENV['OPENDNS_USERNAME']
PASSWORD = ENV['OPENDNS_PASSWORD']
BLOCK_LIMIT = 25
AGENT_NAME = 'Mac Safari'
def dump(page)
File.open('./dump.html', 'w') do |f|
f.write(page.body)
end
end
def block(m, domain)
# Post to https://dashboard.opendns.com/dashboard_ajax.php
# n=ACCOUNT&step2=true&blocked_domain=questionduck.smackjeeves.com&action=add_blocked_domain
m.post('https://dashboard.opendns.com/dashboard_ajax.php',
# query
{
n: ACCOUNT,
step2: 'true',
blocked_domain: domain,
action: 'add_blocked_domain'
},
# headers
{
}
)
sleep 1
end
def block_domains(m)
# whitelist should look like
# api.turnitin.com
# docs.google.com
# en.wikipedia.org
whitelist = Set.new(IO.readlines('./whitelist.txt').map(&:chomp))
# history.txt should look like
# https://schoology.spps.org/course/641766803|153
# https://schoology.spps.org/course/641766803/materials|153
# http://topwebcomics.com/|166
# http://www.howtobeawerewolf.com/|167
# http://www.howtobeawerewolf.com/comic/coming-february-3rd/|167
domain_counts = {}
File.open('./history.txt').each do |line|
host = line[/(https?:\/\/.*?\/)/, 1]
uri = URI(host)
domain_counts[uri.host] ||= 0
domain_counts[uri.host] += 1
end
domain_counts_a = domain_counts.to_a.sort_by { |h| h[1] }.reverse
total_blocked = 0
domain_counts_a.each do |domain_count|
domain, count = domain_count
if whitelist.include?(domain)
puts "Skipping: #{domain}"
else
block(m, domain)
puts "Blocked: #{domain} (#{count})"
total_blocked += 1
exit if total_blocked >= BLOCK_LIMIT
end
end
end
Mechanize.new do |m|
m.user_agent_alias = AGENT_NAME
m.get('https://login.opendns.com') do |login_page|
login_form = login_page.form_with name: 'signin'
login_form['username'] = USERNAME
login_form['password'] = PASSWORD
login_form.submit
settings_page = m.get("https://dashboard.opendns.com/settings/#{ACCOUNT}/content_filtering")
block_domains(m)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment