Merge pull request #785 from pi-hole/tweak/teleporter/regex

Add regex support for Teleporter
This commit is contained in:
Mark Drobnak
2018-07-15 11:56:30 -04:00
committed by GitHub
4 changed files with 52 additions and 56 deletions
-17
View File
@@ -14,23 +14,6 @@ $type = $_POST['list'];
// All of the verification for list editing
list_verify($type);
function add_regex($regex)
{
global $regexfile;
if(file_put_contents($regexfile, "\n".$regex, FILE_APPEND) === FALSE)
{
$err = error_get_last()["message"];
echo "Unable to add regex \"".htmlspecialchars($regex)."\" to ${regexfile}<br>Error message: $err";
}
else
{
// Send SIGHUP to pihole-FTL using a frontend command
// to force reloading of the regex domains
// This will also wipe the resolver's cache
echo exec("sudo pihole restartdns reload");
}
}
switch($type) {
case "white":
if(!isset($_POST["auditlog"]))
+17
View File
@@ -45,4 +45,21 @@ if(!function_exists('hash_equals')) {
}
}
function add_regex($regex, $mode=FILE_APPEND, $append="\n")
{
global $regexfile;
if(file_put_contents($regexfile, $append.$regex, $mode) === FALSE)
{
$err = error_get_last()["message"];
echo "Unable to add regex \"".htmlspecialchars($regex)."\" to ${regexfile}<br>Error message: $err";
}
else
{
// Send SIGHUP to pihole-FTL using a frontend command
// to force reloading of the regex domains
// This will also wipe the resolver's cache
echo exec("sudo pihole restartdns reload");
}
}
?>
+33 -37
View File
@@ -14,14 +14,14 @@ if (php_sapi_name() !== "cli") {
check_csrf(isset($_POST["token"]) ? $_POST["token"] : "");
}
function archive_add_file($path,$name)
function archive_add_file($path,$name,$subdir="")
{
global $archive;
if(file_exists($path.$name))
$archive[$name] = file_get_contents($path.$name);
$archive[$subdir.$name] = file_get_contents($path.$name);
}
function archive_add_directory($path)
function archive_add_directory($path,$subdir="")
{
if($dir = opendir($path))
{
@@ -29,7 +29,7 @@ function archive_add_directory($path)
{
if($entry !== "." && $entry !== "..")
{
archive_add_file($path,$entry);
archive_add_file($path,$entry,$subdir);
}
}
closedir($dir);
@@ -43,7 +43,7 @@ function limit_length(&$item, $key)
$item = substr($item, 0, 253);
}
function process_file($contents)
function process_file($contents,$check=True)
{
$domains = array_filter(explode("\n",$contents));
@@ -51,8 +51,12 @@ function process_file($contents)
// function to every member of the array of domains
array_walk($domains, "limit_length");
// Check validity of domains (after possible clipping)
check_domains($domains);
// Check validity of domains (don't do it for regex filters)
if($check)
{
check_domains($domains);
}
return $domains;
}
@@ -66,27 +70,6 @@ function check_domains($domains)
}
}
function getWildcardListContent() {
if(file_exists("/etc/dnsmasq.d/03-pihole-wildcard.conf"))
{
$rawList = file_get_contents("/etc/dnsmasq.d/03-pihole-wildcard.conf");
$wclist = explode("\n", $rawList);
$list = [];
foreach ($wclist as $entry) {
$expl = explode("/", $entry);
if(count($expl) == 3)
{
array_push($list,$expl[1]);
}
}
return implode("\n",array_unique($list));
}
return "";
}
if(isset($_POST["action"]))
{
if($_FILES["zip_file"]["name"] && $_POST["action"] == "in")
@@ -125,28 +108,41 @@ if(isset($_POST["action"]))
if(isset($_POST["blacklist"]) && $file->getFilename() === "blacklist.txt")
{
$blacklist = process_file(file_get_contents($file));
echo "Processing blacklist.txt<br>\n";
echo "Processing blacklist.txt (".count($blacklist)." entries)<br>\n";
exec("sudo pihole -b -nr --nuke");
exec("sudo pihole -b -q -nr ".implode(" ", $blacklist));
$importedsomething = true;
}
if(isset($_POST["whitelist"]) && $file->getFilename() === "whitelist.txt")
{
$whitelist = process_file(file_get_contents($file));
echo "Processing whitelist.txt<br>\n";
echo "Processing whitelist.txt (".count($whitelist)." entries)<br>\n";
exec("sudo pihole -w -nr --nuke");
exec("sudo pihole -w -q -nr ".implode(" ", $whitelist));
$importedsomething = true;
}
if(isset($_POST["wildlist"]) && $file->getFilename() === "wildcardblocking.txt")
if(isset($_POST["regexlist"]) && $file->getFilename() === "regex.list")
{
$wildlist = process_file(file_get_contents($file));
echo "Processing wildcardblocking.txt<br>\n";
exec("sudo pihole -wild -nr --nuke");
exec("sudo pihole -wild -q -nr ".implode(" ", $wildlist));
$regexraw = file_get_contents($file);
$regexlist = process_file($regexraw,false);
echo "Processing regex.list (".count($regexlist)." entries)<br>\n";
// NULL = overwrite (or create) the regex filter file
add_regex($regexraw, NULL,"");
$importedsomething = true;
}
// Also try to import legacy wildcard list if found
if(isset($_POST["regexlist"]) && $file->getFilename() === "wildcardblocking.txt")
{
$wildlist = process_file(file_get_contents($file));
echo "Processing wildcardblocking.txt (".count($wildlist)." entries)<br>\n";
exec("sudo pihole --wild -nr --nuke");
exec("sudo pihole --wild -q -nr ".implode(" ", $wildlist));
$importedsomething = true;
}
if($importedsomething)
{
exec("sudo pihole restartdns");
@@ -177,9 +173,9 @@ else
archive_add_file("/etc/pihole/","adlists.list");
archive_add_file("/etc/pihole/","setupVars.conf");
archive_add_file("/etc/pihole/","auditlog.list");
archive_add_directory("/etc/dnsmasq.d/");
archive_add_file("/etc/pihole/","regex.list");
archive_add_directory("/etc/dnsmasq.d/","dnsmasq.d/");
$archive["wildcardblocking.txt"] = getWildcardListContent();
$archive->compress(Phar::GZ); // Creates a gziped copy
unlink($archive_file_name); // Unlink original tar file as it is not needed anymore
$archive_file_name .= ".gz"; // Append ".gz" extension to ".tar"
+2 -2
View File
@@ -1096,9 +1096,9 @@ if (isset($_GET['tab']) && in_array($_GET['tab'], array("sysadmin", "blocklists"
Blacklist (exact)</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="wildlist" value="true"
<label><input type="checkbox" name="regexlist" value="true"
checked>
Blacklist (wildcard)</label>
Regex filters</label>
</div>
</div>
</div>