Last active
November 9, 2021 17:38
-
-
Save Crypto-Spartan/8db843c3e9d16dd28a12d4bb5d39d5bd to your computer and use it in GitHub Desktop.
Reformats domain blocklist from ABP format to HOST format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
build with cargo build --release | |
to run, do ./abp_to_host <path_to_abp_blocklist> | |
*/ | |
use std::{fs, env}; | |
use std::io::{BufRead, BufReader, BufWriter, Write}; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
if args.len() != 3 || &args[1] == "-h" || &args[1] == "--help" { | |
println!("Two arguments are required:\n\ | |
\t1. path of the ABP blocklist file\n\ | |
\t2. path of the output file\n\n\ | |
Usage:\n\ | |
\t./abp_to_host <path_to_abp_blocklist> <path_to_output_file>"); | |
return | |
} | |
let path_to_abp = &args[1]; | |
let path_to_host = &args[2]; | |
let mut abp_f = BufReader::new( | |
fs::File::open(path_to_abp).expect("Unable to open file") | |
).lines(); | |
let mut host_f = BufWriter::new( | |
fs::OpenOptions::new() | |
.write(true) | |
.create(true) | |
.open(path_to_host) | |
.expect("Unable to create new file") | |
); | |
{ | |
let found_date = false; | |
let mut date = String::new(); | |
for line in abp_f.by_ref() { | |
let line = line.expect("Unable to read line"); | |
if line.starts_with("! Version: ") { | |
date.push_str(&line[11..].trim_end()); | |
break | |
} else if line.starts_with("||") && !found_date { | |
panic!("No date found in ABP file") | |
} | |
} | |
host_f.write_all("# Title: EU US most prevalent ads & trackers HOST format\n\ | |
# Expires: 30 days\n\ | |
#\n".as_bytes()) | |
.expect("Unable to write to file"); | |
host_f.write_all(format!("# Updated: {}\n", date).as_bytes()) | |
.expect("Unable to write to file"); | |
host_f.write_all("# Please report issues related to website breakage at github\n\ | |
# Kees1958/W3C_annual_most_used_survey_blocklist/issues\n\ | |
#\n\ | |
#\n\ | |
#\n".as_bytes()) | |
.expect("Unable to write to file"); | |
} | |
for line in abp_f { | |
let line = line.expect("Unable to read line"); | |
if line.starts_with("||") { | |
if let Some(end) = line.find("$") { | |
let mut domain = &line[2..end]; | |
if domain.contains("*") { | |
continue | |
} else { | |
domain = domain.trim_end_matches(','); | |
} | |
host_f.write_all(format!("0.0.0.0 {}\n", domain).as_bytes()) | |
.expect("Unable to write to file"); | |
} | |
} | |
} | |
host_f.flush().expect("Unable to flush data to file"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment