mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 19:47:00 +03:00
Merge branch 'master' of https://github.com/ajayyy/SponsorBlockServer into k8s-test
This commit is contained in:
@@ -165,7 +165,7 @@ function loadFromEnv(config: SBSConfig, prefix = "") {
|
|||||||
const fullKey = (prefix ? `${prefix}_` : "") + key;
|
const fullKey = (prefix ? `${prefix}_` : "") + key;
|
||||||
const data = config[key];
|
const data = config[key];
|
||||||
|
|
||||||
if (typeof data === "object" && !Array.isArray(data)) {
|
if (data && typeof data === "object" && !Array.isArray(data)) {
|
||||||
loadFromEnv(data, fullKey);
|
loadFromEnv(data, fullKey);
|
||||||
} else if (process.env[fullKey]) {
|
} else if (process.env[fullKey]) {
|
||||||
const value = process.env[fullKey];
|
const value = process.env[fullKey];
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ import { RateLimitConfig } from "../types/config.model";
|
|||||||
import { Request } from "express";
|
import { Request } from "express";
|
||||||
import { isUserVIP } from "../utils/isUserVIP";
|
import { isUserVIP } from "../utils/isUserVIP";
|
||||||
import { UserID } from "../types/user.model";
|
import { UserID } from "../types/user.model";
|
||||||
import RedisStore from "rate-limit-redis";
|
import RedisStore, { RedisReply } from "rate-limit-redis";
|
||||||
import redis from "../utils/redis";
|
import redis from "../utils/redis";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
|
import { Logger } from "../utils/logger";
|
||||||
|
|
||||||
export function rateLimitMiddleware(limitConfig: RateLimitConfig, getUserID?: (req: Request) => UserID): RateLimitRequestHandler {
|
export function rateLimitMiddleware(limitConfig: RateLimitConfig, getUserID?: (req: Request) => UserID): RateLimitRequestHandler {
|
||||||
return rateLimit({
|
return rateLimit({
|
||||||
@@ -29,7 +30,7 @@ export function rateLimitMiddleware(limitConfig: RateLimitConfig, getUserID?: (r
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
store: config.redis?.enabled ? new RedisStore({
|
store: config.redis?.enabled ? new RedisStore({
|
||||||
sendCommand: (...args: string[]) => redis.sendCommand(args),
|
sendCommand: (...args: string[]) => redis.sendCommand(args).catch((err) => Logger.error(err)) as Promise<RedisReply>,
|
||||||
}) : null,
|
}) : null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import { getHash } from "../utils/getHash";
|
|||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
|
|
||||||
export function userCounter(req: Request, res: Response, next: NextFunction): void {
|
export function userCounter(req: Request, res: Response, next: NextFunction): void {
|
||||||
axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getHash(getIP(req), 1)}`)
|
if (req.method !== "OPTIONS") {
|
||||||
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getHash(getIP(req), 1)}`)
|
||||||
|
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
||||||
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,11 @@ class DiskCache {
|
|||||||
|
|
||||||
return result.status === 200 ? result.data : null;
|
return result.status === 200 ? result.data : null;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(`DiskCache: Error getting key ${key}: ${err}`);
|
const response = (err as AxiosError).response;
|
||||||
|
if (!response || response.status !== 404) {
|
||||||
|
Logger.error(`DiskCache: Error getting key ${key}: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ async function getFromRedis<T extends string>(key: HashedValue): Promise<T & Has
|
|||||||
|
|
||||||
// Otherwise, calculate it
|
// Otherwise, calculate it
|
||||||
const data = getHash(key, cachedHashTimes);
|
const data = getHash(key, cachedHashTimes);
|
||||||
redis.set(key, data);
|
redis.set(key, data).catch((err) => Logger.error(err));
|
||||||
|
|
||||||
return data as T & HashedValue;
|
return data as T & HashedValue;
|
||||||
}
|
}
|
||||||
@@ -16,7 +16,7 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
|
|||||||
|
|
||||||
const data = await fetchFromDB();
|
const data = await fetchFromDB();
|
||||||
|
|
||||||
redis.set(key, JSON.stringify(data));
|
redis.set(key, JSON.stringify(data)).catch((err) => Logger.error(err));
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ async function getAndSplit<T, U extends string>(fetchFromDB: (values: U[]) => Pr
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const key in newResults) {
|
for (const key in newResults) {
|
||||||
redis.set(key, JSON.stringify(newResults[key]));
|
redis.set(key, JSON.stringify(newResults[key])).catch((err) => Logger.error(err));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -77,16 +77,16 @@ async function getAndSplit<T, U extends string>(fetchFromDB: (values: U[]) => Pr
|
|||||||
|
|
||||||
function clearSegmentCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; userID?: UserID; }): void {
|
function clearSegmentCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; userID?: UserID; }): void {
|
||||||
if (videoInfo) {
|
if (videoInfo) {
|
||||||
redis.del(skipSegmentsKey(videoInfo.videoID, videoInfo.service));
|
redis.del(skipSegmentsKey(videoInfo.videoID, videoInfo.service)).catch((err) => Logger.error(err));
|
||||||
redis.del(skipSegmentGroupsKey(videoInfo.videoID, videoInfo.service));
|
redis.del(skipSegmentGroupsKey(videoInfo.videoID, videoInfo.service)).catch((err) => Logger.error(err));
|
||||||
redis.del(skipSegmentsHashKey(videoInfo.hashedVideoID, videoInfo.service));
|
redis.del(skipSegmentsHashKey(videoInfo.hashedVideoID, videoInfo.service)).catch((err) => Logger.error(err));
|
||||||
if (videoInfo.userID) redis.del(reputationKey(videoInfo.userID));
|
if (videoInfo.userID) redis.del(reputationKey(videoInfo.userID)).catch((err) => Logger.error(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearRatingCache(videoInfo: { hashedVideoID: VideoIDHash; service: Service;}): void {
|
function clearRatingCache(videoInfo: { hashedVideoID: VideoIDHash; service: Service;}): void {
|
||||||
if (videoInfo) {
|
if (videoInfo) {
|
||||||
redis.del(ratingHashKey(videoInfo.hashedVideoID, videoInfo.service));
|
redis.del(ratingHashKey(videoInfo.hashedVideoID, videoInfo.service)).catch((err) => Logger.error(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ if (config.redis?.enabled) {
|
|||||||
client.connect();
|
client.connect();
|
||||||
exportClient = client;
|
exportClient = client;
|
||||||
|
|
||||||
const timeoutDuration = 200;
|
const timeoutDuration = 40;
|
||||||
const get = client.get.bind(client);
|
const get = client.get.bind(client);
|
||||||
exportClient.get = (key) => new Promise((resolve, reject) => {
|
exportClient.get = (key) => new Promise((resolve, reject) => {
|
||||||
const timeout = setTimeout(() => reject(), timeoutDuration);
|
const timeout = setTimeout(() => reject(), timeoutDuration);
|
||||||
|
|||||||
Reference in New Issue
Block a user