Connect to webserver from outside LAN raspberry pi 3 windowsiot - c#

I ran a tutorial including HTTP server app for raspberry pi 3.
Link here: https://incredibits.io/project/windows-10-iot-internet-of-things-tips/windows-10-iot-raspberry-pi-web-server
In my local network everything works perfectly. I run server, type in browser local IP of my raspberry (192.168.x.x) and "hello world" appears - great. But, I have an issue when i want reach my HTTP server outside from my LAN.
What i did:
1. forwarded port 80 in my router
2. opened port 80 in raspberry pi with below commands in PowerShell
netsh advfirewall firewall add rule name="Open 80" dir=in action=allow protocol=TCP localport=80
netsh advfirewall firewall add rule name="Open 80" dir=out action=allow protocol=TCP localport=80
checked if above commands works by typing
netsh advfirewall firewall show rule name="Open 80"
and i get that response:
Rule Name: Open 80
----------------------------------------------------------------------
Enabled: Yes
Direction: Out
Profiles: Domain,Private,Public
Grouping:
LocalIP: Any
RemoteIP: Any
Protocol: TCP
LocalPort: 80
RemotePort: Any
Edge traversal: No
Action: Allow
Rule Name: Open 80
----------------------------------------------------------------------
Enabled: Yes
Direction: In
Profiles: Domain,Private,Public
Grouping:
LocalIP: Any
RemoteIP: Any
Protocol: TCP
LocalPort: 80
RemotePort: Any
Edge traversal: No
Action: Allow
Ok.
checked if my port is visible as open with http://www.yougetsignal.com/tools/open-ports/ - it was CLOSED (ofcourse with server app run
ran exactly the same app in my notebook (with windows 10 ofcourse). I forwarded port 80 in router into my netbook, i checked if port is open - and it was OPEN. I type my external IP into browser and it works. I even try to do it from my smartphone to be sure and it also works.
I think that is a proof that a problem is in my raspberry - its settings

Make sure you have the "Internet (Client & Server)" capability selected in your app manifest. You can find this setting by opening the Package.appxmanifest file and clicking the Capabilities tab.

I solved it.
The problem was in my router setup. I have two NAT options (Virtual Server Setup and Port Triggering Setup) and i had settings like below:
Removing "web" setting in NAT - Port Triggering Setup solved my problem. My HTTP server running on raspberry pi became visible for outside world.
I don't know why this settings didnt block HTTP server running on laptop but this is different story. Problem with raspberry pi server - solved.

Related

Kestrel unable to start

