How to get local IP address of user connected through request - Nodejs - node.js

I have not been able to find any answer to this. I am looking for a way to obtain the local IP address of the user connected to my node server. I found the answer to find a way to get the user's public IP address, and was useful. I also need a way to determine which computer is connected, so the local IP as well through, possibly, something like this:
app.post('getip', function(req,res)
{
var localIP = req.headers['something or other'];
}
Thanks,

This should get you the IP in NodeJS if you're behind a proxy like nginx.
app.post('getip', function(req, res)
{
var localIP = req.headers["x-forwarded-for"];
}

Related

How to get mac address of client in node js?

Is it possible to get mac address from the incoming request object?
My application is gonna be used locally on the same subnet and it runs behind a nginx proxy.
You need a tool that accesses the ARP table (a table that contains MAC addresses of IPs in your local network). Something like this arp-lookup package. You can extract the incoming request ip. Then use this IP to extract the MAC address with the arp-lookup.
The code would be something like this:
const{ toMAC } = require('#network-utils/arp-lookup');
// ...
async function handleIncomingRequest( req, res ) {
const ip = req.connection.remoteAddress;
const mac = await toMAC(ip);
// do stomething with the MAC address...
}

How can I get the correct ip address of the visitor in node.js? (I tried node-ip and it doesn't work)

I'm using a node ip taken from here: https://github.com/indutny/node-ip
In my webservice I did a simple thing:
var ip = require('ip');
module.exports = function(app) {
app.get('/gps', function (req, res) {
console.log(ip.address());
}
}
I deployed it to my amazon aws account and now whoever enters the page - I constantly see the same ip address in my console log - 172.31.46.96. I tried to check what is this ip (possible option is that it is related to my amazon aws service?), but who.is does not bring the answer.
How should I change my code to see every visitor's ip address instead?
You're most likely getting an IP of an internal load balancer/proxy and you'll need to configure express to handle that.
This is a good place to start.
Use req.connection.remoteAddress to get the ip of your user.

How to get the correct IP address of a client into a Node socket.io app hosted on Heroku?

I recently hosted my first Node app using Express and socket.io on Heroku and need to find the client's IP address. So far I've tried socket.manager.handshaken[socket.id].address, socket.handshake.address and socket.connection.address , neither of which give the correct address.
App: http://nes-chat.herokuapp.com/ (also contains a link to GitHub repo)
To view IPs of connected users: http://nes-chat.herokuapp.com/users
Anyone know what the problem is?
The client IP address is passed in the X-Forwarded-For HTTP header. I haven't tested, but it looks like socket.io already takes this into account when determining the client IP.
You should also be able to just grab it yourself, here's a guide:
function getClientIp(req) {
var ipAddress;
// Amazon EC2 / Heroku workaround to get real client IP
var forwardedIpsStr = req.header('x-forwarded-for');
if (forwardedIpsStr) {
// 'x-forwarded-for' header may return multiple IP addresses in
// the format: "client IP, proxy 1 IP, proxy 2 IP" so take the
// the first one
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
// Ensure getting client IP address still works in
// development environment
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
You can do it in one line.
function getClientIp(req) {
// The X-Forwarded-For request header helps you identify the IP address of a client when you use HTTP/HTTPS load balancer.
// http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html#x-forwarded-for
// If the value were "client, proxy1, proxy2" you would receive the array ["client", "proxy1", "proxy2"]
// http://expressjs.com/4x/api.html#req.ips
var ip = req.headers['x-forwarded-for'] ? req.headers['x-forwarded-for'].split(',')[0] : req.connection.remoteAddress;
console.log('IP: ', ip);
}
I like to add this to middleware and attach the IP to the request as my own custom object.
The below worked for me.
Var client = require('socket.io').listen(8080).sockets;
client.on('connection',function(socket){
var clientIpAddress= socket.request.socket.remoteAddress;
});

headers.host gets hit by lots of domains that are not mine

I am getting many strange requests that have req.headers.host values that are not my domain.
var mc_domain = "mysubdomain.mydomain.com:8888";
var server = require('http').createServer(function (req, res) {
if (req.headers.host !== my_domain) {
console.log("not the host you are looking for " + req.headers.host);
res.end();
return;
}
});
server.listen("8888");
console output
not the host you are looking for abc.advertising.com
not the host you are looking for parkingaddress1.com
not the host you are looking for gotoinfo.info
...
What is going on, and what can I do to stop/reduce this? Is it just "welcome to the (wild) internet (west)", Or "You need a firewall", or some other foolishness.
Eihter someone's DNS is resolving the other domain name to your IP address or domain is not setup properly. You can use a tool like dig to check the DNS settings at the DNS servers responsible for those domains (you can get them using whois).
You can look at this as problem, or as an opportunity. If I were you, I'd create a landing page for those users and try to sell my product/service.

Get remote IP adress not working on node

I'm trying to get the remote IP address from an incoming connection using express. Already tried the common solutions found in the web:
req.headers['x-forwarded-for'] / result: always undefined
req.socket.remoteAddress / result: always getting the Gateway IP (IE: 192.168.1.1) instead of the external address.
Any clues?
Are you testing this by running the app in a VM on your own machine? If so, you're getting the right answer. The X-Forwarded-For is empty, since there's no proxy, and you're seeing the virtual interface on the host machine which the VM uses to route out from. That's the IP from which the request comes.
Here's some code i used at http://th.issh.it/ip
Maybe it's case sensitive?
exports.iptoy = function(req,res) {
var clientip = req.socket.remoteAddress;
var xffip = req.header('X-Forwarded-For');
var ip = xffip ? xffip : clientip;
if(req.params.format == 'json')
res.send({client:clientip,xforwardedfor:xffip});
else
res.send(ip+"\n");
}

Resources