2015-10-24 18:01:55 +00:00
|
|
|
package eu.faircode.netguard;
|
|
|
|
|
2015-11-03 17:57:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of NetGuard.
|
|
|
|
|
|
|
|
NetGuard is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
NetGuard is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2015 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2015-11-23 10:43:25 +00:00
|
|
|
import android.annotation.TargetApi;
|
2015-11-04 19:47:07 +00:00
|
|
|
import android.app.Notification;
|
2015-11-18 18:33:48 +00:00
|
|
|
import android.app.NotificationManager;
|
2015-10-24 18:01:55 +00:00
|
|
|
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-12-09 13:57:14 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.Path;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.net.ConnectivityManager;
|
2015-12-09 13:57:14 +00:00
|
|
|
import android.net.TrafficStats;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.net.VpnService;
|
2015-11-23 10:43:25 +00:00
|
|
|
import android.os.Build;
|
2015-11-08 07:36:05 +00:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.HandlerThread;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.os.Message;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.os.ParcelFileDescriptor;
|
2015-11-09 13:48:20 +00:00
|
|
|
import android.os.PowerManager;
|
2015-12-09 13:57:14 +00:00
|
|
|
import android.os.SystemClock;
|
2015-10-24 19:50:29 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-10-30 09:51:44 +00:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2015-11-04 20:15:57 +00:00
|
|
|
import android.support.v4.app.NotificationManagerCompat;
|
2015-11-04 20:13:17 +00:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2015-11-25 19:05:17 +00:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-12-01 16:07:37 +00:00
|
|
|
import android.telephony.PhoneStateListener;
|
|
|
|
import android.telephony.ServiceState;
|
2015-12-07 07:12:28 +00:00
|
|
|
import android.telephony.SubscriptionManager;
|
2015-12-01 16:07:37 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
2015-10-24 18:01:55 +00:00
|
|
|
import android.util.Log;
|
2015-12-09 13:57:14 +00:00
|
|
|
import android.widget.RemoteViews;
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-10-29 22:29:01 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2015-10-24 18:01:55 +00:00
|
|
|
import java.io.IOException;
|
2015-10-29 22:29:01 +00:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
2015-12-09 13:57:14 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-10-29 06:47:12 +00:00
|
|
|
public class SinkholeService extends VpnService {
|
2015-10-25 15:16:20 +00:00
|
|
|
private static final String TAG = "NetGuard.Service";
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-12-02 16:53:03 +00:00
|
|
|
private boolean last_connected = false;
|
|
|
|
private boolean last_metered = true;
|
2015-12-01 16:07:37 +00:00
|
|
|
private boolean phone_state = false;
|
2015-12-07 11:23:43 +00:00
|
|
|
private Object subscriptionsChangedListener = null;
|
2015-10-25 15:16:20 +00:00
|
|
|
private ParcelFileDescriptor vpn = null;
|
2015-10-29 22:29:01 +00:00
|
|
|
private boolean debug = false;
|
2015-11-14 11:17:57 +00:00
|
|
|
private Thread debugThread = null;
|
2015-10-29 22:29:01 +00:00
|
|
|
|
2015-11-08 07:36:05 +00:00
|
|
|
private volatile Looper mServiceLooper;
|
|
|
|
private volatile ServiceHandler mServiceHandler;
|
|
|
|
|
2015-11-08 07:21:34 +00:00
|
|
|
private static final int NOTIFY_FOREGROUND = 1;
|
|
|
|
private static final int NOTIFY_DISABLED = 2;
|
2015-12-09 13:57:14 +00:00
|
|
|
private static final int NOTIFY_TRAFFIC = 3;
|
2015-11-04 19:47:07 +00:00
|
|
|
|
2015-10-26 16:23:41 +00:00
|
|
|
private static final String EXTRA_COMMAND = "Command";
|
2015-12-02 16:53:03 +00:00
|
|
|
private static final String EXTRA_REASON = "Reason";
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-12-09 13:57:14 +00:00
|
|
|
private static final int MSG_SERVICE_INTENT = 0;
|
|
|
|
private static final int MSG_STATS_START = 1;
|
|
|
|
private static final int MSG_STATS_STOP = 2;
|
|
|
|
private static final int MSG_STATS_UPDATE = 3;
|
|
|
|
|
2015-10-26 16:23:41 +00:00
|
|
|
private enum Command {start, reload, stop}
|
2015-10-24 18:01:55 +00:00
|
|
|
|
2015-11-09 13:48:20 +00:00
|
|
|
private static volatile PowerManager.WakeLock wlInstance = null;
|
|
|
|
|
|
|
|
synchronized private static PowerManager.WakeLock getLock(Context context) {
|
|
|
|
if (wlInstance == null) {
|
|
|
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
2015-11-14 11:17:57 +00:00
|
|
|
wlInstance = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, context.getString(R.string.app_name) + " wakelock");
|
2015-11-09 13:48:20 +00:00
|
|
|
wlInstance.setReferenceCounted(true);
|
|
|
|
}
|
|
|
|
return wlInstance;
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:36:05 +00:00
|
|
|
private final class ServiceHandler extends Handler {
|
|
|
|
public ServiceHandler(Looper looper) {
|
|
|
|
super(looper);
|
|
|
|
}
|
2015-10-25 09:29:49 +00:00
|
|
|
|
2015-11-08 07:36:05 +00:00
|
|
|
@Override
|
|
|
|
public void handleMessage(Message msg) {
|
2015-11-09 13:48:20 +00:00
|
|
|
try {
|
2015-12-09 13:57:14 +00:00
|
|
|
switch (msg.what) {
|
|
|
|
case MSG_SERVICE_INTENT:
|
|
|
|
handleIntent((Intent) msg.obj);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_STATS_START:
|
|
|
|
startStats();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_STATS_STOP:
|
|
|
|
stopStats();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MSG_STATS_UPDATE:
|
|
|
|
updateStats();
|
|
|
|
break;
|
|
|
|
}
|
2015-11-23 07:34:47 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
Util.sendCrashReport(ex, SinkholeService.this);
|
2015-11-09 13:48:20 +00:00
|
|
|
} finally {
|
2015-12-09 13:57:14 +00:00
|
|
|
if (msg.what == MSG_SERVICE_INTENT)
|
|
|
|
try {
|
|
|
|
PowerManager.WakeLock wl = getLock(SinkholeService.this);
|
|
|
|
if (wl.isHeld())
|
|
|
|
wl.release();
|
|
|
|
else
|
|
|
|
Log.w(TAG, "Wakelock under-locked");
|
|
|
|
Log.i(TAG, "Messages=" + hasMessages(0) + " wakelock=" + wlInstance.isHeld());
|
|
|
|
} catch (Exception ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
Util.sendCrashReport(ex, SinkholeService.this);
|
|
|
|
}
|
2015-11-09 13:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIntent(Intent intent) {
|
2015-11-23 09:58:05 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
|
|
|
|
|
2015-11-24 09:42:45 +00:00
|
|
|
Command cmd = (Command) intent.getSerializableExtra(EXTRA_COMMAND);
|
2015-12-02 16:53:03 +00:00
|
|
|
String reason = intent.getStringExtra(EXTRA_REASON);
|
|
|
|
Log.i(TAG, "Executing intent=" + intent + " command=" + cmd + " reason=" + reason + " vpn=" + (vpn != null));
|
2015-11-23 09:58:05 +00:00
|
|
|
|
2015-12-07 09:46:35 +00:00
|
|
|
// Listen for phone state changes
|
2015-12-01 16:42:38 +00:00
|
|
|
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
2015-12-07 09:46:35 +00:00
|
|
|
if (!phone_state &&
|
|
|
|
Util.hasPhoneStatePermission(SinkholeService.this)) {
|
2015-12-02 16:53:03 +00:00
|
|
|
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_SERVICE_STATE);
|
2015-12-01 16:42:38 +00:00
|
|
|
phone_state = true;
|
|
|
|
Log.i(TAG, "Listening to service state changes");
|
|
|
|
}
|
|
|
|
|
2015-12-07 09:46:35 +00:00
|
|
|
// Listen for data SIM changes
|
2015-12-07 11:23:43 +00:00
|
|
|
if (subscriptionsChangedListener == null &&
|
2015-12-07 09:46:35 +00:00
|
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 &&
|
|
|
|
Util.hasPhoneStatePermission(SinkholeService.this)) {
|
|
|
|
SubscriptionManager sm = SubscriptionManager.from(SinkholeService.this);
|
2015-12-07 11:23:43 +00:00
|
|
|
subscriptionsChangedListener = subscriptionsChangedListener = new SubscriptionManager.OnSubscriptionsChangedListener() {
|
|
|
|
@Override
|
|
|
|
public void onSubscriptionsChanged() {
|
|
|
|
Log.i(TAG, "Subscriptions changed");
|
|
|
|
reload(null, "Subscriptions changed", SinkholeService.this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sm.addOnSubscriptionsChangedListener((SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
|
2015-12-07 09:46:35 +00:00
|
|
|
Log.i(TAG, "Listening to subscription changes");
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:58:05 +00:00
|
|
|
try {
|
|
|
|
switch (cmd) {
|
|
|
|
case start:
|
|
|
|
if (vpn == null) {
|
|
|
|
startForeground(NOTIFY_FOREGROUND, getForegroundNotification(0, 0));
|
|
|
|
vpn = startVPN();
|
|
|
|
if (vpn == null)
|
|
|
|
throw new IllegalStateException("VPN start failed");
|
|
|
|
startDebug(vpn);
|
|
|
|
removeDisabledNotification();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case reload:
|
|
|
|
// Seamless handover
|
|
|
|
ParcelFileDescriptor prev = vpn;
|
2015-11-23 07:34:47 +00:00
|
|
|
vpn = startVPN();
|
2015-11-23 09:58:05 +00:00
|
|
|
if (prev != null && vpn == null) {
|
|
|
|
Log.w(TAG, "Handover failed");
|
|
|
|
stopDebug();
|
|
|
|
stopVPN(prev);
|
|
|
|
prev = null;
|
|
|
|
vpn = startVPN();
|
|
|
|
if (vpn == null)
|
|
|
|
throw new IllegalStateException("Handover failed");
|
|
|
|
}
|
2015-11-08 07:36:05 +00:00
|
|
|
stopDebug();
|
2015-11-23 09:58:05 +00:00
|
|
|
startDebug(vpn);
|
|
|
|
if (prev != null)
|
|
|
|
stopVPN(prev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case stop:
|
2015-11-26 07:34:33 +00:00
|
|
|
if (vpn != null) {
|
2015-11-23 09:58:05 +00:00
|
|
|
stopDebug();
|
|
|
|
stopVPN(vpn);
|
|
|
|
vpn = null;
|
|
|
|
stopForeground(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-11-26 07:34:33 +00:00
|
|
|
|
2015-11-27 12:48:48 +00:00
|
|
|
// Update main view
|
2015-11-26 07:34:33 +00:00
|
|
|
Intent ruleset = new Intent(ActivityMain.ACTION_RULES_CHANGED);
|
|
|
|
ruleset.putExtra("connected", last_connected);
|
|
|
|
ruleset.putExtra("metered", last_metered);
|
|
|
|
LocalBroadcastManager.getInstance(SinkholeService.this).sendBroadcast(ruleset);
|
|
|
|
|
2015-11-27 12:48:48 +00:00
|
|
|
// Update widgets
|
|
|
|
Widget.updateWidgets(SinkholeService.this);
|
|
|
|
|
2015-11-23 09:58:05 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
|
|
|
|
// Disable firewall
|
|
|
|
prefs.edit().putBoolean("enabled", false).apply();
|
|
|
|
Widget.updateWidgets(SinkholeService.this);
|
|
|
|
|
|
|
|
// Report exception
|
|
|
|
Util.sendCrashReport(ex, SinkholeService.this);
|
2015-11-01 06:50:49 +00:00
|
|
|
}
|
2015-11-08 07:36:05 +00:00
|
|
|
}
|
2015-12-09 13:57:14 +00:00
|
|
|
|
|
|
|
private boolean stats = false;
|
|
|
|
|
|
|
|
private long t = -1;
|
|
|
|
private long tx = -1;
|
|
|
|
private long rx = -1;
|
|
|
|
|
|
|
|
private List<Long> gt = new ArrayList<>();
|
|
|
|
private List<Float> gtx = new ArrayList<>();
|
|
|
|
private List<Float> grx = new ArrayList<>();
|
|
|
|
|
|
|
|
private void startStats() {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
|
2015-12-09 18:42:36 +00:00
|
|
|
boolean enabled = (!stats && prefs.getBoolean("show_stats", false));
|
2015-12-09 13:57:14 +00:00
|
|
|
Log.i(TAG, "Stats start enabled=" + enabled);
|
|
|
|
if (enabled) {
|
|
|
|
t = -1;
|
|
|
|
tx = -1;
|
|
|
|
rx = -1;
|
|
|
|
gt.clear();
|
|
|
|
gtx.clear();
|
|
|
|
grx.clear();
|
|
|
|
stats = true;
|
|
|
|
updateStats();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopStats() {
|
|
|
|
Log.i(TAG, "Stats stop");
|
|
|
|
stats = false;
|
2015-12-09 18:42:36 +00:00
|
|
|
mServiceHandler.removeMessages(MSG_STATS_START);
|
|
|
|
mServiceHandler.removeMessages(MSG_STATS_STOP);
|
2015-12-09 16:57:10 +00:00
|
|
|
mServiceHandler.removeMessages(MSG_STATS_UPDATE);
|
2015-12-09 13:57:14 +00:00
|
|
|
NotificationManagerCompat.from(SinkholeService.this).cancel(NOTIFY_TRAFFIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateStats() {
|
|
|
|
// Schedule next update
|
2015-12-09 18:42:19 +00:00
|
|
|
mServiceHandler.sendEmptyMessageDelayed(MSG_STATS_UPDATE, 1000);
|
2015-12-09 13:57:14 +00:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
while (gt.size() >= 100) {
|
|
|
|
gt.remove(0);
|
|
|
|
gtx.remove(0);
|
|
|
|
grx.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate stats
|
|
|
|
float txsec = 0;
|
|
|
|
float rxsec = 0;
|
|
|
|
long ct = SystemClock.elapsedRealtime();
|
|
|
|
long ctx = TrafficStats.getTotalTxBytes();
|
|
|
|
long rtx = TrafficStats.getTotalRxBytes();
|
2015-12-09 16:57:10 +00:00
|
|
|
if (t > 0 && tx > 0 && rx > 0) {
|
2015-12-09 13:57:14 +00:00
|
|
|
float dt = (ct - t) / 1000f;
|
|
|
|
txsec = (ctx - tx) / dt;
|
|
|
|
rxsec = (rtx - rx) / dt;
|
|
|
|
gt.add(ct);
|
|
|
|
gtx.add(txsec);
|
|
|
|
grx.add(rxsec);
|
2015-12-09 16:57:10 +00:00
|
|
|
Log.i(TAG, "tx=" + txsec + " rx=" + rxsec);
|
2015-12-09 13:57:14 +00:00
|
|
|
}
|
|
|
|
t = ct;
|
|
|
|
tx = ctx;
|
|
|
|
rx = rtx;
|
|
|
|
|
|
|
|
// Create bitmap
|
2015-12-09 17:38:35 +00:00
|
|
|
int height = Util.dips2pixels(96, SinkholeService.this);
|
2015-12-09 18:25:40 +00:00
|
|
|
int width = Util.dips2pixels(96 * 5, SinkholeService.this);
|
2015-12-09 13:57:14 +00:00
|
|
|
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
|
|
|
|
// Create canvas
|
|
|
|
Canvas canvas = new Canvas(bitmap);
|
|
|
|
canvas.drawColor(Color.TRANSPARENT);
|
|
|
|
|
2015-12-09 16:43:27 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
|
|
|
|
float base = Integer.parseInt(prefs.getString("stats_base", "5")) * 1024f;
|
|
|
|
|
2015-12-09 13:57:14 +00:00
|
|
|
// Determine max
|
|
|
|
long xmax = 0;
|
2015-12-09 16:43:27 +00:00
|
|
|
float ymax = base * 1.1f;
|
2015-12-09 13:57:14 +00:00
|
|
|
for (int i = 0; i < gt.size(); i++) {
|
|
|
|
long t = gt.get(i);
|
|
|
|
float tx = gtx.get(i);
|
|
|
|
float rx = grx.get(i);
|
|
|
|
if (t > xmax)
|
|
|
|
xmax = t;
|
|
|
|
if (tx > ymax)
|
|
|
|
ymax = tx;
|
|
|
|
if (rx > ymax)
|
|
|
|
ymax = rx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build paths
|
|
|
|
Path ptx = new Path();
|
|
|
|
Path prx = new Path();
|
|
|
|
for (int i = 0; i < gtx.size(); i++) {
|
|
|
|
float x = width * (xmax - gt.get(i)) / 1000f / 100f;
|
|
|
|
float ytx = height - height * gtx.get(i) / ymax;
|
|
|
|
float yrx = height - height * grx.get(i) / ymax;
|
|
|
|
if (i == 0) {
|
|
|
|
ptx.moveTo(x, ytx);
|
|
|
|
prx.moveTo(x, yrx);
|
|
|
|
} else {
|
|
|
|
ptx.lineTo(x, ytx);
|
|
|
|
prx.lineTo(x, yrx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build paint
|
|
|
|
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
paint.setStyle(Paint.Style.STROKE);
|
|
|
|
paint.setStrokeWidth(Util.dips2pixels(3, SinkholeService.this));
|
|
|
|
|
|
|
|
// Draw paths
|
|
|
|
paint.setColor(Color.MAGENTA);
|
|
|
|
canvas.drawPath(ptx, paint);
|
|
|
|
paint.setColor(Color.BLUE);
|
|
|
|
canvas.drawPath(prx, paint);
|
|
|
|
|
|
|
|
// Draw 1 KiB line
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
paint.setColor(Color.GRAY);
|
2015-12-09 16:43:27 +00:00
|
|
|
float y = height - height * base / ymax;
|
2015-12-09 13:57:14 +00:00
|
|
|
canvas.drawLine(0, y, width, y, paint);
|
|
|
|
|
|
|
|
// Update remote view
|
|
|
|
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.traffic);
|
|
|
|
remoteViews.setImageViewBitmap(R.id.ivTraffic, bitmap);
|
2015-12-09 16:57:10 +00:00
|
|
|
if (txsec < 1024 && rxsec < 1024)
|
2015-12-09 18:42:19 +00:00
|
|
|
remoteViews.setTextViewText(R.id.tvTraffic, String.format("▲ %.0f ▼ %.0f B/sec", txsec, rxsec));
|
2015-12-09 16:57:10 +00:00
|
|
|
else if (txsec < 1024 * 1024 && rxsec < 1024 * 1024)
|
2015-12-09 18:42:19 +00:00
|
|
|
remoteViews.setTextViewText(R.id.tvTraffic, String.format("▲ %.1f ▼ %.1f KiB/sec", txsec / 1024, rxsec / 1024));
|
2015-12-09 16:57:10 +00:00
|
|
|
else
|
2015-12-09 18:42:19 +00:00
|
|
|
remoteViews.setTextViewText(R.id.tvTraffic, String.format("▲ %.1f ▼ %.1f MiB/sec", txsec / 1024 / 1024, rxsec / 1024 / 1024));
|
2015-12-09 13:57:14 +00:00
|
|
|
|
|
|
|
// Show notification
|
|
|
|
Intent main = new Intent(SinkholeService.this, ActivityMain.class);
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(SinkholeService.this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(SinkholeService.this)
|
|
|
|
.setSmallIcon(R.drawable.ic_equalizer_white_24dp)
|
|
|
|
.setContent(remoteViews)
|
|
|
|
.setContentIntent(pi)
|
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
2015-12-09 18:25:40 +00:00
|
|
|
.setVisibility(Notification.VISIBILITY_PUBLIC)
|
2015-12-09 13:57:14 +00:00
|
|
|
.setPriority(Notification.PRIORITY_DEFAULT)
|
|
|
|
.setColor(ContextCompat.getColor(SinkholeService.this, R.color.colorPrimary))
|
2015-12-09 18:25:40 +00:00
|
|
|
.setOngoing(true);
|
2015-12-09 13:57:14 +00:00
|
|
|
NotificationManagerCompat.from(SinkholeService.this).notify(NOTIFY_TRAFFIC, builder.build());
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
private ParcelFileDescriptor startVPN() {
|
2015-11-15 09:43:06 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-10-25 15:16:20 +00:00
|
|
|
|
2015-11-04 12:04:27 +00:00
|
|
|
// Check state
|
2015-11-13 10:05:10 +00:00
|
|
|
boolean wifi = Util.isWifiActive(this);
|
2015-11-13 07:07:15 +00:00
|
|
|
boolean metered = Util.isMeteredNetwork(this);
|
2015-11-15 09:43:06 +00:00
|
|
|
boolean useMetered = prefs.getBoolean("use_metered", false);
|
2015-12-08 13:18:06 +00:00
|
|
|
String ssidHome = prefs.getString("wifi_home", "");
|
|
|
|
String ssidNetwork = Util.getWifiSSID(this);
|
2015-12-02 16:53:03 +00:00
|
|
|
String generation = Util.getNetworkGeneration(this);
|
2015-12-02 19:11:06 +00:00
|
|
|
boolean unmetered_2g = prefs.getBoolean("unmetered_2g", false);
|
|
|
|
boolean unmetered_3g = prefs.getBoolean("unmetered_3g", false);
|
|
|
|
boolean unmetered_4g = prefs.getBoolean("unmetered_4g", false);
|
2015-11-30 15:05:08 +00:00
|
|
|
boolean roaming = Util.isRoaming(SinkholeService.this);
|
|
|
|
boolean national = prefs.getBoolean("national_roaming", false);
|
2015-11-29 10:17:16 +00:00
|
|
|
boolean interactive = Util.isInteractive(this);
|
|
|
|
boolean telephony = Util.hasTelephony(this);
|
2015-11-25 19:05:17 +00:00
|
|
|
|
2015-11-25 20:09:00 +00:00
|
|
|
// Update connected state
|
|
|
|
last_connected = Util.isConnected(SinkholeService.this);
|
|
|
|
|
|
|
|
// Update metered state
|
2015-11-29 10:17:16 +00:00
|
|
|
if (wifi && (!useMetered || !telephony))
|
2015-11-25 20:09:00 +00:00
|
|
|
metered = false;
|
2015-12-08 13:18:06 +00:00
|
|
|
if (wifi && !ssidHome.equals("") && !ssidHome.equals(ssidNetwork))
|
|
|
|
metered = true;
|
2015-12-02 19:11:06 +00:00
|
|
|
if (unmetered_2g && "2G".equals(generation))
|
2015-12-02 16:53:03 +00:00
|
|
|
metered = false;
|
2015-12-02 19:11:06 +00:00
|
|
|
if (unmetered_3g && "3G".equals(generation))
|
2015-12-02 16:53:03 +00:00
|
|
|
metered = false;
|
2015-12-02 19:11:06 +00:00
|
|
|
if (unmetered_4g && "4G".equals(generation))
|
2015-12-02 16:53:03 +00:00
|
|
|
metered = false;
|
2015-11-27 07:27:29 +00:00
|
|
|
if (!last_connected)
|
|
|
|
metered = true;
|
2015-11-25 20:09:00 +00:00
|
|
|
last_metered = metered;
|
|
|
|
|
2015-11-25 19:05:17 +00:00
|
|
|
// Update roaming state
|
2015-11-30 15:05:08 +00:00
|
|
|
if (roaming && national)
|
|
|
|
roaming = Util.isInternational(this);
|
2015-11-25 19:05:17 +00:00
|
|
|
|
2015-11-25 20:09:00 +00:00
|
|
|
Log.i(TAG, "Starting connected=" + last_connected +
|
|
|
|
" wifi=" + wifi +
|
|
|
|
" metered=" + metered +
|
2015-11-29 10:17:16 +00:00
|
|
|
" telephony=" + telephony +
|
2015-12-02 16:53:03 +00:00
|
|
|
" generation=" + generation +
|
2015-11-30 15:05:08 +00:00
|
|
|
" roaming=" + roaming +
|
2015-11-08 16:01:23 +00:00
|
|
|
" interactive=" + interactive);
|
2015-10-29 07:48:28 +00:00
|
|
|
|
2015-10-25 15:16:20 +00:00
|
|
|
// Build VPN service
|
|
|
|
final Builder builder = new Builder();
|
2015-11-14 11:17:57 +00:00
|
|
|
builder.setSession(getString(R.string.app_name) + " session");
|
2015-11-08 16:06:12 +00:00
|
|
|
// TODO: make tunnel parameters configurable
|
2015-10-25 15:16:20 +00:00
|
|
|
builder.addAddress("10.1.10.1", 32);
|
2015-10-26 13:08:13 +00:00
|
|
|
builder.addAddress("fd00:1:fd00:1:fd00:1:fd00:1", 64);
|
2015-10-25 15:16:20 +00:00
|
|
|
builder.addRoute("0.0.0.0", 0);
|
2015-10-26 13:08:13 +00:00
|
|
|
builder.addRoute("0:0:0:0:0:0:0:0", 0);
|
2015-10-25 15:16:20 +00:00
|
|
|
|
|
|
|
// Add list of allowed applications
|
2015-11-18 18:33:48 +00:00
|
|
|
int nAllowed = 0;
|
|
|
|
int nBlocked = 0;
|
2015-11-01 13:16:57 +00:00
|
|
|
for (Rule rule : Rule.getRules(true, TAG, this)) {
|
2015-11-04 12:04:27 +00:00
|
|
|
boolean blocked = (metered ? rule.other_blocked : rule.wifi_blocked);
|
2015-11-21 08:48:33 +00:00
|
|
|
boolean screen = (metered ? rule.screen_other : rule.screen_wifi);
|
2015-11-30 15:05:08 +00:00
|
|
|
if ((!blocked || (screen && interactive)) && (!metered || !(rule.roaming && roaming))) {
|
2015-11-18 18:33:48 +00:00
|
|
|
nAllowed++;
|
2015-11-08 08:40:17 +00:00
|
|
|
if (debug)
|
|
|
|
Log.i(TAG, "Allowing " + rule.info.packageName);
|
2015-10-25 15:16:20 +00:00
|
|
|
try {
|
|
|
|
builder.addDisallowedApplication(rule.info.packageName);
|
2015-10-25 18:02:33 +00:00
|
|
|
} catch (PackageManager.NameNotFoundException ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-11-20 09:10:22 +00:00
|
|
|
Util.sendCrashReport(ex, this);
|
2015-10-25 15:16:20 +00:00
|
|
|
}
|
2015-11-18 18:33:48 +00:00
|
|
|
} else
|
|
|
|
nBlocked++;
|
2015-11-01 13:16:57 +00:00
|
|
|
}
|
2015-11-18 18:33:48 +00:00
|
|
|
Log.i(TAG, "Allowed=" + nAllowed + " blocked=" + nBlocked);
|
|
|
|
|
|
|
|
// Update notification
|
|
|
|
Notification notification = getForegroundNotification(nAllowed, nBlocked);
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
|
|
nm.notify(NOTIFY_FOREGROUND, notification);
|
2015-10-25 15:16:20 +00:00
|
|
|
|
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);
|
|
|
|
|
2015-10-29 22:29:01 +00:00
|
|
|
if (debug)
|
|
|
|
builder.setBlocking(true);
|
|
|
|
|
2015-10-25 15:16:20 +00:00
|
|
|
// Start VPN service
|
2015-11-23 09:58:05 +00:00
|
|
|
return builder.establish();
|
2015-10-25 15:16:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
private void stopVPN(ParcelFileDescriptor pfd) {
|
2015-10-25 15:16:20 +00:00
|
|
|
Log.i(TAG, "Stopping");
|
|
|
|
try {
|
2015-10-26 13:32:14 +00:00
|
|
|
pfd.close();
|
2015-10-25 18:02:33 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-11-20 09:10:22 +00:00
|
|
|
Util.sendCrashReport(ex, this);
|
2015-10-25 15:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
private void startDebug(final ParcelFileDescriptor pfd) {
|
2015-11-13 10:35:42 +00:00
|
|
|
if (pfd == null || !debug)
|
2015-10-29 22:29:01 +00:00
|
|
|
return;
|
|
|
|
|
2015-11-14 11:17:57 +00:00
|
|
|
debugThread = new Thread(new Runnable() {
|
2015-10-29 22:29:01 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-11-13 11:18:19 +00:00
|
|
|
FileInputStream in = null;
|
|
|
|
FileOutputStream out = null;
|
2015-10-29 22:29:01 +00:00
|
|
|
try {
|
2015-11-13 11:18:19 +00:00
|
|
|
in = new FileInputStream(pfd.getFileDescriptor());
|
|
|
|
out = new FileOutputStream(pfd.getFileDescriptor());
|
2015-10-29 22:29:01 +00:00
|
|
|
|
|
|
|
ByteBuffer buffer = ByteBuffer.allocate(32767);
|
|
|
|
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
|
|
|
|
Log.i(TAG, "Start receiving");
|
|
|
|
while (!Thread.currentThread().isInterrupted() &&
|
|
|
|
pfd.getFileDescriptor() != null &&
|
|
|
|
pfd.getFileDescriptor().valid())
|
|
|
|
try {
|
|
|
|
buffer.clear();
|
|
|
|
int length = in.read(buffer.array());
|
|
|
|
if (length > 0) {
|
|
|
|
buffer.limit(length);
|
|
|
|
Packet pkt = new Packet(buffer);
|
|
|
|
|
|
|
|
if (pkt.IPv4.protocol == Packet.IPv4Header.TCP && pkt.TCP.SYN) {
|
|
|
|
int uid = pkt.getUid4();
|
|
|
|
if (uid < 0)
|
|
|
|
Log.w(TAG, "uid not found");
|
|
|
|
|
|
|
|
String[] pkg = getPackageManager().getPackagesForUid(uid);
|
|
|
|
if (pkg == null)
|
|
|
|
pkg = new String[]{uid == 0 ? "root" : "unknown"};
|
|
|
|
|
|
|
|
Log.i(TAG, "Connect " + pkt.IPv4.destinationAddress + ":" + pkt.TCP.destinationPort + " uid=" + uid + " pkg=" + pkg[0]);
|
|
|
|
|
|
|
|
// Send RST
|
|
|
|
pkt.swapAddresses();
|
|
|
|
pkt.TCP.clearFlags();
|
|
|
|
pkt.TCP.RST = true;
|
|
|
|
long ack = pkt.TCP.acknowledgementNumber;
|
|
|
|
pkt.TCP.acknowledgementNumber = (pkt.TCP.sequenceNumber + 1) & 0xFFFFFFFFL;
|
|
|
|
pkt.TCP.sequenceNumber = (ack + 1) & 0xFFFFFFFFL;
|
|
|
|
pkt.send(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString());
|
2015-11-20 09:10:22 +00:00
|
|
|
Util.sendCrashReport(ex, SinkholeService.this);
|
2015-10-29 22:29:01 +00:00
|
|
|
}
|
|
|
|
Log.i(TAG, "End receiving");
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
2015-11-20 09:10:22 +00:00
|
|
|
Util.sendCrashReport(ex, SinkholeService.this);
|
2015-11-13 11:18:19 +00:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (in != null)
|
|
|
|
in.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (out != null)
|
|
|
|
out.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
2015-10-29 22:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-14 11:17:57 +00:00
|
|
|
}, getString(R.string.app_name) + " debug");
|
|
|
|
debugThread.start();
|
2015-10-29 22:29:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 07:57:36 +00:00
|
|
|
private void stopDebug() {
|
2015-11-14 11:17:57 +00:00
|
|
|
if (debugThread != null)
|
|
|
|
debugThread.interrupt();
|
2015-10-29 22:29:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 05:29:09 +00:00
|
|
|
private BroadcastReceiver interactiveStateReceiver = new BroadcastReceiver() {
|
2015-10-29 07:48:28 +00:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Log.i(TAG, "Received " + intent);
|
2015-11-20 10:34:23 +00:00
|
|
|
Util.logExtras(intent);
|
2015-12-02 16:53:03 +00:00
|
|
|
reload(null, "interactive state changed", SinkholeService.this);
|
2015-12-09 13:57:14 +00:00
|
|
|
|
|
|
|
// Start/stop stats
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
2015-12-09 18:42:19 +00:00
|
|
|
mServiceHandler.sendEmptyMessage(pm.isInteractive() ? MSG_STATS_START : MSG_STATS_STOP);
|
2015-10-29 07:48:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-23 10:43:25 +00:00
|
|
|
private BroadcastReceiver idleStateReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Log.i(TAG, "Received " + intent);
|
|
|
|
Util.logExtras(intent);
|
|
|
|
|
|
|
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
Log.i(TAG, "device idle=" + pm.isDeviceIdleMode());
|
|
|
|
|
2015-11-24 09:42:45 +00:00
|
|
|
// Reload rules when coming from idle mode
|
2015-11-23 10:43:25 +00:00
|
|
|
if (!pm.isDeviceIdleMode())
|
2015-12-02 16:53:03 +00:00
|
|
|
reload(null, "idle state changed", SinkholeService.this);
|
2015-11-23 10:43:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
private BroadcastReceiver connectivityChangedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-11-25 19:05:17 +00:00
|
|
|
// Filter VPN connectivity changes
|
2015-11-08 08:40:17 +00:00
|
|
|
int networkType = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_DUMMY);
|
|
|
|
if (!debug && networkType == ConnectivityManager.TYPE_VPN)
|
|
|
|
return;
|
|
|
|
|
2015-11-25 19:05:17 +00:00
|
|
|
// Reload rules
|
2015-10-25 09:29:49 +00:00
|
|
|
Log.i(TAG, "Received " + intent);
|
2015-11-20 10:34:23 +00:00
|
|
|
Util.logExtras(intent);
|
2015-12-02 16:53:03 +00:00
|
|
|
reload(null, "connectivity changed", SinkholeService.this);
|
2015-10-25 22:31:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-01 16:07:37 +00:00
|
|
|
private PhoneStateListener phoneStateListener = new PhoneStateListener() {
|
2015-12-02 16:53:03 +00:00
|
|
|
private String last_generation = null;
|
2015-12-06 12:57:02 +00:00
|
|
|
private int last_international = -1;
|
2015-12-02 16:53:03 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDataConnectionStateChanged(int state, int networkType) {
|
|
|
|
if (state == TelephonyManager.DATA_CONNECTED) {
|
2015-12-06 12:57:02 +00:00
|
|
|
String current_generation = Util.getNetworkGeneration(SinkholeService.this);
|
|
|
|
Log.i(TAG, "Data connected generation=" + current_generation);
|
2015-12-02 16:53:03 +00:00
|
|
|
|
|
|
|
if (last_generation == null || !last_generation.equals(current_generation)) {
|
|
|
|
Log.i(TAG, "New network generation=" + current_generation);
|
|
|
|
last_generation = current_generation;
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
|
2015-12-02 19:11:06 +00:00
|
|
|
if (prefs.getBoolean("unmetered_2g", false) ||
|
|
|
|
prefs.getBoolean("unmetered_3g", false) ||
|
|
|
|
prefs.getBoolean("unmetered_4g", false))
|
2015-12-02 16:53:03 +00:00
|
|
|
reload("other", "data connection state changed", SinkholeService.this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 16:07:37 +00:00
|
|
|
@Override
|
|
|
|
public void onServiceStateChanged(ServiceState serviceState) {
|
2015-12-02 06:48:29 +00:00
|
|
|
if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
|
2015-12-06 12:57:02 +00:00
|
|
|
int current_international = (Util.isInternational(SinkholeService.this) ? 1 : 0);
|
|
|
|
Log.i(TAG, "In service international=" + current_international);
|
2015-12-02 16:53:03 +00:00
|
|
|
|
2015-12-06 12:57:02 +00:00
|
|
|
if (last_international != current_international) {
|
|
|
|
Log.i(TAG, "New international=" + current_international);
|
|
|
|
last_international = current_international;
|
2015-12-02 16:53:03 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
|
|
|
|
if (prefs.getBoolean("national_roaming", false))
|
|
|
|
reload(null, "service state changed", SinkholeService.this);
|
2015-12-02 06:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 16:07:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-02 05:29:09 +00:00
|
|
|
private BroadcastReceiver packageAddedReceiver = new BroadcastReceiver() {
|
2015-10-25 22:31:00 +00:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Log.i(TAG, "Received " + intent);
|
2015-11-20 10:34:23 +00:00
|
|
|
Util.logExtras(intent);
|
2015-12-02 16:53:03 +00:00
|
|
|
reload(null, "package added", SinkholeService.this);
|
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
|
|
|
|
2015-11-14 11:17:57 +00:00
|
|
|
HandlerThread thread = new HandlerThread(getString(R.string.app_name) + " handler");
|
2015-11-08 07:36:05 +00:00
|
|
|
thread.start();
|
|
|
|
|
|
|
|
mServiceLooper = thread.getLooper();
|
|
|
|
mServiceHandler = new ServiceHandler(mServiceLooper);
|
|
|
|
|
2015-11-02 05:29:09 +00:00
|
|
|
// Listen for interactive state changes
|
|
|
|
IntentFilter ifInteractive = new IntentFilter();
|
|
|
|
ifInteractive.addAction(Intent.ACTION_SCREEN_ON);
|
|
|
|
ifInteractive.addAction(Intent.ACTION_SCREEN_OFF);
|
|
|
|
registerReceiver(interactiveStateReceiver, ifInteractive);
|
2015-10-29 07:48:28 +00:00
|
|
|
|
2015-11-23 10:43:25 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
// Listen for idle mode state changes
|
|
|
|
IntentFilter ifIdle = new IntentFilter();
|
|
|
|
ifIdle.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
|
|
|
|
registerReceiver(idleStateReceiver, ifIdle);
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:48:28 +00:00
|
|
|
// Listen for connectivity updates
|
|
|
|
IntentFilter ifConnectivity = new IntentFilter();
|
|
|
|
ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
|
|
registerReceiver(connectivityChangedReceiver, ifConnectivity);
|
|
|
|
|
2015-11-02 05:29:09 +00:00
|
|
|
// Listen for added applications
|
|
|
|
IntentFilter ifPackage = new IntentFilter();
|
|
|
|
ifPackage.addAction(Intent.ACTION_PACKAGE_ADDED);
|
|
|
|
ifPackage.addDataScheme("package");
|
|
|
|
registerReceiver(packageAddedReceiver, ifPackage);
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 07:36:05 +00:00
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2015-11-28 15:15:55 +00:00
|
|
|
// Keep awake
|
|
|
|
getLock(this).acquire();
|
2015-11-09 13:48:20 +00:00
|
|
|
|
2015-11-24 09:42:45 +00:00
|
|
|
// Handle service restart
|
|
|
|
if (intent == null) {
|
2015-11-24 09:54:43 +00:00
|
|
|
Log.i(TAG, "Restart");
|
|
|
|
|
2015-11-24 09:42:45 +00:00
|
|
|
// Get enabled
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", false);
|
|
|
|
|
|
|
|
// Recreate intent
|
|
|
|
intent = new Intent(this, SinkholeService.class);
|
|
|
|
intent.putExtra(EXTRA_COMMAND, enabled ? Command.start : Command.stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
Command cmd = (Command) intent.getSerializableExtra(EXTRA_COMMAND);
|
2015-12-02 16:53:03 +00:00
|
|
|
String reason = intent.getStringExtra(EXTRA_REASON);
|
|
|
|
Log.i(TAG, "Start intent=" + intent + " command=" + cmd + " reason=" + reason + " vpn=" + (vpn != null));
|
2015-11-08 07:36:05 +00:00
|
|
|
|
|
|
|
// Queue command
|
|
|
|
Message msg = mServiceHandler.obtainMessage();
|
|
|
|
msg.arg1 = startId;
|
|
|
|
msg.obj = intent;
|
2015-12-09 13:57:14 +00:00
|
|
|
msg.what = MSG_SERVICE_INTENT;
|
2015-11-08 07:36:05 +00:00
|
|
|
mServiceHandler.sendMessage(msg);
|
|
|
|
|
2015-11-24 09:42:45 +00:00
|
|
|
return START_STICKY;
|
2015-11-08 07:36:05 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 09:44:02 +00:00
|
|
|
@Override
|
|
|
|
public void onRevoke() {
|
|
|
|
Log.i(TAG, "Revoke");
|
|
|
|
|
|
|
|
// Disable firewall (will result in stop command)
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.edit().putBoolean("enabled", false).apply();
|
|
|
|
|
|
|
|
// Feedback
|
|
|
|
showDisabledNotification();
|
|
|
|
Widget.updateWidgets(this);
|
|
|
|
|
|
|
|
super.onRevoke();
|
|
|
|
}
|
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
Log.i(TAG, "Destroy");
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-11-08 08:40:17 +00:00
|
|
|
mServiceLooper.quit();
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-10-29 07:48:28 +00:00
|
|
|
unregisterReceiver(interactiveStateReceiver);
|
2015-11-23 10:43:25 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
unregisterReceiver(idleStateReceiver);
|
2015-11-02 05:29:09 +00:00
|
|
|
unregisterReceiver(connectivityChangedReceiver);
|
|
|
|
unregisterReceiver(packageAddedReceiver);
|
2015-10-25 15:28:41 +00:00
|
|
|
|
2015-12-01 16:07:37 +00:00
|
|
|
if (phone_state) {
|
|
|
|
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
|
2015-12-07 09:46:35 +00:00
|
|
|
phone_state = false;
|
|
|
|
}
|
|
|
|
|
2015-12-07 11:23:43 +00:00
|
|
|
if (subscriptionsChangedListener != null &&
|
|
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
|
2015-12-07 09:46:35 +00:00
|
|
|
SubscriptionManager sm = SubscriptionManager.from(this);
|
2015-12-07 11:23:43 +00:00
|
|
|
sm.removeOnSubscriptionsChangedListener((SubscriptionManager.OnSubscriptionsChangedListener) subscriptionsChangedListener);
|
|
|
|
subscriptionsChangedListener = null;
|
2015-12-01 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 08:40:17 +00:00
|
|
|
if (vpn != null) {
|
|
|
|
stopDebug();
|
|
|
|
stopVPN(vpn);
|
|
|
|
vpn = null;
|
|
|
|
}
|
2015-11-08 07:36:05 +00:00
|
|
|
|
2015-10-24 18:01:55 +00:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2015-11-18 18:33:48 +00:00
|
|
|
private Notification getForegroundNotification(int allowed, int blocked) {
|
2015-11-08 07:21:34 +00:00
|
|
|
Intent main = new Intent(this, ActivityMain.class);
|
2015-11-27 07:23:18 +00:00
|
|
|
PendingIntent pi = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
|
2015-11-08 07:21:34 +00:00
|
|
|
|
2015-11-18 18:33:48 +00:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
|
2015-11-08 07:21:34 +00:00
|
|
|
.setSmallIcon(R.drawable.ic_security_white_24dp)
|
|
|
|
.setContentTitle(getString(R.string.app_name))
|
|
|
|
.setContentText(getString(R.string.msg_started))
|
|
|
|
.setContentIntent(pi)
|
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
|
|
|
.setVisibility(Notification.VISIBILITY_SECRET)
|
2015-11-10 14:42:27 +00:00
|
|
|
.setPriority(Notification.PRIORITY_MIN)
|
2015-12-09 13:57:14 +00:00
|
|
|
.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
|
2015-11-08 07:21:34 +00:00
|
|
|
|
2015-11-18 18:33:48 +00:00
|
|
|
if (allowed > 0 || blocked > 0) {
|
|
|
|
NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
|
|
|
|
notification.bigText(getString(R.string.msg_started));
|
|
|
|
notification.setSummaryText(getString(R.string.msg_packages, allowed, blocked));
|
|
|
|
return notification.build();
|
|
|
|
} else
|
|
|
|
return builder.build();
|
2015-11-08 07:21:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 06:44:48 +00:00
|
|
|
private void showDisabledNotification() {
|
2015-11-08 07:21:34 +00:00
|
|
|
Intent main = new Intent(this, ActivityMain.class);
|
2015-11-27 07:23:18 +00:00
|
|
|
PendingIntent pi = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
|
2015-10-30 11:49:24 +00:00
|
|
|
|
|
|
|
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
|
2015-11-28 07:27:20 +00:00
|
|
|
.setSmallIcon(R.drawable.ic_error_white_24dp)
|
2015-10-30 11:49:24 +00:00
|
|
|
.setContentTitle(getString(R.string.app_name))
|
|
|
|
.setContentText(getString(R.string.msg_revoked))
|
2015-11-08 07:21:34 +00:00
|
|
|
.setContentIntent(pi)
|
2015-11-04 20:13:17 +00:00
|
|
|
.setCategory(Notification.CATEGORY_STATUS)
|
|
|
|
.setVisibility(Notification.VISIBILITY_SECRET)
|
|
|
|
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
|
2015-10-30 11:49:24 +00:00
|
|
|
.setAutoCancel(true);
|
|
|
|
|
2015-11-04 20:15:57 +00:00
|
|
|
NotificationManagerCompat.from(this).notify(NOTIFY_DISABLED, notification.build());
|
2015-11-01 06:44:48 +00:00
|
|
|
}
|
2015-10-30 11:49:24 +00:00
|
|
|
|
2015-11-01 06:44:48 +00:00
|
|
|
private void removeDisabledNotification() {
|
2015-11-04 20:15:57 +00:00
|
|
|
NotificationManagerCompat.from(this).cancel(NOTIFY_DISABLED);
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|
2015-10-25 22:04:10 +00:00
|
|
|
|
2015-12-02 16:53:03 +00:00
|
|
|
public static void start(String reason, Context context) {
|
2015-10-29 06:47:12 +00:00
|
|
|
Intent intent = new Intent(context, SinkholeService.class);
|
2015-10-26 16:23:41 +00:00
|
|
|
intent.putExtra(EXTRA_COMMAND, Command.start);
|
2015-12-02 16:53:03 +00:00
|
|
|
intent.putExtra(EXTRA_REASON, reason);
|
2015-10-26 16:23:41 +00:00
|
|
|
context.startService(intent);
|
|
|
|
}
|
|
|
|
|
2015-12-02 16:53:03 +00:00
|
|
|
public static void reload(String network, String reason, Context context) {
|
2015-11-04 22:45:47 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2015-11-15 09:43:06 +00:00
|
|
|
if (prefs.getBoolean("enabled", false)) {
|
2015-11-17 09:44:08 +00:00
|
|
|
boolean wifi = Util.isWifiActive(context);
|
2015-11-15 09:43:06 +00:00
|
|
|
boolean metered = Util.isMeteredNetwork(context);
|
2015-11-17 09:44:08 +00:00
|
|
|
if (wifi && !prefs.getBoolean("use_metered", false))
|
2015-11-15 09:43:06 +00:00
|
|
|
metered = false;
|
|
|
|
if (network == null || ("wifi".equals(network) ? !metered : metered)) {
|
2015-11-04 22:45:47 +00:00
|
|
|
Intent intent = new Intent(context, SinkholeService.class);
|
|
|
|
intent.putExtra(EXTRA_COMMAND, Command.reload);
|
2015-12-02 16:53:03 +00:00
|
|
|
intent.putExtra(EXTRA_REASON, reason);
|
2015-11-04 22:45:47 +00:00
|
|
|
context.startService(intent);
|
|
|
|
}
|
2015-11-15 09:43:06 +00:00
|
|
|
}
|
2015-10-25 22:04:10 +00:00
|
|
|
}
|
2015-10-26 16:23:41 +00:00
|
|
|
|
2015-12-02 16:53:03 +00:00
|
|
|
public static void stop(String reason, Context context) {
|
2015-10-29 06:47:12 +00:00
|
|
|
Intent intent = new Intent(context, SinkholeService.class);
|
2015-10-26 16:23:41 +00:00
|
|
|
intent.putExtra(EXTRA_COMMAND, Command.stop);
|
2015-12-02 16:53:03 +00:00
|
|
|
intent.putExtra(EXTRA_REASON, reason);
|
2015-10-26 16:23:41 +00:00
|
|
|
context.startService(intent);
|
|
|
|
}
|
2015-10-24 18:01:55 +00:00
|
|
|
}
|