diff --git a/README.md b/README.md index 0ad9e31..8a66de1 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,9 @@ services: # Voucher Types, format: expiration in minutes (required),single-use or multi-use vouchers value - '0' is for multi-use - '1' is for single-use (optional),upload speed limit in kbps (optional),download speed limit in kbps (optional),data transfer limit in MB (optional) # To skip a parameter just but nothing in between the comma's # After a voucher type add a semicolon, after the semicolon you can start a new voucher type - VOUCHER_TYPES: '480,0,,,;' + VOUCHER_TYPES: '480,1,,,;' + # Allow users to create custom vouchers types within the UI + VOUCHER_CUSTOM: 'true' # Enable/disable the Web UI SERVICE_WEB: 'true' # Enable/disable the API diff --git a/docker-compose.yml b/docker-compose.yml index 3d38fb8..820885e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,7 @@ services: UNIFI_SITE_ID: 'default' SECURITY_CODE: '0000' DISABLE_AUTH: 'false' - VOUCHER_TYPES: '480,0,,,;' + VOUCHER_TYPES: '480,1,,,;' + VOUCHER_CUSTOM: 'true' SERVICE_WEB: 'true' SERVICE_API: 'false' diff --git a/server.js b/server.js index 7fa1bee..a45d0e4 100644 --- a/server.js +++ b/server.js @@ -38,7 +38,8 @@ const app = express(); /** * Define global functions and variables */ -const voucherTypes = types(config('voucher_types') || process.env.VOUCHER_TYPES || '480,0,,,;'); +const voucherTypes = types(config('voucher_types') || process.env.VOUCHER_TYPES || '480,1,,,;'); +const voucherCustom = (process.env.VOUCHER_CUSTOM === 'true') || true; const webService = (process.env.SERVICE_WEB === 'true') || true; const apiService = (process.env.SERVICE_API === 'true') || false; const authDisabled = (process.env.DISABLE_AUTH === 'true') || false; @@ -182,15 +183,17 @@ if(webService) { return; } - const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.body['voucher-type']); + if(req.body['voucher-type'] !== 'custom') { + const typeCheck = (process.env.VOUCHER_TYPES || '480,1,,,;').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, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`); - return; + 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, `${req.headers['x-ingress-path'] ? req.headers['x-ingress-path'] : ''}/vouchers`); + return; + } } // Create voucher code - const voucherCode = await unifi.create(types(req.body['voucher-type'], true), parseInt(req.body['voucher-amount'])).catch((e) => { + const voucherCode = await unifi.create(types(req.body['voucher-type'] === 'custom' ? `${req.body['voucher-duration']},${req.body['voucher-usage']},${req.body['voucher-upload-limit']},${req.body['voucher-download-limit']},${req.body['voucher-data-limit']};` : req.body['voucher-type'], true), parseInt(req.body['voucher-amount'])).catch((e) => { 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'] : ''}/vouchers`); }); @@ -377,6 +380,7 @@ if(webService) { error_text: req.flashMessage.message || '', timeConvert: time, voucher_types: voucherTypes, + voucher_custom: voucherCustom, vouchers: cache.vouchers, updated: cache.updated }); @@ -407,7 +411,7 @@ if(apiService) { }); }); app.get('/api/voucher/:type', [authorization.api], async (req, res) => { - const typeCheck = (process.env.VOUCHER_TYPES || '480,0,,,;').split(';').includes(req.params.type); + const typeCheck = (process.env.VOUCHER_TYPES || '480,1,,,;').split(';').includes(req.params.type); if(!typeCheck) { res.json({ diff --git a/template/voucher.ejs b/template/voucher.ejs index 0db84db..8ccffdd 100644 --- a/template/voucher.ejs +++ b/template/voucher.ejs @@ -31,12 +31,12 @@
@@ -210,7 +210,7 @@
-
+ @@ -325,6 +361,8 @@