NodeJS - How to get remote IP Address - node.js

For my project, I need to fetch the remote IP address of another NodeJS Server.
In fact, in many countries such as Belgium, the IP address of a computer provided by the telecom is not a fixed IP and can change every xx hours.
I need to know how I can get the Internet IP address of a remote NodeJs computer in real-time.
Example: an internet provider change the IP of a nodeJS computer: I want to know in "relative" real-time the new IP.
Thanks to much,
visualight

If the remote server you want to communicate with switches its ip address and doesn't tell you (as in, contacts your server in some way), you are basically out of luck.
If there is a domain pointing to the other machine, you could use this snippet (which I shamelessly copied from the official docs) to force a DNS resolve:
var dns = require('dns');
dns.resolve4('www.google.com', function (err, addresses) {
if (err) throw err;
console.log('addresses: ' + JSON.stringify(addresses));
addresses.forEach(function (a) {
dns.reverse(a, function (err, domains) {
if (err) {
throw err;
}
console.log('reverse for ' + a + ': ' + JSON.stringify(domains));
});
});
});
UPDATE: There is module on npm that allows you to get the public ip address of a machine. Its very simple, as it just does a request against curlmyip.com and returns the result. You could set up some polling interval using setInterval and send it to the other nodes whenever you detect a change.

Related

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

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"];
}

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.

Can't connect to Node web-server hosted on Amazon EC2

I'm running a node app on an Amazon EC2. The app includes a simple web server intended to serve the index page, but it doesn't work.
Here's the server code:
var http = require('http'),
fs = require('fs'),
io = require('socket.io'),
index;
fs.readFile('client.html', function(err, data){
if (err){
throw err;
}
index = data;
});
var server = http.createServer(function(request, response){
response.writeHeader(200,{"Content-Type": "text/html"});
response.write(index);
response.end();
}).listen(1223);
The EC2 is assigned the public IP address 54.187.31.42. I run the app, open my browser, connect to 54.187.31.42:1223 expecting to be served a web page and get nothing.
What is wrong with my server code?
I used the snippet found in this answer to check the IP of the EC2 running the app, and oddly get 172.31.3.67 - why is the returned address different from the one assigned to the machine by Amazon?
Consequently, trying to connect to 172.31.3.67:1223 also fails.
Straight from the Amazon dev controls, if that helps confirm it isn't an issue of the server IP being wrong or something.
The code looks fine, try connecting with the public IP/public DNS that you see in the AWS console.
Try the following and your application would work:
Open the port (in your case 1223) in the security groups of your instance.
stop the firewall on your machine (i.e. iptables) and now access your server using public ip or public DNS.
If you can now acceess your machine that means something in the iptables is filtering your traffic. You can modify the iptables rules accordingly.
In security group, add a rule with the type "Custom TCP Rule" on the used port (e.g. port: 3000 or 1223 for this case). It works for me.

How do I check connectivity with a machine on my local network using Node.js?

I am able to poll a website using Node.js but how do I check with some local machine.Just want to check if some "xyz" machine is active on the network.
I am not sure exactly what you wanna ask though
there is no difference between local servers and global servers.it's a only matter of whether you access to the server from inside of the LAN or outside of it.
if you install HTTP server on the machine you want to check, you can check its status code like usual website's servers.
and there is also modules for ping such as
https://npmjs.org/package/net-ping/
all you have to do is to replace target IP address to private IP address like 192.168..
Ok so I guess you used http to request an address?
If so, if you request a non active machine, you should get an error. To catch this error, simple react at the error event:
http.get("http://idontexistonthenetwork", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Documentation: http://nodejs.org/api/http.html#http_http_get_options_callback
P.S: Hope it answer your question. Don't hesitate to precise if you need something more specific like using websocket, IP discovering or else...

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.

Resources