Implemented HA ingress path header prefix

This commit is contained in:
Glenn de Haan
2024-03-23 16:58:35 +01:00
parent 0a4c0e358e
commit 1ec0ebedf0
4 changed files with 46 additions and 43 deletions

View File

@@ -133,7 +133,7 @@ app.use(express.static(`${__dirname}/public`));
*/
app.get('/', (req, res) => {
if(webService) {
res.redirect(302, '/voucher');
res.redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/voucher`);
} else {
res.status(501).send();
}
@@ -144,7 +144,7 @@ if(webService) {
app.get('/login', (req, res) => {
// Check if authentication is disabled
if (authDisabled) {
res.redirect(302, '/voucher');
res.redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/voucher`);
return;
}
@@ -152,6 +152,7 @@ if(webService) {
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
res.render('login', {
baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '',
error: req.flashMessage.type === 'error',
error_text: req.flashMessage.message || '',
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
@@ -168,17 +169,18 @@ if(webService) {
const passwordCheck = req.body.password === (process.env.SECURITY_CODE || "0000");
if(!passwordCheck) {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Password Invalid!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/login');
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Password Invalid!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/login`);
return;
}
res.cookie('authorization', req.body.password, {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
res.cookie('authorization', req.body.password, {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/voucher`);
});
app.get('/voucher', [authorization.web], async (req, res) => {
const hour = new Date().getHours();
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
res.render('voucher', {
baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '',
info: req.flashMessage.type === 'info',
info_text: req.flashMessage.message || '',
error: req.flashMessage.type === 'error',
@@ -200,17 +202,17 @@ if(webService) {
const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.body['voucher-type']);
if(!typeCheck) {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Unknown Type!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
res.cookie('flashMessage', JSON.stringify({type: 'error', message: 'Unknown Type!'}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/voucher`);
return;
}
// Create voucher code
const voucherCode = await unifi(types(req.body['voucher-type'], true)).catch((e) => {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/voucher`);
});
if(voucherCode) {
res.cookie('flashMessage', JSON.stringify({type: 'info', message: `Voucher Created: ${voucherCode}`}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
res.cookie('flashMessage', JSON.stringify({type: 'info', message: `Voucher Created: ${voucherCode}`}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/voucher`);
}
});
app.get('/vouchers', [authorization.web], async (req, res) => {
@@ -218,11 +220,12 @@ if(webService) {
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
const vouchers = await unifi('', false).catch((e) => {
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, '/voucher');
res.cookie('flashMessage', JSON.stringify({type: 'error', message: e}), {httpOnly: true, expires: new Date(Date.now() + 24 * 60 * 60 * 1000)}).redirect(302, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/voucher`);
});
if (vouchers) {
res.render('voucher', {
baseUrl: req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : '',
info: req.flashMessage.type === 'info',
info_text: req.flashMessage.message || '',
error: req.flashMessage.type === 'error',

View File

@@ -13,25 +13,25 @@
<meta property="og:type" content="website"/>
<meta property="og:description" content="UniFi Voucher"/>
<link rel="manifest" href="/manifest.json">
<link rel="shortcut icon" href="/images/favicon.ico">
<link rel="apple-touch-icon" href="/images/icon/logo_256x256.png">
<link rel="manifest" href="<%= baseUrl %>/manifest.json">
<link rel="shortcut icon" href="<%= baseUrl %>/images/favicon.ico">
<link rel="apple-touch-icon" href="<%= baseUrl %>/images/icon/logo_256x256.png">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="theme-color" content="#139CDA">
<link rel="preload" href="<%= banner_image %>" as="image">
<link rel="preload" href="/images/logo.png" as="image">
<link rel="preload" href="/dist/style.css" as="style">
<link href="/dist/style.css" rel="stylesheet">
<link rel="preload" href="<%= baseUrl %><%= banner_image %>" as="image">
<link rel="preload" href="<%= baseUrl %>/images/logo.png" as="image">
<link rel="preload" href="<%= baseUrl %>/dist/style.css" as="style">
<link href="<%= baseUrl %>/dist/style.css" rel="stylesheet">
</head>
<body class="bg-white dark:bg-neutral-900 dark:text-gray-100 h-screen">
<div class="w-full flex flex-wrap">
<div class="w-full md:w-1/2 flex flex-col">
<div class="flex justify-center md:justify-start pt-12 md:pl-12 md:-mb-24">
<a href="/" title="Homepage">
<img class="h-20 w-20" alt="UniFi Logo" src="/images/logo.png"/>
<a href="<%= baseUrl %>/" title="Homepage">
<img class="h-20 w-20" alt="UniFi Logo" src="<%= baseUrl %>/images/logo.png"/>
</a>
</div>
@@ -41,7 +41,7 @@
<div class="text-center text-gray-400 pt-12">
<p>
It seems this page does not exist.<br/>
<a href="/" class="underline font-semibold">Go back to the homepage</a>
<a href="<%= baseUrl %>/" class="underline font-semibold">Go back to the homepage</a>
</p>
</div>
</div>
@@ -58,7 +58,7 @@
</div>
<div class="w-1/2 shadow-2xl">
<img class="object-cover w-full h-screen hidden md:block" src="<%= banner_image %>">
<img class="object-cover w-full h-screen hidden md:block" src="<%= baseUrl %><%= banner_image %>">
</div>
</div>
</body>

View File

@@ -13,25 +13,25 @@
<meta property="og:type" content="website"/>
<meta property="og:description" content="UniFi Voucher"/>
<link rel="manifest" href="/manifest.json">
<link rel="shortcut icon" href="/images/favicon.ico">
<link rel="apple-touch-icon" href="/images/icon/logo_256x256.png">
<link rel="manifest" href="<%= baseUrl %>/manifest.json">
<link rel="shortcut icon" href="<%= baseUrl %>/images/favicon.ico">
<link rel="apple-touch-icon" href="<%= baseUrl %>/images/icon/logo_256x256.png">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="theme-color" content="#139CDA">
<link rel="preload" href="<%= banner_image %>" as="image">
<link rel="preload" href="/images/logo.png" as="image">
<link rel="preload" href="/dist/style.css" as="style">
<link href="/dist/style.css" rel="stylesheet">
<link rel="preload" href="<%= baseUrl %><%= banner_image %>" as="image">
<link rel="preload" href="<%= baseUrl %>/images/logo.png" as="image">
<link rel="preload" href="<%= baseUrl %>/dist/style.css" as="style">
<link href="<%= baseUrl %>/dist/style.css" rel="stylesheet">
</head>
<body class="bg-white dark:bg-neutral-900 dark:text-gray-100 h-screen">
<div class="w-full flex flex-wrap">
<div class="w-full md:w-1/2 flex flex-col">
<div class="flex justify-center md:justify-start pt-12 md:pl-12 md:-mb-24">
<a href="/" title="Homepage">
<img class="h-20 w-20" alt="UniFi Logo" src="/images/logo.png"/>
<a href="<%= baseUrl %>/" title="Homepage">
<img class="h-20 w-20" alt="UniFi Logo" src="<%= baseUrl %>/images/logo.png"/>
</a>
</div>
@@ -45,7 +45,7 @@
<div><%= error_text %></div>
</div>
<% } %>
<form id="login-forum" class="flex flex-col pt-3 md:pt-8" action="/login" method="post" enctype="multipart/form-data">
<form id="login-forum" class="flex flex-col pt-3 md:pt-8" action="<%= baseUrl %>/login" method="post" enctype="multipart/form-data">
<div class="flex flex-col pt-4">
<label for="password" class="text-lg">Password</label>
<input type="password" id="password" name="password" placeholder="Password" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mt-1 leading-tight focus:outline-none focus:shadow-outline dark:bg-neutral-800 dark:border-neutral-700 dark:text-gray-100" required>
@@ -67,7 +67,7 @@
</div>
<div class="w-1/2 shadow-2xl">
<img class="object-cover w-full h-screen hidden md:block" src="<%= banner_image %>">
<img class="object-cover w-full h-screen hidden md:block" src="<%= baseUrl %><%= banner_image %>">
</div>
</div>
</body>

View File

@@ -13,25 +13,25 @@
<meta property="og:type" content="website"/>
<meta property="og:description" content="UniFi Voucher"/>
<link rel="manifest" href="/manifest.json">
<link rel="shortcut icon" href="/images/favicon.ico">
<link rel="apple-touch-icon" href="/images/icon/logo_256x256.png">
<link rel="manifest" href="<%= baseUrl %>/manifest.json">
<link rel="shortcut icon" href="<%= baseUrl %>/images/favicon.ico">
<link rel="apple-touch-icon" href="<%= baseUrl %>/images/icon/logo_256x256.png">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="theme-color" content="#139CDA">
<link rel="preload" href="<%= banner_image %>" as="image">
<link rel="preload" href="/images/logo.png" as="image">
<link rel="preload" href="/dist/style.css" as="style">
<link href="/dist/style.css" rel="stylesheet">
<link rel="preload" href="<%= baseUrl %><%= banner_image %>" as="image">
<link rel="preload" href="<%= baseUrl %>/images/logo.png" as="image">
<link rel="preload" href="<%= baseUrl %>/dist/style.css" as="style">
<link href="<%= baseUrl %>/dist/style.css" rel="stylesheet">
</head>
<body class="bg-white dark:bg-neutral-900 dark:text-gray-100 h-screen">
<div class="w-full flex flex-wrap">
<div class="w-full md:w-1/2 flex flex-col">
<div class="flex justify-center md:justify-start pt-12 md:pl-12 md:-mb-24">
<a href="/" title="Homepage">
<img class="h-20 w-20" alt="UniFi Logo" src="/images/logo.png"/>
<a href="<%= baseUrl %>/" title="Homepage">
<img class="h-20 w-20" alt="UniFi Logo" src="<%= baseUrl %>/images/logo.png"/>
</a>
</div>
@@ -53,7 +53,7 @@
<div><%= error_text %></div>
</div>
<% } %>
<form id="voucher-forum" class="flex flex-col pt-3 md:pt-8" action="/voucher" method="post" enctype="multipart/form-data">
<form id="voucher-forum" class="flex flex-col pt-3 md:pt-8" action="<%= baseUrl %>/voucher" method="post" enctype="multipart/form-data">
<div class="flex flex-col pt-4">
<label for="voucher-type" class="text-lg">Voucher Type</label>
<div class="relative inline-block w-full">
@@ -75,7 +75,7 @@
<hr class="border-black dark:border-gray-200 my-4 mx-14"/>
<a id="voucher-list" href="/vouchers" class="bg-black text-white font-bold text-lg hover:bg-gray-700 p-2 cursor-pointer transition-colors text-center dark:text-black dark:bg-gray-200 dark:hover:bg-white">All Vouchers</a>
<a id="voucher-list" href="<%= baseUrl %>/vouchers" class="bg-black text-white font-bold text-lg hover:bg-gray-700 p-2 cursor-pointer transition-colors text-center dark:text-black dark:bg-gray-200 dark:hover:bg-white">All Vouchers</a>
</div>
<div class="text-center text-gray-400 text-sm italic pt-12 pb-12">
@@ -90,7 +90,7 @@
</div>
<div class="w-1/2 shadow-2xl">
<img class="object-cover w-full h-screen hidden md:block" src="<%= banner_image %>">
<img class="object-cover w-full h-screen hidden md:block" src="<%= baseUrl %><%= banner_image %>">
</div>
<% if(vouchers_popup) { %>
@@ -104,7 +104,7 @@
</p>
</div>
<div class="mt-4 sm:ml-16 sm:mt-0 sm:flex-none">
<a href="/voucher" class="block bg-black text-white px-3 py-2 text-center text-sm font-semibold shadow-sm hover:bg-gray-700 cursor-pointer transition-colors dark:text-black dark:bg-gray-200 dark:hover:bg-white">
<a href="<%= baseUrl %>/voucher" class="block bg-black text-white px-3 py-2 text-center text-sm font-semibold shadow-sm hover:bg-gray-700 cursor-pointer transition-colors dark:text-black dark:bg-gray-200 dark:hover:bg-white">
Close
</a>
</div>