Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google-safe-search-example #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby

# build google force safe search ruls for rubyDNS

google_domains = `curl https://www.google.com/supported_domains 2>> /dev/null | sed "s/^.//g"`.split("\n")
Copy link
Member

@ioquatix ioquatix Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be improved.

require 'async'
require 'async/http/internet'

google_domains = Sync do
	internet = Async::HTTP::Internet.new
	response = internet.get("https://www.google.com/supported_domains")
	
	response.read.split(/\n/)
ensure
	response&.close
end


google_domains.each do | dom |

puts ""
puts "#match #{dom}"
puts "match(/^#{dom}$/, IN::A) do |transaction|"
puts " transaction.respond!(Name.create('forcesafesearch.google.com'), resource_class: IN::CNAME)"
puts "end"
puts "match(/^www.#{dom}$/, IN::A) do |transaction|"
puts " transaction.respond!(Name.create('forcesafesearch.google.com'), resource_class: IN::CNAME)"
puts "end"

end
35 changes: 35 additions & 0 deletions examples/google-force-safe-search/google-safe-search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env ruby
require 'rubydns'

INTERFACES = [
[:udp, "0.0.0.0", 5300],
[:tcp, "0.0.0.0", 5300],
]

# Use upstream DNS for name resolution.
UPSTREAM = RubyDNS::Resolver.new([
[:udp, "8.8.8.8", 53],
[:tcp, "8.8.8.8", 53]
])

$R = Resolv::DNS.new
Name = Resolv::DNS::Name
IN = Resolv::DNS::Resource::IN

# Start the RubyDNS server
RubyDNS::run_server(INTERFACES) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domains = Regexp.union(google_domains)

# Start the RubyDNS server
RubyDNS::run_server(INTERFACES) do
	@logger.debug!
	
	match(domains, IN::A) do |transaction|
		transaction.respond!(Name.create('forcesafesearch.google.com'), resource_class: IN::CNAME)
	end
	
	otherwise do |transaction|
		transaction.passthrough!(UPSTREAM)
	end
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might need to tweak domains but this should avoid having to generate a script/server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domains = /#{Regexp.union(google_domains)}\z/

maybe sufficient.

@logger.debug!

#match google.com
match(/^google.com$/, IN::A) do |transaction|
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to preserve sub-domains such as admin.google.com, docs.google.com etc.

It may be necessary to do an exact match. Maybe Google handles this? Probably better to only override the specific top level domains for the force safe search?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the idea you suggested if that can work. Even if it is just matching the exact domains within an array.

transaction.respond!(Name.create('forcesafesearch.google.com'), resource_class: IN::CNAME)
end
match(/^www.google.com$/, IN::A) do |transaction|
transaction.respond!(Name.create('forcesafesearch.google.com'), resource_class: IN::CNAME)
end

otherwise do |transaction|
transaction.passthrough!(UPSTREAM)
end
end