CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
111 lines
3.8 KiB
JavaScript
111 lines
3.8 KiB
JavaScript
const { EventEmitter } = require("node:events");
|
|
const Account = require("../models/Account");
|
|
const Usage = require("../models/Usage");
|
|
const settings = require("./settings");
|
|
const logger = require("../utils/logger");
|
|
|
|
const KEYS = ["tunnels", "persistent", "domains", "traffic"];
|
|
const LABELS = { tunnels: "tunnels", persistent: "persistent tunnels", domains: "custom domains", traffic: "monthly traffic" };
|
|
const FLUSH_INTERVAL = 30 * 1000;
|
|
|
|
const month = () => new Date().toISOString().slice(0, 7);
|
|
|
|
const fail = (message, code) => Object.assign(new Error(message), { code });
|
|
|
|
const parseLimits = (input = {}, partial = false) => {
|
|
const limits = {};
|
|
for (const key of KEYS) {
|
|
const raw = input[key];
|
|
if (raw === undefined || raw === null || raw === "") {
|
|
if (!partial) limits[key] = 0;
|
|
continue;
|
|
}
|
|
const value = Number(raw);
|
|
if (!Number.isInteger(value) || value < 0) throw fail(`${LABELS[key]} must be a whole number, 0 for no limit`, "bad_request");
|
|
limits[key] = value;
|
|
}
|
|
return limits;
|
|
};
|
|
|
|
const NONE = parseLimits();
|
|
|
|
class Quotas extends EventEmitter {
|
|
constructor() {
|
|
super();
|
|
this.defaults = NONE;
|
|
this.pending = new Map();
|
|
this.timer = null;
|
|
}
|
|
|
|
async load() {
|
|
this.defaults = parseLimits((await settings.read()).quotas);
|
|
}
|
|
|
|
async saveDefaults(input) {
|
|
this.defaults = parseLimits(input);
|
|
await settings.write({ quotas: this.defaults });
|
|
return this.defaults;
|
|
}
|
|
|
|
static parseOverride(input) {
|
|
if (input === null) return null;
|
|
const limits = parseLimits(input, true);
|
|
return Object.keys(limits).length ? limits : null;
|
|
}
|
|
|
|
async limitsFor(accountId) {
|
|
const account = await Account.findByPk(accountId, { attributes: ["role", "quotas"] });
|
|
if (!account || account.role === "admin") return NONE;
|
|
return { ...this.defaults, ...(account.quotas ? JSON.parse(account.quotas) : {}) };
|
|
}
|
|
|
|
async trafficFor(accountId) {
|
|
const row = await Usage.findOne({ where: { accountId, month: month() } });
|
|
return row ? Number(row.bytesIn) + Number(row.bytesOut) : 0;
|
|
}
|
|
|
|
async enforce(accountId, key, used) {
|
|
const limit = (await this.limitsFor(accountId))[key];
|
|
if (limit && used >= limit) throw fail(`Limit of ${limit} ${LABELS[key]} reached`, "quota_exceeded");
|
|
}
|
|
|
|
async enforceTraffic(accountId) {
|
|
const limit = (await this.limitsFor(accountId)).traffic;
|
|
if (limit && await this.trafficFor(accountId) >= limit) throw fail("Monthly traffic limit reached", "quota_exceeded");
|
|
}
|
|
|
|
record(accountId, bytesIn, bytesOut, requests) {
|
|
if (!(bytesIn || bytesOut || requests)) return;
|
|
const entry = this.pending.get(accountId) || { bytesIn: 0, bytesOut: 0, requests: 0 };
|
|
entry.bytesIn += bytesIn;
|
|
entry.bytesOut += bytesOut;
|
|
entry.requests += requests;
|
|
this.pending.set(accountId, entry);
|
|
}
|
|
|
|
start() {
|
|
if (this.timer) return;
|
|
this.timer = setInterval(() => this.flush().catch(err => logger.warn(`Could not store usage: ${err.message}`)), FLUSH_INTERVAL);
|
|
this.timer.unref();
|
|
}
|
|
|
|
stop() {
|
|
if (this.timer) clearInterval(this.timer);
|
|
this.timer = null;
|
|
}
|
|
|
|
async flush() {
|
|
const batch = this.pending;
|
|
this.pending = new Map();
|
|
const current = month();
|
|
for (const [accountId, delta] of batch) {
|
|
const [row] = await Usage.findOrCreate({ where: { accountId, month: current } });
|
|
await Usage.increment(delta, { where: { id: row.id } });
|
|
const limit = (await this.limitsFor(accountId)).traffic;
|
|
if (limit && await this.trafficFor(accountId) >= limit) this.emit("exceeded", accountId);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { Quotas };
|