I am deplyong a web application in the Internet and I am checking whether the user's login is valid by checking its client IP address (e.g. 192.168.2.XXX). Actually, I have found a working code (below). This code was completely working before, but after some time, its output is not the same anymore. Now, this code only gives me 1 IP address which is the server's IP address. What is wrong with my code? How can I get the client's static IP address rather than the server's IP address? I have also tried other solutions such as getRemoteAddr() and request.getHeader("PROXY-CLIENT-IP") but it is not working.
Java:
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if(ipAddress == null)
ipAddress = InetAddress.getLocalHost().getHostAddress();
Your are mixing two levels of abstractions and that is rarely a good thing.
The header X-FORWARDED_FOR is inserted by a load balancer or proxy. If the client reaches the server directly, then this header isn't present and you are executing this code InetAddress.getLocalHost().getHostAddress();. Which does exactly what you say: It is retrieving the IP address of the host where this piece of code is running, i.e. the web server.
See also here: Getting the client IP address: REMOTE_ADDR, HTTP_X_FORWARDED_FOR, what else could be useful?
Related
I have found that resolution of a hostname that does NOT exist always returns an address belonging to "akamaitechnologies.com" (e.g. 23.221.222.250). It does work correctly for hosts that do exist.
Code:
InetAddress addr = InetAddress.getByName( "NON-EXISTING.com" );
The documentation for InetAddress or a Google search provide little help. It is claimed that an UnknownHostException should occur but that does not happen for me.
Why is this happening?
This is not an Android/Java defect. It turns out it was a DNS issue. Namely, my AT&T phone uses AT&T's default DNS server (sbcglobal.net). This P.O.S. server returns a valid IP address even for non-existent domains. After I changed to "dns.google" (8.8.8.8), everything worked as expected.
This DNS spoofing is a Bad Thing because many apps depened on a Unknown-Host-Exception to detect an incorrectly entered domain, e.g. in an email address.
Hey i tried to get the ip of the client with ktor.
I used the method
this.context.request.local.remoteHost
(this.context is an Instance of ApplicationCall)
How can i get the real ip an not something like "********.dip0.t-ipconnect.de"
You can also get a remote host from the request's origin: call.request.origin.remoteHost but it returns IP address in not every case too.
I've created an issue in Ktor's bug tracker to address this problem.
i have got an error in this line:
new ServerSocket(2106, 50, InetAddress.getByName("83.4.200.1"));
Error log:
Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind
83.4.200.1 is my ip, when i put there 127.0.0.1 or 192.168.1.2 with same port, everything is working perfect. I have checked all ports by writing netstat -a -n, but 2106 isnt there.
Thanks a lot for reading this, i hope that u can help me with my problem
Your routers address is 83.4.200.1. It's important to note that this isn't the address that your computer responds to, but rather the internal network address 192.168.1.2. If you want to connect to your program from outside the router, you needs to set up port forwarding for 2106 on the router.
1. If you want to access this Server with IP: "83.4.200.1" through Internet, then it must
be your static ip, rather than an dynamic one.
2. Try to run this code with a private ip address or public ip address which is assigned to your pc in LAN (ie. Without internet..JUST WITH WIRELESS CONNECTION).
3. Private ip or Public ip has No meaning until and unless you are on INTERNET.. TILL THEN YOU CAN USE BOTH, AS ITS LAN.
4. Private ip ranges
Class A : 10.0.0.0 - 10.0.0.255
Class B : 172.16.0.0 - 172.31.255.255
Class C : 192.168.0.0 - 192.168.255.255
5. Public is given by your service provider, which will be anyone OUT of the private ip range. If your ip is not static, there is hardly or none of your chances to access the server over internet, there are sites that gives static ip out of your dynamic ips.
83.4.200.1 is my ip
It is the IP address of your router.
It isn't an IP address of the host you are running your code in, so you can't bind to it. You need to bind to a local address of that host, and arrange port forwarding from the router to your host. Most usually the bind-address is best omitted altogether, just specifying a port, in which case the socket will listen on all local IP addresses.
This question already has answers here:
How do I get the remote address of a client in servlet?
(11 answers)
Closed 7 years ago.
Some where I have this in some generic class.
public static String getRequestIp (HttpServletRequest request){
String ipaddr = request.getHeader("X-FORWARDED-FOR");
if (ipaddr == null)ipaddr = request.getRemoteAddr();
return ipaddr;
}
For every request i call that method and in a certain moment i insert a record in a mysql database.
In most cases it works normally and i can see a record for every request with a valid ip address in the right field. But sometimes where the IP should be there is something like this. "unknown, 93.186.30.120" or "10.0.1.169, 186.38.84.3"
Apache is at the front listening at port 80 and used as proxy to Tomcat that listens at port 8081.
My router config would not allow to pass any conection that come by any port other than 80.
Any Help?
Thanks in advance.
The format for X-FORWARDED-FOR HTTP header is:
X-Forwarded-For: client, proxy1, proxy2, ...
Thus
unknown, 93.186.30.120
means request coming from proxy at 93.186.30.120, originating from unknown local address; and
10.0.1.169, 186.38.84.3
similarly means, request from 186.38.84.3 proxy, coming from local ip 10.0.1.169
The "unknown" X-Forwarded-For entry may have been inserted by a proxy that is configured not to insert the originating client IP address into the field.
The Squid configuration directive forwarded_for", for instance, has various options and If set to "on", will append the client IP address. If set to "off", it will appear as "X-Forwarded-For: unknown"
I'm programming a simple web browser depending on NanoHTTPD project and it's required to get number of visitors using the IP address.
Are there any ways to get the client IP using NanoHTTPD?
In NanoHTTPD.java, find the private class HTTPSession object.
Inside this is public void run(). Find the following line and add the second line after it.
decodeHeader(hin, pre, parms, header);
header.put("IPAddress", mySocket.getInetAddress().getHostAddress());
Now inside your serve function, you can just reference the IPAddress header to get the client's ip address.
I know the answer is probably too late to help you, but hopefully it'll help others searching for the same thing.
I found in the latest master branch, you can get the client ip address by header "http-client-ip" in the IHTTPSession session object.