2

I literally have the same question as found in this post: How to redirect complete outgoing phone's traffic to a proxy server?

I have an Android phone acting as a hotspot server (using KWS). I am able to connect another device and access my little webpage by typing 192.168.43.1:8080 into the URL search of my browser, so no problem there.

However, I would like for all requests by connected devices to be redirected to my webpage.

The answer in the linked question suggests running two DNS servers using the Server's Ultimate app. My only hesitation is that it is not free, and the 'free' version is a 7 day trial. I have tried another app called "Set DNS," which seems promising, but it will not recognize my hotspot as a network; it keeps waiting for me to connect to something.

QUESTIONS:

  • Can I simply change the /system/etc/hosts file as mentioned in the linked post?
  • Would I add a line such as: 192.168.43.1:8080 *?
  • If not, must I purchase Server's Ultimate, or is there another alternative?

PS: This is for an advertisement. The webpage is to be static and informative (no malicious activity).

0

NOTE: Solution suggested here requires root access.

I would like for all requests by connected devices to be redirected to my webpage

When you create hotspot, Android runs dnsmasq as DHCP/DNS server (up to Android 9). We can use this DNS server to resolve every domain to your desired IP address. But you need to make sure the connected hosts don't use some public DNS like 1.1.1.1, instead must use DNS provided by DHCP i.e. IP address of Android device.

Create file /system/etc/dnsmasq.conf and add the following option:

address=/#/192.168.43.1

See details in manpage. Start hotspot and test. Similarly you can use other DNS servers on your Android device e.g. dnscrypt-proxy.

NOTE:

For http://example.com request goes to 192.168.43.1:80 and for https://example.com it goes to 192.168.43.1:443. However if you type http://example.com/test.html it requests page 192.168.43.1:80/test.html which should exist, otherwise an error will be returned. An easy way is to configure your advertisement webpage as index page and default error page. For instance index-file.names and server.error-handler options on lighttpd.


  • Can I simply change the /system/etc/hosts file as mentioned in the linked post?
  • Would I add a line such as: 192.168.43.1:8080 *?

hosts file is used by DNS resolvers which are usually part of main C library of OS. As far as I know the built-in resolvers on Android/Linux/Windows don't support wildcards, so the * trick won't work. Secondly, domain names are resolved to IP address (192.168.43.1) not ports (:8080). See below links for details on DNS.

Web browsers by default use port 80 for http requests. If you are running server on some other port, you have to mention the port on web browser. Or redirect all newly generated TCP traffic from port 80 to 8080:

~# iptables -t nat -I PREROUTING -m conntrack ! --ctstate RELATED,ESTABLISHED -p tcp --dport 80 -j REDIRECT --to 8080

RELATED:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.