Last active
April 18, 2022 19:18
-
-
Save Exagone313/9198e2c205d2c040d9a73c015f98f611 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/** | |
* @author Elouan Martinet <[email protected]> | |
* @copyright Copyright (c) 2016-2017, Elouan Martinet | |
* @license AGPL-3.0+ | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Affero General Public License as | |
* published by the Free Software Foundation, either version 3 of the | |
* License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Affero General Public License for more details. | |
* | |
* You should have received a copy of the GNU Affero General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* @version 0.3.1 | |
*/ | |
$target = '0.0.0.0'; | |
$n = PHP_EOL; | |
$header = '# Generated on ' . date('r') . $n | |
. '127.0.0.1 localhost.localdomain localhost' . $n | |
. '::1 localhost.localdomain localhost'; | |
// Warning: the script assumes the format of the following array is correct! | |
$sources = [ // 0: hosts file (allow whitespaces and comments), 1: one domaine per line (strict), array[start_chars, end_chars]: take the middle (strict) | |
['http://winhelp2002.mvps.org/hosts.txt', 0], | |
['https://adaway.org/hosts.txt', 0], | |
['https://hosts-file.net/ad_servers.txt', 0], | |
['https://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext', 1], | |
['https://easylist-downloads.adblockplus.org/malwaredomains_full.txt', ['||', '^']], | |
['https://easylist-downloads.adblockplus.org/adwarefilters.txt', ['||', '^']], | |
['https://raw.githubusercontent.com/Dawsey21/Lists/master/adblock-list.txt', ['||', '^']], | |
]; | |
$whitelist = [ | |
'po.st', | |
]; | |
$blacklist = [ | |
'coin-hive.com', | |
'open.oneplus.net', | |
'suplah.xss.ht', | |
]; | |
function read_file_callback($file, $callable, &$result, $whitelist, $args = null) { | |
$line = strtok($file, "\n"); | |
while($line !== false) { | |
$line = trim($line); | |
if(strlen($line) > 3) { | |
if($args === null) | |
$r = call_user_func($callable, $line); | |
else | |
$r = call_user_func_array($callable, array_merge(array($line), $args)); | |
if($r && !in_array($r, $whitelist) && !in_array($r, $result)) | |
$result[] = $r; | |
} | |
$line = strtok("\n"); | |
} | |
} | |
function filter_host($host) { | |
return preg_match('#^([\p{L}0-9\-]+\.)+[\p{L}0-9\-]{2,}$#u', $host) ? $host : false; | |
} | |
function parse_hosts_line($line) { // split line by whitespaces and take second value | |
if($line[0] === '#') | |
return false; | |
$e = preg_split('/\s+/', $line, 3); | |
if(!$e || count($e) < 2) | |
return false; | |
return filter_host($e[1]); | |
} | |
function parse_middle($line, $begin, $end) { | |
if(substr($line, 0, strlen($begin)) !== $begin | |
|| substr($line, - strlen($end)) !== $end) | |
return false; | |
if(strlen($end) === 0) | |
return filter_host(substr($line, strlen($begin))); | |
else | |
return filter_host(substr($line, strlen($begin), - strlen($end))); | |
} | |
$hosts = $blacklist; | |
foreach($sources as $src) { | |
$file = file_get_contents($src[0]); | |
if(!$file) | |
continue; | |
switch($src[1]) { | |
case 0: | |
// hosts file | |
read_file_callback($file, 'parse_hosts_line', $hosts, $whitelist); | |
break; | |
case 1: | |
// one domain per line | |
read_file_callback($file, 'filter_host', $hosts, $whitelist); | |
break; | |
default: | |
if(is_array($src[1])) { | |
// take middle | |
read_file_callback($file, 'parse_middle', $hosts, $whitelist, array($src[1][0], $src[1][1])); | |
} | |
} | |
} | |
echo $header . $n . $n; | |
foreach($hosts as $r) { | |
echo $target . ' ' . $r . $n; | |
} | |
# vim: noet sw=4 ts=4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment