|
1 | 1 | import type { IncomingMessage, ServerResponse } from "node:http"; |
| 2 | +import type { ResolvedGatewayAuth } from "../server/auth.js"; |
| 3 | +import { authorizeGatewayHttpRequestOrReply } from "../server/auth.js"; |
2 | 4 | import { setSseHeaders, sendJson, sendMethodNotAllowed } from "../http-common.js"; |
3 | 5 | import { readJsonBody } from "../hooks.js"; |
4 | 6 | import { resolveClientIp } from "../net.js"; |
5 | 7 |
|
6 | 8 | type EventsHttpOptions = { |
| 9 | + auth: ResolvedGatewayAuth; |
7 | 10 | trustedProxies?: string[]; |
8 | 11 | allowRealIpFallback?: boolean; |
9 | 12 | }; |
@@ -163,15 +166,30 @@ function checkRateLimit(ip: string): { allowed: boolean; retryAfter?: number } { |
163 | 166 | return { allowed: false, retryAfter: Math.max(1, Math.ceil(retryAfterMs / 1000)) }; |
164 | 167 | } |
165 | 168 | recent.push(now); |
166 | | - rateLimitTimestamps.set(ip, recent); |
| 169 | + if (recent.length === 0) { |
| 170 | + rateLimitTimestamps.delete(ip); |
| 171 | + } else { |
| 172 | + rateLimitTimestamps.set(ip, recent); |
| 173 | + } |
167 | 174 | return { allowed: true }; |
168 | 175 | } |
169 | 176 |
|
170 | 177 | export async function handleEventsHttpRequest( |
171 | 178 | req: IncomingMessage, |
172 | 179 | res: ServerResponse, |
173 | | - opts: EventsHttpOptions = {}, |
| 180 | + opts: EventsHttpOptions, |
174 | 181 | ): Promise<boolean> { |
| 182 | + const requestAuth = await authorizeGatewayHttpRequestOrReply({ |
| 183 | + req, |
| 184 | + res, |
| 185 | + auth: opts.auth, |
| 186 | + trustedProxies: opts.trustedProxies, |
| 187 | + allowRealIpFallback: opts.allowRealIpFallback, |
| 188 | + }); |
| 189 | + if (!requestAuth) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
175 | 193 | const url = new URL(req.url ?? "/", `http://${req.headers.host || "localhost"}`); |
176 | 194 | const pathname = url.pathname; |
177 | 195 |
|
|
0 commit comments