From 9bde12b2befc396dfcd081b9aac4dfb0e5cee925 Mon Sep 17 00:00:00 2001 From: daniel-j Date: Wed, 4 Mar 2020 15:16:07 +0100 Subject: [PATCH] add a max duration to key, change a method from get to post --- download.html | 8 ++++---- index.js | 18 +++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/download.html b/download.html index 6231ad3..b332a7f 100644 --- a/download.html +++ b/download.html @@ -34,17 +34,17 @@ var downloadlink = document.getElementById('downloadlink') var key = null var pollTimer = null -function xhr(url, cb) { +function xhr(method, url, cb) { var x = new XMLHttpRequest() x.onload = function () { cb(x) } - x.open('GET', url, true) + x.open(method, url, true) x.send(null) } function pollFile () { - xhr('/status/' + key, function (x) { + xhr('GET', '/status/' + key, function (x) { var data try { data = JSON.parse(x.responseText) @@ -67,7 +67,7 @@ function generateKey () { keyOutput.textContent = '- - - -' if (pollTimer) clearInterval(pollTimer) downloads.style.display = 'none' - xhr('/generate', function (x) { + xhr('POST', '/generate', function (x) { keyOutput.textContent = x.responseText if (x.responseText !== 'error') { key = x.responseText diff --git a/index.js b/index.js index 188cd7a..1fa86a0 100644 --- a/index.js +++ b/index.js @@ -10,8 +10,9 @@ const fs = require('fs') const { spawn } = require('child_process') const port = 3001 -const expireDelay = 20 -const maxFileSize = 1024 * 1024 * 400 +const expireDelay = 30 // 30 seconds +const maxExpireDuration = 2 * 60 * 60 // 2 hours +const maxFileSize = 1024 * 1024 * 400 // 400 MB const keyChars = "123456789ACEFGHKLMNPRSTUVXYZ" const keyLength = 4 @@ -37,7 +38,7 @@ function removeKey (key) { } app.context.keys.delete(key) } else { - console.log('key dont exist', key) + console.log('Tried to remove non-existing key', key) } } @@ -90,7 +91,7 @@ const upload = multer({ } }) -router.get('/generate', async ctx => { +router.post('/generate', async ctx => { const agent = ctx.get('user-agent') if (!agent.includes('Kobo')) { console.error('Non-Kobo device tried to generate a key: ' + agent) @@ -98,25 +99,28 @@ router.get('/generate', async ctx => { } let key = null let attempts = 0 + console.log('There are currently', ctx.keys.size, 'key(s) in use.') + console.log('Generating unique key...', agent) do { key = randomKey() - console.log(attempts, ctx.keys.size, key) if (attempts > ctx.keys.size) { - console.error('Can\'t generate more keys, map is full.') + console.error('Can\'t generate more keys, map is full.', attempts, ctx.keys.size) ctx.body = 'error' return } attempts++ } while (ctx.keys.has(key)) + console.log('Generated key ' + key + ', '+attempts+' attempt(s)') + const info = { created: new Date(), agent: agent, file: null } - console.log(info) ctx.keys.set(key, info) expireKey(key) + setTimeout(removeKey, maxExpireDuration * 1000, key) ctx.body = key })