Skip to content

Commit

Permalink
Fix inaccurate firewall grace period
Browse files Browse the repository at this point in the history
The timeout did not take into account the time spent during
the device deep sleep

Fixes #390
  • Loading branch information
emanuele-f committed Feb 7, 2024
1 parent 69f06c2 commit fb7adf4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static class PendingMessage {
type = _type;
msg = _msg;
port = _port;
pendingSince = SystemClock.uptimeMillis();
pendingSince = SystemClock.elapsedRealtime();
when = _now;
}
}
Expand Down Expand Up @@ -331,7 +331,7 @@ private void handleMessage(ConnectionDescriptor conn, MsgType type, byte[] messa
private synchronized void addPendingMessage(PendingMessage pending) {
// Purge unresolved connections (should not happen, just in case)
if(mPendingMessages.size() > 32) {
long now = SystemClock.uptimeMillis();
long now = SystemClock.elapsedRealtime();

for(int i = mPendingMessages.size()-1; i>=0; i--) {
ArrayList<PendingMessage> pp = mPendingMessages.valueAt(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void showQrCode(View dialog, String instId) {
return;
}
int timeout_ms = Integer.parseInt(timeout_s) * 1000;
long deadline = SystemClock.uptimeMillis() + timeout_ms;
long deadline = SystemClock.elapsedRealtime() + timeout_ms;
Log.d(TAG, "QR request_id=" + qr_req_id + ", timeout=" + timeout_ms + " ms");

// Step 2: generate QR code
Expand Down Expand Up @@ -357,7 +357,7 @@ private void onQrRequestReady(View dialog, Bitmap qrcode, long deadline) {
View qrLoading = dialog.findViewById(R.id.qr_code_loading);
View qrInfo = dialog.findViewById(R.id.qr_info_text);

mQrStartTime = SystemClock.uptimeMillis();
mQrStartTime = SystemClock.elapsedRealtime();
mQrDeadline = deadline;
updateQrProgress(dialog);

Expand All @@ -373,7 +373,7 @@ private void updateQrProgress(View dialog) {
return;

long interval = mQrDeadline - mQrStartTime;
int progress = Math.min((int)((SystemClock.uptimeMillis() - mQrStartTime) * 100 / interval), 100);
int progress = Math.min((int)((SystemClock.elapsedRealtime() - mQrStartTime) * 100 / interval), 100);
qrProgress.setProgress(100 - progress);

mHandler.postDelayed(() -> updateQrProgress(dialog), 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public Blocklist(Context ctx) {
}

public synchronized boolean unblockAppForMinutes(int uid, int minutes) {
Long old_val = mUidToGrace.put(uid, SystemClock.uptimeMillis() + (minutes * 60_000L));
Long old_val = mUidToGrace.put(uid, SystemClock.elapsedRealtime() + (minutes * 60_000L));
Log.i(TAG, "Grace app: " + uid + " for " + minutes + " minutes (old: " + old_val + ")");
return (old_val == null);
}

public synchronized boolean checkGracePeriods() {
long now = SystemClock.uptimeMillis();
long now = SystemClock.elapsedRealtime();
boolean changed = false;
Iterator<Map.Entry<Integer,Long>> iter = mUidToGrace.entrySet().iterator();

Expand Down

0 comments on commit fb7adf4

Please sign in to comment.