How to set Custom DNS for Android or Flutter app - java

I want to add custom IPV4 dns like 1.1.1.1 in my android or flutter app
it means that all of the traffic of my application such as firebase/glide/retrofit have to route from my custom dns
how should i implement this ?
i'd already tried some ways in NDK to set DNS for whole android device
but i dont want to touch android DNS , just my application should use that dns

What are you trying to achieve?
This looks like a XY problem. You are probably trying to achieve something we don't know with your custom DNS (like replacing a request to a certain host with a different server), but we don't know for sure because you think changing the DNS server to your app will be a better solution for whatever problem you have, but the fact is that it is very hard.
While technically feasible, it is extremely challenging.
It is not possible to change the DNS just for one application, as the DNS configuration is global to the device, so any network request will use the DNS servers of the device by default.
The only possible way of implementing this would be reinventing the wheel: using DnsResolver to manually query the DNS server for each of the target hosts, and then making requests to the specific IP addresses.
But with HTTP you will find out that if you make a request to an IP address, that IP address will be the origin header instead of the domain name, and the only solution to that would be using bare sockets and implementing all the HTTP payloads and headers manually to give a different origin.
It won't work with Firebase and other closed source SDKs as well, since you cannot modify them. It would only work for requests sent by your own code, or SDKs allowing you to change the server address.

Related

Supporting Ip based whitelisting in Java web-service

I want to support ip based whitelisting in one of the java web-service. To implement that i have written a servlet filter in which i am verifying if the remote-machine-ip belong to the whitelisted ips. To determine the remote-machine-ip i am using the following code :-
String ipAddress = httpServletRequest.getRemoteAddr();
I want to know if there is way by which remote-machine-ip can be impersonated by the attacker without going inside the remote machine. if yes then is there a better secure way to determine the remote ip ?
There is a way, however, it's not an easy way, see this answer for more details.
Remember that httpServletRequest.getRemoteAddr() will be no good if you deploy your app behind a proxy or a CDN. In that case, you'd have to check the X-FORWARDED-FOR header and then it's easy to forge that.

Add Java API to online server not localhost

I have just created a Hello World RESTful Java API with IntelliJ IDEA and Spring and I make several requests with http://localhost:8080/function and it works fine and return the JSON data well, but now I want to make request from my android app to get the same JSON data from another network. How to create global API or make my local API global or online?
If it is working on your PC, your next step is to host it somewhere which is reachable from the outside world. If it is still in testing mode, probably what you need to do is port-forwarding from your router, so that your requests to your public IP get forwarded to your PC.
In the long term of course you need to host it somewhere, unless you want to leave your PC running. You can buy a cheap hosting to start with on something like Digital Ocean, or go for the more advanced cloud service providers like Google and AWS.
in your router you have to redirect port 8080 to you network IP and allow port 8080 in your firewall
And use this IP address https://whatismyip.com.br/
You can create your java application in a container and deploy it on one of the cloud providers (AWS, Azure, Google App Engine).

Is there a way to add custom routing rules to an Android VPN natively or with a library?

I am trying to route my traffic with custom IP routes within a VPN. I have already had a similar issue with a windows VPN client and I was able to just write a batch file that runs when launching the client and it just runs route add commands.
The problem is it seems that this is impossible to do for Android. I am aware that there is the adb approach, but it gives me permissions errors when attempting to custom add routes. Upon further research it seemed in order to change the routing rules for the IP tables I would have to root my device, this is not an option in my case.
I have looked at the VPN service on the Android Developer page, but I didn't see any way to start adding custom routes to a VPN. I also saw no libraries readily available that provide a method for additionally routing traffic. Perhaps I am missing a piece of the puzzle.
I am still learning about Android Development and this is an issue that I would like to have some input on as I will not be able to run my application through the default VPN without adding my custom rules, and I do not have the ability to change the VPN subnets around to get rid of this issue at the moment.
I had a similar problem.
Android SDK has addRoute API to add a route policy through VPN.
But there is no remove route API in the public document.
Here is the addRoute documents:
Add a network route to the VPN interface. Both IPv4 and IPv6 routes are supported. Adding a route implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to be routed over the VPN.

Running a web server on an Android device with a custom URL?

I created a simple webserver, which listens on port 8080 on my Android device. I can reach the service by calling 192.168.1.23:8080 in a browser from a laptop which is in the same network.
Question: How can I call the service by an URL?
AirDroid does this, and it works by calling http://web.airdroid.com.
Any domain name can be pointed towards an IP address by setting its DNS settings. If you own a domain name, you can go into the DNS settings and set up a subdomain (eg test.yourdomain.com) with an "A" record with the IP you want (192.168.1.23). Now, assuming you're connected to the internet (so the PC you're accessing from can access DNS servers), if you go to test.yourdomain.com:8080 in a browser on the same network as the phone, it'll request data from 192.168.1.23.
You can do this locally by editing your HOSTS file. If you put an entry into the HOSTS file of a computer (common file locations are on wikipedia) that looks something like this:
192.168.1.23 test.anydomain.com
If you then visit http://test.anydomain.com:8080 on the computer with a modified file, it will redirect to your phone. You don't need to own the domain to do this - your HOSTS entry will override whatever the actual domain owner has set up.
If you don't own a domain, you can generally register a subdomain from noip.com or other providers which you can set up with an A record to your local IP address.
Note that none of these options will negate the need for the ":8080" to be appended on the domain. The two ways to get around that are to a. serve web data on port 80 from your phone (it may not be possible?) or b. surround the data the phone's serving in an iFrame. Services like noip may (I've not used it in years) have an option to do a "masked redirect" or something similar, which would essentially just do that.
The airdroid example is not quite the same as this. It seems that they serve (from their servers) a web interface which (I assume) fetches data from your phone in the background.
first if you use ur webservice on your local pc then you need a server application that can handle this sort of things you can check out IIS if u r using ASP.net and C# or MVC or any microsoft based services .... if u r using PHP there is many software's that can be installed to run php code like WAMP server
second if u r using IIS you will need probably ksoap link to library which implements webservice based on IIS ... if u r using php then you will need to call it from ur app like this http:// (your ip):(port)/directory name here/yourPHPfile.php

My Server sees my router's IP instead of client's

I have a web-server that is behind a router. One of the servlets on that server relies on the IP address of the user, however, i often get the IP of my router (i.e. 192.168.1.1) instead of the real IP.
How can i fix that?
It sounds like your "router" is actually acting like a reverse proxy.
It is unlikely that you will be able to solve this in Java ... or even on your server machine. It sounds like a router configuration problem. You might be better off asking for help on "superuser" or "serverfault".
One thing to bear in mind is that identifying clients by IP address is always a bit dodgy. Even once you've sorted out the current problem, the real client IP address may not be available for various reasons:
The client's IP may be NATed.
The client may be talking to your webserver via a series of HTTP proxies.
The client may be spoofing its IP address.
I would suggest that access http://wwww.whatismyip.com to get you IP adddress in case you have not tried that way.You will more idea from this link.
Getting the 'external' IP address in Java
Grab your global IP from http://www.whatismyip.com/ , I think you might also need to configure port forwarding on router of user that is having servlet.

Categories

Resources