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.
You need to make sure that 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. Or redirect all DNS traffic to local DNS server:
~# iptables -t nat -I PREROUTING -p UDP --dport 53 -j REDIRECT --to 53
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: