From a3d89d0d2eb12da718c4fd3f88bc26bd5e95925d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Dan?= Date: Mon, 18 Sep 2023 12:59:24 +0200 Subject: [PATCH] fix: use request headers for controller throlling --- .../backend/src/faucet/faucet.controller.ts | 4 +++- .../backend/src/faucet/throttler.guard.ts | 11 ++++++++++ packages/frontend/src/App.tsx | 20 +++++-------------- 3 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 packages/backend/src/faucet/throttler.guard.ts diff --git a/packages/backend/src/faucet/faucet.controller.ts b/packages/backend/src/faucet/faucet.controller.ts index 2291b15..a167e8a 100644 --- a/packages/backend/src/faucet/faucet.controller.ts +++ b/packages/backend/src/faucet/faucet.controller.ts @@ -1,13 +1,15 @@ -import { Body, Controller, Headers, Post } from '@nestjs/common' +import { Body, Controller, Headers, Post, UseGuards } from '@nestjs/common' import { GetSubnetAssetsDto } from './faucet.dto' import { FaucetService } from './faucet.service' +import { ThrottlerBehindProxyGuard } from './throttler.guard' @Controller('faucet') export class FaucetController { constructor(private faucetService: FaucetService) {} @Post('getSubnetAssets') + @UseGuards(ThrottlerBehindProxyGuard) async getSubnetAssets( @Body() getSubnetAssetsDto: GetSubnetAssetsDto, @Headers('traceparent') traceparent: string diff --git a/packages/backend/src/faucet/throttler.guard.ts b/packages/backend/src/faucet/throttler.guard.ts new file mode 100644 index 0000000..a6ebbd7 --- /dev/null +++ b/packages/backend/src/faucet/throttler.guard.ts @@ -0,0 +1,11 @@ +import { ThrottlerGuard } from '@nestjs/throttler' +import { Injectable } from '@nestjs/common' + +@Injectable() +export class ThrottlerBehindProxyGuard extends ThrottlerGuard { + protected getTracker(req: Record) { + console.log('IPs from request headers', req.ips) + console.log('IP from request', req.ip) + return req.ips.length ? req.ips[0] : req.ip + } +} diff --git a/packages/frontend/src/App.tsx b/packages/frontend/src/App.tsx index bf82797..3752813 100644 --- a/packages/frontend/src/App.tsx +++ b/packages/frontend/src/App.tsx @@ -12,8 +12,6 @@ import 'antd/dist/reset.css' import useTheme from './hooks/useTheme' import FaucetForm from './components/FaucetForm' import Content from './components/Content' -// import { SERVICE_NAME } from './tracing' -// import { trace } from '@opentelemetry/api' import { TracingContext } from './contexts/tracing' import { SubnetWithId } from './types' import useRegisteredSubnets from './hooks/useRegisteredSubnets' @@ -79,22 +77,14 @@ const App = () => { [registeredSubnets] ) - const transaction = useMemo(() => { - return apm.startTransaction('root', 'app', { managed: true }) - // const tracer = trace.getTracer(SERVICE_NAME) - // return tracer.startSpan('root') - }, []) - - // useEffect(function init() { - // return function onPageClosed() { - // console.log('end root') - // transaction?.end() - // } - // }, []) + const apmTransaction = useMemo( + () => apm.startTransaction('root', 'app', { managed: true }), + [] + ) return ( - +