From 9a8569775996b4b2441b69e06a09b9ab8284519e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 7 Mar 2019 15:23:42 +0100 Subject: [dnbd3] Support specifying port, IPv6 addresses --- modules-available/dnbd3/inc/dnbd3rpc.inc.php | 17 ++++++++++++++--- modules-available/dnbd3/inc/dnbd3util.inc.php | 22 ++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) (limited to 'modules-available/dnbd3/inc') diff --git a/modules-available/dnbd3/inc/dnbd3rpc.inc.php b/modules-available/dnbd3/inc/dnbd3rpc.inc.php index cdcda508..6ed43254 100644 --- a/modules-available/dnbd3/inc/dnbd3rpc.inc.php +++ b/modules-available/dnbd3/inc/dnbd3rpc.inc.php @@ -19,13 +19,24 @@ class Dnbd3Rpc { * @param bool $altservers list of alt servers with status * @return int|array the queried data as an array, or false on error */ - public static function query($server, $port, $stats, $clients, $images, $diskSpace = false, $config = false, $altservers = false) + public static function query($server, $stats, $clients, $images, $diskSpace = false, $config = false, $altservers = false) { // Special case - local server if ($server === '') { - $server = '127.0.0.1'; + $server = '127.0.0.1:5003'; + } elseif (($out = Dnbd3Util::matchAddress($server))) { + if (isset($out['v4'])) { + $server = $out['v4']; + } else { + $server = '[' . $out['v6'] . ']'; + } + if (isset($out['port'])) { + $server .= $out['port']; + } else { + $server .= ':5003'; + } } - $url = 'http://' . $server . ':' . $port . '/query?'; + $url = 'http://' . $server . '/query?'; if ($stats) { $url .= 'q=stats&'; } diff --git a/modules-available/dnbd3/inc/dnbd3util.inc.php b/modules-available/dnbd3/inc/dnbd3util.inc.php index 33581b77..35fad8b3 100644 --- a/modules-available/dnbd3/inc/dnbd3util.inc.php +++ b/modules-available/dnbd3/inc/dnbd3util.inc.php @@ -52,12 +52,11 @@ class Dnbd3Util { // Now query them all $NOW = time(); foreach ($servers as $server) { - $port = 5003; - $data = Dnbd3Rpc::query($server['addr'], $port, true, false, false, true); + $data = Dnbd3Rpc::query($server['addr'], true, false, false, true); if ($data === Dnbd3Rpc::QUERY_UNREACHABLE) { - $error = 'No (HTTP) reply on port ' . $port; + $error = 'No (HTTP) reply from ' . $server['addr']; } elseif ($data === Dnbd3Rpc::QUERY_NOT_200) { - $error = 'No HTTP 200 OK on port ' . $port; + $error = 'No HTTP 200 OK from ' . $server['addr']; } elseif ($data === Dnbd3Rpc::QUERY_NOT_JSON) { $error = 'Reply to status query is not JSON'; } elseif (!is_array($data) || !isset($data['runId'])) { @@ -247,4 +246,19 @@ class Dnbd3Util { ); } + public static function matchAddress($server) + { + if (!preg_match('/^(?:\[(?[a-f0-9:]+)\]|(?[a-f0-9:]+)|(?[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+))(?:\d+)?$/i', + $server, $out)) { + return false; + } + foreach (['v6a', 'v6b'] as $k) { + if (isset($out[$k])) { + $out['v6'] = $out[$k]; + unset($out[$k]); + } + } + return $out; + } + } -- cgit v1.2.3-55-g7522