How to determine a user's IP address in node

后端 未结 19 870
天命终不由人
天命终不由人 2020-11-22 12:46

How can I determine the IP address of a given request from within a controller? For example (in express):

app.post(\'/get/ip/address\', function (req, res) {         


        
相关标签:
19条回答
  • 2020-11-22 13:02

    Had the same problem...im also new at javascript but i solved this with req.connection.remoteAddress; that gave me th IP address (but in ipv6 format ::ffff.192.168.0.101 ) and then .slice to remove the 7 first digits.

    var ip = req.connection.remoteAddress;
    
    if (ip.length < 15) 
    {   
       ip = ip;
    }
    else
    {
       var nyIP = ip.slice(7);
       ip = nyIP;
    }
    
    0 讨论(0)
  • 2020-11-22 13:03

    Warning:

    Don't just blindly use this for important rate-limiting:

    let ip = request.headers['x-forwarded-for'].split(',')[0];
    

    It's very easy to spoof:

    curl --header "X-Forwarded-For: 1.2.3.4" "https://example.com"
    

    In that case ther user's real IP address will be:

    let ip = request.headers['x-forwarded-for'].split(',')[1];
    

    I'm surprised that no other answers have mentioned this.

    0 讨论(0)
  • 2020-11-22 13:04

    In your request object there is a property called connection, which is a net.Socket object. The net.Socket object has a property remoteAddress, therefore you should be able to get the IP with this call:

    request.connection.remoteAddress
    

    See documentation for http and net

    EDIT

    As @juand points out in the comments, the correct method to get the remote IP, if the server is behind a proxy, is request.headers['x-forwarded-for']

    0 讨论(0)
  • 2020-11-22 13:05

    There are two ways to get the ip address :

    1. let ip = req.ip

    2. let ip = req.connection.remoteAddress;

    But there is a problem with above approaches.

    If you are running your app behind Nginx or any proxy, every single IP addresses will be 127.0.0.1.

    So, the best solution to get the ip address of user is :-

    let ip = req.header('x-forwarded-for') || req.connection.remoteAddress;
    
    0 讨论(0)
  • 2020-11-22 13:05

    function getCallerIP(request) {
        var ip = request.headers['x-forwarded-for'] ||
            request.connection.remoteAddress ||
            request.socket.remoteAddress ||
            request.connection.socket.remoteAddress;
        ip = ip.split(',')[0];
        ip = ip.split(':').slice(-1); //in case the ip returned in a format: "::ffff:146.xxx.xxx.xxx"
        return ip;
    }

    0 讨论(0)
  • 2020-11-22 13:07

    You can stay DRY and just use node-ipware that supports both IPv4 and IPv6.

    Install:

    npm install ipware
    

    In your app.js or middleware:

    var getIP = require('ipware')().get_ip;
    app.use(function(req, res, next) {
        var ipInfo = getIP(req);
        console.log(ipInfo);
        // { clientIp: '127.0.0.1', clientIpRoutable: false }
        next();
    });
    

    It will make the best attempt to get the user's IP address or returns 127.0.0.1 to indicate that it could not determine the user's IP address. Take a look at the README file for advanced options.

    0 讨论(0)
提交回复
热议问题