2022-08-08 15:52:56 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
|
|
* This file is part of Auth-Server-Helper which is released under MIT.
|
|
|
|
* See file 'LICENSE' for full license details.
|
|
|
|
* Created by Timo Hocker <timo@scode.ovh>, August 2022
|
|
|
|
*/
|
|
|
|
|
|
|
|
import IORedis from 'ioredis';
|
2022-08-10 16:18:05 +02:00
|
|
|
import { debug } from './debug';
|
|
|
|
import { LabelledKey } from './Key';
|
2022-08-08 15:52:56 +02:00
|
|
|
|
2022-08-10 16:18:05 +02:00
|
|
|
const logger = debug ('redis');
|
2022-08-08 15:52:56 +02:00
|
|
|
|
|
|
|
export class Redis {
|
|
|
|
private _redis: IORedis | null = null;
|
|
|
|
|
2022-08-10 16:18:05 +02:00
|
|
|
public connect (url: string): void {
|
|
|
|
const log = logger.extend ('connect');
|
|
|
|
log ('connecting to redis instance %s', url);
|
2022-08-08 15:52:56 +02:00
|
|
|
if (this._redis !== null) {
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('disconnecting existing redis client');
|
|
|
|
this.disconnect ();
|
2022-08-08 15:52:56 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 16:18:05 +02:00
|
|
|
this._redis = new IORedis (url);
|
|
|
|
this._redis.on ('connect', () => {
|
|
|
|
log ('connected');
|
2022-08-08 15:52:56 +02:00
|
|
|
});
|
2022-08-10 16:18:05 +02:00
|
|
|
this._redis.on ('ready', () => {
|
|
|
|
log ('ready');
|
2022-08-08 15:52:56 +02:00
|
|
|
});
|
2022-08-10 16:18:05 +02:00
|
|
|
this._redis.on ('error', (err) => {
|
|
|
|
log ('error %o', err);
|
2022-08-08 15:52:56 +02:00
|
|
|
});
|
2022-08-10 16:18:05 +02:00
|
|
|
this._redis.on ('reconnecting', () => {
|
|
|
|
log ('reconnecting');
|
2022-08-08 15:52:56 +02:00
|
|
|
});
|
2022-08-10 16:18:05 +02:00
|
|
|
this._redis.on ('end', () => {
|
|
|
|
log ('connection ended');
|
2022-08-08 15:52:56 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:18:05 +02:00
|
|
|
public disconnect (): void {
|
|
|
|
const log = logger.extend ('disconnect');
|
|
|
|
log ('disconnecting redis client');
|
2022-08-08 15:52:56 +02:00
|
|
|
if (this._redis === null) {
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('redis is inactive, skipping');
|
2022-08-08 15:52:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-08-10 16:18:05 +02:00
|
|
|
this._redis.quit ();
|
2022-08-08 15:52:56 +02:00
|
|
|
this._redis = null;
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('done');
|
2022-08-08 15:52:56 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 16:18:05 +02:00
|
|
|
public async set_key (key: LabelledKey): Promise<void> {
|
|
|
|
const log = logger.extend ('set_key');
|
|
|
|
log ('trying to set key %s to redis', key.index);
|
2022-08-08 15:52:56 +02:00
|
|
|
if (this._redis === null) {
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('redis is inactive, skipping');
|
2022-08-08 15:52:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-08-10 16:18:05 +02:00
|
|
|
const valid_for = Math.floor (
|
|
|
|
(key.valid_until - (new Date)
|
|
|
|
.getTime ()) / 1000
|
2022-08-10 16:17:00 +02:00
|
|
|
);
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('key is valid for %d seconds', valid_for);
|
|
|
|
await this._redis.setex (key.index, valid_for, JSON.stringify (key));
|
|
|
|
log ('saved key');
|
2022-08-08 15:52:56 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 16:18:05 +02:00
|
|
|
public async get_key (index: string): Promise<LabelledKey | null> {
|
|
|
|
const log = logger.extend ('get_key');
|
|
|
|
log ('trying to get key %s from redis', index);
|
2022-08-08 15:52:56 +02:00
|
|
|
if (this._redis === null) {
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('redis is inactive, skipping');
|
2022-08-08 15:52:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-10 16:18:05 +02:00
|
|
|
const res = await this._redis.get (index);
|
2022-08-08 15:52:56 +02:00
|
|
|
if (res === null) {
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('key not found in redis');
|
2022-08-08 15:52:56 +02:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-10 16:18:05 +02:00
|
|
|
log ('key found');
|
|
|
|
return JSON.parse (res);
|
2022-08-08 15:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:18:05 +02:00
|
|
|
export const redis = (new Redis);
|