Skip to content

Commit

Permalink
Sync latest NetGuard changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kasnder committed Dec 20, 2024
1 parent 9bc65a6 commit 2446a29
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
11 changes: 9 additions & 2 deletions app/src/main/java/eu/faircode/netguard/ActivityLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,18 @@ private void updateAdapter() {
boolean other = prefs.getBoolean("proto_other", true);
boolean allowed = prefs.getBoolean("traffic_allowed", true);
boolean blocked = prefs.getBoolean("traffic_blocked", true);
adapter.changeCursor(DatabaseHelper.getInstance(this).getLog(udp, tcp, other, allowed, blocked));

String query = null;
if (menuSearch != null && menuSearch.isActionViewExpanded()) {
SearchView searchView = (SearchView) menuSearch.getActionView();
adapter.getFilter().filter(getUidForName(searchView.getQuery().toString()));
if (searchView != null)
query = getUidForName(searchView.getQuery().toString());
}

if (TextUtils.isEmpty(query))
adapter.changeCursor(DatabaseHelper.getInstance(this).getLog(udp, tcp, other, allowed, blocked));
else
adapter.changeCursor(DatabaseHelper.getInstance(ActivityLog.this).searchLog(query));
}
}

Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/eu/faircode/netguard/AdapterLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,21 @@ public void bindView(final View view, final Context context, final Cursor cursor
// Application icon
ApplicationInfo info = null;
PackageManager pm = context.getPackageManager();
String[] pkg = pm.getPackagesForUid(uid);
String[] pkg = null;

try {
pkg = pm.getPackagesForUid(uid);
} catch (SecurityException ignored) {
// STACK_TRACE=java.lang.SecurityException: getPackagesForUid: UID 1010154 requires android.permission.INTERACT_ACROSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS to access user 0.
// at android.os.Parcel.createExceptionOrNull(Parcel.java:3240)
// at android.os.Parcel.createException(Parcel.java:3224)
// at android.os.Parcel.readException(Parcel.java:3200)
// at android.os.Parcel.readException(Parcel.java:3142)
// at android.content.pm.IPackageManager$Stub$Proxy.getPackagesForUid(IPackageManager.java:5176)
// at android.app.ApplicationPackageManager$3.recompute(ApplicationPackageManager.java:1148)
// at android.app.ApplicationPackageManager$3.recompute(ApplicationPackageManager.java:1142)
}

if (pkg != null && pkg.length > 0)
try {
info = pm.getApplicationInfo(pkg[0], 0);
Expand Down
47 changes: 45 additions & 2 deletions app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,8 @@ public void onUnavailable() {
}

private void checkConnectivity(Network network, NetworkInfo ni, NetworkCapabilities capabilities) {
if (ni != null && capabilities != null &&
if (isActiveNetwork(network) &&
ni != null && capabilities != null &&
ni.getDetailedState() != NetworkInfo.DetailedState.SUSPENDED &&
ni.getDetailedState() != NetworkInfo.DetailedState.BLOCKED &&
ni.getDetailedState() != NetworkInfo.DetailedState.DISCONNECTED &&
Expand Down Expand Up @@ -2672,6 +2673,7 @@ private void listenNetworkChanges() {
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);

ConnectivityManager.NetworkCallback nc = new ConnectivityManager.NetworkCallback() {
private Network last_active = null;
private Network last_network = null;
private Boolean last_connected = null;
private Boolean last_metered = null;
Expand All @@ -2681,6 +2683,10 @@ private void listenNetworkChanges() {
@Override
public void onAvailable(Network network) {
Log.i(TAG, "Available network=" + network);
if (!isActiveNetwork(network))
return;

last_active = network;
last_connected = Util.isConnected(ServiceSinkhole.this);
last_metered = Util.isMeteredNetwork(ServiceSinkhole.this);
reload("network available", ServiceSinkhole.this, false);
Expand All @@ -2689,6 +2695,8 @@ public void onAvailable(Network network) {
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
Log.i(TAG, "Changed properties=" + network + " props=" + linkProperties);
if (!isActiveNetwork(network))
return;

// Make sure the right DNS servers are being used
List<InetAddress> dns = linkProperties.getDnsServers();
Expand All @@ -2707,6 +2715,8 @@ public void onLinkPropertiesChanged(Network network, LinkProperties linkProperti
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.i(TAG, "Changed capabilities=" + network + " caps=" + networkCapabilities);
if (!isActiveNetwork(network))
return;

boolean connected = Util.isConnected(ServiceSinkhole.this);
boolean metered = Util.isMeteredNetwork(ServiceSinkhole.this);
Expand Down Expand Up @@ -2745,7 +2755,11 @@ public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCa

@Override
public void onLost(Network network) {
Log.i(TAG, "Lost network=" + network);
Log.i(TAG, "Lost network=" + network + " active=" + isActiveNetwork(network));
if (last_active == null || !last_active.equals(network))
return;

last_active = null;
last_connected = Util.isConnected(ServiceSinkhole.this);
reload("network lost", ServiceSinkhole.this, false);
}
Expand Down Expand Up @@ -2784,6 +2798,35 @@ private void listenConnectivityChanges() {
}
}

private Network getActiveNetwork() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return cm.getActiveNetwork();

NetworkInfo ani = cm.getActiveNetworkInfo();
if (ani == null)
return null;

Network[] networks = cm.getAllNetworks();
for (Network network : networks) {
NetworkInfo ni = cm.getNetworkInfo(network);
if (ni == null)
continue;
if (ni.getType() == ani.getType() &&
ni.getSubtype() == ani.getSubtype())
return network;
}

return null;
}

private boolean isActiveNetwork(Network network) {
return (network != null && network.equals(getActiveNetwork()));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (state == State.enforcing)
Expand Down

0 comments on commit 2446a29

Please sign in to comment.