api: configurable rate limit through env

This commit is contained in:
wukko 2024-05-16 13:54:11 +06:00
parent a70fc840dc
commit 391cf16c87
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 16 additions and 9 deletions

View file

@ -22,8 +22,8 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
}; };
const apiLimiter = rateLimit({ const apiLimiter = rateLimit({
windowMs: 60000, windowMs: env.rateLimitWindow * 1000,
max: 20, max: env.rateLimitMax || 20,
standardHeaders: true, standardHeaders: true,
legacyHeaders: false, legacyHeaders: false,
keyGenerator: req => generateHmac(getIP(req), ipSalt), keyGenerator: req => generateHmac(getIP(req), ipSalt),
@ -36,8 +36,8 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
}) })
const apiLimiterStream = rateLimit({ const apiLimiterStream = rateLimit({
windowMs: 60000, windowMs: env.rateLimitWindow * 1000,
max: 25, max: env.rateLimitMax || 20,
standardHeaders: true, standardHeaders: true,
legacyHeaders: false, legacyHeaders: false,
keyGenerator: req => generateHmac(getIP(req), ipSalt), keyGenerator: req => generateHmac(getIP(req), ipSalt),
@ -51,7 +51,7 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
app.set('trust proxy', ['loopback', 'uniquelocal']); app.set('trust proxy', ['loopback', 'uniquelocal']);
app.use('/api/:type', cors({ app.use('/api', cors({
methods: ['GET', 'POST'], methods: ['GET', 'POST'],
...corsConfig ...corsConfig
})) }))

View file

@ -28,18 +28,25 @@ const
// API mode related environment variables // API mode related environment variables
apiEnvs = { apiEnvs = {
apiURL,
apiPort: process.env.API_PORT || 9000, apiPort: process.env.API_PORT || 9000,
apiName: process.env.API_NAME || 'unknown', apiName: process.env.API_NAME || 'unknown',
listenAddress: process.env.API_LISTEN_ADDRESS, listenAddress: process.env.API_LISTEN_ADDRESS,
freebindCIDR: process.platform === 'linux' && process.env.FREEBIND_CIDR,
corsWildcard: process.env.CORS_WILDCARD !== '0', corsWildcard: process.env.CORS_WILDCARD !== '0',
corsURL: process.env.CORS_URL, corsURL: process.env.CORS_URL,
cookiePath: process.env.COOKIE_PATH, cookiePath: process.env.COOKIE_PATH,
tiktokDeviceInfo: process.env.TIKTOK_DEVICE_INFO && JSON.parse(process.env.TIKTOK_DEVICE_INFO),
rateLimitWindow: (process.env.RATELIMIT_WINDOW && parseInt(process.env.RATELIMIT_WINDOW)) || 60,
rateLimitMax: (process.env.RATELIMIT_MAX && parseInt(process.env.RATELIMIT_MAX)) || 20,
processingPriority: process.platform !== 'win32' processingPriority: process.platform !== 'win32'
&& process.env.PROCESSING_PRIORITY && process.env.PROCESSING_PRIORITY
&& parseInt(process.env.PROCESSING_PRIORITY), && parseInt(process.env.PROCESSING_PRIORITY)
tiktokDeviceInfo: process.env.TIKTOK_DEVICE_INFO && JSON.parse(process.env.TIKTOK_DEVICE_INFO),
freebindCIDR: process.platform === 'linux' && process.env.FREEBIND_CIDR,
apiURL
} }
export const export const