From 2dcaccd75a00bb417c4b6b5e8f7c71b841e20a3e Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 4 Feb 2016 16:43:06 +0100 Subject: [PATCH] Add root to application list Shell is already part of the list Fixes #283 --- .../faircode/netguard/ActivitySettings.java | 5 +- .../main/java/eu/faircode/netguard/Rule.java | 54 ++++++++++++------- .../eu/faircode/netguard/SinkholeService.java | 4 +- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java index 21e39e92..20139e1b 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java +++ b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java @@ -1095,7 +1095,10 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere int block = Integer.parseInt(attributes.getValue("block")); try { - packet.uid = getPackageManager().getApplicationInfo(pkg, 0).uid; + if ("root".equals(pkg)) + packet.uid = 0; + else + packet.uid = getPackageManager().getApplicationInfo(pkg, 0).uid; // This assumes ordered export if (!listUid.contains(packet.uid)) { diff --git a/app/src/main/java/eu/faircode/netguard/Rule.java b/app/src/main/java/eu/faircode/netguard/Rule.java index dd703f55..d48e246e 100644 --- a/app/src/main/java/eu/faircode/netguard/Rule.java +++ b/app/src/main/java/eu/faircode/netguard/Rule.java @@ -22,6 +22,7 @@ package eu.faircode.netguard; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.XmlResourceParser; @@ -49,6 +50,7 @@ public class Rule { public boolean system; public boolean internet; public boolean enabled; + public Intent intent; public boolean wifi_default; public boolean other_default; @@ -70,31 +72,37 @@ public class Rule { public boolean changed; - public Intent intent; - public boolean expanded = false; private Rule(PackageInfo info, Context context) { PackageManager pm = context.getPackageManager(); this.info = info; - this.name = info.applicationInfo.loadLabel(pm).toString(); - this.system = Util.isSystem(info.packageName, context); - this.internet = Util.hasInternet(info.packageName, context); + if (info.applicationInfo.uid == 0) { + this.name = context.getString(R.string.title_root); + this.system = true; + this.internet = true; + this.enabled = true; + this.intent = null; + } else { + this.name = info.applicationInfo.loadLabel(pm).toString(); + this.system = Util.isSystem(info.packageName, context); + this.internet = Util.hasInternet(info.packageName, context); - int setting; - try { - setting = pm.getApplicationEnabledSetting(info.packageName); - } catch (IllegalArgumentException ex) { - setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; - Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + int setting; + try { + setting = pm.getApplicationEnabledSetting(info.packageName); + } catch (IllegalArgumentException ex) { + setting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; + Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + } + if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) + this.enabled = info.applicationInfo.enabled; + else + this.enabled = (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED); + + this.intent = pm.getLaunchIntentForPackage(info.packageName); } - if (setting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) - this.enabled = info.applicationInfo.enabled; - else - this.enabled = (setting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED); - - this.intent = pm.getLaunchIntentForPackage(info.packageName); } public static List getRules(boolean all, String tag, Context context) { @@ -168,8 +176,18 @@ public class Rule { // Build rule list List listRules = new ArrayList<>(); + List listPI = context.getPackageManager().getInstalledPackages(0); - for (PackageInfo info : context.getPackageManager().getInstalledPackages(0)) { + PackageInfo root = new PackageInfo(); + root.packageName = "root"; + root.versionCode = 0; + root.versionName = "0"; + root.applicationInfo = new ApplicationInfo(); + root.applicationInfo.uid = 0; + root.applicationInfo.icon = 0; + listPI.add(root); + + for (PackageInfo info : listPI) { Rule rule = new Rule(info, context); if (pre_system.containsKey(info.packageName)) diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index 5d6a5e1c..d782b73c 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -680,7 +680,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS dh.insertLog(packet, dname, (last_connected ? last_metered ? 2 : 1 : 0), last_interactive); // Application log - if (log_app && packet.uid > 0) { + if (log_app && packet.uid >= 0) { if (dh.updateAccess(packet, dname, -1)) if (notify && prefs.getBoolean("notify_" + packet.uid, true) && (system || !Util.isSystem(packet.uid, SinkholeService.this))) @@ -1094,7 +1094,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS if (packet.protocol == 6 /* TCP */ || packet.protocol == 17 /* UDP */ || packet.protocol == 1 /* ICMPv4 */ || packet.protocol == 58 /* ICMPv6 */) { if (prefs.getBoolean("filter", false)) { - if (packet.uid <= 0) // unknown, root + if (packet.uid < 0) // unknown packet.allowed = true; else { boolean filtered = false;