initial commit for directory flags federation

This commit is contained in:
Mario Vavti
2023-04-26 23:01:56 +02:00
parent 60f0739c3e
commit bdb25315b6
7 changed files with 79 additions and 43 deletions

View File

@@ -37,13 +37,14 @@ class Onedirsync {
$h = Libzot::zot_record_preferred($h); $h = Libzot::zot_record_preferred($h);
if (($h) && (($h['hubloc_status'] & HUBLOC_OFFLINE) || $h['hubloc_deleted'] || $h['hubloc_error'])) { if (($h) && (($h['hubloc_status'] & HUBLOC_OFFLINE) || $h['hubloc_deleted'] || $h['hubloc_error'])) {
q("update updates set ud_flags = 9 where ud_hash = '%s' and ud_flags != 9",
dbesc($r[0]['ud_hash'])
);
// 2023-04-12: Flag the entry deleted but try to update anyway since the info is not always correct // 2023-04-12: Try to update anyway since the info is not always correct
// This might change after all directory servers run the new code. // This might change after all directory servers run the new code.
// q("update updates set ud_flags = 9 where ud_hash = '%s' and ud_flags != 9",
// dbesc($r[0]['ud_hash'])
//);
// return; // return;
} }

View File

@@ -170,7 +170,8 @@ class Poller {
$dirmode = intval(get_config('system', 'directory_mode')); $dirmode = intval(get_config('system', 'directory_mode'));
if ($dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY) { if ($dirmode == DIRECTORY_MODE_SECONDARY || $dirmode == DIRECTORY_MODE_PRIMARY) {
$r = q("SELECT * FROM updates WHERE ud_update = 1 AND (ud_last = '%s' OR ud_last > %s - INTERVAL %s)", $r = q("SELECT * FROM updates WHERE ud_update != %d AND (ud_last = '%s' OR ud_last > %s - INTERVAL %s)",
intval(DIRECTORY_UPDATE_OK),
dbesc(NULL_DATE), dbesc(NULL_DATE),
db_utcnow(), db_utcnow(),
db_quoteinterval('7 DAY') db_quoteinterval('7 DAY')

View File

@@ -965,7 +965,7 @@ class Libzot {
} }
// update updates if anything changed bump the ud_date // update updates if anything changed bump the ud_date
Libzotdir::update($xchan_hash, $address, $changed); Libzotdir::update($xchan_hash, $address, $changed, $r[0]['xchan_censored']);
if (empty($ret['message'])) { if (empty($ret['message'])) {
$ret['success'] = true; $ret['success'] = true;

View File

@@ -233,6 +233,8 @@ class Libzotdir {
if (! $r) if (! $r)
return; return;
$dir_trusted_hosts = get_directory_fallback_servers();
foreach ($r as $rr) { foreach ($r as $rr) {
if (! $rr['site_directory']) if (! $rr['site_directory'])
continue; continue;
@@ -264,8 +266,7 @@ class Libzotdir {
if (is_array($j['transactions']) && count($j['transactions'])) { if (is_array($j['transactions']) && count($j['transactions'])) {
foreach ($j['transactions'] as $t) { foreach ($j['transactions'] as $t) {
if (empty($t['hash']) || empty($t['host']) || empty($t['address'])) {
if (empty($t['hash'])) {
continue; continue;
} }
@@ -274,23 +275,40 @@ class Libzotdir {
); );
if ($r) { if ($r) {
$update = 0;
if ($r[0]['ud_date'] >= $t['timestamp']) { // no need to look at updates that originated from our own site
if ($t['host'] === z_root()) {
continue; continue;
} }
q("UPDATE updates SET ud_update = 1 WHERE ud_id = %d", // there is more recent xchan information
if ($r[0]['ud_date'] >= $t['timestamp']) {
$update = DIRECTORY_UPDATE_XCHAN;
}
// the host is trusted and flags have changed
if (in_array($t['host'], $dir_trusted_hosts) && intval($r[0]['ud_flags']) !== intval($t['flags'])) {
$update = (($update) ? DIRECTORY_UPDATE_BOTH : DIRECTORY_UPDATE_FLAGS);
}
if (!$update) {
continue;
}
q("UPDATE updates SET ud_update = %d WHERE ud_id = %d",
intval($update),
dbesc($r[0]['ud_id']) dbesc($r[0]['ud_id'])
); );
} }
else { else {
$t['transaction_id'] = strpos($t['transaction_id'], '@') === false ? $t['transaction_id'] : substr($t['transaction_id'], strpos($t['transaction_id'], '@') + 1); q("insert into updates ( ud_hash, ud_host, ud_date, ud_addr, ud_update, ud_flags )
q("insert into updates ( ud_hash, ud_guid, ud_date, ud_addr, ud_update ) values ( '%s', '%s', '%s', '%s', 1, %d) ",
values ( '%s', '%s', '%s', '%s', 1 ) ",
dbesc($t['hash']), dbesc($t['hash']),
dbesc($t['host'] ?? $t['transaction_id']), // 2023-04-12 transaction_id is deprecated dbesc($t['host']),
dbesc($t['timestamp']), dbesc($t['timestamp']),
dbesc($t['address']) dbesc($t['address']),
dbesc(in_array($t['host'], $dir_trusted_hosts) ? $t['flags'] : 0)
); );
} }
} }
@@ -318,15 +336,19 @@ class Libzotdir {
logger('update_directory_entry: ' . print_r($ud,true), LOGGER_DATA); logger('update_directory_entry: ' . print_r($ud,true), LOGGER_DATA);
if (empty($ud['ud_hash']) || empty($ud['ud_addr'])) { // set the flag if requested?
q("DELETE FROM updates WHERE ud_id = %d", if (in_array($ud['ud_flags'], [DIRECTORY_UPDATE_FLAGS, DIRECTORY_UPDATE_BOTH])) {
dbesc($ud['ud_id']) q("UPDATE xchan SET xchan_censored = %d WHERE xchan_hash = '%s'",
intval($ud['ud_flags']),
dbesc($ud['ud_hash'])
); );
return false; }
if (intval($ud['ud_flags']) === DIRECTORY_UPDATE_FLAGS) {
return true;
} }
$href = ((strpos($ud['ud_addr'], '://') === false) ? Webfinger::zot_url(punify($ud['ud_addr'])) : punify($ud['ud_addr'])); $href = ((strpos($ud['ud_addr'], '://') === false) ? Webfinger::zot_url(punify($ud['ud_addr'])) : punify($ud['ud_addr']));
if($href) { if($href) {
$zf = Zotfinger::exec($href); $zf = Zotfinger::exec($href);
if($zf && array_path_exists('signature/signer',$zf) && $zf['signature']['signer'] === $href && intval($zf['signature']['header_valid'])) { if($zf && array_path_exists('signature/signer',$zf) && $zf['signature']['signer'] === $href && intval($zf['signature']['header_valid'])) {
@@ -643,9 +665,11 @@ class Libzotdir {
* @param bool $bump_date (optional) default true * @param bool $bump_date (optional) default true
*/ */
static function update($hash, $addr, $bump_date = true) { static function update($hash, $addr, $bump_date = true, $flag = DIRECTORY_FLAG_OK) {
hz_syslog('flag: '. print_r($flag, true));
$dirmode = intval(get_config('system', 'directory_mode')); $dirmode = intval(get_config('system', 'directory_mode'));
hz_syslog('gothere');
if($dirmode == DIRECTORY_MODE_NORMAL) { if($dirmode == DIRECTORY_MODE_NORMAL) {
return; return;
@@ -655,7 +679,7 @@ class Libzotdir {
return; return;
} }
$u = q("SELECT ud_id FROM updates WHERE ud_hash = '%s' LIMIT 1", $u = q("SELECT * FROM updates WHERE ud_hash = '%s' LIMIT 1",
dbesc($hash) dbesc($hash)
); );
@@ -665,21 +689,23 @@ class Libzotdir {
} }
if ($u) { if ($u) {
$x = q("UPDATE updates SET $date_sql ud_last = '%s', ud_guid = '%s', ud_addr = '%s', ud_flags = 0 WHERE ud_id = %d", $x = q("UPDATE updates SET $date_sql ud_last = '%s', ud_host = '%s', ud_addr = '%s', ud_update = 0, ud_flags = %d WHERE ud_id = %d",
dbesc(NULL_DATE), dbesc(NULL_DATE),
dbesc(\App::get_hostname()), dbesc(z_root()),
dbesc($addr), dbesc($addr),
intval($flag),
intval($u[0]['ud_id']) intval($u[0]['ud_id'])
); );
return; return;
} }
q("INSERT INTO updates (ud_hash, ud_guid, ud_date, ud_addr ) VALUES ( '%s', '%s', '%s', '%s' )", q("INSERT INTO updates (ud_hash, ud_host, ud_date, ud_addr, ud_flags) VALUES ( '%s', '%s', '%s', '%s', %d )",
dbesc($hash), dbesc($hash),
dbesc(\App::get_hostname()), dbesc(z_root()),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc($addr) dbesc($addr),
intval($flag)
); );
return; return;

View File

@@ -4,6 +4,7 @@ namespace Zotlabs\Module;
use App; use App;
use Zotlabs\Web\Controller; use Zotlabs\Web\Controller;
use Zotlabs\Lib\Libzotdir;
class Dircensor extends Controller { class Dircensor extends Controller {
@@ -20,6 +21,7 @@ class Dircensor extends Controller {
} }
$xchan = argv(1); $xchan = argv(1);
if(! $xchan) { if(! $xchan) {
return; return;
} }
@@ -33,25 +35,28 @@ class Dircensor extends Controller {
} }
$severity = intval($_REQUEST['severity'] ?? 0); $severity = intval($_REQUEST['severity'] ?? 0);
$flag = DIRECTORY_FLAG_OK;
if ($severity < 0) { if ($severity === 1) {
$severity = 0; $flag = DIRECTORY_FLAG_UNSAFE;
} }
if ($severity > 2) { if ($severity === 2) {
$severity = 2; $flag = DIRECTORY_FLAG_HIDDEN;
} }
hz_syslog('flag: '. print_r($flag, true));
Libzotdir::update($xchan, $r[0]['xchan_url'], true, $flag);
q("update xchan set xchan_censored = %d where xchan_hash = '%s'", q("update xchan set xchan_censored = %d where xchan_hash = '%s'",
intval($severity), intval($flag),
dbesc($xchan) dbesc($xchan)
); );
if($severity) { if($flag) {
info( t('Entry censored') . EOL); info( t('Entry censored') . EOL);
} }
else { else {
info( t('Entry uncensored') . EOL); info( t('Entry OK') . EOL);
} }
goaway(z_root() . '/directory'); goaway(z_root() . '/directory');

View File

@@ -228,8 +228,8 @@ class Dirsearch extends Controller {
$spkt['transactions'][] = [ $spkt['transactions'][] = [
'hash' => $rr['ud_hash'], 'hash' => $rr['ud_hash'],
'address' => $rr['ud_addr'], 'address' => $rr['ud_addr'],
'host' => $rr['ud_guid'], 'host' => $rr['ud_host'],
'transaction_id' => $rr['ud_guid'], // deprecated 2023-04-12 'transaction_id' => $rr['ud_host'], // deprecated 2023-04-12
'timestamp' => $rr['ud_date'], 'timestamp' => $rr['ud_date'],
'flags' => $rr['ud_flags'] 'flags' => $rr['ud_flags']
]; ];

View File

@@ -84,6 +84,15 @@ define('DIRECTORY_MODE_PRIMARY', 0x0001); // There can only be *one* primary dir
define('DIRECTORY_MODE_SECONDARY', 0x0002); // All other mirror directory servers define('DIRECTORY_MODE_SECONDARY', 0x0002); // All other mirror directory servers
define('DIRECTORY_MODE_STANDALONE', 0x0100); // A detached (off the grid) hub with itself as directory server. define('DIRECTORY_MODE_STANDALONE', 0x0100); // A detached (off the grid) hub with itself as directory server.
define('DIRECTORY_UPDATE_OK', 0);
define('DIRECTORY_UPDATE_XCHAN', 1);
define('DIRECTORY_UPDATE_FLAGS', 2);
define('DIRECTORY_UPDATE_BOTH', 3);
define('DIRECTORY_FLAG_OK', 0);
define('DIRECTORY_FLAG_UNSAFE', 1);
define('DIRECTORY_FLAG_HIDDEN', 2);
// We will look for upstream directories whenever me make contact // We will look for upstream directories whenever me make contact
// with other sites, but if this is a new installation and isn't // with other sites, but if this is a new installation and isn't
// a standalone hub, we need to seed the service with a starting // a standalone hub, we need to seed the service with a starting
@@ -96,8 +105,7 @@ define('DIRECTORY_FALLBACK_MASTER', 'https://hub.netzgemeinde.eu');
function get_directory_fallback_servers() { function get_directory_fallback_servers() {
$ret = [ $ret = [
'https://hub.netzgemeinde.eu', 'https://hub.netzgemeinde.eu',
'https://zotsite.net', 'https://zotsite.net'
'https://hub.libranet.de'
]; ];
return $ret; return $ret;
@@ -364,11 +372,6 @@ define('POLL_TENSCALE', 0x0002); // 1-10
define('POLL_MULTIPLE_CHOICE', 0x0004); define('POLL_MULTIPLE_CHOICE', 0x0004);
define('POLL_OVERWRITE', 0x8000); // If you vote twice remove the prior entry define('POLL_OVERWRITE', 0x8000); // If you vote twice remove the prior entry
define('UPDATE_FLAGS_UPDATED', 0x0001);
define('UPDATE_FLAGS_FORCED', 0x0002);
define('UPDATE_FLAGS_DELETED', 0x1000);
define('HUBLOC_OFFLINE', 0x0001); define('HUBLOC_OFFLINE', 0x0001);
define('DROPITEM_NORMAL', 0); define('DROPITEM_NORMAL', 0);