add a max duration to key, change a method from get to post
This commit is contained in:
@@ -34,17 +34,17 @@ var downloadlink = document.getElementById('downloadlink')
|
|||||||
var key = null
|
var key = null
|
||||||
var pollTimer = null
|
var pollTimer = null
|
||||||
|
|
||||||
function xhr(url, cb) {
|
function xhr(method, url, cb) {
|
||||||
var x = new XMLHttpRequest()
|
var x = new XMLHttpRequest()
|
||||||
x.onload = function () {
|
x.onload = function () {
|
||||||
cb(x)
|
cb(x)
|
||||||
}
|
}
|
||||||
x.open('GET', url, true)
|
x.open(method, url, true)
|
||||||
x.send(null)
|
x.send(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
function pollFile () {
|
function pollFile () {
|
||||||
xhr('/status/' + key, function (x) {
|
xhr('GET', '/status/' + key, function (x) {
|
||||||
var data
|
var data
|
||||||
try {
|
try {
|
||||||
data = JSON.parse(x.responseText)
|
data = JSON.parse(x.responseText)
|
||||||
@@ -67,7 +67,7 @@ function generateKey () {
|
|||||||
keyOutput.textContent = '- - - -'
|
keyOutput.textContent = '- - - -'
|
||||||
if (pollTimer) clearInterval(pollTimer)
|
if (pollTimer) clearInterval(pollTimer)
|
||||||
downloads.style.display = 'none'
|
downloads.style.display = 'none'
|
||||||
xhr('/generate', function (x) {
|
xhr('POST', '/generate', function (x) {
|
||||||
keyOutput.textContent = x.responseText
|
keyOutput.textContent = x.responseText
|
||||||
if (x.responseText !== 'error') {
|
if (x.responseText !== 'error') {
|
||||||
key = x.responseText
|
key = x.responseText
|
||||||
|
|||||||
18
index.js
18
index.js
@@ -10,8 +10,9 @@ const fs = require('fs')
|
|||||||
const { spawn } = require('child_process')
|
const { spawn } = require('child_process')
|
||||||
|
|
||||||
const port = 3001
|
const port = 3001
|
||||||
const expireDelay = 20
|
const expireDelay = 30 // 30 seconds
|
||||||
const maxFileSize = 1024 * 1024 * 400
|
const maxExpireDuration = 2 * 60 * 60 // 2 hours
|
||||||
|
const maxFileSize = 1024 * 1024 * 400 // 400 MB
|
||||||
|
|
||||||
const keyChars = "123456789ACEFGHKLMNPRSTUVXYZ"
|
const keyChars = "123456789ACEFGHKLMNPRSTUVXYZ"
|
||||||
const keyLength = 4
|
const keyLength = 4
|
||||||
@@ -37,7 +38,7 @@ function removeKey (key) {
|
|||||||
}
|
}
|
||||||
app.context.keys.delete(key)
|
app.context.keys.delete(key)
|
||||||
} else {
|
} 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')
|
const agent = ctx.get('user-agent')
|
||||||
if (!agent.includes('Kobo')) {
|
if (!agent.includes('Kobo')) {
|
||||||
console.error('Non-Kobo device tried to generate a key: ' + agent)
|
console.error('Non-Kobo device tried to generate a key: ' + agent)
|
||||||
@@ -98,25 +99,28 @@ router.get('/generate', async ctx => {
|
|||||||
}
|
}
|
||||||
let key = null
|
let key = null
|
||||||
let attempts = 0
|
let attempts = 0
|
||||||
|
console.log('There are currently', ctx.keys.size, 'key(s) in use.')
|
||||||
|
console.log('Generating unique key...', agent)
|
||||||
do {
|
do {
|
||||||
key = randomKey()
|
key = randomKey()
|
||||||
console.log(attempts, ctx.keys.size, key)
|
|
||||||
if (attempts > ctx.keys.size) {
|
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'
|
ctx.body = 'error'
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
attempts++
|
attempts++
|
||||||
} while (ctx.keys.has(key))
|
} while (ctx.keys.has(key))
|
||||||
|
|
||||||
|
console.log('Generated key ' + key + ', '+attempts+' attempt(s)')
|
||||||
|
|
||||||
const info = {
|
const info = {
|
||||||
created: new Date(),
|
created: new Date(),
|
||||||
agent: agent,
|
agent: agent,
|
||||||
file: null
|
file: null
|
||||||
}
|
}
|
||||||
console.log(info)
|
|
||||||
ctx.keys.set(key, info)
|
ctx.keys.set(key, info)
|
||||||
expireKey(key)
|
expireKey(key)
|
||||||
|
setTimeout(removeKey, maxExpireDuration * 1000, key)
|
||||||
|
|
||||||
ctx.body = key
|
ctx.body = key
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user