Intercepting webpage requests through Wireless Hosted Network - c#

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.

Related

Redirect to Asp.Net MVC error page while client logged in through Wi-Fi Connected Laptop

I have two sites. One is 80 Port and another is 8080 Port. The First One is perfectly logged in through LAN and Wi-Fi network from everywhere. Even if I change port for that no error occurred.
But another one is perfectly logged in through both LAN and Wi-Fi from different laptop of my home or office. Even Client office can work perfectly with LAN connection. But when client wants to login the site with Wi-Fi network from their different branch office, The Login page is redirected to Asp.Net MVC default error page.
I can't understand whether it is networking problem or login authentication problem or connection string problem. Please help me to solve the problem.
Thanks in advance for your nice co-operation.
The Problem is solved. I don't know why some WiFi Network can't detect IPAddress.Parse(Request.UserHostName) while the site is on Live.
After commented the following lines everything is now Ok on both LAN and Wifi.
But the awful matter is that now I have to avoid to show Login PC name from the login info list.
private void GetLoggedPCInfo()
{
//string ip = Request.UserHostName;
//IPAddress myIP = IPAddress.Parse(ip);
//IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
//List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
//Session["LoginPC_Name"] = compName.First().ToUpper();
string dateTime = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss tt");
Session["LoginDateTime"] = dateTime.ToString();
}

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.

Connecting through AstManProxy using Asterisk.NET

I am developing an application that uses asterisk server to manage SIP calls.
I am using Asterisk.NET library for this.
I needed to connect the asterisk server through a proxy server AstManProxy. But, when I connect through the proxy, it gives me a TimeOut Exception.
Can Anybody tell me how can I connect through AstManProxy?
First test that you can connect to asterisk WITHOUT proxy.
After that check again you correctly configured proxy.
If all seams ok, use tcpdump or wireshark to see communication between your app and proxy.
This is the procedure I typically follow if astmanproxy is running on the same machine as Asterisk.
ENABLE AMI ON PORT 5039
vi /etc/asterisk/manager.conf
[general]
enabled = yes
port = 5039
bindaddr = 0.0.0.0
timestampevents = yes
[username]
secret = password
read = all
write = all
THEN ENSURE AMI IS RESPONDING
telnet localhost 5039
Action: Command
Command: core show channels<enter><enter>
Action: Logoff <enter><enter>
CONFIGURE ASTERISK MANAGER PROXY ON PORT 5038
vi /etc/asterisk/astmanproxy.conf
host = localhost, 5039, username, password, on, off
listenport = 5038
THEN TEST ON PROXY PORT 5038
telnet localhost 5038
Action: Command
Command: core show channels<enter><enter>
Action: Logoff <enter><enter>
THEN USE MONIT TO ENSURE ASTMANPROXY STAYS RUNNING
vi /etc/monit/monitrc
#Asterisk AMI Manager Proxy Monitoring Rule
check process astmanproxy with pidfile /var/run/astmanproxy.pid
start program = "/etc/init.d/astmanproxy start"
stop program = "/etc/init.d/astmanproxy stop"
if failed host 127.0.0.1 port 5038 then restart
if 5 restarts within 5 cycles then timeout

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

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.

using httpListener to create custom website with URL as my IP-Address

I want to create a website with URL as my IP-address[ex: 192.X.X.X]
That website would respond with a "HELLO THERE" message to any user who accesses my URL.
I use the following code to do this![its just a basic code with no threading]
class listenToHTTP
{
HttpListener _listner;
public void start()
{
_listner = new HttpListener();
_listner.Prefixes.Add("http://localhost/");//default port 80
_listner.Start();
}
public void process()
{
while (true)
{
HttpListenerContext context = _listner.GetContext();
byte[] output = Encoding.ASCII.GetBytes("HELLO THERE");
context.Response.ContentEncoding = Encoding.ASCII;
context.Response.ContentLength64 = output.Length;
context.Response.OutputStream.Write(output, 0, output.Length);
}
}
}
The problem is that i dont know the IP-address through which anyone would access.
It perfectly shows the response "HELLO THERE" when I use http://localhost/ as URL.
But what IP-address would other people use so that they can access my simple website.
I have tried my IP-address in the browser but it doesnt work.
There are two things to look out for when doing this;
If you listen to a localhost address, only localhost will be able to connect to your HttpListener. You'll need to add a prefix with http://192.X.X.X/ (where the 192.X.X.X is your local IP of course) and listen to that. That may (depending on your operating system) require you to run as admin, at least if you want to do it on a port < 1024. You can test if this works by connecting to your IP# from your local machine instead of a localhost address.
If you're running Windows, the firewall may get in the way. If it seems to (ie you can connect to your IP# from the local machine but nothing else can connect) you'll need to open the port manually. There are plenty of guides how to do this on Google.
#Joachim reply is already good enough. I will like to add a bit more...
You need to expose the above mentioned IP address publicly to get the URL accessible to others.
In case of Exposing the URL to your domain only (i.e. in case of Intranet), check with your System Administrator to configure the IP Address on Intranet.
Localhost settings accessibility is confined to your machine only.
Make sure to check the firewall constraint for the URL accessibility to implement Point 1 or 2
For more information check the reference for HTTPListener
HTTPListener
HTTPListener
HTTPListener
The problem was that I was referring to a private network address which are local to the network and cannot be accessed by anyone outside that private network..
These are the range of ip-address that are used in private network and so systems with this address cannot be a server or host a website..
10.0.0.0 to 10.255.255.255
172.16.0.0 to 172.31.255.255
192.168.0.0 to 192.168.255.255
You should use a public address..

Categories