Optionally clear DNS cache on connectivity changes

This commit is contained in:
M66B
2024-06-22 07:36:06 +02:00
parent f2502b181e
commit 3783689572
6 changed files with 63 additions and 4 deletions

View File

@@ -33,7 +33,9 @@ import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import org.minidns.AbstractDnsClient;
import org.minidns.DnsCache;
import org.minidns.DnsClient;
import org.minidns.cache.LruCache;
import org.minidns.dane.DaneVerifier;
import org.minidns.dnsmessage.DnsMessage;
import org.minidns.dnsqueryresult.DnsQueryResult;
@@ -465,6 +467,26 @@ public class DnsHelper {
return result;
}
static void clear(Context context) {
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
for (String key : prefs.getAll().keySet())
if (key != null && key.startsWith("dns."))
editor.remove(key);
editor.apply();
for (ResolverApi resolver : new ResolverApi[]{DnssecResolverApi.INSTANCE, ResolverApi.INSTANCE}) {
AbstractDnsClient client = resolver.getClient();
DnsCache cache = client.getCache();
if (cache instanceof LruCache)
((LruCache) cache).clear();
}
} catch (Throwable ex) {
Log.e(ex);
}
}
static boolean hasDnsSec() {
if (BuildConfig.PLAY_STORE_RELEASE)
return false;