mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Merge branch 'limiter' into 'main'
Rate-limit HTTP requests See merge request soapbox-pub/ditto!390
This commit is contained in:
commit
5ac19715cd
5 changed files with 26 additions and 3 deletions
|
|
@ -44,6 +44,7 @@
|
||||||
"entities": "npm:entities@^4.5.0",
|
"entities": "npm:entities@^4.5.0",
|
||||||
"fast-stable-stringify": "npm:fast-stable-stringify@^1.0.0",
|
"fast-stable-stringify": "npm:fast-stable-stringify@^1.0.0",
|
||||||
"formdata-helper": "npm:formdata-helper@^0.3.0",
|
"formdata-helper": "npm:formdata-helper@^0.3.0",
|
||||||
|
"hono-rate-limiter": "npm:hono-rate-limiter@^0.3.0",
|
||||||
"iso-639-1": "npm:iso-639-1@2.1.15",
|
"iso-639-1": "npm:iso-639-1@2.1.15",
|
||||||
"isomorphic-dompurify": "npm:isomorphic-dompurify@^2.11.0",
|
"isomorphic-dompurify": "npm:isomorphic-dompurify@^2.11.0",
|
||||||
"kysely": "npm:kysely@^0.27.3",
|
"kysely": "npm:kysely@^0.27.3",
|
||||||
|
|
|
||||||
1
deno.lock
generated
1
deno.lock
generated
|
|
@ -1395,6 +1395,7 @@
|
||||||
"npm:entities@^4.5.0",
|
"npm:entities@^4.5.0",
|
||||||
"npm:fast-stable-stringify@^1.0.0",
|
"npm:fast-stable-stringify@^1.0.0",
|
||||||
"npm:formdata-helper@^0.3.0",
|
"npm:formdata-helper@^0.3.0",
|
||||||
|
"npm:hono-rate-limiter@^0.3.0",
|
||||||
"npm:iso-639-1@2.1.15",
|
"npm:iso-639-1@2.1.15",
|
||||||
"npm:isomorphic-dompurify@^2.11.0",
|
"npm:isomorphic-dompurify@^2.11.0",
|
||||||
"npm:kysely@^0.27.3",
|
"npm:kysely@^0.27.3",
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,10 @@ server {
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection "upgrade";
|
proxy_set_header Connection "upgrade";
|
||||||
proxy_set_header Host $http_host;
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
root /opt/ditto/public;
|
root /opt/ditto/public;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,14 @@ import Debug from '@soapbox/stickynotes/debug';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { cron } from '@/cron.ts';
|
import { cron } from '@/cron.ts';
|
||||||
import { startFirehose } from '@/firehose.ts';
|
import { startFirehose } from '@/firehose.ts';
|
||||||
|
import { Time } from '@/utils/time.ts';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
accountController,
|
accountController,
|
||||||
accountLookupController,
|
accountLookupController,
|
||||||
accountSearchController,
|
accountSearchController,
|
||||||
accountStatusesController,
|
accountStatusesController,
|
||||||
|
blockController,
|
||||||
createAccountController,
|
createAccountController,
|
||||||
familiarFollowersController,
|
familiarFollowersController,
|
||||||
favouritesController,
|
favouritesController,
|
||||||
|
|
@ -22,6 +24,7 @@ import {
|
||||||
followingController,
|
followingController,
|
||||||
muteController,
|
muteController,
|
||||||
relationshipsController,
|
relationshipsController,
|
||||||
|
unblockController,
|
||||||
unfollowController,
|
unfollowController,
|
||||||
unmuteController,
|
unmuteController,
|
||||||
updateCredentialsController,
|
updateCredentialsController,
|
||||||
|
|
@ -110,11 +113,10 @@ import { nodeInfoController, nodeInfoSchemaController } from '@/controllers/well
|
||||||
import { nostrController } from '@/controllers/well-known/nostr.ts';
|
import { nostrController } from '@/controllers/well-known/nostr.ts';
|
||||||
import { auth98Middleware, requireProof, requireRole } from '@/middleware/auth98Middleware.ts';
|
import { auth98Middleware, requireProof, requireRole } from '@/middleware/auth98Middleware.ts';
|
||||||
import { cspMiddleware } from '@/middleware/cspMiddleware.ts';
|
import { cspMiddleware } from '@/middleware/cspMiddleware.ts';
|
||||||
|
import { rateLimitMiddleware } from '@/middleware/rateLimitMiddleware.ts';
|
||||||
import { requireSigner } from '@/middleware/requireSigner.ts';
|
import { requireSigner } from '@/middleware/requireSigner.ts';
|
||||||
import { signerMiddleware } from '@/middleware/signerMiddleware.ts';
|
import { signerMiddleware } from '@/middleware/signerMiddleware.ts';
|
||||||
import { storeMiddleware } from '@/middleware/storeMiddleware.ts';
|
import { storeMiddleware } from '@/middleware/storeMiddleware.ts';
|
||||||
import { blockController } from '@/controllers/api/accounts.ts';
|
|
||||||
import { unblockController } from '@/controllers/api/accounts.ts';
|
|
||||||
import { uploaderMiddleware } from '@/middleware/uploaderMiddleware.ts';
|
import { uploaderMiddleware } from '@/middleware/uploaderMiddleware.ts';
|
||||||
|
|
||||||
interface AppEnv extends HonoEnv {
|
interface AppEnv extends HonoEnv {
|
||||||
|
|
@ -145,6 +147,8 @@ if (Conf.cronEnabled) {
|
||||||
cron();
|
cron();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.use('*', rateLimitMiddleware(300, Time.minutes(5)));
|
||||||
|
|
||||||
app.use('/api/*', logger(debug));
|
app.use('/api/*', logger(debug));
|
||||||
app.use('/.well-known/*', logger(debug));
|
app.use('/.well-known/*', logger(debug));
|
||||||
app.use('/users/*', logger(debug));
|
app.use('/users/*', logger(debug));
|
||||||
|
|
|
||||||
15
src/middleware/rateLimitMiddleware.ts
Normal file
15
src/middleware/rateLimitMiddleware.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { MiddlewareHandler } from '@hono/hono';
|
||||||
|
import { rateLimiter } from 'hono-rate-limiter';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rate limit middleware for Hono, based on [`hono-rate-limiter`](https://github.com/rhinobase/hono-rate-limiter).
|
||||||
|
*/
|
||||||
|
export function rateLimitMiddleware(limit: number, windowMs: number): MiddlewareHandler {
|
||||||
|
// @ts-ignore Mismatched hono versions.
|
||||||
|
return rateLimiter({
|
||||||
|
limit,
|
||||||
|
windowMs,
|
||||||
|
skip: (c) => !c.req.header('x-real-ip'),
|
||||||
|
keyGenerator: (c) => c.req.header('x-real-ip')!,
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue