Why HttpWebRequest.GetResponse() fails after connecting to a VPN? - c#

I am trying to control a computer in another room over a local LAN and not the internet which is an important distinction in this case. This is done by sending HttpWebRequests with query strings that command the remote computer to do certain things. One of commands is to tell the remote computer to connect to a VPN using Cisco's AnyConnect client. When it connects, it should send back a reply to the controlling computer that it successfully connected.
For some reason, the reply which is a HttpWebRequest will time out. The strange thing is that I can send HttpWebRequests over the internet with no problem, so it seems to be only a local LAN issue. To get around this issue, I have created a separate console app that is called just to send the replys. I am not sure what is different about it, but it works.
Here is the code from the console app and those are two same lines used in the main program that fail to work.
static void Main(string[] args)
{
// args[0] contains query string ie ?reply=VPNSuccess
HttpWebRequest httpWebReq = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.1.11:1000" + args[0]));
HttpWebResponse httpWebResp = (HttpWebResponse)httpWebReq.GetResponse();
}

Once the vpn is connected the remote machine likely has a new IP address on a different subnet that cannot route back to you.
If I connect a vpn to my company HQ from my house, once the vpn connects I can no longer ping machines on my local network by default. This can be changed via various settings.

Related

TCPListener behind router(port forwarded) not working for me

If I connect my pc directly to internet everything is ok, however with my router connected the following code get stuck in readline:
`
TcpListener webserver = new TcpListener(IPAddress.Any, 1302);
webserver.Start();
Debug.WriteLine("Started");
TcpClient client = webserver.AcceptTcpClient();
Debug.WriteLine("Client accepted");
sr = new StreamReader(client.GetStream());
sw = new StreamWriter(client.GetStream());
try
{
string req = sr.ReadLine();
Debug.WriteLine("Request: " + req);
if (req != null)
{
//HTTP/1.0 200 OK\n
//+read file to tcpstream
}`
I'm not trying to build a webserver I'm just testing things out with tcp.
If I use http://192.168.1.100:1302/ in a webbrowser or my public IP without the router the webpage loads so I'm guessing it's sg to do with my router or do I have to implement something else?
What I'm seeing is:
readline is stuck(If I refresh the page a few times the GET / HTTP/1.1 gets trough)
If I use read() I see a load of -1
I used wireshark and I'm seeing a lot of greyed out packages and a lot of retransmissions:
wireshark
I'm stuck first I thought I have to implement Upnp or nat punch through, but since some things are getting through I don't think establishing the connection is the problem.
Why are there packet losts? Where should I even start to debug the problem?
UPDATE #1:
The client(chrome webbrowser) is the same computer, but I tried outside computers(with and without behind a router) as well same result.
UPDATE #2: I think I might know what the problem is, I just tried an external webpage tester and there were no problems, so far every device was in the same ISP. I have to try it with vpn or a device that is outside of my ISP network.
I don't know if this is normal or not, but because of packet loss most packets never arrives to my application. This problem is defently caused by my router(and maybe sg. else), but I even tried to connect to my app with other devices in the same ISP same problem. I confirmed this by using vpn and also with another computer using different ISP.

'No connection could be made because the target machine actively refused it'

I am working on a 'Smart Device Project' using .Net Framework 3.5. I am trying to connect to some Java SOAP services on a remote server.
In order to do that, I added 'Web References' to my project.
When I try to call my web service I get a WebException 'Unable to connect to the remote server' with the inner exception being 'No connection could be made because the target machine actively refused it'.
I searched quite a lot on the Web and StackOverflow and found a lot of ASP configuration and 'Unavaliable port' answers, but as I have another application using the exact same Service successfully, I can't get why the new one isn't getting through (It did sometimes through my tests so I suppose my client implementation isn't that bad)
I tried to look if there was some connection issue on the port by using some TcpClient:
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
try
{
client.Connect("myServerName", 8087);
MessageBox.Show("Success");
} catch (Exception ex)
{
MessageBox.Show("Failure");
}
finally
{
client.Close();
}
This connection succeed.
Here is a sample on how I call my WebService:
WSServiceExtended srv = new WSServiceExtended();
srv.Proxy = new System.Net.WebProxy();
ServeurWSI wsi = new ServeurWSI();
srv.Url = "http://myServerName:8087/myServerApp/services/myService";
wsr = srv.login(wsi);
The service is called 'Extended' because I overrided the auto-generated one in order to add Cookie managment since I am using the Compact Framework. Following the sample in this thread:
https://social.msdn.microsoft.com/Forums/en-US/34d88228-0b68-4fda-a8cd-58efe6b47958/no-cookies-sessionstate-in-compact-framework?forum=vssmartdevicesvbcs
EDIT:
I made some new tests with the Web references and got it to work.
When I add the Web Reference, I have to put some Url to the Web Service. When I set it with the actual hostname instead of the 'localhost' everything is fine.
But then, since I set it manually to the real address just before the call, it shouldn't matter
srv.Url = "http://myServerName:8087/myServerApp/services/myService";
EDIT2:
I might have forgotten some specifics about my environnement.
The Web Services are exposed on my computer on some Tomcat Server.
The application I am working on is also developped on this computer (That's why I can add Web References by putting 'localhost' in the address)
The application is then deployed on a distant device (Windows CE) that will make calls the Web Services through WIFI (There, localhost wouldn't work then)
I tried calling the Web services from other computers successfully.
I'm beginning to think that there might be some differential between the called Url and the one that is set, otherwise, how would I have a difference in behaviour such as the one described in the first edit?
EDIT3:
Well..Seems like it's not a network issue but a .Net compact framework (usage?) issue...
The Url property of the Web Service implementation is simply ignored and the one in the Reference.cs is used in place.
If someone had some idea on how I could troubleshot this, I would really appreciate it.
That error means that you reached a server and the server said "no way". So you're either hitting the wrong server or the wrong port.
I find the telnet client is useful for testing stuff like this. From the command line, you can do:
telnet [servername] [port]
So something like:
telnet myServerName 8087
If it goes to a blank screen, then it connected successfully. If it does not connect, it'll tell you.
The telnet client is no longer installed by default in Windows 7+, so you'll have to install it. See here for instructions: https://technet.microsoft.com/en-ca/library/cc771275
If the connection does open, you could paste in an actual HTTP request to see what happens. A simple GET would look something like this:
GET /myServerApp/services/myService HTTP/1.1
Host: myServerName:8087
One reason for this error can be that the service binds to only a certain IP address. It could well be that the service only listens on the IP that is assigned to the host name, but not on the localhost IP (127.0.0.1).
For example:
If the host myServerName has the public IP 192.168.0.1, your service can choose to listen on all IPs assigned to the host (sometimes specifying 0.0.0.0), or it can specifically listen on 192.168.0.1 only. In that case you will not be able to connect through 127.0.0.1, because the service simply doesn't listen on that IP.
You can "use" this inverse of this feature to make a service accessible only to local clients, not on the public IP-Address, by listening on 127.0.0.1 only, but not on the public IP. This is sometimes used on Linux for example to make MySQL only accessible on the host itself.
I was starting to forget this post but I finally found the problem that was messing things up and it has nothing to do with programmation.
I was doing the calls while the device was connected to the computer via the 'Windows Mobile Device Center' allowing to access the device from Windows.
While connected, the host provided is ignored and all calls on the specified port are handled by the connected computer.
Disconnecting the device allows to communicate properly...

SignalR on Home Network

Edit/Summary
After a lot of trial and error, Paul Kearney - pk helped lead me to the answer, although I still don't know why this happens, at least I know how to get it to work.
Quick Summary: Clients can connect to port 8080 on my laptop when I'm directly connected to my network at work. Clients cannot connect to port 8080 when I'm on a home network. To solve this, I created a firewall rule (on my laptop) to allow inbound traffic on 8080. I'd really like to know why this is the case. Does my laptop's Windows Firewall service actually change its settings based on the network I'm connected to?
Note: This all works when I'm on my network at work, but it doesn't work on my home network, or someone else's home network. The host computer (my laptop) is the same at both locations.
I have a web app that uses SignalR. Everything works when I run the web app on the same machine as where the SignalR host is running. When I try to connect from a different machine, I get this error:
> GET http://10.0.0.13:8080/signalr/hubs net::ERR_CONNECTION_TIMED_OUT. Cannot read property 'client' of undefined.
That error comes from my index.html page:
<script src="http://10.0.0.13:8080/signalr/hubs"></script>
From the research that I've done, I know that I shouldn't be using localhost in my SignalR URL. So I used an asterisk. This is in my self-hosted SignalR app:
class Program
{
static void Main(string[] args)
{
string url = "http://*:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class RaceHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
And this is my JavaScript to connect:
var signalRUrl = 'http://10.0.0.13:8080/signalr';
$.connection.hub.url = signalRUrl;
var hub = $.connection.raceHub; // proxy reference to hub
// Create a function that the hub can call to broadcast messages.
hub.client.addMessage = function (name, message) {
receiveSignalRMessage(name, message);
};
$.connection.hub.start(); // Start the connection.
Like I said, this all works locally. But it doesn't work from another machine. I do not have Windows Firewall enabled, so that's not the issue. Any ideas?
HTTPS didn't work. It works when I'm on my network at work, but not at home. The configuration is the same. Must have something to do with my home network.
Also, this is what the page looks like when it's working properly:
And this is what happens when it's not working:
(Full disclosure - #BobHorn typed up this answer based on my comments)
There are different firewall settings on your machine, depending on the type of network you are attached to.
Have you ever noticed when you connect to a new network for the very first time, it asks you to define it as "work, private, public, home, etc"?
Whatever you choose for that network is sticky, and the related firewall policy is applied when you are on that type of network.
To check this, you can either completely turn off your firewall, or make sure inbound port 8080 is open for all firewall profiles, especially "home" networks and/or the "home" firewall profile.
ahh.. so you say your windows firewall is off. Are you sure it's not OFF only for the WORK network profile, but enabled for the HOME network profile ?
To troubleshoot this, I would put wireshark on one of the client computers trying to connect to port 8080 at home, and see how/why it's being blocked or cannot connect.
You can also try to telnet to port 8080 from the client machine at home (if the Telnet Client feature is enabled). You'll know right away if the port is open and connection succeeds, or a big fat denied. Quick and easy test. type TELNET ServerIPAddress PortNumber from a command line. If Telnet command not found, you need to turn on the Telnet Client in Windows Features/Components.
Does your network use IP security? You might try using HTTPS instead to see if that alleviates anything.
It looks like your work network is blocking your IP. Try whitelisting your home IP on your work network.
I believe the issue here is that your laptop's IP address changes from one network (i.e. work) to another (i.e. home), but you may not be updating the value of the signalRUrl variable in your javascript file accordingly, thus the client side of your app is looking for a hub at an IP address that doesn't exist on your home network.
When you change networks, you need to update the value of signalRUrl in javascript to reflect the current IP address of the host machine.

Intercepting webpage requests through Wireless Hosted Network

Good Evening,
I have created a Wireless Hosted Network on my Windows 8 Laptop, via the following 2 batch commands:
"netsh wlan set hostednetwork mode=allow ssid=testing key=12345678"
and
"netsh wlan start hostednetwork"
Afterwards, a device (Android-based mobile phone) is able to connect successfully to this Wireless Hosted Network hosted on my laptop. I've also created a HttpListener on port 1234 in C#, which the device can successfully access by using the built in web browser to access:
http://192.168.138.1:1234
However, how would the host (I.E. my laptop) intercept and redirect webpage requests? That is, how would a .NET program running on my laptop be notified when a device tries to access any webpage, say, google.com or facebook.com, and then redirect the device to another webpage by sending this webpage to it instead?
I have tried the following:
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+:80/");
listener.Start();
Console.WriteLine("Started");
while (true)
{
var c = listener.GetContext();
Console.WriteLine("Connection");
string response = "<HTML><BODY>My web page.<br></BODY></HTML>"; //Redirects device to this
byte[] b = Encoding.UTF8.GetBytes(response);
c.Response.ContentLength64 = b.Length;
c.Response.OutputStream.Write(b, 0, b.Length);
c.Response.OutputStream.Close();
}
However, the program running on my laptop does not pick up anything at all when I use the device to access any webpage (E.G. google.com). Sorry if I have asked a repeated question, I couldn't find it anywhere!
You will need to set up your own DNS server for this to work. The problem is that your laptop makes a DNS query for google.com and receives an IP which points to Google's servers and not yours.
Or, if there is only a few sites you want to redirect - add entries for them in your hosts file.

No response from sever on external IP in client-server IM app

I'm following a tutorial # http://www.geekpedia.com/tutorial239_Csharp-Chat-Part-1---Building-the-Chat-Client.html to try and gather the basics of networking. For those not wanting to hit the jump, it's a quick tut demonstrating how to program a simple client-server-model chat application.
When I try and run the code in the tut, it works fine as long as both the client and the server are on the same network, but the second I try and do it externally (getting a mate to run the client app, and running the server app my side), it all goes to pot. The fact that the code works when in the same network leads me to believe that it's not a coding issue, but an issue with the way my network is set up.
I'm trying to run the server on my IP address at port 21719, which I have opened, but still other people can't connect to my server, not able to get any form of response at all.
The code (from the tut) that is being used for the server to listen to connections is:
public void StartListening()
{
IPAddress ipaLocal = ipAddress; //ipAddress is parsed from txtIP
tlsClient = new TcpListener(ipaLocal, 21719);
tlsClient.Start();
ServRunning = true; //for the running loop
// Start the new tread that hosts the listener
thrListener = new Thread(KeepListening);
thrListener.Start();
}
Now, the tutorial does actually point out that
IPAddress ipaLocal = ipAddress;
Will cause issues on some configurations, and I'm beginning to fear that my configuration may be included in that.
So, does anyone have any solution for me?
Thanks,
Sam
What is the local IP address that you're using? (ipAddress) If it's 127.0.0.1, that's not correct (I don't know how it would work internally either, but Windows seems to use magic from time to time). Also, if you have multiple NICs in your local machine, maybe the port forwarding is only set up to forward to one of them, and you're using the IP of the other?
If that's not the problem, here are a few generic suggestions:
Grab a copy of netcat. It's a small network testing util whose only job is to form a simple TCP connection. That will allow you to eliminate your code as a variable in all this. If netcat can form a connection, then you know the problem is your code. If not, you've confirmed that it's your router.
You can use WireShark (or TShark) to look for ICMP packets. Capture ICMP packets on the remote machine. If you get "Destination Unreachable" from the router, you've again proved that it's your router.
As Spencer said you need to make sure Port Forwarding is setup on your router, to forward all packets that come in on port 21719 to your internal machine. As for exactly how to do that, it's hard to say without knowing what type of router.
Are you having people use your external (internet) IP address? (See yours here.)
Have you pinholed your router to forward all communications from port 21719 to your server?
Some tips:
What kind of operating system are you using? Please check the Scope and/or Profiles (under Advanced tab) of your firewall rule.
While your friend is trying to telnet to the port (connect to the im server) monitor the traffic using Wireshark or Network Monitor (Wireshark have problems with Vista and Win 7). If you don't see anything hitting your machine the problem is probably on the router side. Double check the settings - you said you set the forward rule (NAT) but did it also set the rule on firewall of your router?

Categories