When specifying a port to bind to with .UseKestrel() I get the errors listed below.. but if I remove the kestrel options everything works normally if I check the API from my browser.
I've tried binding to the port that my application defaults to with no ports chosen and I've tried checking netstat to actively avoid any ports that are in use. Nothing works but removing the options entirely. This is not replicated on my Mac or another Windows 10 machine. This device is Windows 10.
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 50470);
options.Listen(IPAddress.Any, 80);
})
: Microsoft.AspNetCore.Server.Kestrel[0]
Overriding address(es) 'http://localhost:50470/'. Binding to endpoints defined
in UseKestrel() instead.
crit: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to start Kestrel.
System.Net.Sockets.SocketException (10013): An attempt was made to access a
socket in a way forbidden by its access permissions
at
System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException
(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress
socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransport.
BindAsync() at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.
<>c__DisplayClass21_01.<<StartAsync>g__OnBind|0>d.MoveNext() `
Also check Darkthread's answer here: https://superuser.com/questions/1486417/unable-to-start-kestrel-getting-an-attempt-was-made-to-access-a-socket-in-a-way
We discovered that a port we had been using for a long time wasn't acccessible anymore because it had been reserved by Windows! You may wish to check reserved ports using this command:
netsh interface ipv4 show excludedportrange protocol=tcp
After Windows Update, some ports are reserved by Windows and applications cannot bind to these ports. please check this command for forbiden port on Os
netsh interface ipv4 show excludedportrange protocol=tcp
In my case, Removing invalid local IP and Port address combinations from the app's launchSettings.json did it.
The additional binding of port 80 in ".UseKestrel(options => { options.Listen(...) })" was causing the issue in my case.
When you run an ASP.NET Core application directly through Kestrel, without an additional reverse proxy like IIS or nginx, you will need to configure the hosting URL properly.
The problem was because, you did not follow Port sharing limitation in Kestrel web server.
When you to use the Kestrel web server, you should set unique port to app. (if you use the port 80, make sure no apps use this port).
and your app has enough permissions, too.
more info:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2
I hope is useful.
Try Updating The Https Certificate
with this command. It worked for me!!
dotnet dev-certs https
The problem was with the additional binding of port 80, updating this corrected the problem.

Apache Thrift remote service timeout

I set up Thrift to build two entities and a corresponding service. I genereated the code for C#, created a small console application for the server and another one for a test client. I ran both on my local machine and it worked like a charm. But when deploying the server on my Azure VM running Windows Server 2012, with an endpoint created on port 9090 for TCP, my client can't connect. There's a timeout.
Similar questions on SO: This one is about a php library specific problem and this one is about two linux machines. The latter one is close to my question, but I can imagine my problem is Windows or Azure VM specific.
I'm using Thrift 0.9.0.0 from NuGet. I tried SimpleServer and ThreadPoolServer. This is what I'm doing on the server:
Thrift.Server.TServer server = new Thrift.Server.TThreadPoolServer(new TodoThriftService.Processor(new TodoThriftServiceHandler()), new TServerSocket(9090));
This is what I'm doing on the client:
TodoThriftService.Client client = new TodoThriftService.Client(new TBinaryProtocol(new TSocket("my.host.com", 9090)));
I also tried the server IP address instead of its host name, but it didn't work either.
netstat -an output is:
TCP 0.0.0.0:9090 0.0.0.0:0 LISTENING
Output from telnet from client (telnet my.host.com 9090):
Verbindungsaufbau zu my.host.com...Es konnte keine Verbindung mit dem Host hergestellt werden, auf Port 9090: Verbindungsfehler
Which is German for "connection error".
When I visit http://canyouseeme.org/ on the server and check port 9090 the output is:
Error: I could not see your service on 123.34.567.890 on port (9090). Reason: Connection timed out
When I sniff packets with Wireshark on the server the output is:
1 0.000000000 12.3.456.78 12.34.567.89 TCP 66 53566 > websm [SYN] Seq=0 Win=8192 Len=0 MSS=1452 WS=256 SACK_PERM=1
And two retransmissions.
Note: The destination IP shown in Wireshark differs from the IP that canyouseeme.org determined.
Also note: Both connection attempts from my test client as well as from canyouseeme.org are shown in Wireshark.
Is my server console application doing anything wrong? It works fine on my local machine.
Because netstat reports that your server is listening on 9090, and because tcpdump reports that SYN packets are arriving to 9090, something else must be blocking the client.
My suspicion in this case would be some sort of local firewall (because the packets did arrive, but the OS should have answered with a SYN packet and you didn't see that). Check Windows Firewall to make sure port 9090 is open.

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

WinCE 6.0, work with GPRS/WiFi

I'm working on a project, which should connect to servers through wifi/gprs. Project is an application for Windows CE 6.0 device, I'm writing in Visual Studio 2008 on C#.
I have two severs to work with. The first I have to connect via wifi, second - via gprs. So I need to know, how can I change the method of connecting between wifi and gprs?
I found and tried this way: I turn on both wifi and gprs on my device. So I work via wifi because it has a higher priority. When I need to work via gprs, I turn off wifi (SetDevicePower function). But when I turn wifi on, it doesn't connect back to my Preferred Network.
Also I heard about the way to change priority between gprs/wifi in OS priority table programmatically, but I didn't find any information about how to do this.
I hope you can help me.
I would use the route command from a shell.
lets assume
server1 ip: 123.123.123.1
server2 ip: 123.123.123.2
wifi ip : 192.168.1.101
gateway: 192.168.1.1
gprs ip : 10.1.2.3
gateway: 10.1.1.1
Now you can excute in a command prompt
route add 123.123.123.1 MASK 255.255.255.255 192.168.1.1
and
route add: 123.123.123.2 MASK 255.255.255.255 10.1.1.1
This should route all trafic to server 1 over wifi and to server 2 over gprs, without changing a single line of code in your app.
You can verify it worked with
tracert 123.123.123.1
tracert 123.123.123.2
However, you could use your app to periodically perform this task (I assume gprs ip could change from time to time) with Process.Start(...)
- delete route 1
- add route 1
- delete route 2
- add route 2
You even could specify the interface with the IF 2 switch (route list prints the interface id for your network cards).
Another interesting post to read is this one: http://ce4all.blogspot.com/2007/05/routing-ip-traffic-via-specified.html
The author uses the GetAdapterAddresses() and CreateIpForwardEntry() P/Invokes:
http://msdn.microsoft.com/en-us/library/ms927384.aspx
http://msdn.microsoft.com/en-us/library/ee495149%28v=winembedded.60%29.aspx

Programmatically retrieve disconnected network adapter information in .NET

I have an application written in C# that needs to retrieve information like IP address, subnet mask from a disconnected network adapter.
I've tried using various methods such as WMI and the .NET NetworkAdapter class but they don't return any useful data when the network adapter is disconnected. I'm pretty sure Windows keeps this information somewhere, since I can apply network settings using netsh and it appears correctly in the Control Panel.
One thing that worked for me in XP was to parse the output of the netsh tool and it would return information even for a disconnected adapter. However, this doesn't seem to work on Windows 7.
Win XP output:
Configuration for interface "Local Area Connection 5"
DHCP enabled: No
IP Address: 169.254.0.128
SubnetMask: 255.255.255.0
InterfaceMetric: 0
Win7 output:
Configuration for interface "Local Area Connection 2"
DHCP enabled: No
InterfaceMetric: 5
Any ideas?
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
and/or
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\*
- List Interfaces
and then
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\\*\Parameters\Tcpip
current settings parameters
if DHCP - ON then only NetworkChange.NetworkAddressChanged because current IP is impossible to define

Categories