mirror of
https://github.com/glenndehaan/unifi-voucher-site.git
synced 2026-04-05 08:53:53 -04:00
Implemented remapping of expiration
This commit is contained in:
20
modules/time.js
Normal file
20
modules/time.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Convert time minutes
|
||||||
|
*
|
||||||
|
* @param minutes
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
module.exports = (minutes) => {
|
||||||
|
if (minutes < 60) {
|
||||||
|
return `${minutes} minute(s)`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hours = minutes / 60;
|
||||||
|
|
||||||
|
if (hours < 24) {
|
||||||
|
return `${hours % 1 === 0 ? hours : hours.toFixed(2)} ${(hours > 1) ? 'hours' : 'hour'}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const days = hours / 24;
|
||||||
|
return `${days} ${(days > 1) ? 'days' : 'day'}`;
|
||||||
|
}
|
||||||
20
server.js
20
server.js
@@ -11,6 +11,7 @@ const app = express();
|
|||||||
*/
|
*/
|
||||||
const logo = require('./modules/logo');
|
const logo = require('./modules/logo');
|
||||||
const types = require('./modules/types');
|
const types = require('./modules/types');
|
||||||
|
const time = require('./modules/time');
|
||||||
const unifi = require('./modules/unifi');
|
const unifi = require('./modules/unifi');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,7 +30,7 @@ logo();
|
|||||||
*/
|
*/
|
||||||
console.log('[VoucherType] Loaded the following types:');
|
console.log('[VoucherType] Loaded the following types:');
|
||||||
voucherTypes.forEach((type, key) => {
|
voucherTypes.forEach((type, key) => {
|
||||||
console.log(`[VoucherType][${key}] ${type.expiration} minutes, ${type.usage === '1' ? 'single-use' : 'multi-use'}${typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : `${typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : ''}${typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : ''}${typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : ''}`}`);
|
console.log(`[VoucherType][${key}] ${time(type.expiration)}, ${type.usage === '1' ? 'single-use' : 'multi-use'}${typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : `${typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : ''}${typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : ''}${typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : ''}`}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,6 +80,7 @@ app.get('/', (req, res) => {
|
|||||||
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
|
banner_image: process.env.BANNER_IMAGE || `/images/bg-${random(1, 10)}.jpg`,
|
||||||
app_header: timeHeader,
|
app_header: timeHeader,
|
||||||
sid: uuidv4(),
|
sid: uuidv4(),
|
||||||
|
timeConvert: time,
|
||||||
voucher_types: voucherTypes
|
voucher_types: voucherTypes
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -88,13 +90,20 @@ app.post('/', async (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const check = req.body.password === (process.env.SECURITY_CODE || "0000");
|
const passwordCheck = req.body.password === (process.env.SECURITY_CODE || "0000");
|
||||||
|
|
||||||
if(!check) {
|
if(!passwordCheck) {
|
||||||
res.redirect(encodeURI(`/?error=Invalid password!`));
|
res.redirect(encodeURI(`/?error=Invalid password!`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.body['voucher-type']);
|
||||||
|
|
||||||
|
if(!typeCheck) {
|
||||||
|
res.redirect(encodeURI(`/?error=Unknown type!`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res.redirect(encodeURI(`/voucher?code=${req.body.password}&type=${req.body['voucher-type']}`));
|
res.redirect(encodeURI(`/voucher?code=${req.body.password}&type=${req.body['voucher-type']}`));
|
||||||
});
|
});
|
||||||
app.get('/voucher', async (req, res) => {
|
app.get('/voucher', async (req, res) => {
|
||||||
@@ -103,6 +112,11 @@ app.get('/voucher', async (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!(process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.query.type)) {
|
||||||
|
res.status(400).send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const hour = new Date().getHours();
|
const hour = new Date().getHours();
|
||||||
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
const timeHeader = hour < 12 ? 'Good Morning' : hour < 18 ? 'Good Afternoon' : 'Good Evening';
|
||||||
const voucherCode = await unifi(types(req.query.type, true));
|
const voucherCode = await unifi(types(req.query.type, true));
|
||||||
|
|||||||
@@ -56,7 +56,7 @@
|
|||||||
<div class="relative inline-block w-full">
|
<div class="relative inline-block w-full">
|
||||||
<select id="voucher-type" name="voucher-type" class="shadow appearance-none border rounded w-full py-2 px-3 pr-8 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>
|
<select id="voucher-type" name="voucher-type" class="shadow appearance-none border rounded w-full py-2 px-3 pr-8 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>
|
||||||
<% voucher_types.forEach((type) => { %>
|
<% voucher_types.forEach((type) => { %>
|
||||||
<option value="<%= type.raw %>"><%= type.expiration %> minutes, <%= type.usage === '1' ? 'single-use' : 'multi-use' %><%= typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : '' %><%= typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : '' %><%= typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : '' %><%= typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : '' %></option>
|
<option value="<%= type.raw %>"><%= timeConvert(type.expiration) %>, <%= type.usage === '1' ? 'single-use' : 'multi-use' %><%= typeof type.upload === "undefined" && typeof type.download === "undefined" && typeof type.megabytes === "undefined" ? ', no limits' : '' %><%= typeof type.upload !== "undefined" ? `, upload bandwidth limit: ${type.upload} kb/s` : '' %><%= typeof type.download !== "undefined" ? `, download bandwidth limit: ${type.download} kb/s` : '' %><%= typeof type.megabytes !== "undefined" ? `, quota limit: ${type.megabytes} mb` : '' %></option>
|
||||||
<% }); %>
|
<% }); %>
|
||||||
</select>
|
</select>
|
||||||
<div class="absolute inset-y-0 right-0 flex items-center px-2 mt-1 pointer-events-none">
|
<div class="absolute inset-y-0 right-0 flex items-center px-2 mt-1 pointer-events-none">
|
||||||
|
|||||||
Reference in New Issue
Block a user