Selectively sinkhole in non filtering mode

This commit is contained in:
M66B 2021-02-21 14:36:26 +01:00
parent b0f3d9ce2c
commit 57d40a479f
2 changed files with 48 additions and 13 deletions

View File

@ -43,6 +43,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
public class Rule { public class Rule {
private static final String TAG = "NetGuard.Rule"; private static final String TAG = "NetGuard.Rule";
@ -445,6 +446,21 @@ public class Rule {
updateChanged(default_wifi, default_other, default_roaming); updateChanged(default_wifi, default_other, default_roaming);
} }
@Override
public boolean equals(Object obj) {
if (obj instanceof Rule) {
Rule other = (Rule) obj;
return (this.uid == other.uid &&
Objects.equals(this.packageName, other.packageName));
} else
return false;
}
@Override
public int hashCode() {
return (this.packageName == null ? this.uid : this.packageName.hashCode());
}
@Override @Override
public String toString() { public String toString() {
// This is used in the port forwarding dialog application selector // This is used in the port forwarding dialog application selector

View File

@ -1397,19 +1397,22 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
// Add list of allowed applications // Add list of allowed applications
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try { if (last_connected && !filter) {
builder.addDisallowedApplication(getPackageName()); for (Rule rule : listRule)
} catch (PackageManager.NameNotFoundException ex) { if (!listAllowed.contains(rule))
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); try {
} Log.i(TAG, "Sink=" + rule.packageName);
if (last_connected && !filter) builder.addAllowedApplication(rule.packageName);
for (Rule rule : listAllowed) } catch (PackageManager.NameNotFoundException ex) {
try { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
builder.addDisallowedApplication(rule.packageName); }
} catch (PackageManager.NameNotFoundException ex) { } else if (filter) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); try {
} builder.addDisallowedApplication(getPackageName());
else if (filter) } catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
for (Rule rule : listRule) for (Rule rule : listRule)
if (!rule.apply || (!system && rule.system)) if (!rule.apply || (!system && rule.system))
try { try {
@ -1418,6 +1421,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
} catch (PackageManager.NameNotFoundException ex) { } catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
} }
}
} }
// Build configure intent // Build configure intent
@ -3057,6 +3061,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
private List<String> listAddress = new ArrayList<>(); private List<String> listAddress = new ArrayList<>();
private List<String> listRoute = new ArrayList<>(); private List<String> listRoute = new ArrayList<>();
private List<InetAddress> listDns = new ArrayList<>(); private List<InetAddress> listDns = new ArrayList<>();
private List<String> listAllowed = new ArrayList<>();
private List<String> listDisallowed = new ArrayList<>(); private List<String> listDisallowed = new ArrayList<>();
private Builder() { private Builder() {
@ -3100,6 +3105,13 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
return this; return this;
} }
@Override
public Builder addAllowedApplication(String packageName) throws PackageManager.NameNotFoundException {
listAllowed.add(packageName);
super.addAllowedApplication(packageName);
return this;
}
@Override @Override
public Builder addDisallowedApplication(String packageName) throws PackageManager.NameNotFoundException { public Builder addDisallowedApplication(String packageName) throws PackageManager.NameNotFoundException {
listDisallowed.add(packageName); listDisallowed.add(packageName);
@ -3130,6 +3142,9 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
if (this.listDns.size() != other.listDns.size()) if (this.listDns.size() != other.listDns.size())
return false; return false;
if (this.listAllowed.size() != other.listAllowed.size())
return false;
if (this.listDisallowed.size() != other.listDisallowed.size()) if (this.listDisallowed.size() != other.listDisallowed.size())
return false; return false;
@ -3145,6 +3160,10 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
if (!other.listDns.contains(dns)) if (!other.listDns.contains(dns))
return false; return false;
for (String pkg : this.listAllowed)
if (!other.listAllowed.contains(pkg))
return false;
for (String pkg : this.listDisallowed) for (String pkg : this.listDisallowed)
if (!other.listDisallowed.contains(pkg)) if (!other.listDisallowed.contains(pkg))
return false; return false;