Giter Site home page Giter Site logo

p0vidl0 / nestjs-real-ip Goto Github PK

View Code? Open in Web Editor NEW
44.0 3.0 2.0 710 KB

A concise decorator for retrieving an IP address from http request with Nest.js controller method.

License: MIT License

TypeScript 100.00%
nestjs nest typescript real-ip ip ip-address

nestjs-real-ip's Introduction

Nest.js real ip decorator

A concise decorator for retrieving an IP address from http request with Nest.js controller method.

NPM package link

Installation

# Install the upstream version for projects based on NestJS v8 and above
npm install nestjs-real-ip

# Or use the version 1.0.3 for projects based on NestJS v7
npm install [email protected]

Example controller

import { RealIP } from 'nestjs-real-ip';

@Controller('/')
class TestController {
  @Get('my-ip')
  get(@RealIP() ip: string): string {
    return ip;
  }
}

Under the hood

Based on the tiny module @supercharge/request-ip. It supports a wide list of request headers and properties to get working in almost any environment. See the request-ip module description for details.

Also, see the decorator's tests.

License

The code is under MIT license. See the LICENSE file for details.

Development

# Update code, commit and push with git
npm version [ major | minor | patch ]
npm publish

nestjs-real-ip's People

Contributors

awesometic avatar kffl avatar p0vidl0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kffl awesometic

nestjs-real-ip's Issues

Access through proxy or load balancer

Hello, Thank you for creating an impressive library!

Is it possible to identify the IP correctly even when the client accesses through a proxy or load balancer?

const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;

RealIp returns ::1

Hi!
I have trouble with RealIp decorator it returns ::1.
What can it be?

How to use in NestInterceptor?

Working fine on controller,
But, I am using LoggingInterceptor to log requests, how to use @RealIP() in NestInterceptor?

constructor(
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const startAt = Date.now();
    const req = context.switchToHttp().getRequest<Request>();

   // How to get real ip address from request here to log

});

Fastify deprecation warning [FSTDEP005]

When using the Fastify adapter for NestJS and running the app without a reverse proxy (i.e. locally for development), upon each request with the @RealIP decorator, the following warning is printed:

[FSTDEP005] FastifyDeprecation: You are accessing the deprecated "request.connection" property. Use "request.socket" instead.

It seems that the underlying request-ip library attempts to read the remote address form request.connection before trying to do so from request.socket. Given the fact that the request-ip library maintenance status appears to be rather stale, I propose swapping that dependency for https://github.com/supercharge/request-ip, which has already addressed that issue: supercharge/request-ip#5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.