Skip to content

Commit

Permalink
Fix blacklists update when the clock goes backward
Browse files Browse the repository at this point in the history
This fixes a possible corner case when the wall clock is set
back in time, which could cause the blacklists not to be
updated.

Closes #413
  • Loading branch information
emanuele-f committed Feb 18, 2024
1 parent 630e911 commit e502fe1
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions app/src/main/java/com/emanuelef/remote_capture/Blacklists.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.util.ArrayMap;

import androidx.collection.ArraySet;
Expand Down Expand Up @@ -56,7 +57,7 @@
*/
public class Blacklists {
public static final String PREF_BLACKLISTS_STATUS = "blacklists_status";
public static final int BLACKLISTS_UPDATE_SECONDS = 86400; // 1d
public static final long BLACKLISTS_UPDATE_MILLIS = 86400 * 1000; // 1d
private static final String TAG = "Blacklists";
private final ArrayList<BlacklistDescriptor> mLists = new ArrayList<>();
private final ArrayMap<String, BlacklistDescriptor> mListByFname = new ArrayMap<>();
Expand All @@ -66,11 +67,13 @@ public class Blacklists {
private boolean mUpdateInProgress;
private boolean mStopRequest;
private long mLastUpdate;
private long mLastUpdateMonotonic;
private int mNumDomainRules;
private int mNumIPRules;

public Blacklists(Context ctx) {
mLastUpdate = 0;
mLastUpdateMonotonic = -BLACKLISTS_UPDATE_MILLIS;
mNumDomainRules = 0;
mNumIPRules = 0;
mContext = ctx;
Expand Down Expand Up @@ -110,11 +113,15 @@ public void deserialize() {
String serialized = mPrefs.getString(PREF_BLACKLISTS_STATUS, "");
if(!serialized.isEmpty()) {
JsonObject obj = JsonParser.parseString(serialized).getAsJsonObject();

mLastUpdate = obj.getAsJsonPrimitive("last_update").getAsLong();
mNumDomainRules = obj.getAsJsonPrimitive("num_domain_rules").getAsInt();
mNumIPRules = obj.getAsJsonPrimitive("num_ip_rules").getAsInt();

// set the monotonic time based on the last update wall clock time
long millis_since_last_update = System.currentTimeMillis() - mLastUpdate;
if (millis_since_last_update > 0)
mLastUpdateMonotonic = SystemClock.elapsedRealtime() - millis_since_last_update;

JsonObject blacklists_obj = obj.getAsJsonObject("blacklists");
if(blacklists_obj != null) { // support old format
for(Map.Entry<String, JsonElement> bl_entry: blacklists_obj.entrySet()) {
Expand Down Expand Up @@ -179,7 +186,7 @@ private void checkFiles() {

if(!f.exists()) {
// must update
mLastUpdate = 0;
mLastUpdateMonotonic = -BLACKLISTS_UPDATE_MILLIS;
}
}

Expand All @@ -200,9 +207,9 @@ private void checkFiles() {
}

public boolean needsUpdate(boolean firstUpdate) {
long now = System.currentTimeMillis();
return((now - mLastUpdate) >= BLACKLISTS_UPDATE_SECONDS * 1000)
|| (firstUpdate && (getNumUpdatedBlacklists() < getNumBlacklists()));
long now = SystemClock.elapsedRealtime();
return (((now - mLastUpdateMonotonic) >= BLACKLISTS_UPDATE_MILLIS)
|| (firstUpdate && (getNumUpdatedBlacklists() < getNumBlacklists())));
}

// NOTE: invoked in a separate thread (CaptureService.mBlacklistsUpdateThread)
Expand Down Expand Up @@ -232,6 +239,7 @@ public void update() {
}

mLastUpdate = System.currentTimeMillis();
mLastUpdateMonotonic = SystemClock.elapsedRealtime();
notifyListeners();
}

Expand Down

0 comments on commit e502fe1

Please sign in to comment.