Skip to content

Commit

Permalink
Merge pull request friendica#14819 from annando/redirected
Browse files Browse the repository at this point in the history
Handle redirected servers in server detection.
  • Loading branch information
MrPetovan authored Feb 16, 2025
2 parents abc38be + 1bc2cfa commit c746017
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 15 deletions.
6 changes: 4 additions & 2 deletions database.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- ------------------------------------------
-- Friendica 2025.02-dev (Interrupted Fern)
-- DB_UPDATE_VERSION 1577
-- DB_UPDATE_VERSION 1578
-- ------------------------------------------


Expand Down Expand Up @@ -41,10 +41,12 @@ CREATE TABLE IF NOT EXISTS `gserver` (
`blocked` boolean COMMENT 'Server is blocked',
`failed` boolean COMMENT 'Connection failed',
`next_contact` datetime DEFAULT '0001-01-01 00:00:00' COMMENT 'Next connection request',
`redirect-gsid` int unsigned COMMENT 'Target Gserver id in case of a redirect',
PRIMARY KEY(`id`),
UNIQUE INDEX `nurl` (`nurl`(190)),
INDEX `next_contact` (`next_contact`),
INDEX `network` (`network`)
INDEX `network` (`network`),
FOREIGN KEY (`redirect-gsid`) REFERENCES `gserver` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Global servers';

--
Expand Down
7 changes: 7 additions & 0 deletions doc/database/db_gserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Fields
| blocked | Server is blocked | boolean | YES | | NULL | |
| failed | Connection failed | boolean | YES | | NULL | |
| next_contact | Next connection request | datetime | YES | | 0001-01-01 00:00:00 | |
| redirect-gsid | Target Gserver id in case of a redirect | int unsigned | YES | | NULL | |

Indexes
------------
Expand All @@ -52,5 +53,11 @@ Indexes
| next_contact | next_contact |
| network | network |

Foreign Keys
------------

| Field | Target Table | Target Field |
|-------|--------------|--------------|
| redirect-gsid | [gserver](help/database/db_gserver) | id |

Return to [database documentation](help/database)
6 changes: 3 additions & 3 deletions src/Database/PostUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ private static function update1348()

DBA::update(
'contact',
['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
['gsid' => GServer::getRealID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
['id' => $contact['id']]
);

Expand Down Expand Up @@ -705,7 +705,7 @@ private static function update1349()

DBA::update(
'apcontact',
['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
['gsid' => GServer::getRealID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
['url' => $apcontact['url']]
);

Expand Down Expand Up @@ -1243,7 +1243,7 @@ private static function update1506()

DBA::update(
'contact',
['gsid' => GServer::getID($server, true), 'baseurl' => GServer::cleanURL($server)],
['gsid' => GServer::getRealID($server, true), 'baseurl' => GServer::cleanURL($server)],
['id' => $contact['id']]
);

Expand Down
2 changes: 1 addition & 1 deletion src/Model/APContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ private static function compactProfile(array $apcontact, array $compacted, strin
}

if (!empty($apcontact['baseurl']) && empty($fetched_contact['gsid'])) {
$apcontact['gsid'] = GServer::getID($apcontact['baseurl']);
$apcontact['gsid'] = GServer::getRealID($apcontact['baseurl']);
} elseif (!empty($fetched_contact['gsid'])) {
$apcontact['gsid'] = $fetched_contact['gsid'];
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static function selectFirstAccountUser(array $fields = [], array $conditi
public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT): int
{
if (!empty($fields['baseurl']) && empty($fields['gsid'])) {
$fields['gsid'] = GServer::getID($fields['baseurl'], true);
$fields['gsid'] = GServer::getRealID($fields['baseurl'], true);
}

$fields['uri-id'] = ItemURI::getIdByURI($fields['url']);
Expand Down Expand Up @@ -913,7 +913,7 @@ public static function updateSelfFromUserID(int $uid, bool $update_avatar = fals
$fields['unsearchable'] = !$profile['net-publish'];
$fields['manually-approve'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP, User::PAGE_FLAGS_COMM_MAN]);
$fields['baseurl'] = DI::baseUrl();
$fields['gsid'] = GServer::getID($fields['baseurl'], true);
$fields['gsid'] = GServer::getRealID($fields['baseurl'], true);

$update = false;

Expand Down
33 changes: 32 additions & 1 deletion src/Model/GServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ public static function getID(string $url, bool $no_check = false): ?int
return self::getID($url, true);
}


/**
* Get the real ID for the given server URL, follows redirects.
*
* @param string $url
* @param boolean $no_check Don't check if the server hadn't been found
*
* @return int|null gserver id or NULL on empty URL or failed check
*/
public static function getRealID(string $url, bool $no_check = false): ?int
{
$gsid = self::getID($url, $no_check);
if (empty($gsid)) {
return $gsid;
}

$gserver = DBA::selectFirst('gserver', ['redirect-gsid'], ['id' => $gsid]);
if (!empty($gserver['redirect-gsid'])) {
$redirect = DBA::selectFirst('gserver', ['failed'], ['id' => $gserver['redirect-gsid']]);
if (isset($redirect['failed']) && !$redirect['failed']) {
return $gserver['redirect-gsid'];
}
}
return $gsid;
}

/**
* Retrieves all the servers which base domain are matching the provided domain pattern
*
Expand Down Expand Up @@ -569,8 +595,13 @@ private static function detect(string $url, string $network = '', bool $only_nod
(((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) {
DI::logger()->debug('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $valid_url]);
self::setFailureByUrl($url);
if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
$target_id = self::getID($valid_url, true);
if (!$target_id && !Network::isUrlBlocked($valid_url)) {
self::detect($valid_url, $network, $only_nodeinfo);
$target_id = self::getID($valid_url, true);
}
if ($target_id) {
self::update(['redirect-gsid' => $target_id], ['nurl' => Strings::normaliseLink($url)]);
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private static function createSystemAccount()
$system['thumb'] = Contact::getDefaultAvatar($system, Proxy::SIZE_THUMB);
$system['micro'] = Contact::getDefaultAvatar($system, Proxy::SIZE_MICRO);
$system['nurl'] = Strings::normaliseLink($system['url']);
$system['gsid'] = GServer::getID($system['baseurl']);
$system['gsid'] = GServer::getRealID($system['baseurl']);

Contact::insert($system);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Network/Probe.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static function rearrangeData(array $data): array
$newdata['baseurl'] = $data['networks'][$network]['baseurl'];
}
if (!empty($newdata['baseurl'])) {
$newdata['gsid'] = $data['networks'][$network]['gsid'] = GServer::getID($newdata['baseurl']);
$newdata['gsid'] = $data['networks'][$network]['gsid'] = GServer::getRealID($newdata['baseurl']);
} else {
$newdata['gsid'] = $data['networks'][$network]['gsid'] = null;
}
Expand Down Expand Up @@ -436,7 +436,7 @@ public static function uri(string $uri, string $network = '', int $uid = -1): ar
}

if (!empty($data['baseurl']) && empty($data['gsid'])) {
$data['gsid'] = GServer::getID($data['baseurl']);
$data['gsid'] = GServer::getRealID($data['baseurl']);
}

// Ensure that local connections always are DFRN
Expand Down Expand Up @@ -2164,7 +2164,7 @@ private static function localProbe(string $url): array
$split_name = Diaspora::splitName($owner['name']);

if (empty($owner['gsid'])) {
$owner['gsid'] = GServer::getID($approfile['generator']['url']);
$owner['gsid'] = GServer::getRealID($approfile['generator']['url']);
}

$data = [
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/ATProtocol/Actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function updateContactByDID(string $did, int $contact_uid): void

if (!empty($fields['baseurl'])) {
GServer::check($fields['baseurl'], Protocol::BLUESKY);
$fields['gsid'] = GServer::getID($fields['baseurl'], true);
$fields['gsid'] = GServer::getRealID($fields['baseurl'], true);
}

foreach ($directory->verificationMethod as $method) {
Expand Down
3 changes: 2 additions & 1 deletion static/dbstructure.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

// This file is required several times during the test in DbaDefinition which justifies this condition
if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1577);
define('DB_UPDATE_VERSION', 1578);
}

return [
Expand Down Expand Up @@ -85,6 +85,7 @@
"blocked" => ["type" => "boolean", "comment" => "Server is blocked"],
"failed" => ["type" => "boolean", "comment" => "Connection failed"],
"next_contact" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => "Next connection request"],
"redirect-gsid" => ["type" => "int unsigned", "foreign" => ["gserver" => "id"], "comment" => "Target Gserver id in case of a redirect"],
],
"indexes" => [
"PRIMARY" => ["id"],
Expand Down

0 comments on commit c746017

Please sign in to comment.