Option to track quota for analysis

This commit is contained in:
Cadence Ember 2020-07-23 01:20:06 +12:00
parent 112d9cc90e
commit f7c4ae19f4
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412
3 changed files with 32 additions and 8 deletions

View File

@ -117,7 +117,8 @@ let constants = {
timeframe: 20*60*60*1000, timeframe: 20*60*60*1000,
count: 50, count: 50,
ip_mode: "header", // one of: "header", "address" ip_mode: "header", // one of: "header", "address"
ip_header: "x-forwarded-for" ip_header: "x-forwarded-for",
track: false
}, },
user_settings: [ user_settings: [
@ -288,7 +289,8 @@ let constants = {
additional_routes: [], additional_routes: [],
database_version: 9 database_version: 10,
actually_backup_on_database_upgrade: true
} }
// Override values from config and export the result // Override values from config and export the result

View File

@ -1,7 +1,8 @@
const constants = require("../constants") const constants = require("../constants")
const LimitByFrame = require("./LimitByFrame") const LimitByFrame = require("./LimitByFrame")
const {getIdentifier} = require("./get_identifier") const {getIdentifier} = require("./get_identifier")
require("../testimports")(LimitByFrame, getIdentifier) const db = require("../db")
require("../testimports")(LimitByFrame, getIdentifier, db)
const limiter = new LimitByFrame() const limiter = new LimitByFrame()
@ -13,19 +14,27 @@ function getIPFromReq(req) {
} }
} }
const preparedTrack = db.prepare("INSERT INTO QuotaHistory VALUES (?, ?, ?)")
function remaining(req) { function remaining(req) {
if (!constants.quota.enabled) return Infinity // sure. if (!constants.quota.enabled) return Infinity // sure.
const ip = getIPFromReq(req) const ip = getIPFromReq(req)
const identifier = getIdentifier(ip) const identifier = String(getIdentifier(ip))
return limiter.remaining(identifier) const remaining = limiter.remaining(identifier)
if (constants.quota.track) {
preparedTrack.run(identifier, Date.now(), remaining)
}
return remaining
} }
function add(req, count) { function add(req, count) {
if (!constants.quota.enabled) return Infinity // why not. if (!constants.quota.enabled) return Infinity // why not.
const ip = getIPFromReq(req) const ip = getIPFromReq(req)
const identifier = getIdentifier(ip) const identifier = String(getIdentifier(ip))
return limiter.add(identifier, count) return limiter.add(identifier, count)
} }

View File

@ -112,6 +112,15 @@ const deltas = new Map([
db.prepare("CREATE TABLE SavedRequests (url TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (url))") db.prepare("CREATE TABLE SavedRequests (url TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (url))")
.run() .run()
})() })()
}],
// version 9 to version 10
[10, function() {
db.transaction(() => {
db.prepare("DROP TABLE IF EXISTS QuotaHistory")
.run()
db.prepare("CREATE Table QuotaHistory (identifier TEXT NOT NULL, timestamp INTEGER NOT NULL, remaining INTEGER NOT NULL)")
.run()
})()
}] }]
]) ])
@ -134,8 +143,12 @@ function writeProgress(i) {
async function createBackup(entry) { async function createBackup(entry) {
const filename = `backups/bibliogram.db.bak-v${entry-1}` const filename = `backups/bibliogram.db.bak-v${entry-1}`
process.stdout.write(`Backing up current to ${filename}... `) process.stdout.write(`Backing up current to ${filename}... `)
await db.backup(pj(__dirname, "../../../db", filename)) if (constants.actually_backup_on_database_upgrade) {
process.stdout.write("done.\n") await db.backup(pj(__dirname, "../../../db", filename))
process.stdout.write("done.\n")
} else {
process.stdout.write("jk. You turned off backups.\n")
}
} }
/** /**