revert clearing primary on dead channels. Mark the hubloc deleted instead. Zotfinger will redirect to any living clones we know about. By default, Zotfinger requests will recurse, but this can be disabled if needed.

This commit is contained in:
nobody 2022-01-23 02:13:59 -08:00
parent 0ebb2eebe5
commit cbd8c77873
4 changed files with 86 additions and 62 deletions

View file

@ -7,7 +7,7 @@ use Zotlabs\Web\HTTPSig;
class Zotfinger
{
public static function exec($resource, $channel = null, $verify = true)
public static function exec($resource, $channel = null, $verify = true, $recurse = true)
{
if (!$resource) {
@ -42,39 +42,29 @@ class Zotfinger
$redirects = 0;
$x = z_post_url($resource, $data, $redirects, ['headers' => $h]);
if (intval($x['return_code'] === 404)) {
if (in_array(intval($x['return_code']), [ 404, 410 ]) && $recurse) {
// if this resource returns "not found", mark any corresponding hubloc deleted and
// change the primary if needed. We need to catch it at this level because we
// can't really sync the locations if we've got no data to work with.
$h = Activity::get_actor_hublocs($resource, 'zot6,not_deleted');
// The resource has been deleted or doesn't exist at this location.
// Try to find another nomadic resource for this channel and return that.
// First, see if there's a hubloc for this site. Fetch that record to
// obtain the nomadic identity hash. Then use that to find any additional
// nomadic locations.
$h = Activity::get_actor_hublocs($resource, 'zot6');
if ($h) {
$primary = intval($h[0]['hubloc_primary']);
q("update hubloc set hubloc_deleted = 1, hubloc_primary = 0 where hubloc_id = %d",
intval($h[0]['hubloc_id'])
);
if ($primary) {
// find another hub that can act as primary since this one cannot. If this
// fails, it may be that there are no other instances of the channel known
// to this site. In that case, we'll just leave the entry without a primary
// until/id we hear from them again at a new location.
$a = q("select * from hubloc where hubloc_hash = '%s' and hubloc_deleted = 0",
dbesc($h[0]['hubloc_hash'])
);
if ($a) {
$new_primary = array_shift($a);
q("update hubloc set hubloc_primary = 1 where hubloc_id = %d",
intval($new_primary['hubloc_id'])
);
$new_primary['hubloc_primary'] = 1;
hubloc_change_primary($new_primary);
// mark this location deleted
hubloc_delete($h[0]);
$hubs = Activity::get_actor_hublocs($h[0]['hubloc_hash']);
if ($hubs) {
foreach ($hubs as $hub) {
if ($hub['hubloc_id_url'] !== $resource and !$hub['hubloc_deleted']) {
return $self::exec($hub['hubloc_id_url'],$channel,$verify);
}
}
}
}
}
}
if ($x['success']) {
if ($verify) {

View file

@ -390,32 +390,21 @@ function remove_all_xchan_resources($xchan, $channel_id = 0)
dbesc($xchan)
);
if ($dirmode === false || $dirmode == DIRECTORY_MODE_NORMAL) {
$r = q(
"delete from xchan where xchan_hash = '%s'",
dbesc($xchan)
);
$r = q(
"delete from hubloc where hubloc_hash = '%s'",
dbesc($xchan)
);
$r = q(
"delete from xprof where xprof_hash = '%s'",
dbesc($xchan)
);
} else {
// directory servers need to keep the record around for sync purposes - mark it deleted
$r = q(
"update hubloc set hubloc_deleted = 1 where hubloc_hash = '%s'",
dbesc($xchan)
);
$r = q(
"update hubloc set hubloc_deleted = 1 where hubloc_hash = '%s'",
dbesc($xchan)
);
$r = q(
"update xchan set xchan_deleted = 1 where xchan_hash = '%s'",
dbesc($xchan)
);
$r = q(
"delete from xprof where xprof_hash = '%s'",
dbesc($xchan)
);
$r = q(
"update xchan set xchan_deleted = 1 where xchan_hash = '%s'",
dbesc($xchan)
);
}
}
}

View file

@ -17,7 +17,8 @@ use Zotlabs\Daemon\Run;
*/
function hubloc_store_lowlevel($arr)
{
$update = array_key_exists('hubloc_id',$arr) && $arr['hubloc_id'] ? 'hubloc_id = ' . intval($arr['hubloc_id']) : false;
$store = [
'hubloc_guid' => ((array_key_exists('hubloc_guid', $arr)) ? $arr['hubloc_guid'] : ''),
'hubloc_guid_sig' => ((array_key_exists('hubloc_guid_sig', $arr)) ? $arr['hubloc_guid_sig'] : ''),
@ -41,8 +42,8 @@ function hubloc_store_lowlevel($arr)
'hubloc_error' => ((array_key_exists('hubloc_error', $arr)) ? $arr['hubloc_error'] : 0),
'hubloc_deleted' => ((array_key_exists('hubloc_deleted', $arr)) ? $arr['hubloc_deleted'] : 0)
];
return create_table_from_array('hubloc', $store);
return $update ? update_table_from_array('hubloc', $store, $update) : create_table_from_array('hubloc', $store);
}
function site_store_lowlevel($arr)
@ -292,7 +293,14 @@ function hubloc_mark_as_down($posturl)
);
}
function hubloc_delete($hubloc) {
if (is_array($hubloc) && array_key_exists('hubloc_id',$hubloc)) {
q("update hubloc set hubloc_deleted = 1 where hubloc_id = %d",
intval($hubloc['hubloc_id'])
);
}
}
/**
* @brief return comma separated string of non-dead clone locations (net addresses) for a given netid
*
@ -316,18 +324,17 @@ function locations_by_netid($netid)
function ping_site($url)
{
$ret = [ 'success' => false ];
$ret = array('success' => false);
$r = Zotlabs\Lib\Zotfinger::exec($url);
$r = Zotlabs\Lib\Zotfinger::exec($url);
if (! $r['data']) {
$ret['message'] = 'no answer from ' . $url;
return $ret;
}
$ret['success'] = true;
return $ret;
$ret['success'] = true;
return $ret;
}

View file

@ -3742,6 +3742,44 @@ function create_table_from_array($table, $arr, $binary_fields = [])
return $r;
}
function update_table_from_array($table, $arr, $where, $binary_fields = [])
{
if (! ($arr && $table)) {
return false;
}
$columns = db_columns($table);
$clean = [];
foreach ($arr as $k => $v) {
if (! in_array($k, $columns)) {
continue;
}
$matches = false;
if (preg_match('/([^a-zA-Z0-9\-\_\.])/', $k, $matches)) {
return false;
}
if (in_array($k, $binary_fields)) {
$clean[$k] = dbescbin($v);
} else {
$clean[$k] = dbesc($v);
}
}
$sql = "UPDATE " . TQUOT . $table . TQUOT . " SET ";
foreach ($clean as $k => $v) {
$sql .= TQUOT . $k . TQUOT . ' = "' . $v . '",';
}
$sql = rtrim($sql,',');
$r = dbq($sql . " WHERE " . $where);
return $r;
}
function share_shield($m)
{
return str_replace($m[1], '!=+=+=!' . base64url_encode($m[1]) . '=+!=+!=', $m[0]);