From 0ccf727eddba69b32158c6af30ba89cc81d9d756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Wed, 8 Jun 2022 08:29:19 +0200 Subject: [PATCH] Use API gateway endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- scripts/pi-hole/php/func.php | 11 +++++++++++ settings.php | 12 ++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/scripts/pi-hole/php/func.php b/scripts/pi-hole/php/func.php index f4e0274c..3a7d10d7 100644 --- a/scripts/pi-hole/php/func.php +++ b/scripts/pi-hole/php/func.php @@ -544,4 +544,15 @@ function piholeStatus() { return $ret; } + +//Returns the default gateway address and interface +function getGateway() { + $gateway= callFTLAPI("gateway"); + if (array_key_exists("FTLnotrunning", $gateway)) { + $ret = array("ip" => -1); + } else { + $ret = array_combine(["ip", "iface"], explode(" ", $gateway[0])); + } + return $ret; +} ?> diff --git a/settings.php b/settings.php index f1b443d4..54f59f67 100644 --- a/settings.php +++ b/settings.php @@ -8,6 +8,7 @@ require "scripts/pi-hole/php/header.php"; require "scripts/pi-hole/php/savesettings.php"; require_once "scripts/pi-hole/php/FTL.php"; +require_once "scripts/pi-hole/php/func.php"; // Reread ini file as things might have been changed // DEFAULT_FTLCONFFILE is set in "scripts/pi-hole/php/FTL.php"; $setupVars = parse_ini_file("/etc/pihole/setupVars.conf"); @@ -77,12 +78,11 @@ if (isset($setupVars["PIHOLE_INTERFACE"])) { $piHoleInterface = "unknown"; } -// get the gateway address for the default route on the Pi-hole interface -if ($piHoleInterface !== "unknown") { - $IPv4GW=shell_exec("ip -4 route | grep default | grep '${piHoleInterface}' | cut -d ' ' -f 3"); -} -// if the route did not return anything or $piHoleInterface is unknown -if (empty($IPv4GW)) { +// get the gateway IP +$IPv4GW=getGateway()["ip"]; + +// if the default gateway address is unknown or FTL is not running +if ($IPv4GW == "0.0.0.0"|| $IPv4GW == -1) { $IPv4GW = "unknown"; }