Created
November 7, 2013 23:20
-
-
Save tzbob/7363582 to your computer and use it in GitHub Desktop.
automatically update pdnsd.adblock
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
#!/usr/bin/env scala | |
import scala.io.Source | |
import java.io.FileWriter | |
import java.util.Calendar | |
import scala.util.matching.Regex | |
val resources = List( | |
Resource("http://someonewhocares.org/hosts/hosts"), | |
Resource( | |
"http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0" | |
+ "&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=&mimetype=plaintext" | |
), | |
Resource("http://winhelp2002.mvps.org/hosts.txt") | |
) | |
val whitelist = List( | |
"local", | |
"localhost" | |
) map (Host(_)) | |
val pdnsd = HostFile("/etc/pdnsd.adblock", """neg\{name=(.*);types=domain;\}""".r) | |
case class Resource(url: String, ipReplacement: String = "127.0.0.1") { | |
def fetch: Set[Host] = { | |
val potLines = Source.fromURL(url).getLines filter (_ startsWith ipReplacement) | |
val rawHosts = potLines map (_.drop(ipReplacement.length).trim) | |
val hosts = rawHosts map (_ takeWhile (!_.isWhitespace)) map (Host(_)) | |
hosts.toSet | |
} | |
} | |
case class HostFile(loc: String, extractor: Regex) { | |
def fetch: Set[Host] = { | |
val rawLines = Source.fromFile(loc).getLines | |
val rawLinesWithoutComments = rawLines filter (_(0) != '#') | |
val extractions = rawLinesWithoutComments map (_ match { | |
case extractor(host) => Some(Host(host)) | |
case _ => None | |
}) | |
extractions.flatten.toSet | |
} | |
} | |
case class Host(name: String) { | |
def pdnsd: String = s"neg{name=$name;types=domain;}" | |
} | |
val resourceFetchings = resources.map(_.fetch) | |
val resourceHosts = resourceFetchings.foldLeft(Set[Host]())(_ ++ _) | |
val pdnsdHosts = pdnsd.fetch | |
val newHosts = resourceHosts -- pdnsdHosts -- whitelist | |
val header = s"#${newHosts.size} were added on ${Calendar.getInstance.getTime}\n" | |
println(header) | |
if (newHosts.size > 0) { | |
val fw = new FileWriter(pdnsd.loc, true) | |
try { | |
fw.write(header) | |
fw.write(newHosts.map(_.pdnsd).mkString("\n")) | |
fw.write("\n") | |
} finally { | |
fw.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi :)
Thanks for this, i'm using it since any years and it has worked very good!
After the last days it stopped working :( Always throwing java.nio.charset.MalformedInputException: Input length = 1 exception :/
Is there any update for it available?
Thank you :)