mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Merge branch 'ipwhitelist' into 'main'
Add IP_WHITELIST variable to bypass rate limiting See merge request soapbox-pub/ditto!627
This commit is contained in:
commit
0375608909
3 changed files with 16 additions and 2 deletions
|
|
@ -36,6 +36,10 @@ class Conf {
|
||||||
static get port(): number {
|
static get port(): number {
|
||||||
return parseInt(Deno.env.get('PORT') || '4036');
|
return parseInt(Deno.env.get('PORT') || '4036');
|
||||||
}
|
}
|
||||||
|
/** IP addresses not affected by rate limiting. */
|
||||||
|
static get ipWhitelist(): string[] {
|
||||||
|
return Deno.env.get('IP_WHITELIST')?.split(',') || [];
|
||||||
|
}
|
||||||
/** Relay URL to the Ditto server's relay. */
|
/** Relay URL to the Ditto server's relay. */
|
||||||
static get relay(): `wss://${string}` | `ws://${string}` {
|
static get relay(): `wss://${string}` | `ws://${string}` {
|
||||||
const { protocol, host } = Conf.url;
|
const { protocol, host } = Conf.url;
|
||||||
|
|
|
||||||
|
|
@ -210,7 +210,12 @@ const relayController: AppController = (c, next) => {
|
||||||
return c.text('Please use a Nostr client to connect.', 400);
|
return c.text('Please use a Nostr client to connect.', 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ip = c.req.header('x-real-ip');
|
let ip = c.req.header('x-real-ip');
|
||||||
|
|
||||||
|
if (ip && Conf.ipWhitelist.includes(ip)) {
|
||||||
|
ip = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
if (ip) {
|
if (ip) {
|
||||||
const remaining = Object
|
const remaining = Object
|
||||||
.values(limiters)
|
.values(limiters)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import { MiddlewareHandler } from '@hono/hono';
|
import { MiddlewareHandler } from '@hono/hono';
|
||||||
import { rateLimiter } from 'hono-rate-limiter';
|
import { rateLimiter } from 'hono-rate-limiter';
|
||||||
|
|
||||||
|
import { Conf } from '@/config.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rate limit middleware for Hono, based on [`hono-rate-limiter`](https://github.com/rhinobase/hono-rate-limiter).
|
* Rate limit middleware for Hono, based on [`hono-rate-limiter`](https://github.com/rhinobase/hono-rate-limiter).
|
||||||
*/
|
*/
|
||||||
|
|
@ -14,7 +16,10 @@ export function rateLimitMiddleware(limit: number, windowMs: number, includeHead
|
||||||
c.header('Cache-Control', 'no-store');
|
c.header('Cache-Control', 'no-store');
|
||||||
return c.text('Too many requests, please try again later.', 429);
|
return c.text('Too many requests, please try again later.', 429);
|
||||||
},
|
},
|
||||||
skip: (c) => !c.req.header('x-real-ip'),
|
skip: (c) => {
|
||||||
|
const ip = c.req.header('x-real-ip');
|
||||||
|
return !ip || Conf.ipWhitelist.includes(ip);
|
||||||
|
},
|
||||||
keyGenerator: (c) => c.req.header('x-real-ip')!,
|
keyGenerator: (c) => c.req.header('x-real-ip')!,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue