IP address issue when deployed in Server - servlets

I have a Servlet project developed using Tomcat 7.
My projects logic is that whenever a request comes from certain IP it responds to the same IP with a new instance of browser as an response.
When I run and test this application locally it works perfect and by the way my local working IP address will be something like this 10.52.xxx.xxx. So request has 10.52.xxx.xxx and response is given to 10.52.xxx.xxx and all happies
Now things looks perfect and I planned to deploy this project in a Server which has an IP addess of 172.32.xxx.xxx and I have completed deployment of the same.
Now the issue which I am facing is that when I try to make a request to the project in the server (172.32.xxx.xxx) from my network (10.52.xxx.xxx).
In my project I have code like
String ip = request.getRemoteAddr(); //gets request IP address to give a new instance of browser
Now the variable ip contains an IP address of 172.32.xxx.xxx. So my code will try to give a browser instance to 172.32.xxx.xxx which doesn't exist. It should ideally give the browser instance back to 10.52.xxx.xxx as this is from which it had received the request so it has to respond to this IP.
I know both Server and the client request are in different IP network but I would like to know all possible solutions to fix the same so that browser is given back to 10.52.xxx.xxx.
Any help highly appreciated.

Related

Asp.net - Getting IP Address from devices accessing the local server question

I am developing a web app that uses Asp.net. I found some code that I believe could get ip address of devices. The web app will only run on a network and the devices that are accessing are on the same network. So in other words, the server is local and devices do not need to be accessed through the internet, only on a local intranet.
The code I found is as follows:
string ipAdd = HttpContext.Current.Request.UserHostAddress;
When debugging, I found that the variable ipAdd is set to '::1'. I believe I am getting that value because I am accessing the webserver through the same device. Will I be able to get the device's local IP with that piece of code, if I had a device access the server from another device, say an iPad?
Yes, that has been my experience as well.
Localhost: http://localhost/GK/4.1/Account/AutoLogin.aspx
IP: ::1
Server: http://iis10/GK/Account/AutoLogin.aspx
IP: 192.168.1.70 (my PC's IP. running the app on my IIS 10 VM).
This article by Phil Haack is a little dated; but a good read (re-reading your question, your app will be hosted as an intranet, you might not run into this gotcha): A Gotcha Identifying the User's IP Address
I've used HttpContext.Current.Request.ServerVariables("REMOTE_ADDR") and reading Phil's article he pointed out:
The UserHostAddress property is simply a wrapper around the
REMOTE_ADDR server variable which can be accessed like so.
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]

Can't access IIS website from local IP

I have a .Net service hosted on IIS. The service was accessible from IP:Port/Service.asmx, until recently when I couldn't access it from IP or System name again but only from Localhost:port/Service.asmx. I have pinged the server and it is replying. I have also confirmed that the local ip is listening on port 80. Really I don't know what is wrong, I have been on this for so long.
That's a hard one. Things I'd try:
Rebooting system.
Set a breakpoint at the earliest part of the request pipeline to verify whether the request is making it into your .Net code
Make a browser request via IP for a static file like an image. Is that served? That lets you see if the server will respond for a request that likely isn't going through Asp.Net.

Host Name is sometimes empty

In one of my applications (ASP.NET/VB.NET), I need to read the Client Machine Name. Based on the Client Machine we trigger a Point of Sale payment device to accept the payment. On each of these systems we have a stand alone software installed which communicates to the bank using HTTP requests. I am using the following .NET code to read the Client Computer Name.
Dim name As String = String.Empty
Dim hostEntry = Dns.GetHostEntry(HttpContext.Current.Request.UserHostAddress)
If hostEntry.HostName.Contains(".") Then
name = hostEntry.HostName.Substring(0, hostEntry.HostName.IndexOf("."))
Else
name = hostEntry.HostName.Trim
End If
In the development environment, all our systems are in a domain ("xyz.com") and we don't have any issues. In the customer location they don't have a domain name setup. My above logic works well in some of the systems in the client environment and is able to make payments but in most of the systems our logic fails and is not able to read the host name. Any help will be appreciated.
Your question doesn't have the specifics required to answer your question. There are many questions that need to be answered about both environments to give a correct answer. Since I can't ask questions, I will make some assumptions which might apply to future readers of this post and be able to help them out.
I would ask a question but my profile was forked for some unknown reason and I don't have the required reputation to ask a question. That being said I will run through the list of issues I can identify off the bat and suggest solutions for the issue and hopefully one will lead you to a solution.
So...
1) You state you need to read the client machine name. However, if your application isn't running on an internal LAN (aka an intranet) you can't read client machine names period. So this could be your first problem.
2) Combining point 1 and given that you are reading the IP Address from UserHostAddress of the client to look up a DNS host name and when the host look up succeeds you are taking the first part of the name up until the first "." it should be safe to assume that this an intranet application running on a LAN in both your development environment and at the client environment. With that assumption and given the statement that all machines are given an domain of xyz.com it can be assumed that DNS in your development environment is being dynamically updated from presumably through Active Directory (AD). In such case, whenever a client machine on development network requests an IP address, presumably through AD, the DHCP server integrated with AD issues the new IP Address. When it does and the DHCP offer is acknowledged and accepted by the client AD updates DNS (which on a windows network is also AD integrated) by adding a host entry with the computer name of the client machine pointing to the IP Address. Additionally a DNS pointer, depending on configuration, can be added to AD's DNS which allows an IP Address lookup to resolve to the record (which in this case would be the Client's machine name). So with your development environment (presumably running on Windows Active Directory Domain) everything works. Addi tonally, by default the top level domain name (XYZ.COM) gets appended to the clients computer name in initial DNS requests from the client.
3) Your client is not running a domain which leaves further questions. Are they running windows? If they are running windows is it as a non-Ad environment, for example a work group. First assumption would be they are not AD integrated or otherwise you most likely wouldn't be having this problem although I can think of a few rare case scenarios where they might. However, odds are the relevant questions are What DNS server are they running and what DHCP sever are they running? Your application is trying to use a client IP Address on their network and the host name lookup based on their IP is failing so it tells me in their environment for one reason or another you can't get a host name from the IP Address of the client. Mind you if they could be on AD and configured entirely correctly their DNS server is just overwhelmed and not responding within 2 seconds causing the name lookup failure but that is the rate case. With more information I could help more.
3) Assuming in 2 that they are not on AD, do you have the ability manually code host names on the computer your application is running? For example, lets say yourapp.exe runs client-server-01 and clients connect to it. Then on client-server-01 you could add static DNS entries in the host file for each PC on the client network that you expect to connect. On the other hand if your application is running locally on the client PCs you could pass the machine name as a header in the web request and then read it from the Request.Headers variable on the server.
4) Again, making another assumption the clients are web based and your application in the client environment is being hosted on the server... Is the server on a DMZ outside the client environment? If so the client environment may likely be configured, per best practices, that the server host your web app is in a DMZ and DNS requests to the box are forwarded to the client's ISP and not back into their network that has the DNS server capable of resolving an internal IP to a client machine name. If this is the case you need to send the client machine name as a variable from your client or code local IPs to host names in the servers host file (assuming the internal network isn't behind NAT and exposes the real client machine's IP) or request that the DMZ'd server can access the internal DNS and configure the access accordingly.
....
The list really goes on and on but I think I highlighted the problems for 99% of the situations and provided answers to their various solutions.
You can try to take it from X-Forwarded-For header
The X-Forwarded-For (XFF) HTTP header field is a common method for
identifying the originating IP address of a client connecting to a web
server through an HTTP proxy or load balancer.
This is what X-Forwarded-For should return:
X-Forwarded-For: client, proxy1, proxy2
Here some example code:
string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ;
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Split(',');
ip = ipRange[0];
}
else
{
ip = Request.ServerVariables["REMOTE_ADDR"];
}
There was an issue with Firewall setup on the client machine.Due to that our .NET code was failing. After adding an exception to all the incoming requests from xyz.com. My code is working without any issues.
Thank you guys #Alexander Higgins, #halfer for the help.

How to get client IP in 3-tier architecture?

I am currently working on module which has the following architecture:
When a client makes a request it is forwarded to a node server which has a ELB in front of it. The node server gets the data from backend .NET code hosted in another server with a different call.
Is it possible to access the client IP in the backend code written in .NET considering this architecture?
you can get ip in node
In your request object there is a property called connection, which is a net.Socket object. The net.Socket object has a property remoteAddress but it may be possible ip server is behind proxy server so try the below code to get IP
request.headers['x-forwarded-for']
Check the below Stack Link for more answers
How to determine a user's IP address in node

use local machine IP address to test website on local machine

Hi i am developing a website and want to test it on local machine
the flow is like this:
A -- my website
B -- third-party website
From A a request is sent to B with a url eg: http://mywebsite/abc.aspx which is a url on which B reports success or error.
so i need to define the url to report to
for this i need to give a absolute url or path.
thats why i cannot test it on local machine and have to upload it and test it.
but this doesn't allows me to track down errors and debug it.(through break points.)
so i am trying to get the local machine address like "http://123.456.7.8/abc.aspx"
but doesn't seems to work.
my pc is running on windows xp and is connected to router and my pc is in a network and i think that's why gets a dynamic IP.
have also tried public ip address but no luck.
any work around or help for this.
I believe you're trying to get an external website to contact your local PC. Your local PC is behind a router and getting a private IP from your local router.
You'll need to supply the external website with an address that has the external IP of your router and configure your router to forward incoming requests to yuor PC.
i.e. to forward port 80 on the router to port 80 on your XP machine.
Thus when the external website, tries the contact http://1.2.3.4/abc.aspx it will talk to your router, that will then pass ("forward") the request onto your PC at e.g. http://192.168.0.1/abc.aspx.
I believe You can try to use your Machine name.
You can set up a local DNS server or simply utilize hosts file, to cheat your web browsers. For example, register test.com to your machine's IP address, and then you can use http://test.com to access IIS default web site (or your web site on IIS if you configure it for the default URL).
http://en.wikipedia.org/wiki/Hosts_%28file%29
This is a typical approach to test out web application locally.
try hosting your website on IIS and then give fix IP to your system and try to access it with your IP. Another thing is get your global IP (you can check it from Whatismyip.com ) and set it in your binding in IIS for getting request to it from across the internet.

Resources