Skip to content

Commit

Permalink
Improve blacklists update process
Browse files Browse the repository at this point in the history
- Stop blacklist download if it takes more than 10 sec
- Abort downloads if capture is stopped
- Update the UI status during each individual download

Closes emanuele-f#224
  • Loading branch information
emanuele-f committed Dec 18, 2022
1 parent 68bd85b commit 5537727
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
14 changes: 9 additions & 5 deletions app/src/main/java/com/emanuelef/remote_capture/Blacklists.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public class Blacklists {
private final ArrayList<BlacklistsStateListener> mListeners = new ArrayList<>();
private final SharedPreferences mPrefs;
private final Context mContext;
private boolean mFirstUpdate;
private boolean mUpdateInProgress;
private long mLastUpdate;
private int mNumDomainRules;
Expand All @@ -74,7 +73,6 @@ public Blacklists(Context ctx) {
mNumDomainRules = 0;
mNumIPRules = 0;
mContext = ctx;
mFirstUpdate = true;
mUpdateInProgress = false;
mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);

Expand Down Expand Up @@ -200,10 +198,10 @@ private void checkFiles() {
}
}

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

// NOTE: invoked in a separate thread (CaptureService.mBlacklistsUpdateThread)
Expand All @@ -214,15 +212,21 @@ public void update() {
notifyListeners();

Log.i(TAG, "Updating " + mLists.size() + " blacklists...");
mFirstUpdate = false;

for(BlacklistDescriptor bl: mLists) {
if(!CaptureService.isServiceActive()) {
Log.i(TAG, "Capture stopped, abort");
break;
}

Log.i(TAG, "\tupdating " + bl.fname + "...");

if(Utils.downloadFile(bl.url, getListPath(bl)))
bl.setUpdated(System.currentTimeMillis());
else
bl.setOutdated();

notifyListeners();
}

mLastUpdate = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ else if(mSettings.dump_mode == Prefs.DumpMode.PCAP_FILE) {

mMalwareWhitelist = PCAPdroid.getInstance().getMalwareWhitelist();
mBlacklists = PCAPdroid.getInstance().getBlacklists();
if(mMalwareDetectionEnabled && !mBlacklists.needsUpdate())
if(mMalwareDetectionEnabled && !mBlacklists.needsUpdate(true))
reloadBlacklists();
checkBlacklistsUpdates();
checkBlacklistsUpdates(true);

mBlocklist = PCAPdroid.getInstance().getBlocklist();
mFirewallWhitelist = PCAPdroid.getInstance().getFirewallWhitelist();
Expand Down Expand Up @@ -876,11 +876,11 @@ public static boolean isLockdownVPN() {
return ((INSTANCE != null) && INSTANCE.isLockdownEnabled());
}

private void checkBlacklistsUpdates() {
private void checkBlacklistsUpdates(boolean firstUpdate) {
if(!mMalwareDetectionEnabled || (mBlacklistsUpdateThread != null))
return;

if(mBlacklistsUpdateRequested || mBlacklists.needsUpdate()) {
if(mBlacklistsUpdateRequested || mBlacklists.needsUpdate(firstUpdate)) {
mBlacklistsUpdateThread = new Thread(this::updateBlacklistsWork, "Blacklists Update");
mBlacklistsUpdateThread.start();
}
Expand Down Expand Up @@ -1074,7 +1074,7 @@ private void connUpdateWork() {
ConnectionDescriptor[] new_conns = item.first;
ConnectionUpdate[] conns_updates = item.second;

checkBlacklistsUpdates();
checkBlacklistsUpdates(false);
if(mBlocklist.checkGracePeriods())
mHandler.post(this::reloadBlocklist);

Expand Down Expand Up @@ -1197,7 +1197,7 @@ public String getDnsServer() {
return(dns_server);
}

public String getIpv6DnsServer() { return(Prefs.getDnsServerV4(mPrefs)); }
public String getIpv6DnsServer() { return(Prefs.getDnsServerV6(mPrefs)); }

public int getSocks5Enabled() { return mSocks5Enabled ? 1 : 0; }

Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/emanuelef/remote_capture/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -950,6 +951,8 @@ public static boolean downloadFile(String _url, String path) {
try {
// Necessary otherwise the connection will stay open
con.setRequestProperty("Connection", "Close");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);

try(InputStream in = new BufferedInputStream(con.getInputStream())) {
byte[] bytesIn = new byte[4096];
Expand All @@ -958,6 +961,8 @@ public static boolean downloadFile(String _url, String path) {
bos.write(bytesIn, 0, read);
has_contents |= (read > 0);
}
} catch (SocketTimeoutException _ignored) {
Log.w(TAG, "Timeout while fetching " + _url);
}
} finally {
con.disconnect();
Expand All @@ -971,7 +976,7 @@ public static boolean downloadFile(String _url, String path) {
try {
//noinspection ResultOfMethodCallIgnored
(new File(path + ".tmp")).delete(); // if exists
} catch (Exception e) {
} catch (Exception ignored) {
// ignore
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public class MalwareStatusFragment extends Fragment implements MenuProvider {
private Blacklists mBlacklists;
private Handler mHandler;
private ImageView mStatusIcon;
private View mConnectionsCard;
private TextView mStatus;
private TextView mNumMalicious;
private TextView mNumUpToDate;
Expand All @@ -81,7 +80,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mStatus = view.findViewById(R.id.status);
mHandler = new Handler(Looper.getMainLooper());
mStatusIcon = view.findViewById(R.id.status_icon);
mConnectionsCard = view.findViewById(R.id.show_connections);
View mConnectionsCard = view.findViewById(R.id.show_connections);
mNumMalicious = view.findViewById(R.id.num_malicious);
mNumUpToDate = view.findViewById(R.id.num_up_to_date);
mNumChecked = view.findViewById(R.id.num_checked);
Expand Down Expand Up @@ -150,6 +149,10 @@ private void updateStatus() {
mStatusIcon.setImageResource(R.drawable.ic_bug);
mStatusIcon.setColorFilter(mGrayColor);
mStatus.setText(R.string.capture_not_running_status);
} else if(mBlacklists.isUpdateInProgress()) {
mStatusIcon.setImageResource(R.drawable.ic_exclamation_triangle_solid);
mStatusIcon.setColorFilter(mGrayColor);
mStatus.setText(R.string.status_updating);
} else if(mBlacklists.getNumUpdatedBlacklists() < mBlacklists.getNumBlacklists()) {
mStatusIcon.setImageResource(R.drawable.ic_exclamation_triangle_solid);
mStatusIcon.setColorFilter(mWarnColor);
Expand Down

0 comments on commit 5537727

Please sign in to comment.