refactor: frontpage and proxy setting (#4214)

This commit is contained in:
Dag 2024-08-18 19:11:11 +02:00 committed by GitHub
parent e9d3a657ba
commit c0e37bcf35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 26 additions and 17 deletions

View file

@ -51,6 +51,7 @@ class DisplayAction implements ActionInterface
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'This bridge is not whitelisted']), 400); return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'This bridge is not whitelisted']), 400);
} }
// Disable proxy (if enabled and per user's request)
if ( if (
Configuration::getConfig('proxy', 'url') Configuration::getConfig('proxy', 'url')
&& Configuration::getConfig('proxy', 'by_bridge') && Configuration::getConfig('proxy', 'by_bridge')

View file

@ -4,6 +4,8 @@ final class FrontpageAction implements ActionInterface
{ {
public function __invoke(Request $request): Response public function __invoke(Request $request): Response
{ {
$token = $request->attribute('token');
$messages = []; $messages = [];
$activeBridges = 0; $activeBridges = 0;
@ -20,19 +22,21 @@ final class FrontpageAction implements ActionInterface
$body = ''; $body = '';
foreach ($bridgeClassNames as $bridgeClassName) { foreach ($bridgeClassNames as $bridgeClassName) {
if ($bridgeFactory->isEnabled($bridgeClassName)) { if ($bridgeFactory->isEnabled($bridgeClassName)) {
$body .= BridgeCard::render($bridgeClassName, $request); $body .= BridgeCard::render($bridgeClassName, $token);
$activeBridges++; $activeBridges++;
} }
} }
// todo: cache this renderered template? $response = new Response(render(__DIR__ . '/../templates/frontpage.html.php', [
return new Response(render(__DIR__ . '/../templates/frontpage.html.php', [ 'messages' => $messages,
'messages' => $messages, 'admin_email' => Configuration::getConfig('admin', 'email'),
'admin_email' => Configuration::getConfig('admin', 'email'), 'admin_telegram' => Configuration::getConfig('admin', 'telegram'),
'admin_telegram' => Configuration::getConfig('admin', 'telegram'), 'bridges' => $body,
'bridges' => $body, 'active_bridges' => $activeBridges,
'active_bridges' => $activeBridges, 'total_bridges' => count($bridgeClassNames),
'total_bridges' => count($bridgeClassNames),
])); ]));
// TODO: The rendered template could be cached, but beware config changes that changes the html
return $response;
} }
} }

View file

@ -86,8 +86,8 @@ telegram = ""
donations = true donations = true
[proxy] [proxy]
; The HTTP proxy to tunnel requests through
; Sets the proxy url (i.e. "tcp://192.168.0.0:32") ; https://curl.se/libcurl/c/CURLOPT_PROXY.html
; "" = Proxy disabled (default) ; "" = Proxy disabled (default)
url = "" url = ""

View file

@ -2,7 +2,7 @@
final class BridgeCard final class BridgeCard
{ {
public static function render(string $bridgeClassName, Request $request): string public static function render(string $bridgeClassName, ?string $token): string
{ {
$bridgeFactory = new BridgeFactory(); $bridgeFactory = new BridgeFactory();
@ -14,10 +14,15 @@ final class BridgeCard
$description = $bridge->getDescription(); $description = $bridge->getDescription();
$contexts = $bridge->getParameters(); $contexts = $bridge->getParameters();
if (Configuration::getConfig('proxy', 'url') && Configuration::getConfig('proxy', 'by_bridge')) { // Checkbox for disabling of proxy (if enabled)
if (
Configuration::getConfig('proxy', 'url')
&& Configuration::getConfig('proxy', 'by_bridge')
) {
$proxyName = Configuration::getConfig('proxy', 'name') ?: Configuration::getConfig('proxy', 'url');
$contexts['global']['_noproxy'] = [ $contexts['global']['_noproxy'] = [
'name' => 'Disable proxy (' . (Configuration::getConfig('proxy', 'name') ?: Configuration::getConfig('proxy', 'url')) . ')', 'name' => sprintf('Disable proxy (%s)', $proxyName),
'type' => 'checkbox' 'type' => 'checkbox',
]; ];
} }
@ -47,8 +52,6 @@ final class BridgeCard
CARD; CARD;
$token = $request->attribute('token');
if (count($contexts) === 0) { if (count($contexts) === 0) {
// The bridge has zero parameters // The bridge has zero parameters
$card .= self::renderForm($bridgeClassName, '', [], $token); $card .= self::renderForm($bridgeClassName, '', [], $token);

View file

@ -113,6 +113,7 @@ final class CurlHttpClient implements HttpClient
if ($config['proxy']) { if ($config['proxy']) {
curl_setopt($ch, CURLOPT_PROXY, $config['proxy']); curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
} }
if (curl_setopt_array($ch, $config['curl_options']) === false) { if (curl_setopt_array($ch, $config['curl_options']) === false) {
throw new \Exception('Tried to set an illegal curl option'); throw new \Exception('Tried to set an illegal curl option');
} }