2015-10-24 18:01:55 +00:00
|
|
|
package eu.faircode.netguard;
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2015-10-25 09:29:49 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-10-25 15:16:20 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.net.VpnService;
|
|
|
|
import android.os.ParcelFileDescriptor;
|
2015-10-24 19:50:29 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2015-10-25 15:16:20 +00:00
|
|
|
public class BlackHoleService extends VpnService {
|
|
|
|
private static final String TAG = "NetGuard.Service";
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-10-25 15:16:20 +00:00
|
|
|
private ParcelFileDescriptor vpn = null;
|
2015-10-25 09:29:49 +00:00
|
|
|
public static final String EXTRA_COMMAND = "Command";
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-10-25 09:29:49 +00:00
|
|
|
public enum Command {start, reload, stop}
|
2015-10-24 18:01:55 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2015-10-25 15:28:41 +00:00
|
|
|
// Get enabled
|
2015-10-25 09:29:49 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", false);
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Get command
|
2015-10-25 09:29:49 +00:00
|
|
|
Command cmd = (intent == null ? Command.start : (Command) intent.getSerializableExtra(EXTRA_COMMAND));
|
2015-10-25 15:16:20 +00:00
|
|
|
Log.i(TAG, "Start intent=" + intent + " command=" + cmd + " enabled=" + enabled + " vpn=" + (vpn != null));
|
2015-10-25 09:29:49 +00:00
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Process command
|
2015-10-25 09:29:49 +00:00
|
|
|
if (cmd == Command.reload || cmd == Command.stop) {
|
2015-10-25 15:16:20 +00:00
|
|
|
if (vpn != null)
|
|
|
|
vpnStop();
|
2015-10-25 09:29:49 +00:00
|
|
|
if (cmd == Command.stop)
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
if (cmd == Command.start || cmd == Command.reload) {
|
2015-10-25 15:16:20 +00:00
|
|
|
if (enabled && vpn == null) {
|
2015-10-25 09:29:49 +00:00
|
|
|
Log.i(TAG, "Starting");
|
2015-10-25 15:16:20 +00:00
|
|
|
vpnStart();
|
2015-10-25 09:29:49 +00:00
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:16:20 +00:00
|
|
|
private void vpnStart() {
|
|
|
|
Log.i(TAG, "Starting");
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Check if Wi-Fi
|
2015-10-25 15:16:20 +00:00
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo ni = cm.getActiveNetworkInfo();
|
|
|
|
boolean wifi = (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI);
|
|
|
|
Log.i(TAG, "wifi=" + wifi);
|
|
|
|
|
|
|
|
// Build VPN service
|
|
|
|
final Builder builder = new Builder();
|
2015-10-25 15:28:41 +00:00
|
|
|
builder.setSession(getString(R.string.app_name));
|
2015-10-25 15:16:20 +00:00
|
|
|
builder.addAddress("10.1.10.1", 32);
|
|
|
|
builder.addRoute("0.0.0.0", 0);
|
|
|
|
builder.setBlocking(false);
|
|
|
|
|
|
|
|
// Add list of allowed applications
|
|
|
|
for (Rule rule : Rule.getRules(this))
|
|
|
|
if (!(wifi ? rule.wifi_blocked : rule.other_blocked)) {
|
|
|
|
Log.i(TAG, "Allowing " + rule.info.packageName);
|
|
|
|
try {
|
|
|
|
builder.addDisallowedApplication(rule.info.packageName);
|
|
|
|
} catch (PackageManager.NameNotFoundException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:28:41 +00:00
|
|
|
// Build configure intent
|
2015-10-25 15:16:20 +00:00
|
|
|
Intent configure = new Intent(this, ActivityMain.class);
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(this, 0, configure, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
builder.setConfigureIntent(pi);
|
|
|
|
|
|
|
|
// Start VPN service
|
|
|
|
vpn = builder.establish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void vpnStop() {
|
|
|
|
Log.i(TAG, "Stopping");
|
|
|
|
try {
|
|
|
|
vpn.close();
|
|
|
|
vpn = null;
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
private BroadcastReceiver connectivityChangedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-10-25 09:29:49 +00:00
|
|
|
Log.i(TAG, "Received " + intent);
|
|
|
|
Util.logExtras(TAG, intent);
|
|
|
|
if (intent.hasExtra(ConnectivityManager.EXTRA_NETWORK_TYPE) &&
|
|
|
|
intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_DUMMY) == ConnectivityManager.TYPE_WIFI) {
|
|
|
|
Intent service = new Intent(BlackHoleService.this, BlackHoleService.class);
|
|
|
|
service.putExtra(BlackHoleService.EXTRA_COMMAND, Command.reload);
|
|
|
|
startService(service);
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2015-10-24 19:50:29 +00:00
|
|
|
Log.i(TAG, "Create");
|
2015-10-25 15:28:41 +00:00
|
|
|
|
|
|
|
// Request connectivity updates
|
2015-10-24 18:01:55 +00:00
|
|
|
IntentFilter intentFilter = new IntentFilter();
|
|
|
|
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
|
|
registerReceiver(connectivityChangedReceiver, intentFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
Log.i(TAG, "Destroy");
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-10-25 15:16:20 +00:00
|
|
|
if (vpn != null)
|
|
|
|
vpnStop();
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
unregisterReceiver(connectivityChangedReceiver);
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRevoke() {
|
|
|
|
Log.i(TAG, "Revoke");
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-10-25 15:16:20 +00:00
|
|
|
if (vpn != null)
|
|
|
|
vpnStop();
|
2015-10-25 15:28:41 +00:00
|
|
|
|
|
|
|
// Disable firewall
|
2015-10-25 09:29:49 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.edit().putBoolean("enabled", false).apply();
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
super.onRevoke();
|
|
|
|
}
|
|
|
|
}
|