WIP: happy eyeball with dns caching for 5min (#464)
* happy eyeball: fix dnssec for plain ip an srv-cname * reimplement dns resolver cache + add timeout for cache
This commit is contained in:
parent
3ff205a724
commit
dec454ab82
3 changed files with 97 additions and 9 deletions
src/main/java/de/pixart/messenger
|
@ -53,6 +53,7 @@ import de.pixart.messenger.services.ShortcutService;
|
|||
import de.pixart.messenger.utils.CryptoHelper;
|
||||
import de.pixart.messenger.utils.CursorUtils;
|
||||
import de.pixart.messenger.utils.FtsUtils;
|
||||
import de.pixart.messenger.utils.Resolver;
|
||||
import de.pixart.messenger.xmpp.InvalidJid;
|
||||
import de.pixart.messenger.xmpp.mam.MamReference;
|
||||
import rocks.xmpp.addr.Jid;
|
||||
|
@ -60,7 +61,7 @@ import rocks.xmpp.addr.Jid;
|
|||
public class DatabaseBackend extends SQLiteOpenHelper {
|
||||
|
||||
public static final String DATABASE_NAME = "history";
|
||||
public static final int DATABASE_VERSION = 50; // = Conversations DATABASE_VERSION + 4
|
||||
public static final int DATABASE_VERSION = 51; // = Conversations DATABASE_VERSION + 5
|
||||
private static DatabaseBackend instance = null;
|
||||
|
||||
private static String CREATE_CONTATCS_STATEMENT = "create table "
|
||||
|
@ -148,6 +149,20 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
|||
+ ") ON CONFLICT IGNORE"
|
||||
+ ");";
|
||||
|
||||
private static String RESOLVER_RESULTS_TABLENAME = "resolver_results";
|
||||
|
||||
private static String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
|
||||
+ Resolver.Result.DOMAIN + " TEXT,"
|
||||
+ Resolver.Result.HOSTNAME + " TEXT,"
|
||||
+ Resolver.Result.IP + " BLOB,"
|
||||
+ Resolver.Result.PRIORITY + " NUMBER,"
|
||||
+ Resolver.Result.DIRECT_TLS + " NUMBER,"
|
||||
+ Resolver.Result.AUTHENTICATED + " NUMBER,"
|
||||
+ Resolver.Result.PORT + " NUMBER,"
|
||||
+ Resolver.Result.TIME_REQUESTED + " NUMBER,"
|
||||
+ "UNIQUE(" + Resolver.Result.DOMAIN + ") ON CONFLICT REPLACE"
|
||||
+ ");";
|
||||
|
||||
private static String CREATE_MESSAGE_TIME_INDEX = "create INDEX message_time_index ON " + Message.TABLENAME + "(" + Message.TIME_SENT + ")";
|
||||
private static String CREATE_MESSAGE_CONVERSATION_INDEX = "create INDEX message_conversation_index ON " + Message.TABLENAME + "(" + Message.CONVERSATION + ")";
|
||||
private static String CREATE_MESSAGE_DELETED_INDEX = "create index message_deleted_index ON " + Message.TABLENAME + "(" + Message.DELETED + ")";
|
||||
|
@ -246,6 +261,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
|||
db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
|
||||
db.execSQL(CREATE_IDENTITIES_STATEMENT);
|
||||
db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
|
||||
db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
|
||||
db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
|
||||
db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
|
||||
db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
|
||||
|
@ -557,7 +573,11 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
|||
Log.d(Config.LOGTAG, "deleted old edit information in " + diff + "ms");
|
||||
}
|
||||
|
||||
db.execSQL("DROP TABLE IF EXISTS resolver_results");
|
||||
if (oldVersion < 51 && newVersion >= 51) {
|
||||
// values in resolver_result are cache and not worth to store
|
||||
db.execSQL("DROP TABLE IF EXISTS " + RESOLVER_RESULTS_TABLENAME);
|
||||
db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isColumnExisting(SQLiteDatabase db, String TableName, String ColumnName) {
|
||||
|
@ -694,6 +714,34 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
|||
return result;
|
||||
}
|
||||
|
||||
public void saveResolverResult(String domain, Resolver.Result result) {
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
ContentValues contentValues = result.toContentValues();
|
||||
contentValues.put(Resolver.Result.DOMAIN, domain);
|
||||
db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
|
||||
}
|
||||
|
||||
public synchronized Resolver.Result findResolverResult(String domain) {
|
||||
SQLiteDatabase db = this.getReadableDatabase();
|
||||
String where = Resolver.Result.DOMAIN + "=?";
|
||||
String[] whereArgs = {domain};
|
||||
final Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
|
||||
Resolver.Result result = null;
|
||||
if (cursor != null) {
|
||||
try {
|
||||
if (cursor.moveToFirst()) {
|
||||
result = Resolver.Result.fromCursor(cursor);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.d(Config.LOGTAG, "unable to find cached resolver result in database " + e.getMessage());
|
||||
return null;
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void insertPresenceTemplate(PresenceTemplate template) {
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
String whereToDelete = PresenceTemplate.MESSAGE + "=?";
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package de.pixart.messenger.utils;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
@ -168,6 +169,7 @@ public class Resolver {
|
|||
final Result result = new Result();
|
||||
result.ip = InetAddress.getByName(domain);
|
||||
result.port = port;
|
||||
result.authenticated = true;
|
||||
return result;
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -202,18 +204,20 @@ public class Resolver {
|
|||
}));
|
||||
fallbackThreads.add(new Thread(() -> {
|
||||
try {
|
||||
for (CNAME cname : resolveWithFallback(record.name, CNAME.class, result.isAuthenticData()).getAnswersOrEmptySet()) {
|
||||
final List<Result> ipv6s = resolveIp(record, cname.name, AAAA.class, result.isAuthenticData(), directTls);
|
||||
ResolverResult<CNAME> cnames = resolveWithFallback(record.name, CNAME.class, result.isAuthenticData());
|
||||
for (CNAME cname : cnames.getAnswersOrEmptySet()) {
|
||||
final List<Result> ipv6s = resolveIp(record, cname.name, AAAA.class, cnames.isAuthenticData(), directTls);
|
||||
synchronized (fallbackResults) {
|
||||
fallbackResults.addAll(ipv6s);
|
||||
}
|
||||
final List<Result> ipv4s = resolveIp(record, cname.name, A.class, result.isAuthenticData(), directTls);
|
||||
final List<Result> ipv4s = resolveIp(record, cname.name, A.class, cnames.isAuthenticData(), directTls);
|
||||
synchronized (results) {
|
||||
fallbackResults.addAll(ipv4s);
|
||||
}
|
||||
}
|
||||
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "cname in srv (agains RFC2782) - run slow fallback");
|
||||
} catch (Throwable throwable) {
|
||||
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving srv cname-fallback records", throwable);
|
||||
Log.i(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving srv cname-fallback records", throwable);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
@ -358,12 +362,14 @@ public class Resolver {
|
|||
}
|
||||
|
||||
public static class Result implements Comparable<Result>, Callable<Result> {
|
||||
public static final String DOMAIN = "domain";
|
||||
public static final String IP = "ip";
|
||||
public static final String HOSTNAME = "hostname";
|
||||
public static final String PORT = "port";
|
||||
public static final String PRIORITY = "priority";
|
||||
public static final String DIRECT_TLS = "directTls";
|
||||
public static final String AUTHENTICATED = "authenticated";
|
||||
public static final String TIME_REQUESTED = "time_requested";
|
||||
|
||||
private InetAddress ip;
|
||||
private DNSName hostname;
|
||||
|
@ -371,12 +377,14 @@ public class Resolver {
|
|||
private boolean directTls = false;
|
||||
private boolean authenticated = false;
|
||||
private int priority;
|
||||
private long timeRequested;
|
||||
private Socket socket;
|
||||
|
||||
private String logID = "";
|
||||
|
||||
static Result fromRecord(final SRV srv, final boolean directTls) {
|
||||
Result result = new Result();
|
||||
result.timeRequested = System.currentTimeMillis();
|
||||
result.port = srv.port;
|
||||
result.hostname = srv.name;
|
||||
result.directTls = directTls;
|
||||
|
@ -386,6 +394,7 @@ public class Resolver {
|
|||
|
||||
static Result createDefault(final DNSName hostname, final InetAddress ip, final int port) {
|
||||
Result result = new Result();
|
||||
result.timeRequested = System.currentTimeMillis();
|
||||
result.port = port;
|
||||
result.hostname = hostname;
|
||||
result.ip = ip;
|
||||
|
@ -430,6 +439,10 @@ public class Resolver {
|
|||
return authenticated;
|
||||
}
|
||||
|
||||
public boolean isOutdated() {
|
||||
return (System.currentTimeMillis() - timeRequested) > 300_000;
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
@ -506,6 +519,23 @@ public class Resolver {
|
|||
throw new Exception("Resolver.Result was not possible to connect - should be catched by executor");
|
||||
}
|
||||
|
||||
public static Result fromCursor(Cursor cursor) {
|
||||
final Result result = new Result();
|
||||
try {
|
||||
result.ip = InetAddress.getByAddress(cursor.getBlob(cursor.getColumnIndex(IP)));
|
||||
} catch (UnknownHostException e) {
|
||||
result.ip = null;
|
||||
}
|
||||
final String hostname = cursor.getString(cursor.getColumnIndex(HOSTNAME));
|
||||
result.hostname = hostname == null ? null : DNSName.from(hostname);
|
||||
result.port = cursor.getInt(cursor.getColumnIndex(PORT));
|
||||
result.directTls = cursor.getInt(cursor.getColumnIndex(DIRECT_TLS)) > 0;
|
||||
result.authenticated = cursor.getInt(cursor.getColumnIndex(AUTHENTICATED)) > 0;
|
||||
result.priority = cursor.getInt(cursor.getColumnIndex(PRIORITY));
|
||||
result.timeRequested = cursor.getLong(cursor.getColumnIndex(TIME_REQUESTED));
|
||||
return result;
|
||||
}
|
||||
|
||||
public ContentValues toContentValues() {
|
||||
final ContentValues contentValues = new ContentValues();
|
||||
contentValues.put(IP, ip == null ? null : ip.getAddress());
|
||||
|
@ -514,6 +544,7 @@ public class Resolver {
|
|||
contentValues.put(PRIORITY, priority);
|
||||
contentValues.put(DIRECT_TLS, directTls ? 1 : 0);
|
||||
contentValues.put(AUTHENTICATED, authenticated ? 1 : 0);
|
||||
contentValues.put(TIME_REQUESTED, timeRequested);
|
||||
return contentValues;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -317,11 +317,17 @@ public class XmppConnection implements Runnable {
|
|||
}
|
||||
} else {
|
||||
final String domain = account.getJid().getDomain();
|
||||
final Resolver.Result result;
|
||||
final Resolver.Result storedBackupResult = mXmppConnectionService.databaseBackend.findResolverResult(domain);
|
||||
Resolver.Result result = null;
|
||||
final boolean hardcoded = extended && !account.getHostname().isEmpty();
|
||||
if (hardcoded) {
|
||||
result = Resolver.fromHardCoded(account.getHostname(), account.getPort());
|
||||
} else {
|
||||
} else if (storedBackupResult != null && !storedBackupResult.isOutdated()) {
|
||||
storedBackupResult.connect();
|
||||
result = storedBackupResult;
|
||||
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": loaded backup resolver result from db: " + storedBackupResult);
|
||||
}
|
||||
if (result == null || result.getSocket() == null) {
|
||||
result = Resolver.resolve(domain);
|
||||
}
|
||||
if (result == null) {
|
||||
|
@ -348,6 +354,9 @@ public class XmppConnection implements Runnable {
|
|||
localSocket.setSoTimeout(Config.SOCKET_TIMEOUT * 1000);
|
||||
if (startXmpp(localSocket)) {
|
||||
localSocket.setSoTimeout(0); //reset to 0; once the connection is established we don’t want this
|
||||
if (!hardcoded && !result.equals(storedBackupResult)) {
|
||||
mXmppConnectionService.databaseBackend.saveResolverResult(domain, result);
|
||||
}
|
||||
// successfully connected to server that speaks xmpp
|
||||
} else {
|
||||
FileBackend.close(localSocket);
|
||||
|
@ -1994,4 +2003,4 @@ public class XmppConnection implements Runnable {
|
|||
return Config.USE_BOOKMARKS2 /* || hasDiscoFeature(account.getJid().asBareJid(), Namespace.BOOKMARKS2_COMPAT)*/;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue