Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geoip blocking #110

Closed
wants to merge 2 commits into from
Closed

Geoip blocking #110

wants to merge 2 commits into from

Conversation

Himasnhu-AT
Copy link
Contributor

@Himasnhu-AT Himasnhu-AT commented May 26, 2024

Resolves #44

How to Use the Interceptor

We get the user's location using the IP(v4, v6) address of the user and Samagra database using Axios command:

const resp = await this.httpService.axiosRef.get(
        `https://geoip.samagra.io/city/${ip}`,
      );

To use the GeoIPInterceptor globally in your application, you need to add it with an array of allowed countries. If the array is empty, all countries will be permitted.

app.useGlobalInterceptors(new GeoIPInterceptor(['India']));

In this example, the GeoIPInterceptor is configured to allow only requests from India.

@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be @samagra-x/schematics

"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these three need to start with stencil instead of nest for being compatible with stencil-cli.

Need to also install @samagra-x/stencil-cli and @samagra-x/schematics in the project

this.allowedCountries.length > 0 &&
!this.allowedCountries.includes(country)
) {
console.log(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be console.error

private async getLocation(ip: any): Promise<any> {
try {
const resp = await this.httpService.axiosRef.get(
`https://geoip.samagra.io/city/${ip}`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we pick this from the environment using something like a configService

next: CallHandler,
): Promise<Observable<any>> {
const request = context.switchToHttp().getRequest();
const clientIp = request.headers['x-forwarded-for'] || request.ip; // Get client IP address

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesnt work for localhost. can we allow for that as well

image

it works when using via a localtunnel.

@techsavvyash
Copy link
Member

closing this since #134 incorporates this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[stencil] geoip blocking interceptor
3 participants