diff --git a/.gitignore b/.gitignore index 6abceb3..c925ae0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ /dist /worker /config.json -/hosts.txt \ No newline at end of file +/blocklist.txt \ No newline at end of file diff --git a/generate_blocklist.sh b/generate_blocklist.sh new file mode 100755 index 0000000..1c58b43 --- /dev/null +++ b/generate_blocklist.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +function import_hosts() { + local url=$1 + curl "$url" | sed '/^#/d' | sed 's/0.0.0.0 //g' | sed 's/127.0.0.1 //g' | grep '\S' +} + +URLS=( + "https://someonewhocares.org/hosts/zero/hosts" + "https://adaway.org/hosts.txt" +) + +echo "" > blocklist.txt +for url in ${URLS[@]}; do + echo "Importing $url" + import_hosts $url >> blocklist.txt +done \ No newline at end of file diff --git a/src/override.rs b/src/override.rs index 091a016..0510f59 100644 --- a/src/override.rs +++ b/src/override.rs @@ -2,16 +2,21 @@ use crate::trie_map::TrieMap; use domain::base::{rdata::UnknownRecordData, Compose, Dname, Question, Record, Rtype}; use domain::rdata::{Aaaa, AllRecordData, A}; use lazy_static::lazy_static; -use std::collections::HashMap; -use std::net::IpAddr; +use std::collections::{HashMap, HashSet}; +use std::net::{IpAddr, Ipv4Addr}; lazy_static! { - static ref PLAIN_HOSTS_MAP: HashMap = parse_plain_hosts_file(); + // Put a simple blocklist of domains at ../blocklist.txt + // All domains in the file will be resolved to 0.0.0.0 + // This can be used for ad-blocking, as converting the + // blocklists to JSON config file would not be a great idea, + // but converting them to a dumb list of domains should be trivial + static ref BLOCK_LIST: HashSet = parse_blocklist_file(); } -fn parse_plain_hosts_file() -> HashMap { - let mut ret = HashMap::new(); - for line in include_str!("../hosts.txt").lines() { +fn parse_blocklist_file() -> HashSet { + let mut ret = HashSet::new(); + for line in include_str!("../blocklist.txt").lines() { if line.is_empty() { continue; } @@ -20,11 +25,7 @@ fn parse_plain_hosts_file() -> HashMap { continue; } - if let [ip, domain] = line.split_whitespace().collect::>().as_slice() { - if let Ok(addr) = ip.parse() { - ret.insert(domain.to_string(), addr); - } - } + ret.insert(line.trim().to_string()); } ret } @@ -84,8 +85,8 @@ impl OverrideResolver { let name = question.qname().to_string(); if let Some(addr) = self.simple_matches.get(&name) { self.respond_with_addr(question, addr) - } else if let Some(addr) = PLAIN_HOSTS_MAP.get(&name) { - self.respond_with_addr(question, addr) + } else if BLOCK_LIST.get(&name).is_some() { + self.respond_with_addr(question, &IpAddr::V4(Ipv4Addr::UNSPECIFIED)) } else if let Some(addr) = self .suffix_matches .get_by_prefix(name.chars().rev().collect::())