mirror of
https://github.com/wukko/cobalt.git
synced 2025-06-03 16:39:25 +02:00
api/api-keys: watch for file changes instead of polling
This commit is contained in:
parent
e43f712eb6
commit
06ee65b55d
43
api/src/misc/file-watcher.js
Normal file
43
api/src/misc/file-watcher.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { EventEmitter } from 'node:events';
|
||||||
|
import * as fs from 'node:fs/promises';
|
||||||
|
|
||||||
|
export class FileWatcher extends EventEmitter {
|
||||||
|
#path;
|
||||||
|
#hasWatcher = false;
|
||||||
|
#lastChange = new Date().getTime();
|
||||||
|
|
||||||
|
constructor({ path, ...rest }) {
|
||||||
|
super(rest);
|
||||||
|
this.#path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
async #setupWatcher() {
|
||||||
|
if (this.#hasWatcher)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.#hasWatcher = true;
|
||||||
|
const watcher = fs.watch(this.#path);
|
||||||
|
for await (const _ of watcher) {
|
||||||
|
if (new Date() - this.#lastChange > 50) {
|
||||||
|
this.emit('file-updated');
|
||||||
|
this.#lastChange = new Date().getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
read() {
|
||||||
|
this.#setupWatcher();
|
||||||
|
return fs.readFile(this.#path, 'utf8');
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromFileProtocol(url_) {
|
||||||
|
const url = new URL(url_);
|
||||||
|
if (url.protocol !== 'file:') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pathname = url.pathname === '/' ? '' : url.pathname;
|
||||||
|
const file_path = decodeURIComponent(url.host + pathname);
|
||||||
|
return new this({ path: file_path });
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import { env } from "../config.js";
|
import { env } from "../config.js";
|
||||||
import { readFile } from "node:fs/promises";
|
|
||||||
import { Green, Yellow } from "../misc/console-text.js";
|
import { Green, Yellow } from "../misc/console-text.js";
|
||||||
import ip from "ipaddr.js";
|
import ip from "ipaddr.js";
|
||||||
import * as cluster from "../misc/cluster.js";
|
import * as cluster from "../misc/cluster.js";
|
||||||
|
import { FileWatcher } from "../misc/file-watcher.js";
|
||||||
|
|
||||||
// this function is a modified variation of code
|
// this function is a modified variation of code
|
||||||
// from https://stackoverflow.com/a/32402438/14855621
|
// from https://stackoverflow.com/a/32402438/14855621
|
||||||
@ -13,7 +13,7 @@ const generateWildcardRegex = rule => {
|
|||||||
|
|
||||||
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||||
|
|
||||||
let keys = {};
|
let keys = {}, reader = null;
|
||||||
|
|
||||||
const ALLOWED_KEYS = new Set(['name', 'ips', 'userAgents', 'limit']);
|
const ALLOWED_KEYS = new Set(['name', 'ips', 'userAgents', 'limit']);
|
||||||
|
|
||||||
@ -118,34 +118,39 @@ const formatKeys = (keyData) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updateKeys = (newKeys) => {
|
const updateKeys = (newKeys) => {
|
||||||
|
validateKeys(newKeys);
|
||||||
|
|
||||||
|
cluster.broadcast({ api_keys: newKeys });
|
||||||
|
|
||||||
keys = formatKeys(newKeys);
|
keys = formatKeys(newKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadKeys = async (source) => {
|
const loadRemoteKeys = async (source) => {
|
||||||
let updated;
|
updateKeys(
|
||||||
if (source.protocol === 'file:') {
|
await fetch(source).then(a => a.json())
|
||||||
const pathname = source.pathname === '/' ? '' : source.pathname;
|
);
|
||||||
updated = JSON.parse(
|
}
|
||||||
await readFile(
|
|
||||||
decodeURIComponent(source.host + pathname),
|
|
||||||
'utf8'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
updated = await fetch(source).then(a => a.json());
|
|
||||||
}
|
|
||||||
|
|
||||||
validateKeys(updated);
|
const loadLocalKeys = async () => {
|
||||||
|
updateKeys(
|
||||||
cluster.broadcast({ api_keys: updated });
|
JSON.parse(await reader.read())
|
||||||
|
);
|
||||||
updateKeys(updated);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const wrapLoad = (url, initial = false) => {
|
const wrapLoad = (url, initial = false) => {
|
||||||
loadKeys(url)
|
let load = loadRemoteKeys.bind(null, url);
|
||||||
.then(() => {
|
|
||||||
|
if (url.protocol === 'file:') {
|
||||||
if (initial) {
|
if (initial) {
|
||||||
|
reader = FileWatcher.fromFileProtocol(url);
|
||||||
|
reader.on('file-updated', () => wrapLoad(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
load = loadLocalKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
load().then(() => {
|
||||||
|
if (initial || reader) {
|
||||||
console.log(`${Green('[✓]')} api keys loaded successfully!`)
|
console.log(`${Green('[✓]')} api keys loaded successfully!`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -214,7 +219,7 @@ export const validateAuthorization = (req) => {
|
|||||||
export const setup = (url) => {
|
export const setup = (url) => {
|
||||||
if (cluster.isPrimary) {
|
if (cluster.isPrimary) {
|
||||||
wrapLoad(url, true);
|
wrapLoad(url, true);
|
||||||
if (env.keyReloadInterval > 0) {
|
if (env.keyReloadInterval > 0 && url.protocol !== 'file:') {
|
||||||
setInterval(() => wrapLoad(url), env.keyReloadInterval * 1000);
|
setInterval(() => wrapLoad(url), env.keyReloadInterval * 1000);
|
||||||
}
|
}
|
||||||
} else if (cluster.isWorker) {
|
} else if (cluster.isWorker) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user