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);
}
// Disable proxy (if enabled and per user's request)
if (
Configuration::getConfig('proxy', 'url')
&& Configuration::getConfig('proxy', 'by_bridge')

View file

@ -4,6 +4,8 @@ final class FrontpageAction implements ActionInterface
{
public function __invoke(Request $request): Response
{
$token = $request->attribute('token');
$messages = [];
$activeBridges = 0;
@ -20,13 +22,12 @@ final class FrontpageAction implements ActionInterface
$body = '';
foreach ($bridgeClassNames as $bridgeClassName) {
if ($bridgeFactory->isEnabled($bridgeClassName)) {
$body .= BridgeCard::render($bridgeClassName, $request);
$body .= BridgeCard::render($bridgeClassName, $token);
$activeBridges++;
}
}
// todo: cache this renderered template?
return new Response(render(__DIR__ . '/../templates/frontpage.html.php', [
$response = new Response(render(__DIR__ . '/../templates/frontpage.html.php', [
'messages' => $messages,
'admin_email' => Configuration::getConfig('admin', 'email'),
'admin_telegram' => Configuration::getConfig('admin', 'telegram'),
@ -34,5 +35,8 @@ final class FrontpageAction implements ActionInterface
'active_bridges' => $activeBridges,
'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
[proxy]
; Sets the proxy url (i.e. "tcp://192.168.0.0:32")
; The HTTP proxy to tunnel requests through
; https://curl.se/libcurl/c/CURLOPT_PROXY.html
; "" = Proxy disabled (default)
url = ""

View file

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

View file

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