Tumgik
#uhttpd
amythical · 8 years
Text
Bypass captive portal for Android
So you have created your Access Point on your device.
But when you try and connect to that WIFI on your mobile, you get an ugly popup - asking you if its OK to connect to this WIFI even though there is no internet.
If you are making an app, this throws your smooth app workflow off-track, causes one more confusing step to the customer, needs that you add an FAQ or instruct the customer why this is happening and distracts the consumer from your intended process.
How does this happen?
This is called ‘Captive Portal’.When a WIFI connects, android tries reaching out to connectivitycheck.gstatic.com for Marshmallow and clients3.google.com for Kitkat.
Android then calls a page generate_204 on these domains i.e it makes a call to connectivitycheck.gstatic.com/generate_204  for Marshmallow and clients3.google.com/generate_204 for Kitkat.
It expects a 204 HTTP response code in return instead of the usual 200 HTTP response code.
How I fixed it -
Step 1 - Resolve the domains locally
On my device I was running dnsmasq.Look at addn-hosts in the dnsmasq config file.In this directory pointed to by addn-hosts create a file called custom.
custom (contents below)
192.168.1.1 connectivitycheck.gstatic.com
192.168.1.1 clients3.google.com
restart dnsmasq.
Now connectivitycheck.gstatic.com & clients3.google.com resolve to your device’s webserver when connected by a client connected to your AP.
Step 2 - Server a 204 response
I was using uhttpd so did not have the url_rewrite option.What I managed to do is specified a php page as a 404 redirect in uhttpd conf.
       option error_page /204.php           
In the 404 php script, I returned a 204 header.Dirty hack but it works !!!
<?php
// .... log whatever you need to log, then reply:
header("HTTP/1.0 204 No Content");
?>
Now when connected to my AP, when the android device requests for connectivitycheck.gstatic.com/generate_204 it goes to a 404, which returns a 204 header and android is happy, it thinks its connected to the internet and the Captive Portal goes away automatically.
For Step 2 - if you have apache, do a url rewrite/htaccess mapping and redirect generate_204 to a page returning a 204 HTTP header.
Happy bypassing the Captive Portal for Android.
Bypassing IOS maybe later when I get to it. 
References 
http://android.stackexchange.com/questions/123129/how-does-wifi-in-android-detect-if-the-device-has-to-sign-in-or-not
https://en.wikipedia.org/wiki/Captive_portal
https://foxdogstudios.com/making-phones-believe-the-wifi-has-internet
0 notes