npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zhf.get-client-ip

v1.1.3

Published

no description

Downloads

12

Readme

nodejs 服务端获取客户端(请求来源)的ip

  • 安装
npm i --save zhf.get-client-ip
  • 使用
const getClientIp = require('zhf.get-client-ip');
getClientIp(req); // xxx.xxx.xxx.xxx

如果你在服务器端使用了nginx进行端口转发

  • 首先请配置你的nginx(必须配置)
server {
        listen 80;
        server_name www.xxx.com xxx.com;
        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header x-real-ip $remote_addr;
            proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
            proxy_set_header host $http_host;
        }
}
  • 使用(第二参数'nginx'必须传入)
const getClientIp = require('zhf.get-client-ip');
getClientIp(req, 'nginx'); // xxx.xxx.xxx.xxx

注意1

  • 如果你使用了nginx代理,但是你没有配置nginx的x-real-ip和x-forwarded-for
  • 会存在ip被伪造的漏洞 - 如果我伪造x-real-ip和x-forwarded-for

注意2

  • 如果你使用了nginx代理,但是你函数调用的时候没有传入第二参数'nginx'
  • 会导致获取到的ip一直是127.0.0.1

注意3

  • 如果你没使用nginx代理,但是你函数调用的时候传入了第二参数'nginx'
  • 会存在ip被伪造的漏洞 - 如果我伪造x-real-ip和x-forwarded-for

其他

  • 如果你没使用nginx做代理,你直接req.connection.remoteAddress就可以获取到公网ip,也没必要使用我这个东西
  • 我的这个东西是为了解决服务端使用了nginx做端口转发,导致req.connection.remoteAddress获取到的ip一直是127.0.0.1而存在的