tcpclient: Get the A and AAAA records before querying the CNAME only if needed (speedup if the CNAME DNS can not be found and hang the request)

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4040 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-01-23 15:38:54 +00:00
parent 8503ca915e
commit 1feab2e762

View File

@@ -56,10 +56,16 @@ class tcpclient
$i = 0; $i = 0;
while (empty ($this->ipv4) && empty ($this->ipv6) && $i < 10) while (empty ($this->ipv4) && empty ($this->ipv6) && $i < 10)
{ {
$nsRecords = @dns_get_record ($ipOrName, DNS_AAAA + DNS_A + DNS_CNAME); $nsRecords = @dns_get_record ($ipOrName, DNS_A + DNS_AAAA);
if ($nsRecords === false) if ($nsRecords === false)
throw new \Exception ("Can not find the IP for $ipOrName : ". {
"DNS Error", 500); // There is some problems with CNAME if they are not defined.
// So enter in this case only if there is no other solution
$nsRecords = @dns_get_record ($ipOrName, DNS_CNAME);
if ($nsRecords === false)
throw new \Exception ("Can not find the IP for $ipOrName : ".
"DNS Error (No A, AAAA, CNAME entries)", 500);
}
foreach ($nsRecords as $val) foreach ($nsRecords as $val)
{ {
if ($val["type"] === "CNAME") if ($val["type"] === "CNAME")
@@ -153,11 +159,12 @@ class tcpclient
if ($this->socket === null) if ($this->socket === null)
throw new \Exception ("Can not send to server $this->ipOrName : ". throw new \Exception ("Can not send to server $this->ipOrName : ".
"The server is not connected", 500); "The server is not connected", 500);
// Setting the options allow the IP to be decided by the connect and valid
// the certificate of the server by the name
$options = array ("ssl" => array ( $options = array ("ssl" => array (
"peer_name" => $this->ipOrName, "peer_name" => $this->ipOrName,
"verify_peer" => true,
"verify_peer_name" => true, "verify_peer_name" => true,
"capture_peer_cert" => true,
"capture_peer_cert_chain" => true,
"SNI_enabled" => true, "SNI_enabled" => true,
)); ));
stream_set_blocking ($this->socket, true); stream_set_blocking ($this->socket, true);
@@ -204,13 +211,13 @@ class tcpclient
{ {
$read = stream_get_line ($this->socket, $maxLength, "\r\n"); $read = stream_get_line ($this->socket, $maxLength, "\r\n");
if ($read === false) if ($read === false)
throw new \Exception ("Can not read from server", 500); throw new \Exception ("Can not read from server in text", 500);
} }
else else
{ {
$read = @fread ($this->socket, $maxLength); $read = fread ($this->socket, $maxLength);
if ($read === false) if ($read === false)
throw new \Exception ("Can not read from server" , 500); throw new \Exception ("Can not read from server in binary" , 500);
} }
return $read; return $read;
} }