I'm trying to build an app that will set up my android wifi as open to public. And when someone close connects to it, it will serve them (or open) a webpage asking for a username and password (and some other stuff). The auth will need to be validated with a database running on the wifi tetherer's (my) phone. If the auth passes then the user is connected to wifi through my phone. The best is to inject html code or redirect the http requests the user makes while connected to my wifi.
How do I go about building something like this? what APIs do I have to implement?
For your user to access login page at least once you should allow them to get in Wifi using some server technique. You can use NanoHTTPD in android.
Here is the reference link
This is what you mobile server service will look like
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* Created by Ashish on 10/01/16.
*/
public class ServerService extends Service {
private static final int DEFAULT_PORT = 8912;
private Server server;
private BroadcastReceiver broadcastReceiverNetworkState;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String ip = getIpAccess();
server = new Server(DEFAULT_PORT);
try {
server.start();
showNotification(ip);
} catch (IOException e) {
e.printStackTrace();
}
return START_STICKY;
}
@Override
public void onDestroy() {
server.stop();
super.unregisterReceiver(broadcastReceiverNetworkState);
super.onDestroy();
}
private String getIpAccess() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
return "http://" + formatedIpAddress + ":" + DEFAULT_PORT;
}
private void showNotification(String serverAddress) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Server Running")
.setAutoCancel(false)
.setCategory("SystemServer")
.setContentText(serverAddress);
int mNotificationId = 110;
startForeground(mNotificationId, mBuilder.build());
}
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
}
Server.java
import fi.iki.elonen.NanoHTTPD;
import in.ashish29agre.androidnanohttpd.services.WebService;
/**
* Created by Ashish on 10/01/16.
*/
public class Server extends NanoHTTPD {
private static final String APPLICATION_JSON = "application/json";
public Server(int port) {
super(port);
}
public Server(String hostname, int port) {
super(hostname, port);
}
@Override
public Response serve(IHTTPSession session) {
return new WebService(session).get();
}
}
WebService.java
public class WebService {
private static final String APPLICATION_JSON = "application/json";
private NanoHTTPD.IHTTPSession session;
public WebService(NanoHTTPD.IHTTPSession session) {
this.session = session;
}
public NanoHTTPD.Response get() {
Map<String, String> response = new HashMap<>();
String value = this.session.getUri();
response.put("value", value);
response.put("request_parameters", GsonProvider.getInstance().getGson().toJson(this.session.getParms()));
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED, APPLICATION_JSON, GsonProvider.getInstance().getGson().toJson(response));
}
}
For complete running example please clone git repository reference in link.