Accessing NATed HTTP server - android

I have a HTTP server hosted on my android device. I want it to be able to access it from the internet even when it is connected on home wifi(behind a wireless router).
Further, configuring static port forwarding on router is not an option, since from the internet, I won't have access to the router configuration.
Do I have any option?
I tried this:
Android App sends a HTTP request from android device(with local IP address, say 192.168.0.1) from a particular port number(say 8080), to a web server on internet through home WIFI,
Router would modify the source address to that of its public IP address(say 111.100.45.65) and source port number to any random port number(say 39824) and makes an entry in its dynamic NAT table.
Internal IP Internal port External port
192.168.0.1 8080 39824
As per my understanding this entry is retained for atleast few hours.
Web server on the internet would report the source IP and port as 111.100.45.65 and 39824 respectively.
After sending the HTTP request from android app to web server on the internet, I run an HTTP server on port 8080 of the device. Now, if I were to send HTTP request from the internet to the address 111.100.45.65 at port 39824, it should be received by HTTP server running on device(router would resolve the request from its dynamic NAT table and direct it to my device).
But for some reason, this request doesn't get resolved. Any pointers.

Firstly regarding access to router configurations from the Internet, most routers do actually support logins from the WAN interface. It's turned off by default on most routers, but there is no reason why you can't enable this as long as your password is secure.
Secondly, what you tried would work if you setup a proxy on the web server on the Internet.
Punch a TCP hole by initiating connection from your Android device to the web server on the Internet.
Now the web server on the Internet knows the public IP and the destination port to be able to talk to the Android device.
An automated script can configure the proxy server on the web server on the Internet that will forward HTTP requests to the Android device.
By the way, I am doubtful that the NAT entry is retained for at least a few hours. You may wish to implement a keep-alive mechanism between your Android device and the web server on the Internet so that the TCP hole remains open.

No. Your app can communicate over port 39824 with the http server on the web. Nobody else can use that port to make contact from web to your http server on your device. You need portforwarding on your router for your device be reachable from internet.

Related

How can I send request to my mobile http server from other device in local network?

I want to host a server on my mobile device and send request to it from my computer. Both device are in the same network. I use flutter HttpServer.bind(InternetAddress.loopbackIPv4.host,8080); to initialize my server.
I can send requests to it by addressing localhost:8080 e.g. in my phone's browser, but I'm not able to connect to it from my pc while using my phone's local ip address that I found in wi-fi details on my phone. I can ping said address, but that's all.
Does anybody know why that happens? Am I using wrong IP, are there some android firewall settings that I don't know about or is it just impossible?

Server on Android reachable from devices not on local network?

I have been trying to set up an app that allows communication between two devices, a server and a client. I have established the connection and can send messages between the two as long as they are connected to the same source of internet. When I connect the devices to two different sources, I am not able to establish a connection between the two. It works by entering the IP address of the server you want to connect to.
For example, if my server device is connected to my home Wi-Fi and my client device is connected to the same Wi-Fi router, communication works as supposed to. But, when I connect the server to Wi-Fi and my client to cellular data (or different Wi-Fi router), the client is not able to find the server.
From all the searching I've done, I found that it is because my server is bound to my local host. So my question is, How can I make my server reachable from anywhere regardless of what network you are connected to?
I used this tutorial to get my server running:
http://android-er.blogspot.com/2014/02/android-sercerclient-example-server.html
The very first comment is a question that asks why it only works on a local network and someone answered saying you have to set up port forwarding on your router. From what I understand, this will work only if the server device is always connected to the same router. This will not be the case for my app. The server will have a new IP address as the device changes networks. I want this IP address to always be reachable no matter what network the server is connected to.
You will need to create a relay server. This would be set up using a java application on the PC and it would manage virtual "rooms" that then relay the messages to the clients. The computer you run your relay server off of will need port forwarding but the clients will not. Much of the code from that tutorial could be applied to this concept.
See: How to create a java Server that accepts client connections and then build a relay connection for a client pair
If your server is behind a firewall, you will not be able to reach it unless that port is open on the firewall. The only way to avoid this is to have a non-firewalled server.

Using an Android-hosted DNS server to filter website requests

