Know local ip server from client in node.js - javascript

I have a Node.js server, the client is a mobile app. I want to know which would be the best way to know the local ip of the server. I mean...
Server runs in localhost:8080 and the client has to connect to the server like this 192.168.0.107:8080 (pc's local ip).
I want to guest this IP, 192.168.0.107 without writing it in client code because it could change.

My guess is that you have a server with a dynamic ip, you want to connect your client to this IP without hardcoding that to the client's code. I'd say have many options:
Set up DNS, so that you connect to a known hostname and then translate that into an IP with your DNS server.
Implement/use a discovery service using your network's broadcast address. (A server could regularly broadcast information to a broadcast IP, then your client will receive this information and use it to show the user a list of available servers)
Let the client have an input box where he can enter the IP address (this is the easiest way, just have a guy input the ip address, maybe save the address for future autocomplete etc)

IMHO this is not possible in a really reliable way. There are several reasons for this, including:
A server may have more than one IP, e.g. multiple network adapaters.
A server has an IPv4 and an IPv6 address.
…
So either you need to narrow down what you want to achieve or you have to live with some kind of uncertainness.
The easiest way then would be to do a DNS lookup, using the hostname, to get the well-known IP addresses, and then select one of them (however you want to do this, that's up to you).
PS: I'm not perfectly sure that I really got your question. If I understood you correctly, you want to find out the IP address of the client on the client, because you need to send it to the server, right?

If you are looking for a simple answer, there is a npm module that does this for you, it's really small so just install it and use the instructions to get the ip of the client:
https://www.npmjs.com/package/request-ip
Getting Started:
var requestIp = require('request-ip');
// inside middleware handler
var ipMiddleware = function(req, res, next) {
var clientIp = requestIp.getClientIp(req); // on localhost > 127.0.0.1
next();
};

Depending upon the network topology details of your actual implementation (which aren't included in your question), you have at least three different options:
Server Directly on Public Interent
On a public network (e.g the open internet), DNS is the normal answer to this type of question. You follow the following steps:
Make sure you have a public IP address. As you may or may not know 192.168.0.107 is not a public IP address. It is a local internet address and cannot be routed on the open, public internet.
With a DNS service, create a DNS record that establishes a link between a host name such as "google.com" and your public IP address.
Code the client to connect to your "name", not your IP address.
Server and Mobile App Only on Private Network
If your mobile app runs only on your private network (perhaps via WiFi), then you can configure your own DNS server on your private network that will do essentially the same thing as the steps above, but will work for a private address like 192.168.0.107 on your private network.
Server Lives on Home Network, Want to Access From Internet
If your server lives in your private network (and thus only has a private IP address), but you want to be able to reach it from the public network, then you need to configure some sort of routing or port forwarding at the boundary between your private network and your public network. In a home router situation, this would involve the following steps:
Get the public IP address of your home network connection.
Set up a public DNS entry on the open internet for that IP address.
If your public IP address is not a static IP address (e.g. it could change over time), then you may be able to use one of the "dynamic DNS" services that are built for this type of situation.
At your home router, configure port forwarding so that incoming requests from the internet on the port your web server is running on (usually port 80) or forwarded to the private IP address of your actual node.js server on your home network.

I got your question as this,
Your client have a private & dynamic IP because it is behind some NAT( i.e firewall),
mobile app can request some STUN server to get its public IP and Port, then mobile app can send these data to server to identify the client. Similar to peer-to-peer connection establishment.
Read more...

Related

Is there any method so that i can get the client public IP in dns query

I have configured the PowerDNS server with a recursor server, I want to put some domain filtering process on the basis of client IP.
hence I want
Anytime a client making a request, so its dns-query must be with client's public IP, using that I can put a filter role for the client.
as with all other services openDNS, freeDNS are all working
Thanks in Advance.
I have used the lua script to get the client IP using the function dq.remoteaddr:toString() but using this I am getting only my dnsdist (load balancer) server IP address, not client IP address because I have configured DOH and DOT in the dnsdist server, hence all the queries are coming through dnsdist server.

How to point my node.js app to a non existing domain

I have an app running on a server where I access using the servers ip. But now I need to have a subdomain configured in order to make an external API work but I have no clue of how achieve it.
Now I access to my server using IP -> XX.XXX.XXX.XXX:3000
And I need to change it for something like -> myapp.companyname.com
You don’t, because the Internet doesn’t work that way.
You create the subdomain by configuring the DNS server for the domain to point that subdomain at the IP address.
If you want to change the port number (from the default of 80 to 3000) without putting it in the URL itself, then you need to change the Node.js program or put a proxy server in front of it.
This is indeed a notorious problem with distributed web development. Presumably your API requires you to register your own hostname in some sort of allow-list, and then checks incoming requests for matches to that allow-list. (Why? Cybercreeps.)
You need to find out the IP address of your development machine (not 127.0.0.1, that's the loopback IP and every machine has it). It's OK if your development machine's address is on a private network, like '192.168.0.20for example. Give the commandifconfigand look for the address (it'sipconfig` on Windows).
You then need to put that IP address into a DNS server.
Here's a free way to do that.
Create a FreeDNS account by visiting https://freedns.afraid.org/
Click on Subdomains.
Click the Add link.
Create a subdomain hostname under one of FreeDNS's public domains. Maybe javier.ortega.mooo.com is a good choice
Put your machine's IP address into it.
Then, use https://javier.ortega.mooo.com:3000 to hit your development machine's nodejs app.
You can pay FreeDNS to register your own domain name and use that if you prefer.
The solution I needed was modifying the file hosts from -> system32/divers/etc/hosts and adding there the configuration relative to the ip - domain relation I was lookin for.

Citrix retrieve client IP address from web application

Situation
We have:
A JSF web application that runs on an Application Server.
A Java Server Socket application for smart card access that runs on the client computer.
The web application needs to read a smart card in a smart card reader on the client computer.
For this the web application makes a connection to the Server Socket application on the client computer with the IP address of the client computer.
This IP address is determined through ServletRequest object of our web application call.
Problem
When this setup runs in a RD environment (eg Citrix), we receive the server IP address instead of the client computer IP address.
Question
How can we retrieve the client IP address?
Perhaps by using WebSockets?
Comment
I am not sure if these are correct, but I have looked around and some possible solutions found are:
using a Virtual IP Policy (I understand that this would make the localhost call revert to the correct IP address?)
Adding a X-Forwarded-For header to the request
These would require modification/configuration of the RD environment and we don't manage this level.
Of course, if there is no other way, I can try to request this.
You should try X-Forwarded-For. Worked for us. Somehow different scenario but it worked for us.

Can i get clients DNS IP address using JS? [duplicate]

Detecting visitor IP is easy. But how about detecting DNS server ips of a visitor ?
I found this PHP function, however it finds only domain names' DNS.
dns_get_record("website.com", DNS_ANY);
Is it possible to detect visitor DNS server ?
Yes, you can, like detecting page resolution of visitors.
You need own DNS server and force user to resolve unique dns name. If user tried to resolve it then they will leaks to your DNS server own DNS server address. Next to DNS server have to share information who asked about the unique dns name to your web apps.
It's not easy, but it can be done. There's a demonstration of the approach suggested in a separate answer by Adam Dobrawy at http://ipleak.net/
To add a bit of detail, the way you can implement something like this is:
Part 1 - Set up your own DNS server on myspecialdomain.com
This DNS server needs to be custom written to log and store the incoming request and the source IP address. This storage only needs to be for a short period of time, so something like memcache might work nicely.
The DNS response should be an NXDOMAIN.
Part 2 - Your client-side code
In your Javscript make and store a large random number. Make the browser lookup .myspecialdomain.com. Load this via a JS img tag with an error handler.
In that error handler, now make a query to your server side code passing the random number.
Part 3 - Your web application (server side)
You need to implement some server side logic that takes the random string, looks it up in the datastore, and retrieves the IP address of the DNS server.
Note the IP address here will be the IP Unicast address of the particular server, it won't be an IP Anycast address like 8.8.8.8.
Here you can use GeoIP or Whois databases to determine the owner of that IP address (OpenDNS, Google etc). You can then generate a response to send to the client logic.
DNS resolution is not part of the request itself which means there is no way for the receiver of the request to know which DNS was used by the client (browser).
The DNS request happens first, as it is required to resolve the hostname to an IP address. Once this is complete, then a separate request is performed to the address in question.
The answer is NO. All the server got is a TCP connection to the visitor, that is, an [IP, Port] pair. DNS resolution depends on visitor's local configuration and can be done by a proxy.

Connect to Express server using it's IP address

Wanted to know if it's possible to connect to an express server using that servers UP address. If it is how would you do it?(some code examples would be very appreciated)
Same as with any http server. This is not specific to Express at all. It's just browsers and TCP.
From a browser, you would just type:
http://192.168.1.100/somePath
Where you put in the IP address of the server and the desired path for the URL.
If you're connecting programmatically, you'd have to tell us what client environment you're using so we could offer a relevant exampale. All TCP connections are fundamentally connecting via the IP address (that's how TCP connections work).
Some APIs allow you to use the DNS name and then the first step under the covers of the API is to look up the IP address that corresponds to that DNS name and then connect to the IP address.
Wanted to know if it's possible to connect to an express server using that servers IP address.
Yes, that's actually the only way to connect to any server that uses TCP as the transport. All TCP connections are made from one IP address to another IP address.
Just use the IP address in place of where you would put localhost or the domain.
For example:
https://www.google.com/ -> https://216.58.217.164/
https://localhost/hello.html -> https://127.0.0.1/hello.html
https://localhost:8080/ -> https://127.0.0.1:8080/
I can add a code sample if you add your current code to the question or say what you are using to make the connection.

Categories

Resources