Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions app/controllers/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Executor\Executor;
use MaxMind\Db\Reader;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Table;
use Utopia\Config\Config;
use Utopia\Console;
use Utopia\Database\Database;
Expand Down Expand Up @@ -1073,21 +1074,20 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S
->inject('queueForCertificates')
->inject('platform')
->inject('authorization')
->action(function (Request $request, Document $console, Database $dbForPlatform, Certificate $queueForCertificates, array $platform, Authorization $authorization) {
->inject('certifiedDomains')
->action(function (Request $request, Document $console, Database $dbForPlatform, Certificate $queueForCertificates, array $platform, Authorization $authorization, Table $certifiedDomains) {
$hostname = $request->getHostname();
$cache = Config::getParam('hostnames', []);
$platformHostnames = $platform['hostnames'] ?? [];

// 1. Cache hit
if (array_key_exists($hostname, $cache)) {
if ($certifiedDomains->exists(md5($hostname))) {
return;
}

// 2. Domain validation
$domain = new Domain(!empty($hostname) ? $hostname : '');
if (empty($domain->get()) || !$domain->isKnown() || $domain->isTest()) {
$cache[$domain->get()] = false;
Config::setParam('hostnames', $cache);
$certifiedDomains->set(md5($domain->get()), ['value' => 0]);
return;
}

Expand All @@ -1101,7 +1101,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S
}

// 4. Check/create rule (requires DB access)
$authorization->skip(function () use ($dbForPlatform, $domain, $console, $queueForCertificates, &$cache) {
$authorization->skip(function () use ($dbForPlatform, $domain, $console, $queueForCertificates, $certifiedDomains) {
try {
// TODO: (@Meldiron) Remove after 1.7.x migration
$isMd5 = System::getEnv('_APP_RULES_FORMAT') === 'md5';
Expand Down Expand Up @@ -1164,8 +1164,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S
} catch (Duplicate $e) {
Console::info('Certificate already exists');
} finally {
$cache[$domain->get()] = true;
Config::setParam('hostnames', $cache);
$certifiedDomains->set(md5($domain->get()), ['value' => 1]);
}
});
});
Expand Down
26 changes: 18 additions & 8 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@
$files = new Files();
$files->load(__DIR__ . '/../public');

$domains = new Table(1_000_000); // 1 million rows
$domains->column('value', Table::TYPE_INT, 1);
$domains->create();
$riskyDomains = new Table(100_000);
$riskyDomains->column('value', Table::TYPE_INT, 1);
$riskyDomains->create();

$certifiedDomains = new Table(100_000);
$certifiedDomains->column('value', Table::TYPE_INT, 1);
$certifiedDomains->create();

Http::setResource('riskyDomains', fn () => $riskyDomains);
Http::setResource('certifiedDomains', fn () => $certifiedDomains);

$http = new Server(
host: "0.0.0.0",
Expand Down Expand Up @@ -71,7 +78,7 @@
function dispatch(Server $server, int $fd, int $type, $data = null): int
{
$resolveWorkerId = function (Server $server, $data = null) {
global $totalWorkers, $domains;
global $totalWorkers, $riskyDomains;

// If data is not set we can send request to any worker
// first we try to pick idle worker, if not we randomly pick a worker
Expand Down Expand Up @@ -103,7 +110,7 @@ function dispatch(Server $server, int $fd, int $type, $data = null): int
$risky = false;
if (str_starts_with($request, 'POST') && str_contains($request, '/executions')) {
$risky = true;
} elseif ($domains->get(md5($domain), 'value') === 1) {
} elseif ($riskyDomains->get(md5($domain), 'value') === 1) {
// executions request coming from custom domain
$risky = true;
} else {
Expand Down Expand Up @@ -579,7 +586,7 @@ function createDatabase(Http $app, string $resourceKey, string $dbName, array $c
});

// Fetch domains every `DOMAIN_SYNC_TIMER` seconds and update in the memory
$http->on(Constant::EVENT_TASK, function () use ($register, $domains) {
$http->on(Constant::EVENT_TASK, function () use ($register) {
$lastSyncUpdate = null;
$pools = $register->get('pools');
Http::setResource('pools', fn () => $pools);
Expand All @@ -588,7 +595,10 @@ function createDatabase(Http $app, string $resourceKey, string $dbName, array $c
/** @var Utopia\Database\Database $dbForPlatform */
$dbForPlatform = $app->getResource('dbForPlatform');

Timer::tick(DOMAIN_SYNC_TIMER * 1000, function () use ($dbForPlatform, $domains, &$lastSyncUpdate, $app) {
/** @var Table $riskyDomains */
$riskyDomains = $app->getResource('riskyDomains');

Timer::tick(DOMAIN_SYNC_TIMER * 1000, function () use ($dbForPlatform, $riskyDomains, &$lastSyncUpdate, $app) {
try {
$time = DateTime::now();
$limit = 1000;
Expand Down Expand Up @@ -641,7 +651,7 @@ function createDatabase(Http $app, string $resourceKey, string $dbName, array $c
continue;
}

$domains->set(md5($domain), ['value' => 1]);
$riskyDomains->set(md5($domain), ['value' => 1]);
}
$latestDocument = !empty(array_key_last($results)) ? $results[array_key_last($results)] : null;
}
Expand Down
Loading