reogranized app and added more headers

This commit is contained in:
Bojan Kucera 2025-06-05 23:10:18 -04:00
parent 1048ab5c20
commit 223f426a58
10 changed files with 925 additions and 218 deletions

35
src/middleware.ts Normal file
View file

@ -0,0 +1,35 @@
/**
* Middleware functions for the proxy detection API
*/
import { FastifyRequest, FastifyReply } from 'fastify';
import { config } from './config';
/**
* API Key authentication middleware
* Skips authentication for health check and debug endpoints
*/
export async function authMiddleware(request: FastifyRequest, reply: FastifyReply) {
// Skip auth for health check and debug endpoints
if (request.url === '/health' || request.url === '/ip-debug') {
return;
}
const apiKey = request.headers['x-api-key'] || (request.query as Record<string, any>)?.api_key;
if (!apiKey) {
reply.code(401).send({
error: 'API key required',
message: 'Please provide API key in X-API-Key header or api_key query parameter'
});
return;
}
if (apiKey !== config.api.key) {
reply.code(403).send({
error: 'Invalid API key',
message: 'The provided API key is not valid'
});
return;
}
}