Skip to content

Commit

Permalink
Selectively sinkhole in non filtering mode
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Feb 21, 2021
1 parent b0f3d9c commit 57d40a4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/eu/faircode/netguard/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

public class Rule {
private static final String TAG = "NetGuard.Rule";
Expand Down Expand Up @@ -445,6 +446,21 @@ public void updateChanged(Context context) {
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
public String toString() {
// This is used in the port forwarding dialog application selector
Expand Down
45 changes: 32 additions & 13 deletions app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java
Original file line number Diff line number Diff line change
Expand Up @@ -1397,19 +1397,22 @@ private Builder getBuilder(List<Rule> listAllowed, List<Rule> listRule) {

// Add list of allowed applications
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
builder.addDisallowedApplication(getPackageName());
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
if (last_connected && !filter)
for (Rule rule : listAllowed)
try {
builder.addDisallowedApplication(rule.packageName);
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
else if (filter)
if (last_connected && !filter) {
for (Rule rule : listRule)
if (!listAllowed.contains(rule))
try {
Log.i(TAG, "Sink=" + rule.packageName);
builder.addAllowedApplication(rule.packageName);
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
} else if (filter) {
try {
builder.addDisallowedApplication(getPackageName());
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}

for (Rule rule : listRule)
if (!rule.apply || (!system && rule.system))
try {
Expand All @@ -1418,6 +1421,7 @@ else if (filter)
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
}

// Build configure intent
Expand Down Expand Up @@ -3057,6 +3061,7 @@ private class Builder extends VpnService.Builder {
private List<String> listAddress = new ArrayList<>();
private List<String> listRoute = new ArrayList<>();
private List<InetAddress> listDns = new ArrayList<>();
private List<String> listAllowed = new ArrayList<>();
private List<String> listDisallowed = new ArrayList<>();

private Builder() {
Expand Down Expand Up @@ -3100,6 +3105,13 @@ public Builder addDnsServer(InetAddress address) {
return this;
}

@Override
public Builder addAllowedApplication(String packageName) throws PackageManager.NameNotFoundException {
listAllowed.add(packageName);
super.addAllowedApplication(packageName);
return this;
}

@Override
public Builder addDisallowedApplication(String packageName) throws PackageManager.NameNotFoundException {
listDisallowed.add(packageName);
Expand Down Expand Up @@ -3130,6 +3142,9 @@ public boolean equals(Object obj) {
if (this.listDns.size() != other.listDns.size())
return false;

if (this.listAllowed.size() != other.listAllowed.size())
return false;

if (this.listDisallowed.size() != other.listDisallowed.size())
return false;

Expand All @@ -3145,6 +3160,10 @@ public boolean equals(Object obj) {
if (!other.listDns.contains(dns))
return false;

for (String pkg : this.listAllowed)
if (!other.listAllowed.contains(pkg))
return false;

for (String pkg : this.listDisallowed)
if (!other.listDisallowed.contains(pkg))
return false;
Expand Down

0 comments on commit 57d40a4

Please sign in to comment.