Don't use proxy for localhost (Oxidized and Prometheus) (#13450)

fixes #13382
This commit is contained in:
Tony Murray
2021-10-29 03:10:17 -05:00
committed by GitHub
parent 9f8bb8caae
commit b979761cef
3 changed files with 17 additions and 4 deletions

View File

@@ -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;
}

View File

@@ -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');

View File

@@ -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;
}