Added Satori routes to Awaki.

This commit is contained in:
flash 2024-05-22 21:56:39 +00:00
parent b3a3e837bc
commit 00f98e463b
4 changed files with 75 additions and 22 deletions

View file

@ -35,4 +35,4 @@ if($config->hasValues('sentry:dsn'))
$db = DbTools::create($config->getString('database:dsn', 'null:'));
$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';');
$awk = new AwakiContext($db, $config->scopeTo('urls'));
$awk = new AwakiContext($db, $config);

View file

@ -12,13 +12,12 @@ use Syokuhou\IConfig;
class AwakiContext {
private const DB_INIT = 'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';';
private IDbConnection $dbConn;
private HttpRouter $router;
private IConfig $urls;
public function __construct(IDbConnection $dbConn, IConfig $urls) {
$this->dbConn = $dbConn;
$this->urls = $urls;
public function __construct(
private IDbConnection $dbConn,
private IConfig $config
) {
$dbConn->execute(self::DB_INIT);
}
@ -55,6 +54,7 @@ class AwakiContext {
}
private function registerHttpRoutes(): void {
$this->router->register(new RedirectorRoutes($this->dbConn, $this->urls));
$this->router->register(new SatoriRoutes($this->dbConn, $this->config->scopeTo('satori')));
$this->router->register(new RedirectorRoutes($this->dbConn, $this->config->scopeTo('urls')));
}
}

View file

@ -50,21 +50,6 @@ final class RedirectorRoutes implements IRouteHandler {
$this->redirect($response, $request, $info->getString(1));
}
#[HttpGet('/[bg]/([A-Za-z0-9]+)')]
public function redirectSatoriShort($response, $request, string $linkId) {
$linkId = Base62::decode($linkId);
$getInfo = $this->dbConn->prepare('SELECT redir_url FROM awk_satori_redirects WHERE redir_id = ?');
$getInfo->addParameter(1, $linkId);
$getInfo->execute();
$info = $getInfo->getResult();
if(!$info->next())
return 404;
$this->redirect($response, $request, $info->getString(0));
}
private function redirectSimple($response, $request, string $format, string $argument) {
$scheme = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$argument = rawurlencode($argument);

68
src/SatoriRoutes.php Normal file
View file

@ -0,0 +1,68 @@
<?php
namespace Awaki;
use Index\Data\IDbConnection;
use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler};
use Index\Serialisation\Base62;
use Syokuhou\IConfig;
final class SatoriRoutes extends RouteHandler {
public function __construct(
private IDbConnection $dbConn,
private IConfig $config
) {}
#[HttpGet('/[bg]/([A-Za-z0-9]+)')]
public function redirectSatoriShort($response, $request, string $linkId) {
$linkId = Base62::decode($linkId);
$getInfo = $this->dbConn->prepare('SELECT redir_url FROM awk_satori_redirects WHERE redir_id = ?');
$getInfo->addParameter(1, $linkId);
$getInfo->execute();
$info = $getInfo->getResult();
if(!$info->next())
return 404;
$response->redirect($info->getString(0), true);
}
#[HttpPost('/satori/create')]
public function createRedirect($response, $request) {
if(!$request->isFormContent())
return 400;
$content = $request->getContent();
$url = (string)$content->getParam('u');
$time = (int)$content->getParam('t', FILTER_SANITIZE_NUMBER_INT);
$sign = base64_decode((string)$content->getParam('s'));
$hash = hash_hmac('sha256', "satori#create#{$time}#{$url}", $this->config->getString('secret'), true);
if(!hash_equals($hash, $sign))
return 403;
$currentTime = time();
if($time < $currentTime - 30 || $time > $currentTime + 30)
return 403;
$stmt = $this->dbConn->prepare('SELECT redir_id FROM awk_satori_redirects WHERE redir_url = ?');
$stmt->addParameter(1, $url);
$stmt->execute();
$result = $stmt->getResult();
if($result->next()) {
$linkId = $stmt->getInteger(0);
} else {
$stmt = $this->dbConn->prepare('INSERT INTO awk_satori_redirects (redir_url) VALUES (?)');
$stmt->addParameter(1, $url);
$stmt->execute();
$linkId = (int)$stmt->getLastInsertId();
}
return [
'url' => sprintf($this->config->getString('format'), Base62::encode($linkId)),
];
}
}