1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00
appwrite/app/realtime.php

581 lines
21 KiB
PHP
Raw Normal View History

2020-10-16 20:31:09 +13:00
<?php
use Appwrite\Auth\Auth;
2021-06-25 00:22:32 +12:00
use Appwrite\Event\Event;
2021-06-29 02:34:28 +12:00
use Appwrite\Messaging\Adapter\Realtime;
2021-06-25 00:22:32 +12:00
use Appwrite\Network\Validator\Origin;
2021-08-27 21:20:49 +12:00
use Appwrite\Utopia\Response;
2021-06-25 00:22:32 +12:00
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Runtime;
use Swoole\Table;
use Swoole\Timer;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\TimeLimit;
2021-06-16 21:09:12 +12:00
use Utopia\App;
2021-06-25 00:22:32 +12:00
use Utopia\CLI\Console;
2021-10-01 00:18:50 +13:00
use Utopia\Database\Database;
use Utopia\Cache\Adapter\Redis as RedisCache;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
2021-06-25 00:22:32 +12:00
use Utopia\Swoole\Request;
use Utopia\WebSocket\Server;
use Utopia\WebSocket\Adapter;
2021-02-25 06:12:38 +13:00
require_once __DIR__ . '/init.php';
2020-10-16 20:31:09 +13:00
2021-06-25 00:22:32 +12:00
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
2020-10-19 00:51:16 +13:00
2021-08-19 03:44:11 +12:00
$realtime = new Realtime();
2021-06-25 00:22:32 +12:00
2021-08-19 03:44:11 +12:00
/**
* Table for statistics across all workers.
*/
2021-06-25 00:22:32 +12:00
$stats = new Table(4096, 1);
$stats->column('projectId', Table::TYPE_STRING, 64);
2021-08-19 22:14:19 +12:00
$stats->column('teamId', Table::TYPE_STRING, 64);
2021-06-25 00:22:32 +12:00
$stats->column('connections', Table::TYPE_INT);
$stats->column('connectionsTotal', Table::TYPE_INT);
$stats->column('messages', Table::TYPE_INT);
$stats->create();
2021-07-01 22:31:48 +12:00
$containerId = uniqid();
$documentId = null;
2021-08-19 03:44:11 +12:00
$adapter = new Adapter\Swoole(port: App::getEnv('PORT', 80));
$adapter->setPackageMaxLength(64000); // Default maximum Package Size (64kb)
2021-06-25 00:22:32 +12:00
2021-08-19 03:44:11 +12:00
$server = new Server($adapter);
2021-06-29 02:34:28 +12:00
2021-07-01 22:31:48 +12:00
$server->onStart(function () use ($stats, $register, $containerId, &$documentId) {
2021-06-25 00:22:32 +12:00
Console::success('Server started succefully');
2021-10-01 00:18:50 +13:00
// $getConsoleDb = function () use ($register) {
// $db = $register->get('dbPool')->get();
// $cache = $register->get('redisPool')->get();
2021-07-01 22:31:48 +12:00
2021-10-01 00:18:50 +13:00
// $cache = new Cache(new RedisCache($cache));
// $database = new Database(new MariaDB($db), $cache);
2021-07-01 22:31:48 +12:00
2021-10-01 00:18:50 +13:00
// return [
// $database,
// function () use ($register, $db, $cache) {
// $register->get('dbPool')->put($db);
// $register->get('redisPool')->put($cache);
// }
// ];
// };
2021-08-17 21:08:18 +12:00
2021-07-01 22:31:48 +12:00
/**
2021-08-19 03:44:11 +12:00
* Create document for this worker to share stats across Containers.
2021-07-01 22:31:48 +12:00
*/
2021-10-01 00:18:50 +13:00
// go(function () use ($getConsoleDb, $containerId, &$documentId) {
// try {
// [$consoleDb, $returnConsoleDb] = call_user_func($getConsoleDb);
// // $document = [
// // '$collection' => Database::SYSTEM_COLLECTION_CONNECTIONS,
// // '$permissions' => [
// // 'read' => ['*'],
// // 'write' => ['*'],
// // ],
// // 'container' => $containerId,
// // 'timestamp' => time(),
// // 'value' => '{}'
// // ];
// // Authorization::disable();
// // $document = $consoleDb->createDocument($document);
// // Authorization::enable();
// // $documentId = $document->getId();
// } catch (\Throwable $th) {
// Console::error('[Error] Type: ' . get_class($th));
// Console::error('[Error] Message: ' . $th->getMessage());
// Console::error('[Error] File: ' . $th->getFile());
// Console::error('[Error] Line: ' . $th->getLine());
// } finally {
// call_user_func($returnConsoleDb);
// }
// });
// /**
// * Save current connections to the Database every 5 seconds.
// */
// Timer::tick(5000, function () use ($stats, $getConsoleDb, $containerId, &$documentId) {
// foreach ($stats as $projectId => $value) {
// if (empty($value['connections']) && empty($value['messages'])) {
// continue;
// }
// $connections = $stats->get($projectId, 'connections');
// $messages = $stats->get($projectId, 'messages');
// $usage = new Event('v1-usage', 'UsageV1');
// $usage
// ->setParam('projectId', $projectId)
// ->setParam('realtimeConnections', $connections)
// ->setParam('realtimeMessages', $messages)
// ->setParam('networkRequestSize', 0)
// ->setParam('networkResponseSize', 0);
// $stats->set($projectId, [
// 'messages' => 0,
// 'connections' => 0
// ]);
// if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
// $usage->trigger();
// }
// }
// $payload = [];
// foreach ($stats as $projectId => $value) {
// if (!empty($value['connectionsTotal'])) {
// $payload[$projectId] = $stats->get($projectId, 'connectionsTotal');
// }
// }
// if (empty($payload)) {
// return;
// }
// try {
// [$consoleDb, $returnConsoleDb] = call_user_func($getConsoleDb);
// // $consoleDb->updateDocument([
// // '$id' => $documentId,
// // '$collection' => Database::SYSTEM_COLLECTION_CONNECTIONS,
// // '$permissions' => [
// // 'read' => ['*'],
// // 'write' => ['*'],
// // ],
// // 'container' => $containerId,
// // 'timestamp' => time(),
// // 'value' => json_encode($payload)
// // ]);
// } catch (\Throwable $th) {
// Console::error('[Error] Type: ' . get_class($th));
// Console::error('[Error] Message: ' . $th->getMessage());
// Console::error('[Error] File: ' . $th->getFile());
// Console::error('[Error] Line: ' . $th->getLine());
// } finally {
// call_user_func($returnConsoleDb);
// }
// });
2021-06-25 00:22:32 +12:00
});
2021-06-30 04:22:10 +12:00
$server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, $realtime) {
2021-06-25 00:22:32 +12:00
Console::success('Worker ' . $workerId . ' started succefully');
$attempts = 0;
$start = time();
2021-09-16 01:28:21 +12:00
Timer::tick(5000, function () use ($server, $register, $realtime, $stats) {
2021-08-17 23:18:32 +12:00
/**
* Sending current connections to project channels on the console project every 5 seconds.
*/
2021-06-29 02:34:28 +12:00
if ($realtime->hasSubscriber('console', 'role:member', 'project')) {
2021-07-01 22:31:48 +12:00
$db = $register->get('dbPool')->get();
2021-10-01 00:18:50 +13:00
$redis = $register->get('redisPool')->get();
2021-07-01 22:31:48 +12:00
2021-10-01 00:18:50 +13:00
$cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_console_internal');
2021-07-01 22:31:48 +12:00
2021-06-25 00:22:32 +12:00
$payload = [];
2021-10-01 00:18:50 +13:00
$list = [];
// $list = $consoleDb->getCollection([
// 'filters' => [
// '$collection=' . Database::SYSTEM_COLLECTION_CONNECTIONS,
// 'timestamp>' . (time() - 15)
// ],
// ]);
2021-07-01 22:31:48 +12:00
2021-08-19 03:44:11 +12:00
/**
* Aggregate stats across containers.
*/
2021-07-01 22:31:48 +12:00
foreach ($list as $document) {
foreach (json_decode($document->getAttribute('value')) as $projectId => $value) {
if (array_key_exists($projectId, $payload)) {
$payload[$projectId] += $value;
} else {
$payload[$projectId] = $value;
}
}
2021-06-25 00:22:32 +12:00
}
2021-06-29 02:34:28 +12:00
2021-06-25 00:22:32 +12:00
foreach ($stats as $projectId => $value) {
2021-08-28 04:33:09 +12:00
if (!array_key_exists($projectId, $payload)) {
continue;
}
$event = [
'project' => 'console',
'roles' => ['team:' . $stats->get($projectId, 'teamId')],
'data' => [
2021-08-27 23:26:26 +12:00
'event' => 'stats.connections',
'channels' => ['project'],
'timestamp' => time(),
'payload' => [
$projectId => $payload[$projectId]
]
]
];
2021-08-27 23:26:26 +12:00
$server->send($realtime->getSubscribers($event), json_encode([
'type' => 'event',
'data' => $event['data']
]));
2021-06-25 00:22:32 +12:00
}
2021-08-19 20:03:52 +12:00
$register->get('dbPool')->put($db);
2021-10-01 00:18:50 +13:00
$register->get('redisPool')->put($redis);
2021-08-17 23:18:32 +12:00
}
/**
* Sending test message for SDK E2E tests every 5 seconds.
*/
if ($realtime->hasSubscriber('console', 'role:guest', 'tests')) {
$payload = ['response' => 'WS:/v1/realtime:passed'];
$event = [
'project' => 'console',
'roles' => ['role:guest'],
'data' => [
2021-08-27 23:26:26 +12:00
'event' => 'test.event',
'channels' => ['tests'],
'timestamp' => time(),
'payload' => $payload
2021-08-17 23:18:32 +12:00
]
];
2021-08-27 23:26:26 +12:00
$server->send($realtime->getSubscribers($event), json_encode([
'type' => 'event',
'data' => $event['data']
]));
2021-06-25 00:22:32 +12:00
}
});
while ($attempts < 300) {
try {
if ($attempts > 0) {
Console::error('Pub/sub connection lost (lasted ' . (time() - $start) . ' seconds, worker: ' . $workerId . ').
Attempting restart in 5 seconds (attempt #' . $attempts . ')');
sleep(5); // 5 sec delay between connection attempts
}
$start = time();
/** @var Redis $redis */
2021-07-01 22:31:48 +12:00
$redis = $register->get('redisPool')->get();
2021-06-25 00:22:32 +12:00
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
if ($redis->ping(true)) {
$attempts = 0;
Console::success('Pub/sub connection established (worker: ' . $workerId . ')');
} else {
Console::error('Pub/sub failed (worker: ' . $workerId . ')');
}
2021-08-19 20:24:41 +12:00
$redis->subscribe(['realtime'], function (Redis $redis, string $channel, string $payload) use ($server, $workerId, $stats, $register, $realtime) {
2021-06-25 00:22:32 +12:00
$event = json_decode($payload, true);
if ($event['permissionsChanged'] && isset($event['userId'])) {
2021-06-29 02:34:28 +12:00
$projectId = $event['project'];
2021-06-25 00:22:32 +12:00
$userId = $event['userId'];
2021-06-29 02:34:28 +12:00
if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) {
$connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId]));
2021-06-25 00:22:32 +12:00
} else {
return;
}
2021-06-25 00:22:32 +12:00
$db = $register->get('dbPool')->get();
$cache = $register->get('redisPool')->get();
2021-10-01 00:18:50 +13:00
$cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_' . $projectId .'_internal');
2021-10-01 00:18:50 +13:00
$user = $database->getDocument('users', $userId);
$roles = Auth::getRoles($user);
2021-06-29 02:34:28 +12:00
$realtime->subscribe($projectId, $connection, $roles, $realtime->connections[$connection]['channels']);
2021-06-25 00:22:32 +12:00
$register->get('dbPool')->put($db);
$register->get('redisPool')->put($cache);
}
2021-07-14 03:18:02 +12:00
$receivers = $realtime->getSubscribers($event);
2021-08-19 20:03:52 +12:00
if (App::isDevelopment() && !empty($receivers)) {
2021-06-25 00:22:32 +12:00
Console::log("[Debug][Worker {$workerId}] Receivers: " . count($receivers));
Console::log("[Debug][Worker {$workerId}] Receivers Connection IDs: " . json_encode($receivers));
Console::log("[Debug][Worker {$workerId}] Event: " . $payload);
}
$server->send(
$receivers,
2021-08-27 20:20:44 +12:00
json_encode([
'type' => 'event',
'data' => $event['data']
])
2021-06-25 00:22:32 +12:00
);
if (($num = count($receivers)) > 0) {
$stats->incr($event['project'], 'messages', $num);
}
});
} catch (\Throwable $th) {
Console::error('Pub/sub error: ' . $th->getMessage());
2021-07-01 22:31:48 +12:00
$register->get('redisPool')->put($redis);
2021-06-25 00:22:32 +12:00
$attempts++;
continue;
}
$attempts++;
}
Console::error('Failed to restart pub/sub...');
});
2021-06-30 04:22:10 +12:00
$server->onOpen(function (int $connection, SwooleRequest $request) use ($server, $register, $stats, &$realtime) {
2021-06-25 00:22:32 +12:00
$app = new App('UTC');
$request = new Request($request);
2021-08-27 21:20:49 +12:00
$response = new Response(new SwooleResponse());
2021-06-25 00:22:32 +12:00
/** @var PDO $db */
$db = $register->get('dbPool')->get();
/** @var Redis $redis */
$redis = $register->get('redisPool')->get();
2021-06-30 04:22:10 +12:00
Console::info("Connection open (user: {$connection})");
2021-06-25 00:22:32 +12:00
App::setResource('db', function () use (&$db) {
return $db;
});
App::setResource('cache', function () use (&$redis) {
return $redis;
});
App::setResource('request', function () use ($request) {
return $request;
});
2021-08-27 21:20:49 +12:00
App::setResource('response', function () use ($response) {
return $response;
2021-06-25 00:22:32 +12:00
});
try {
2021-10-01 00:18:50 +13:00
/** @var \Utopia\Database\Document $user */
2021-06-25 00:22:32 +12:00
$user = $app->getResource('user');
2021-10-01 00:18:50 +13:00
/** @var \Utopia\Database\Document $project */
2021-06-25 00:22:32 +12:00
$project = $app->getResource('project');
2021-10-01 00:18:50 +13:00
/** @var \Utopia\Database\Document $console */
2021-06-25 00:22:32 +12:00
$console = $app->getResource('console');
2021-10-01 00:18:50 +13:00
$cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_' . $project->getId() .'_internal');
2021-06-25 00:22:32 +12:00
/*
* Project Check
*/
if (empty($project->getId())) {
throw new Exception('Missing or unknown project ID', 1008);
}
/*
* Abuse Check
*
* Abuse limits are connecting 128 times per minute and ip address.
*/
2021-10-01 00:18:50 +13:00
// $timeLimit = new TimeLimit('url:{url},ip:{ip}', 128, 60, $database);
// $timeLimit
// ->setParam('{ip}', $request->getIP())
// ->setParam('{url}', $request->getURI())
// ->setup();
2021-06-25 00:22:32 +12:00
2021-10-01 00:18:50 +13:00
// $abuse = new Abuse($timeLimit);
2021-06-25 00:22:32 +12:00
2021-10-01 00:18:50 +13:00
// if ($abuse->check() && App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') === 'enabled') {
// throw new Exception('Too many requests', 1013);
// }
2021-06-25 00:22:32 +12:00
/*
* Validate Client Domain - Check to avoid CSRF attack.
* Adding Appwrite API domains to allow XDOMAIN communication.
* Skip this check for non-web platforms which are not required to send an origin header.
*/
$origin = $request->getOrigin();
$originValidator = new Origin(\array_merge($project->getAttribute('platforms', []), $console->getAttribute('platforms', [])));
if (!$originValidator->isValid($origin) && $project->getId() !== 'console') {
throw new Exception($originValidator->getDescription(), 1008);
}
$roles = Auth::getRoles($user);
2021-06-25 00:22:32 +12:00
2021-07-14 03:18:02 +12:00
$channels = Realtime::convertChannels($request->getQuery('channels', []), $user->getId());
2021-06-25 00:22:32 +12:00
/**
* Channels Check
*/
if (empty($channels)) {
throw new Exception('Missing channels', 1008);
}
2021-06-29 02:34:28 +12:00
$realtime->subscribe($project->getId(), $connection, $roles, $channels);
2021-06-25 00:22:32 +12:00
2021-08-27 21:20:49 +12:00
$user = empty($user->getId()) ? null : $response->output($user, Response::MODEL_USER);
$server->send([$connection], json_encode([
'type' => 'connected',
'data' => [
'channels' => array_keys($channels),
'user' => $user
]
]));
2021-06-25 00:22:32 +12:00
2021-08-19 22:14:19 +12:00
$stats->set($project->getId(), [
'projectId' => $project->getId(),
'teamId' => $project->getAttribute('teamId')
]);
2021-06-25 00:22:32 +12:00
$stats->incr($project->getId(), 'connections');
$stats->incr($project->getId(), 'connectionsTotal');
} catch (\Throwable $th) {
$response = [
2021-08-27 20:20:44 +12:00
'type' => 'error',
'data' => [
'code' => $th->getCode(),
'message' => $th->getMessage()
]
2021-06-25 00:22:32 +12:00
];
2021-08-19 20:03:52 +12:00
2021-06-25 00:22:32 +12:00
$server->send([$connection], json_encode($response));
$server->close($connection, $th->getCode());
2021-08-19 20:03:52 +12:00
if (App::isDevelopment()) {
Console::error('[Error] Connection Error');
Console::error('[Error] Code: ' . $response['data']['code']);
Console::error('[Error] Message: ' . $response['data']['message']);
2021-10-01 00:18:50 +13:00
var_dump($th->getFile(), $th->getLine(), $th->getTraceAsString());
2021-08-19 20:03:52 +12:00
}
if ($th instanceof PDOException) {
$db = null;
}
2021-06-25 00:22:32 +12:00
} finally {
/**
* Put used PDO and Redis Connections back into their pools.
*/
$register->get('dbPool')->put($db);
$register->get('redisPool')->put($redis);
}
});
$server->onMessage(function (int $connection, string $message) use ($server, $register, $realtime, $containerId) {
try {
2021-08-27 21:20:49 +12:00
$response = new Response(new SwooleResponse());
$db = $register->get('dbPool')->get();
$cache = $register->get('redisPool')->get();
2021-10-01 00:18:50 +13:00
$cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_' . $realtime->connections[$connection]['projectId'] .'_internal');
/*
* Abuse Check
*
* Abuse limits are sending 32 times per minute and connection.
*/
2021-10-01 00:18:50 +13:00
// $timeLimit = new TimeLimit('url:{url},conection:{connection}', 32, 60, $database);
// $timeLimit
// ->setParam('{connection}', $connection)
// ->setParam('{container}', $containerId)
// ->setup();
2021-10-01 00:18:50 +13:00
// $abuse = new Abuse($timeLimit);
2021-10-01 00:18:50 +13:00
// if ($abuse->check() && App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') === 'enabled') {
// throw new Exception('Too many messages', 1013);
// }
$message = json_decode($message, true);
if (is_null($message) || (!array_key_exists('type', $message) && !array_key_exists('data', $message))) {
2021-08-27 04:10:53 +12:00
throw new Exception('Message format is not valid.', 1003);
}
switch ($message['type']) {
/**
* This type is used to authenticate.
*/
case 'authentication':
if (!array_key_exists('session', $message['data'])) {
2021-08-27 21:31:26 +12:00
throw new Exception('Payload is not valid.', 1003);
}
$session = Auth::decodeSession($message['data']['session']);
Auth::$unique = $session['id'];
Auth::$secret = $session['secret'];
2021-10-01 00:18:50 +13:00
$user = $database->getDocument('users', Auth::$unique);
if (
empty($user->getId()) // Check a document has been found in the DB
|| !Auth::sessionVerify($user->getAttribute('sessions', []), Auth::$secret) // Validate user has valid login token
) {
// cookie not valid
2021-08-27 21:31:26 +12:00
throw new Exception('Session is not valid.', 1003);
}
$roles = Auth::getRoles($user);
$channels = Realtime::convertChannels(array_flip($realtime->connections[$connection]['channels']), $user->getId());
$realtime->subscribe($realtime->connections[$connection]['projectId'], $connection, $roles, $channels);
2021-08-27 21:20:49 +12:00
$user = $response->output($user, Response::MODEL_USER);
2021-08-27 20:20:44 +12:00
$server->send([$connection], json_encode([
'type' => 'response',
'data' => [
'to' => 'authentication',
2021-08-27 21:20:49 +12:00
'success' => true,
'user' => $user
2021-08-27 20:20:44 +12:00
]
]));
break;
default:
2021-08-27 21:31:26 +12:00
throw new Exception('Message type is not valid.', 1003);
break;
}
} catch (\Throwable $th) {
$response = [
2021-08-27 20:20:44 +12:00
'type' => 'error',
'data' => [
'code' => $th->getCode(),
'message' => $th->getMessage()
]
];
$server->send([$connection], json_encode($response));
if ($th->getCode() === 1008) {
$server->close($connection, $th->getCode());
}
} finally {
$register->get('dbPool')->put($db);
$register->get('redisPool')->put($cache);
}
2021-06-25 00:22:32 +12:00
});
2021-06-30 04:22:10 +12:00
$server->onClose(function (int $connection) use ($realtime, $stats) {
2021-06-29 02:34:28 +12:00
if (array_key_exists($connection, $realtime->connections)) {
$stats->decr($realtime->connections[$connection]['projectId'], 'connectionsTotal');
2021-06-25 00:22:32 +12:00
}
2021-06-29 02:34:28 +12:00
$realtime->unsubscribe($connection);
2021-06-25 00:22:32 +12:00
Console::info('Connection close: ' . $connection);
});
$server->start();