diff options
| author | dakkar <dakkar@thenautilus.net> | 2024-09-13 18:55:10 +0000 |
|---|---|---|
| committer | dakkar <dakkar@thenautilus.net> | 2024-09-13 18:55:10 +0000 |
| commit | 0411812b970b6a5860b83a7586e640a57b396b9b (patch) | |
| tree | 13e796260840251f9e5b74c8e1c00b7f34bf55cd /packages/backend/src/server/api/StreamingApiServerService.ts | |
| parent | merge: bump version (!561) (diff) | |
| parent | Merge branch 'stable' into bump-version (diff) | |
| download | sharkey-0411812b970b6a5860b83a7586e640a57b396b9b.tar.gz sharkey-0411812b970b6a5860b83a7586e640a57b396b9b.tar.bz2 sharkey-0411812b970b6a5860b83a7586e640a57b396b9b.zip | |
merge: Bump version (!620)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/620
Approved-by: Julia <julia@insertdomain.name>
Diffstat (limited to 'packages/backend/src/server/api/StreamingApiServerService.ts')
| -rw-r--r-- | packages/backend/src/server/api/StreamingApiServerService.ts | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/packages/backend/src/server/api/StreamingApiServerService.ts b/packages/backend/src/server/api/StreamingApiServerService.ts index b8f448477b..9b8464f705 100644 --- a/packages/backend/src/server/api/StreamingApiServerService.ts +++ b/packages/backend/src/server/api/StreamingApiServerService.ts @@ -19,7 +19,15 @@ import { ChannelFollowingService } from '@/core/ChannelFollowingService.js'; import { AuthenticateService, AuthenticationError } from './AuthenticateService.js'; import MainStreamConnection from './stream/Connection.js'; import { ChannelsService } from './stream/ChannelsService.js'; +import { RateLimiterService } from './RateLimiterService.js'; +import { RoleService } from '@/core/RoleService.js'; +import { getIpHash } from '@/misc/get-ip-hash.js'; +import proxyAddr from 'proxy-addr'; +import ms from 'ms'; import type * as http from 'node:http'; +import type { IEndpointMeta } from './endpoints.js'; +import { LoggerService } from '@/core/LoggerService.js'; +import type Logger from '@/logger.js'; @Injectable() export class StreamingApiServerService { @@ -41,10 +49,36 @@ export class StreamingApiServerService { private notificationService: NotificationService, private usersService: UserService, private channelFollowingService: ChannelFollowingService, + private rateLimiterService: RateLimiterService, + private roleService: RoleService, + private loggerService: LoggerService, ) { } @bindThis + private async rateLimitThis( + user: MiLocalUser | null | undefined, + requestIp: string | undefined, + limit: IEndpointMeta['limit'] & { key: NonNullable<string> }, + ) : Promise<boolean> { + let limitActor: string; + if (user) { + limitActor = user.id; + } else { + limitActor = getIpHash(requestIp || 'wtf'); + } + + const factor = user ? (await this.roleService.getUserPolicies(user.id)).rateLimitFactor : 1; + + if (factor <= 0) return false; + + // Rate limit + return await this.rateLimiterService.limit(limit, limitActor, factor) + .then(() => { return false; }) + .catch(err => { return true; }); + } + + @bindThis public attach(server: http.Server): void { this.#wss = new WebSocket.WebSocketServer({ noServer: true, @@ -57,6 +91,21 @@ export class StreamingApiServerService { return; } + // ServerServices sets `trustProxy: true`, which inside + // fastify/request.js ends up calling `proxyAddr` in this way, + // so we do the same + const requestIp = proxyAddr(request, () => { return true; } ); + + if (await this.rateLimitThis(null, requestIp, { + key: 'wsconnect', + duration: ms('5min'), + max: 32, + })) { + socket.write('HTTP/1.1 429 Rate Limit Exceeded\r\n\r\n'); + socket.destroy(); + return; + } + const q = new URL(request.url, `http://${request.headers.host}`).searchParams; let user: MiLocalUser | null = null; @@ -94,13 +143,27 @@ export class StreamingApiServerService { return; } + const rateLimiter = () => { + // rather high limit, because when catching up at the top of a + // timeline, the frontend may render many many notes, each of + // which causes a message via `useNoteCapture` to ask for + // realtime updates of that note + return this.rateLimitThis(user, requestIp, { + key: 'wsmessage', + duration: ms('2sec'), + max: 4096, + }); + }; + const stream = new MainStreamConnection( this.channelsService, this.noteReadService, this.notificationService, this.cacheService, this.channelFollowingService, - user, app, + this.loggerService, + user, app, requestIp, + rateLimiter, ); await stream.init(); |