From 8be630531a433372862f5294b8c0dfd943bc98d4 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 2 Feb 2025 19:31:06 +0000 Subject: [PATCH] Only serve ID stuff on main domain and just redirect the id.flashii.net URLs. --- config/config.example.cfg | 2 +- src/MisuzuContext.php | 150 +++++++++++++-------------- src/OAuth2/OAuth2WebRoutes.php | 21 ++-- src/Routing/BackedRoutingContext.php | 6 +- src/Routing/RoutingContext.php | 2 +- src/Routing/ScopedRoutingContext.php | 6 +- 6 files changed, 92 insertions(+), 95 deletions(-) diff --git a/config/config.example.cfg b/config/config.example.cfg index e581553b..21422f97 100644 --- a/config/config.example.cfg +++ b/config/config.example.cfg @@ -7,5 +7,5 @@ database:dsn mariadb://:@/?charset=utf8mb4 ;sentry:tracesRate 1.0 ;sentry:profilesRate 1.0 -domain:localhost main redirect id +domain:localhost main redirect domain:localhost:redirect:path /go diff --git a/src/MisuzuContext.php b/src/MisuzuContext.php index 20c0e08b..1d1149f1 100644 --- a/src/MisuzuContext.php +++ b/src/MisuzuContext.php @@ -23,10 +23,10 @@ use Misuzu\OAuth2\OAuth2Context; use Misuzu\Perms\PermissionsData; use Misuzu\Profile\ProfileFieldsData; use Misuzu\Redirects\RedirectsContext; -use Misuzu\Routing\BackedRoutingContext; +use Misuzu\Routing\{BackedRoutingContext,RoutingContext}; use Misuzu\Users\{UsersContext,UserInfo}; use RPCii\HmacVerificationProvider; -use RPCii\Server\HttpRpcServer; +use RPCii\Server\{HttpRpcServer,RpcServer}; // this class should function as the root for everything going forward // no more magical static classes that are just kind of assumed to exist @@ -178,84 +178,82 @@ class MisuzuContext { new HmacVerificationProvider(fn() => $this->config->getString('aleister.secret')) )); - if(in_array('main', $purposes)) { - $scopedInfo = $hostInfo->scopeTo('main'); - $scopedCtx = $routingCtx->scopeTo( - $scopedInfo->getString('name'), - $scopedInfo->getString('path') + if(in_array('main', $purposes)) + $this->registerMainRoutes( + $routingCtx->scopeTo($hostInfo->getString('main:path')), + $rpcServer ); - $scopedCtx->register($this->deps->constructLazy(Home\HomeRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(Users\Assets\AssetsRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(Info\InfoRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(News\NewsRoutes::class)); - $scopedCtx->register($this->deps->constructLazy( - Messages\MessagesRoutes::class, - config: $this->config->scopeTo('messages') - )); - $scopedCtx->register($this->deps->constructLazy(Forum\ForumCategoriesRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(Forum\ForumTopicsRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(Forum\ForumPostsRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(Changelog\ChangelogRoutes::class)); - $scopedCtx->register($this->deps->constructLazy( - SharpChat\SharpChatRoutes::class, - config: $this->config->scopeTo('sockChat'), - impersonateConfig: $this->config->scopeTo('impersonate') - )); - $scopedCtx->register($this->deps->constructLazy( - Satori\SatoriRoutes::class, - config: $this->config->scopeTo('satori') - )); - $scopedCtx->register($this->deps->constructLazy(LegacyRoutes::class)); - - $rpcServer->register($this->deps->constructLazy( - Auth\AuthRpcHandler::class, - impersonateConfig: $this->config->scopeTo('impersonate') - )); - $rpcServer->register($this->deps->constructLazy(Emoticons\EmotesRpcHandler::class)); - $rpcServer->register($this->deps->constructLazy(Users\UsersRpcHandler::class)); - - $hanyuuRpcServer = new HttpRpcServer; - $scopedCtx->scopeTo('', '/_hanyuu')->register($hanyuuRpcServer->createRouteHandler( - new HmacVerificationProvider(fn() => $this->config->getString('hanyuu.secret')) - )); - - $hanyuuRpcServer->register($this->deps->constructLazy( - Hanyuu\HanyuuRpcHandler::class, - getBaseUrl: fn() => $this->config->getString('hanyuu.endpoint'), - impersonateConfig: $this->config->scopeTo('impersonate') - )); - } - - if(in_array('id', $purposes)) { - $scopedInfo = $hostInfo->scopeTo('id'); - $scopedCtx = $routingCtx->scopeTo( - $scopedInfo->getString('name'), - $scopedInfo->getString('path') + if(in_array('redirect', $purposes)) + $this->registerRedirectorRoutes( + $routingCtx->scopeTo($hostInfo->getString('redirect:path')) ); - $scopedCtx->register($this->deps->constructLazy(OAuth2\OAuth2ApiRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(OAuth2\OAuth2WebRoutes::class)); - $rpcServer->register($this->deps->constructLazy(OAuth2\OAuth2RpcHandler::class)); - } - - if(in_array('redirect', $purposes)) { - $scopedInfo = $hostInfo->scopeTo('redirect'); - $scopedCtx = $routingCtx->scopeTo( - $scopedInfo->getString('name'), - $scopedInfo->getString('path') - ); - - $scopedCtx->register($this->deps->constructLazy(Redirects\LandingRedirectsRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(Redirects\AliasRedirectsRoutes::class)); - $scopedCtx->register($this->deps->constructLazy(Redirects\IncrementalRedirectsRoutes::class)); - $scopedCtx->register($this->deps->constructLazy( - Redirects\SocialRedirectsRoutes::class, - getWebAssetInfo: $this->getWebAssetInfo(...) - )); - $scopedCtx->register($this->deps->constructLazy(Redirects\NamedRedirectsRoutes::class)); - } - return $routingCtx; } + + public function registerMainRoutes( + RoutingContext $routingCtx, + RpcServer $rpcServer + ): void { + $routingCtx->register($this->deps->constructLazy(Home\HomeRoutes::class)); + $routingCtx->register($this->deps->constructLazy(Users\Assets\AssetsRoutes::class)); + $routingCtx->register($this->deps->constructLazy(Info\InfoRoutes::class)); + $routingCtx->register($this->deps->constructLazy(News\NewsRoutes::class)); + $routingCtx->register($this->deps->constructLazy( + Messages\MessagesRoutes::class, + config: $this->config->scopeTo('messages') + )); + + $routingCtx->register($this->deps->constructLazy(OAuth2\OAuth2ApiRoutes::class)); + $routingCtx->register($this->deps->constructLazy(OAuth2\OAuth2WebRoutes::class)); + $rpcServer->register($this->deps->constructLazy(OAuth2\OAuth2RpcHandler::class)); + + $routingCtx->register($this->deps->constructLazy(Forum\ForumCategoriesRoutes::class)); + $routingCtx->register($this->deps->constructLazy(Forum\ForumTopicsRoutes::class)); + $routingCtx->register($this->deps->constructLazy(Forum\ForumPostsRoutes::class)); + + $routingCtx->register($this->deps->constructLazy(Changelog\ChangelogRoutes::class)); + $routingCtx->register($this->deps->constructLazy( + SharpChat\SharpChatRoutes::class, + config: $this->config->scopeTo('sockChat'), + impersonateConfig: $this->config->scopeTo('impersonate') + )); + + $routingCtx->register($this->deps->constructLazy( + Satori\SatoriRoutes::class, + config: $this->config->scopeTo('satori') + )); + + $routingCtx->register($this->deps->constructLazy(LegacyRoutes::class)); + + $rpcServer->register($this->deps->constructLazy( + Auth\AuthRpcHandler::class, + impersonateConfig: $this->config->scopeTo('impersonate') + )); + $rpcServer->register($this->deps->constructLazy(Emoticons\EmotesRpcHandler::class)); + $rpcServer->register($this->deps->constructLazy(Users\UsersRpcHandler::class)); + + $hanyuuRpcServer = new HttpRpcServer; + $routingCtx->scopeTo('', '/_hanyuu')->register($hanyuuRpcServer->createRouteHandler( + new HmacVerificationProvider(fn() => $this->config->getString('hanyuu.secret')) + )); + + $hanyuuRpcServer->register($this->deps->constructLazy( + Hanyuu\HanyuuRpcHandler::class, + getBaseUrl: fn() => $this->config->getString('hanyuu.endpoint'), + impersonateConfig: $this->config->scopeTo('impersonate') + )); + } + + public function registerRedirectorRoutes(RoutingContext $routingCtx): void { + $routingCtx->register($this->deps->constructLazy(Redirects\LandingRedirectsRoutes::class)); + $routingCtx->register($this->deps->constructLazy(Redirects\AliasRedirectsRoutes::class)); + $routingCtx->register($this->deps->constructLazy(Redirects\IncrementalRedirectsRoutes::class)); + $routingCtx->register($this->deps->constructLazy( + Redirects\SocialRedirectsRoutes::class, + getWebAssetInfo: $this->getWebAssetInfo(...) + )); + $routingCtx->register($this->deps->constructLazy(Redirects\NamedRedirectsRoutes::class)); + } } diff --git a/src/OAuth2/OAuth2WebRoutes.php b/src/OAuth2/OAuth2WebRoutes.php index 71820574..bb2e48db 100644 --- a/src/OAuth2/OAuth2WebRoutes.php +++ b/src/OAuth2/OAuth2WebRoutes.php @@ -17,7 +17,6 @@ final class OAuth2WebRoutes implements RouteHandler { private OAuth2Context $oauth2Ctx, private UsersContext $usersCtx, private UrlRegistry $urls, - private SiteInfo $siteInfo, private AuthInfo $authInfo ) { } @@ -216,8 +215,8 @@ final class OAuth2WebRoutes implements RouteHandler { 'user' => [ 'name' => $this->authInfo->userInfo->name, 'colour' => (string)$this->usersCtx->getUserColour($this->authInfo->userInfo), - 'profile_uri' => $this->siteInfo->url . $this->urls->format('user-profile', ['user' => $this->authInfo->userInfo->id]), - 'avatar_uri' => $this->siteInfo->url . $this->urls->format('user-avatar', ['user' => $this->authInfo->userInfo->id, 'res' => 120]), + 'profile_uri' => $this->urls->format('user-profile', ['user' => $this->authInfo->userInfo->id]), + 'avatar_uri' => $this->urls->format('user-avatar', ['user' => $this->authInfo->userInfo->id, 'res' => 120]), ], 'scope' => $scope, ]; @@ -226,9 +225,9 @@ final class OAuth2WebRoutes implements RouteHandler { $result['user']['guise'] = [ 'name' => $this->authInfo->realUserInfo->name, 'colour' => (string)$this->usersCtx->getUserColour($this->authInfo->realUserInfo), - 'profile_uri' => $this->siteInfo->url . $this->urls->format('user-profile', ['user' => $this->authInfo->realUserInfo->id]), - 'revert_uri' => $this->siteInfo->url . $this->urls->format('auth-revert', ['csrf' => CSRF::token()]), - 'avatar_uri' => $this->siteInfo->url . $this->urls->format('user-avatar', ['user' => $this->authInfo->realUserInfo->id, 'res' => 60]), + 'profile_uri' => $this->urls->format('user-profile', ['user' => $this->authInfo->realUserInfo->id]), + 'revert_uri' => $this->urls->format('auth-revert', ['csrf' => CSRF::token()]), + 'avatar_uri' => $this->urls->format('user-avatar', ['user' => $this->authInfo->realUserInfo->id, 'res' => 60]), ]; return $result; @@ -411,8 +410,8 @@ final class OAuth2WebRoutes implements RouteHandler { 'user' => [ 'name' => $this->authInfo->userInfo->name, 'colour' => (string)$this->usersCtx->getUserColour($this->authInfo->userInfo), - 'profile_uri' => $this->siteInfo->url . $this->urls->format('user-profile', ['user' => $this->authInfo->userInfo->id]), - 'avatar_uri' => $this->siteInfo->url . $this->urls->format('user-avatar', ['user' => $this->authInfo->userInfo->id, 'res' => 120]), + 'profile_uri' => $this->urls->format('user-profile', ['user' => $this->authInfo->userInfo->id]), + 'avatar_uri' => $this->urls->format('user-avatar', ['user' => $this->authInfo->userInfo->id, 'res' => 120]), ], ]; @@ -420,9 +419,9 @@ final class OAuth2WebRoutes implements RouteHandler { $result['user']['guise'] = [ 'name' => $this->authInfo->realUserInfo->name, 'colour' => (string)$this->usersCtx->getUserColour($this->authInfo->realUserInfo), - 'profile_uri' => $this->siteInfo->url . $this->urls->format('user-profile', ['user' => $this->authInfo->realUserInfo->id]), - 'revert_uri' => $this->siteInfo->url . $this->urls->format('auth-revert', ['csrf' => CSRF::token()]), - 'avatar_uri' => $this->siteInfo->url . $this->urls->format('user-avatar', ['user' => $this->authInfo->realUserInfo->id, 'res' => 60]), + 'profile_uri' => $this->urls->format('user-profile', ['user' => $this->authInfo->realUserInfo->id]), + 'revert_uri' => $this->urls->format('auth-revert', ['csrf' => CSRF::token()]), + 'avatar_uri' => $this->urls->format('user-avatar', ['user' => $this->authInfo->realUserInfo->id, 'res' => 60]), ]; return $result; diff --git a/src/Routing/BackedRoutingContext.php b/src/Routing/BackedRoutingContext.php index 1852bcb1..a52623ea 100644 --- a/src/Routing/BackedRoutingContext.php +++ b/src/Routing/BackedRoutingContext.php @@ -25,10 +25,10 @@ class BackedRoutingContext implements RoutingContext { $this->urls->register($handler); } - public function scopeTo(string $namePrefix, string $pathPrefix): RoutingContext { + public function scopeTo(string $prefix): RoutingContext { return new ScopedRoutingContext( - $this->router->scopeTo($pathPrefix), - $this->urls->scopeTo($namePrefix, $pathPrefix) + $this->router->scopeTo($prefix), + $this->urls->scopeTo(pathPrefix: $prefix) ); } diff --git a/src/Routing/RoutingContext.php b/src/Routing/RoutingContext.php index 509680dc..7c2ffcd0 100644 --- a/src/Routing/RoutingContext.php +++ b/src/Routing/RoutingContext.php @@ -7,5 +7,5 @@ use Index\Urls\UrlSource; interface RoutingContext { public function register(RouteHandler|UrlSource $handler): void; - public function scopeTo(string $namePrefix, string $pathPrefix): RoutingContext; + public function scopeTo(string $prefix): RoutingContext; } diff --git a/src/Routing/ScopedRoutingContext.php b/src/Routing/ScopedRoutingContext.php index d32c49d6..2669bde0 100644 --- a/src/Routing/ScopedRoutingContext.php +++ b/src/Routing/ScopedRoutingContext.php @@ -18,10 +18,10 @@ class ScopedRoutingContext implements RoutingContext { $this->urls->register($handler); } - public function scopeTo(string $namePrefix, string $pathPrefix): RoutingContext { + public function scopeTo(string $prefix): RoutingContext { return new ScopedRoutingContext( - $this->router->scopeTo($pathPrefix), - $this->urls->scopeTo($namePrefix, $pathPrefix) + $this->router->scopeTo($prefix), + $this->urls->scopeTo(pathPrefix: $prefix) ); } }