From b979761cefc609c1d934604b037c5597fd546615 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Fri, 29 Oct 2021 03:10:17 -0500 Subject: [PATCH] Don't use proxy for localhost (Oxidized and Prometheus) (#13450) fixes #13382 --- LibreNMS/Data/Store/Prometheus.php | 3 ++- LibreNMS/Util/Proxy.php | 15 +++++++++++++-- includes/functions.php | 3 ++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/LibreNMS/Data/Store/Prometheus.php b/LibreNMS/Data/Store/Prometheus.php index 153d189e4b..ef9ecc6de4 100644 --- a/LibreNMS/Data/Store/Prometheus.php +++ b/LibreNMS/Data/Store/Prometheus.php @@ -30,6 +30,7 @@ use App\Polling\Measure\Measurement; use GuzzleHttp\Exception\GuzzleException; use Illuminate\Support\Str; use LibreNMS\Config; +use LibreNMS\Util\Proxy; use Log; class Prometheus extends BaseDatastore @@ -56,7 +57,7 @@ class Prometheus extends BaseDatastore $this->default_opts = [ 'headers' => ['Content-Type' => 'text/plain'], ]; - if ($proxy = get_proxy()) { + if ($proxy = Proxy::get($url)) { $this->default_opts['proxy'] = $proxy; } diff --git a/LibreNMS/Util/Proxy.php b/LibreNMS/Util/Proxy.php index c666f43ec6..4de18028a8 100644 --- a/LibreNMS/Util/Proxy.php +++ b/LibreNMS/Util/Proxy.php @@ -29,14 +29,25 @@ use LibreNMS\Config; class Proxy { + /** + * Check if if the proxy should be used. + * (it should not be used for connections to localhost) + */ + public static function shouldBeUsed(string $target_url): bool + { + return (bool) preg_match('#//:(localhost|127\.|::1)#', $target_url); + } + /** * Return the proxy url * * @return array|bool|false|string */ - public static function get() + public static function get(?string $target_url = null) { - if (getenv('http_proxy')) { + if ($target_url && ! self::shouldBeUsed($target_url)) { + return false; + } elseif (getenv('http_proxy')) { return getenv('http_proxy'); } elseif (getenv('https_proxy')) { return getenv('https_proxy'); diff --git a/includes/functions.php b/includes/functions.php index ab20b38597..96dd4e60ef 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -21,6 +21,7 @@ use LibreNMS\Modules\Core; use LibreNMS\Util\Debug; use LibreNMS\Util\IPv4; use LibreNMS\Util\IPv6; +use LibreNMS\Util\Proxy; function array_sort_by_column($array, $on, $order = SORT_ASC) { @@ -1687,7 +1688,7 @@ function oxidized_node_update($hostname, $msg, $username = 'not_provided') $postdata = ['user' => $username, 'msg' => $msg]; $oxidized_url = Config::get('oxidized.url'); if (! empty($oxidized_url)) { - Requests::put("$oxidized_url/node/next/$hostname", [], json_encode($postdata), ['proxy' => get_proxy()]); + Requests::put("$oxidized_url/node/next/$hostname", [], json_encode($postdata), ['proxy' => Proxy::get($oxidized_url)]); return true; }