Skip to content

Instantly share code, notes, and snippets.

@Fursje
Created February 5, 2016 09:36
Show Gist options
  • Save Fursje/bfaecae51813fe62d4b9 to your computer and use it in GitHub Desktop.
Save Fursje/bfaecae51813fe62d4b9 to your computer and use it in GitHub Desktop.
DNS blacklist updater to host format.
<?php
$a = new adsblock();
$a->run();
class adsblock {
public $output_hosts_file = "/etc/powerdns/hosts-ads";
public $redirect_ip = "0.0.0.0";
public $fileUrls = array(
'https://adaway.org/hosts.txt',
'http://adblock.gjtech.net/?format=unix-hosts',
'http://hosts-file.net/ad_servers.txt',
'http://www.malwaredomainlist.com/hostslist/hosts.txt',
'http://pgl.yoyo.org/adservers/serverlist.php?',
'http://someonewhocares.org/hosts/hosts',
'http://winhelp2002.mvps.org/hosts.txt',
);
public $domains = array();
public function run() {
$this->getFiles();
$this->shuffle();
$this->generateHosts();
}
private function getFiles() {
$data = array();
foreach ($this->fileUrls as $url) {
$data[] = file_get_contents($url);
}
$this->rawdata = $data;
}
private function shuffle() {
foreach ($this->rawdata as $value) {
$tmp = explode("\n",$value);
foreach ($tmp as $k=>$value) {
$v = trim($value);
if (preg_match("/^#/",$v)) { continue; }
$b = preg_split("/\s/",$v);
if (count($b) >= 2) {
if (isset($b[0]) && isset($b[1])) {
if (!in_array($b[1],$this->domains)) {
$this->domains[] = $b[1];
}
}
}
}
}
}
private function generateHosts() {
$tmp = "";
foreach ($this->domains as $domain) {
$tmp .= sprintf("%s %s\n",$this->redirect_ip, $domain);
}
file_put_contents($this->output_hosts_file, $tmp);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment