mirror of
https://github.com/friendica/friendica
synced 2024-11-18 07:03:40 +00:00
Merge pull request #14229 from annando/probing
Reduce probing / handle exception for invalid url / always check for https
This commit is contained in:
commit
11f305de68
3 changed files with 30 additions and 18 deletions
|
@ -2124,7 +2124,12 @@ class BBCode
|
||||||
}
|
}
|
||||||
|
|
||||||
$parts['host'] = idn_to_ascii(urldecode($parts['host']));
|
$parts['host'] = idn_to_ascii(urldecode($parts['host']));
|
||||||
|
try {
|
||||||
return (string)Uri::fromParts($parts);
|
return (string)Uri::fromParts($parts);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
Logger::notice('Exception on unparsing url', ['url' => $url, 'parts' => $parts, 'code' => $th->getCode(), 'message' => $th->getMessage()]);
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function unifyLinks(string $text): string
|
private static function unifyLinks(string $text): string
|
||||||
|
|
|
@ -2653,6 +2653,14 @@ class Contact
|
||||||
|
|
||||||
$data = Probe::uri($contact['url'], $network, $contact['uid']);
|
$data = Probe::uri($contact['url'], $network, $contact['uid']);
|
||||||
|
|
||||||
|
if (in_array($data['network'], Protocol::FEDERATED) && (parse_url($data['url'], PHP_URL_SCHEME) == 'http')) {
|
||||||
|
$ssl_url = str_replace('http://', 'https://', $contact['url']);
|
||||||
|
$ssl_data = Probe::uri($ssl_url, $network, $contact['uid']);
|
||||||
|
if (($ssl_data['network'] == $data['network']) && (parse_url($ssl_data['url'], PHP_URL_SCHEME) != 'http')) {
|
||||||
|
$data = $ssl_data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($data['network'] == Protocol::DIASPORA) {
|
if ($data['network'] == Protocol::DIASPORA) {
|
||||||
try {
|
try {
|
||||||
DI::dsprContact()->updateFromProbeArray($data);
|
DI::dsprContact()->updateFromProbeArray($data);
|
||||||
|
@ -2811,7 +2819,7 @@ class Contact
|
||||||
// We must not try to update relay contacts via probe. They are no real contacts.
|
// We must not try to update relay contacts via probe. They are no real contacts.
|
||||||
// See Relay::updateContact() for more details.
|
// See Relay::updateContact() for more details.
|
||||||
// We check after the probing to be able to correct falsely detected contact types.
|
// We check after the probing to be able to correct falsely detected contact types.
|
||||||
if (($contact['contact-type'] == self::TYPE_RELAY) && Strings::compareLink($contact['url'], $contact['baseurl']) &&
|
if (($contact['contact-type'] == self::TYPE_RELAY) && Strings::compareLink($contact['url'], $contact['baseurl'] ?? '') &&
|
||||||
(!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))
|
(!Strings::compareLink($ret['url'], $contact['url']) || in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]))
|
||||||
) {
|
) {
|
||||||
if (GServer::reachable($contact)) {
|
if (GServer::reachable($contact)) {
|
||||||
|
|
|
@ -244,7 +244,7 @@ class Probe
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_object($xrd) && !empty($url)) {
|
if ($ssl_connection_error && !is_object($xrd) && !empty($url)) {
|
||||||
$curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
|
$curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
|
||||||
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
|
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
|
||||||
if ($curlResult->isTimeout()) {
|
if ($curlResult->isTimeout()) {
|
||||||
|
@ -568,7 +568,7 @@ class Probe
|
||||||
}
|
}
|
||||||
|
|
||||||
$webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
|
$webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
|
||||||
if (empty($webfinger)) {
|
if (empty($webfinger) && !is_null($webfinger)) {
|
||||||
$lrdd = self::hostMeta($host);
|
$lrdd = self::hostMeta($host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -582,7 +582,7 @@ class Probe
|
||||||
}
|
}
|
||||||
|
|
||||||
$webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
|
$webfinger = self::getWebfinger($parts['scheme'] . '://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
|
||||||
if (empty($webfinger)) {
|
if (empty($webfinger) && !is_null($webfinger)) {
|
||||||
$lrdd = self::hostMeta($host);
|
$lrdd = self::hostMeta($host);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -604,11 +604,12 @@ class Probe
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($webfinger)) {
|
if (is_null($webfinger)) {
|
||||||
$webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
|
$webfinger = self::getWebfinger('http://' . $host . self::WEBFINGER, HttpClientAccept::JRD_JSON, $uri, $addr);
|
||||||
if (self::$isTimeout) {
|
if (self::$isTimeout || is_null($webfinger)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
$baseurl = 'http://' . $host;
|
||||||
} else {
|
} else {
|
||||||
$baseurl = 'https://' . $host;
|
$baseurl = 'https://' . $host;
|
||||||
}
|
}
|
||||||
|
@ -619,8 +620,6 @@ class Probe
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
$baseurl = self::$baseurl;
|
$baseurl = self::$baseurl;
|
||||||
} else {
|
|
||||||
$baseurl = 'http://' . $host;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Logger::info('URI was not detectable', ['uri' => $uri]);
|
Logger::info('URI was not detectable', ['uri' => $uri]);
|
||||||
|
@ -661,11 +660,11 @@ class Probe
|
||||||
*
|
*
|
||||||
* @return array webfinger results
|
* @return array webfinger results
|
||||||
*/
|
*/
|
||||||
private static function getWebfinger(string $template, string $type, string $uri, string $addr): array
|
private static function getWebfinger(string $template, string $type, string $uri, string $addr): ?array
|
||||||
{
|
{
|
||||||
if (Network::isUrlBlocked($template)) {
|
if (Network::isUrlBlocked($template)) {
|
||||||
Logger::info('Domain is blocked', ['url' => $template]);
|
Logger::info('Domain is blocked', ['url' => $template]);
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// First try the address because this is the primary purpose of webfinger
|
// First try the address because this is the primary purpose of webfinger
|
||||||
|
@ -673,8 +672,8 @@ class Probe
|
||||||
$detected = $addr;
|
$detected = $addr;
|
||||||
$path = str_replace('{uri}', urlencode('acct:' . $addr), $template);
|
$path = str_replace('{uri}', urlencode('acct:' . $addr), $template);
|
||||||
$webfinger = self::webfinger($path, $type);
|
$webfinger = self::webfinger($path, $type);
|
||||||
if (self::$isTimeout) {
|
if (is_null($webfinger)) {
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -683,8 +682,8 @@ class Probe
|
||||||
$detected = $uri;
|
$detected = $uri;
|
||||||
$path = str_replace('{uri}', urlencode($uri), $template);
|
$path = str_replace('{uri}', urlencode($uri), $template);
|
||||||
$webfinger = self::webfinger($path, $type);
|
$webfinger = self::webfinger($path, $type);
|
||||||
if (self::$isTimeout) {
|
if (is_null($webfinger)) {
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1001,7 +1000,7 @@ class Probe
|
||||||
* @return array webfinger data
|
* @return array webfinger data
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
private static function webfinger(string $url, string $type): array
|
private static function webfinger(string $url, string $type): ?array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$curlResult = DI::httpClient()->get(
|
$curlResult = DI::httpClient()->get(
|
||||||
|
@ -1011,12 +1010,12 @@ class Probe
|
||||||
);
|
);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
Logger::notice($e->getMessage(), ['url' => $url, 'type' => $type, 'class' => get_class($e)]);
|
Logger::notice($e->getMessage(), ['url' => $url, 'type' => $type, 'class' => get_class($e)]);
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($curlResult->isTimeout()) {
|
if ($curlResult->isTimeout()) {
|
||||||
self::$isTimeout = true;
|
self::$isTimeout = true;
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
$data = $curlResult->getBodyString();
|
$data = $curlResult->getBodyString();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue