Provide basepath support via X-Forwarded headers

This commit is contained in:
bellington3 2020-10-17 12:14:21 +02:00
parent 9d48ec19e7
commit 18e6f4bcc1
7 changed files with 59 additions and 48 deletions

View File

@ -76,6 +76,11 @@ You will need PHP 7.2 (or higher) and the following PHP modules:
## Web server configuration
If you want to serve the application under a basepath and/or with a different internal than external port (scenario: nginx->docker setup) alltube supports the following headers:
* X-Forwarded-Port (example: `5555`)
* X-Forwarded-Path (example: `/alltube`)
### Apache
The following modules are recommended:

View File

@ -133,22 +133,6 @@ class Config
*/
public $debug = false;
/**
* Forward port.
*
* @var int
*/
public $forwardPort = 443;
/**
* Base Path.
* *Must* start with a '/' and *not* end with one.
*
* @var string
*/
public $basePath = '';
/**
* Default to audio.
*
@ -337,13 +321,4 @@ class Config
return $version->getPrettyVersion();
}
/**
* Return the configured base_path usable in smarty.
*
* @return string
*/
public function getBasePath($params, $smarty)
{
return $this->basePath;
}
}

View File

@ -34,24 +34,54 @@ class ViewFactory
$view = new Smarty(__DIR__ . '/../templates/');
/** @var Config $config */
$config = $container->get('config');
$scheme = 'http';
$uri = $request->getUri();
if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) {
$scheme = 'https';
$forwardPort = $config->forwardPort;
$request = $request->withUri($request->getUri()->withScheme($scheme)->withPort($forwardPort));
$uri = $uri->withScheme('https')->withPort(443);
}
$port = ViewFactory::extractHeader($request, 'X-Forwarded-Port');
if (!is_null($port)) {
$uri = $uri->withPort(intVal($port));
}
$path = ViewFactory::extractHeader($request, 'X-Forwarded-Path');
if (!is_null($path)) {
$uri = $uri->withBasePath($path);
}
$request = $request->withUri($uri);
/** @var LocaleManager $localeManager */
$localeManager = $container->get('locale');
$smartyPlugins = new SmartyPlugins($container->get('router'), $request->getUri()->withUserInfo(null));
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('function', 'base_path', [$config, 'getBasePath']);
$view->registerPlugin('block', 't', [$localeManager, 'smartyTranslate']);
return $view;
}
static function extractHeader(Request $request = null, string $headerName) {
if (is_null($request)) {
return null;
}
$header = $request->getHeader($headerName);
if (!isset($header)) {
return null;
}
$count = sizeof($header);
if ($count != 1) {
return null;
}
return $header[0];
}
public static function getBasePath(Request $request = null) {
return ViewFactory::extractHeader($request, 'X-Forwarded-Path');
}
}

View File

@ -53,38 +53,39 @@ $container['config'] = $config;
$container['notAllowedHandler'] = [$frontController, 'notAllowed'];
// Routes.
$basePath = ViewFactory::getBasepath($container->get('request'));
$app->get(
$config->basePath . '/',
$basePath . '/',
[$frontController, 'index']
)->setName('index');
$app->get(
$config->basePath . '/extractors',
$base_path . '/extractors',
[$frontController, 'extractors']
)->setName('extractors');
$app->any(
$config->basePath . '/info',
$base_path . '/info',
[$frontController, 'info']
)->setName('info');
$app->any(
$config->basePath . '/watch',
$base_path . '/watch',
[$frontController, 'info']
);
$app->any(
$config->basePath . '/download',
$base_path . '/download',
[$downloadController, 'download']
)->setName('download');
$app->get(
$config->basePath . '/locale/{locale}',
$base_path . '/locale/{locale}',
[$frontController, 'locale']
)->setName('locale');
$app->get(
$config->basePath . '/json',
$base_path . '/json',
[$jsonController, 'json']
)->setName('json');

View File

@ -8,19 +8,19 @@
<meta name="twitter:description" content="{$description|escape}"/>
<meta property="og:description" content="{$description|escape}"/>
{/if}
<link rel="stylesheet" href="{base_url}{base_path}/css/fonts.css"/>
<link rel="stylesheet" href="{base_url}{base_path}/css/style.css"/>
<link rel="stylesheet" href="{base_url}/css/fonts.css"/>
<link rel="stylesheet" href="{base_url}/css/style.css"/>
<title>{$config->appName}{if isset($title)} - {$title|escape}{/if}</title>
<link rel="canonical" href="{$canonical}"/>
<link rel="icon" href="{base_url}{base_path}/img/favicon.png"/>
<link rel="icon" href="{base_url}/img/favicon.png"/>
<meta property="og:title" content="{$config->appName}{if isset($title)} - {$title|escape}{/if}"/>
<meta property="og:image" content="{base_url}{base_path}/img/logo.png"/>
<meta property="og:image" content="{base_url}/img/logo.png"/>
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="{$config->appName}{if isset($title)} - {$title|escape}{/if}"/>
<meta name="twitter:image" content="{base_url}{base_path}/img/logo.png"/>
<meta name="twitter:image" content="{base_url}/img/logo.png"/>
<meta name="twitter:creator" content="@Tael67"/>
<meta name="theme-color" content="#4F4F4F"/>
<link rel="manifest" href="{base_url}{base_path}/resources/manifest.json"/>
<link rel="manifest" href="{base_url}/resources/manifest.json"/>
<meta name="generator" content="AllTube Download ({$config->getAppVersion()})"/>
</head>
<body class="{$class}">

View File

@ -1,5 +1,5 @@
<h1 class="logobis">
<a class="logocompatible" href="{base_url}{base_path}">
<span class="logocompatiblemask"><img src="{base_url}{base_path}/img/logocompatiblemask.png" width="447" height="107"
<a class="logocompatible" href="{base_url}">
<span class="logocompatiblemask"><img src="{base_url}/img/logocompatiblemask.png" width="447" height="107"
alt="{$config->appName}"/></span>
</a></h1>

View File

@ -1,7 +1,7 @@
{include file='inc/head.tpl'}
{include file='inc/header.tpl'}
<main class="main">
<div><img class="logo" src="{base_url}{base_path}/img/logo.png"
<div><img class="logo" src="{base_url}/img/logo.png"
alt="{$config->appName}" width="328" height="284"></div>
<form action="{path_for name="info"}">
<label class="labelurl" for="url">