I am developing an application with system privileges on a rooted device. I have managed to
change the DNS settings programmatically for the WIFI connection I am connected to. To check, I run the application, connect the device through USB and run "getprop" command from "adb shell". The application successfully changes the DNS for the session and browsers are blocked from connecting to any website using domains.
I have installed an application called DNS Server which will run a DNS Server from my Android device. I have set DNS Server to block requests to specific shady websites and forward every other request to the original DNS server, whose IP address is extracted from the WIFI connection settings.
My application on the other hand will change the device's DNS setting and point towards the device's public IP itself. In this manner every request will pass through DNS Server application, which in turn will filter out blocked websites and forward requests to the original DNS Server.
The problem lies in connection between every browser/app request and DNS Server application. From the logs, DNS Server is listening to connections but is not receiving any requests. Is there any other way to achieve this or perhaps someone can enlighten me if I'm setting something wrong?
From my application, I tried to set the device's DNS server to 127.0.0.1 (loopback), the IP address of the device itself (both public and private) but without any success. Connection cannot be established.
Further info: DNS Server requires a port to listen to, but I cannot set this port globally (I'm assuming the default 53 DNS Port is being used for every request).

Android Client App with 3G and WIFI Network

I'm developing an application that need an internet permission all the time.
When there is a WIFI connection the application can connect to my server (if they're in the same network)
actually I have 2 question..
1) Lets say that the server is connected to network A and the client is connected to Network B (on
WIFI) why it doesn't show me on the server side the message that I send?
I made a simple project that when the client is connecting to the server it send "hello" to the
server, the server read it and tell me the address of the client and the message (print to the
console), it works fine when both server and client are at the same Network.. but when they're in
different networks it doesn't work.
2) How a 3G application is working? how can I connect the client who uses a 3G network to my server?
When you are on the same network then you give the the ip the ROUTER gave to your pc and phone (assuming that what your using) and it looks something like 192.168.0.101\2 (its your router Default address + 1 each time a device connects to the router) ...
The problem is that when you want to connnect from an outside source you must give the ip of your real address given by your ISP.
go to command\terminal and type ipconfig and look for IPv4 ------ thats your real ip when looking from the "outside" world , now thats not all you also need to configure your router to port forwad the ports your using to the specific PC that is the server.
How what and why : Port Forwarding
hope this helps.

Using Android phone as wifi web server

I got this crazy idea that I don't know is possible or not... what I want to do is set up my phone as wifi access point, then allow people to connect to it, but handle all incoming http traffic myself.
So:
phone wifi in access point mode; open wifi network (this tidbit works - yes I know it's not officially supported but then I have no intention to distribute put this app on the open market or so, it works on my phone and that's good enough for me).
client can connect to the phone (e.g. my laptop: this also works).
when client tries to open an http connection to any random server, this has to be intercepted by my app and handled by a local web server. This is the point that I'm stuck on.
The web server part shouldn't be too hard; there are (open-source) web servers available. But getting the traffic to that web server, there is the problem.
The behaviour I'm after is a bit similar to what you get when connecting to some open hotspot, like hotel hotspots, that then redirect you to a login page. This intercepting of the connection request (I suspect I have to look at DNS requests?) is what I'm after.
I have seen an app that is doing this but they require the phone to be rooted. I'm hoping to find a way to do it without rooting the phone.
Wifi hotspots would usually use IPTables to redirect port 80/443 traffic to a local web server.
I have run squid on linux machines in the past on port 3128, then sent all port 80/443 traffic to squid. Then I used squid redirect any url that I didnt explicitly allow to a local web server.
IPTables is accessible on rooted android devices only though.
And I doubt there are many proxy servers available on android, so you would have to send HTTP traffic to a proxy server over the network connection.
Like has been said, you'll need IPTables, but you'll also need a proxy server, like Squid.
Google "transparent proxy with squid", or check here:
Linux: Setup a transparent proxy with Squid in three easy steps
you do not need to have a proxy, you just need a full LAMP stack on your device and IPtables, you can mark all packets that have not been authenticated to forward through to your local page, then using php change the rule for that IP address once the address has authenticated, you could do the same for mac addresses if you wish, you would then also need a daemon that periodically reset each exception

Categories

Resources