-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add geoip based request blocking
- Loading branch information
1 parent
49f854d
commit 1237bc7
Showing
4 changed files
with
814 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, Logger, HttpStatus, InternalServerErrorException, HttpException } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { HttpService } from '@nestjs/axios'; | ||
|
||
@Injectable() | ||
export class GeoIPInterceptor implements NestInterceptor { | ||
private logger: Logger; | ||
private readonly httpService: HttpService; | ||
|
||
constructor() { | ||
this.logger = new Logger('GeoIPInterceptor'); | ||
this.httpService = new HttpService(); | ||
} | ||
|
||
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> { | ||
const request = context.switchToHttp().getRequest(); | ||
const clientIp = request.headers['x-forwarded-for'] || request.ip; // Get client IP address | ||
|
||
// Call the geolocation service to get the country from the IP | ||
const { country, regionName } = await this.getLocation(clientIp); | ||
|
||
if (country !== 'India') { | ||
this.logger.verbose('Denying request from ip: ' + clientIp + ' country: ' + country) | ||
throw new HttpException('Access Denied', HttpStatus.FORBIDDEN); | ||
} | ||
|
||
this.logger.verbose('Allowed request from ip: ' + clientIp + ' region: ' + regionName) | ||
return next.handle(); | ||
} | ||
|
||
private async getLocation(ip: any): Promise<any> { | ||
try { | ||
const resp = await this.httpService.axiosRef.get(`https://geoip.samagra.io/city/${ip}`) | ||
return { country: resp.data.country, regionName: resp.data.regionName }; | ||
} catch (err) { | ||
this.logger.error('Error occured while reading the geoip database', err); | ||
throw new InternalServerErrorException('Error occured while reading the geoip database'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.