Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions pve2_api.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public function __construct ($hostname, $username, $realm, $password, $port = 80
}
// Check hostname resolves.
if (gethostbyname($hostname) == $hostname && !filter_var($hostname, FILTER_VALIDATE_IP)) {
throw new PVE2_Exception("Cannot resolve {$hostname}.", 2);
// Fallback: check for IPv6 (AAAA) records if IPv4 lookup failed
if (!checkdnsrr($hostname, 'AAAA')) {
throw new PVE2_Exception("Cannot resolve {$hostname}.", 2);
}
}
// Check port is between 1 and 65535.
if (!filter_var($port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 65535]])) {
Expand Down Expand Up @@ -78,7 +81,13 @@ public function login () {

// Perform login request.
$prox_ch = curl_init();
curl_setopt($prox_ch, CURLOPT_URL, "https://{$this->hostname}:{$this->port}/api2/json/access/ticket");

// Handle IPv6 literals in URL
$host_url = $this->hostname;
if (filter_var($this->hostname, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$host_url = '[' . $this->hostname . ']';
}
curl_setopt($prox_ch, CURLOPT_URL, "https://{$host_url}:{$this->port}/api2/json/access/ticket");
curl_setopt($prox_ch, CURLOPT_POST, true);
curl_setopt($prox_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $login_postfields_string);
Expand Down Expand Up @@ -168,7 +177,13 @@ private function action ($action_path, $http_method, $put_post_parameters = null

// Prepare cURL resource.
$prox_ch = curl_init();
curl_setopt($prox_ch, CURLOPT_URL, "https://{$this->hostname}:{$this->port}/api2/json{$action_path}");

// Handle IPv6 literals in URL
$host_url = $this->hostname;
if (filter_var($this->hostname, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$host_url = '[' . $this->hostname . ']';
}
curl_setopt($prox_ch, CURLOPT_URL, "https://{$host_url}:{$this->port}/api2/json{$action_path}");

$put_post_http_headers = array();
$put_post_http_headers[] = "CSRFPreventionToken: {$this->login_ticket['CSRFPreventionToken']}";
Expand Down