1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-25 09:27:28 +00:00

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,
count: 50,
ip_mode: "header", // one of: "header", "address"
ip_header: "x-forwarded-for"
ip_header: "x-forwarded-for",
track: false
},
user_settings: [
@ -288,7 +289,8 @@ let constants = {
additional_routes: [],
database_version: 9
database_version: 10,
actually_backup_on_database_upgrade: true
}
// Override values from config and export the result

View File

@ -1,7 +1,8 @@
const constants = require("../constants")
const LimitByFrame = require("./LimitByFrame")
const {getIdentifier} = require("./get_identifier")
require("../testimports")(LimitByFrame, getIdentifier)
const db = require("../db")
require("../testimports")(LimitByFrame, getIdentifier, db)
const limiter = new LimitByFrame()
@ -13,19 +14,27 @@ function getIPFromReq(req) {
}
}
const preparedTrack = db.prepare("INSERT INTO QuotaHistory VALUES (?, ?, ?)")
function remaining(req) {
if (!constants.quota.enabled) return Infinity // sure.
const ip = getIPFromReq(req)
const identifier = getIdentifier(ip)
return limiter.remaining(identifier)
const identifier = String(getIdentifier(ip))
const remaining = limiter.remaining(identifier)
if (constants.quota.track) {
preparedTrack.run(identifier, Date.now(), remaining)
}
return remaining
}
function add(req, count) {
if (!constants.quota.enabled) return Infinity // why not.
const ip = getIPFromReq(req)
const identifier = getIdentifier(ip)
const identifier = String(getIdentifier(ip))
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))")
.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) {
const filename = `backups/bibliogram.db.bak-v${entry-1}`
process.stdout.write(`Backing up current to ${filename}... `)
if (constants.actually_backup_on_database_upgrade) {
await db.backup(pj(__dirname, "../../../db", filename))
process.stdout.write("done.\n")
} else {
process.stdout.write("jk. You turned off backups.\n")
}
}
/**