reimplement dns resolver cache + add timeout for cache
This commit is contained in:
parent
b28b1dd154
commit
c3bcaaa76b
|
@ -55,6 +55,7 @@ import eu.siacs.conversations.utils.CryptoHelper;
|
||||||
import eu.siacs.conversations.utils.CursorUtils;
|
import eu.siacs.conversations.utils.CursorUtils;
|
||||||
import eu.siacs.conversations.utils.FtsUtils;
|
import eu.siacs.conversations.utils.FtsUtils;
|
||||||
import eu.siacs.conversations.utils.MimeUtils;
|
import eu.siacs.conversations.utils.MimeUtils;
|
||||||
|
import eu.siacs.conversations.utils.Resolver;
|
||||||
import eu.siacs.conversations.xmpp.InvalidJid;
|
import eu.siacs.conversations.xmpp.InvalidJid;
|
||||||
import eu.siacs.conversations.xmpp.mam.MamReference;
|
import eu.siacs.conversations.xmpp.mam.MamReference;
|
||||||
import rocks.xmpp.addr.Jid;
|
import rocks.xmpp.addr.Jid;
|
||||||
|
@ -62,7 +63,7 @@ import rocks.xmpp.addr.Jid;
|
||||||
public class DatabaseBackend extends SQLiteOpenHelper {
|
public class DatabaseBackend extends SQLiteOpenHelper {
|
||||||
|
|
||||||
private static final String DATABASE_NAME = "history";
|
private static final String DATABASE_NAME = "history";
|
||||||
private static final int DATABASE_VERSION = 46;
|
private static final int DATABASE_VERSION = 47;
|
||||||
private static DatabaseBackend instance = null;
|
private static DatabaseBackend instance = null;
|
||||||
private static String CREATE_CONTATCS_STATEMENT = "create table "
|
private static String CREATE_CONTATCS_STATEMENT = "create table "
|
||||||
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
|
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
|
||||||
|
@ -149,6 +150,20 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
||||||
+ ") ON CONFLICT IGNORE"
|
+ ") 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_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_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 + ")";
|
private static String CREATE_MESSAGE_DELETED_INDEX = "create index message_deleted_index ON " + Message.TABLENAME + "(" + Message.DELETED + ")";
|
||||||
|
@ -243,6 +258,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
||||||
db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
|
db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
|
||||||
db.execSQL(CREATE_IDENTITIES_STATEMENT);
|
db.execSQL(CREATE_IDENTITIES_STATEMENT);
|
||||||
db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
|
db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
|
||||||
|
db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
|
||||||
db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
|
db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
|
||||||
db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
|
db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
|
||||||
db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
|
db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
|
||||||
|
@ -537,7 +553,12 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
||||||
Log.d(Config.LOGTAG,"deleted old edit information in "+diff+"ms");
|
Log.d(Config.LOGTAG,"deleted old edit information in "+diff+"ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
db.execSQL("DROP TABLE IF EXISTS resolver_results");
|
if (oldVersion < 47 && newVersion >= 47) {
|
||||||
|
// 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 void canonicalizeJids(SQLiteDatabase db) {
|
private void canonicalizeJids(SQLiteDatabase db) {
|
||||||
|
@ -660,6 +681,34 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
||||||
return result;
|
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) {
|
public void insertPresenceTemplate(PresenceTemplate template) {
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
String whereToDelete = PresenceTemplate.MESSAGE + "=?";
|
String whereToDelete = PresenceTemplate.MESSAGE + "=?";
|
||||||
|
|
|
@ -353,12 +353,14 @@ public class Resolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Result implements Comparable<Result>, Callable<Result> {
|
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 IP = "ip";
|
||||||
public static final String HOSTNAME = "hostname";
|
public static final String HOSTNAME = "hostname";
|
||||||
public static final String PORT = "port";
|
public static final String PORT = "port";
|
||||||
public static final String PRIORITY = "priority";
|
public static final String PRIORITY = "priority";
|
||||||
public static final String DIRECT_TLS = "directTls";
|
public static final String DIRECT_TLS = "directTls";
|
||||||
public static final String AUTHENTICATED = "authenticated";
|
public static final String AUTHENTICATED = "authenticated";
|
||||||
|
public static final String TIME_REQUESTED = "time_requested";
|
||||||
|
|
||||||
private InetAddress ip;
|
private InetAddress ip;
|
||||||
private DNSName hostname;
|
private DNSName hostname;
|
||||||
|
@ -366,12 +368,14 @@ public class Resolver {
|
||||||
private boolean directTls = false;
|
private boolean directTls = false;
|
||||||
private boolean authenticated = false;
|
private boolean authenticated = false;
|
||||||
private int priority;
|
private int priority;
|
||||||
|
private long timeRequested;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
|
|
||||||
private String logID = "";
|
private String logID = "";
|
||||||
|
|
||||||
static Result fromRecord(SRV srv, boolean directTls) {
|
static Result fromRecord(SRV srv, boolean directTls) {
|
||||||
Result result = new Result();
|
Result result = new Result();
|
||||||
|
result.timeRequested = System.currentTimeMillis();
|
||||||
result.port = srv.port;
|
result.port = srv.port;
|
||||||
result.hostname = srv.name;
|
result.hostname = srv.name;
|
||||||
result.directTls = directTls;
|
result.directTls = directTls;
|
||||||
|
@ -381,6 +385,7 @@ public class Resolver {
|
||||||
|
|
||||||
static Result createDefault(DNSName hostname, InetAddress ip, int port) {
|
static Result createDefault(DNSName hostname, InetAddress ip, int port) {
|
||||||
Result result = new Result();
|
Result result = new Result();
|
||||||
|
result.timeRequested = System.currentTimeMillis();
|
||||||
result.port = port;
|
result.port = port;
|
||||||
result.hostname = hostname;
|
result.hostname = hostname;
|
||||||
result.ip = ip;
|
result.ip = ip;
|
||||||
|
@ -425,6 +430,10 @@ public class Resolver {
|
||||||
return authenticated;
|
return authenticated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isOutdated() {
|
||||||
|
return (System.currentTimeMillis() - timeRequested) > 300_000;
|
||||||
|
}
|
||||||
|
|
||||||
public Socket getSocket() {
|
public Socket getSocket() {
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
@ -498,6 +507,24 @@ public class Resolver {
|
||||||
throw new Exception("Resolver.Result was not possible to connect - should be catched by executor");
|
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() {
|
public ContentValues toContentValues() {
|
||||||
final ContentValues contentValues = new ContentValues();
|
final ContentValues contentValues = new ContentValues();
|
||||||
contentValues.put(IP, ip == null ? null : ip.getAddress());
|
contentValues.put(IP, ip == null ? null : ip.getAddress());
|
||||||
|
@ -506,6 +533,7 @@ public class Resolver {
|
||||||
contentValues.put(PRIORITY, priority);
|
contentValues.put(PRIORITY, priority);
|
||||||
contentValues.put(DIRECT_TLS, directTls ? 1 : 0);
|
contentValues.put(DIRECT_TLS, directTls ? 1 : 0);
|
||||||
contentValues.put(AUTHENTICATED, authenticated ? 1 : 0);
|
contentValues.put(AUTHENTICATED, authenticated ? 1 : 0);
|
||||||
|
contentValues.put(TIME_REQUESTED, timeRequested);
|
||||||
return contentValues;
|
return contentValues;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -291,11 +291,17 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final String domain = account.getJid().getDomain();
|
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();
|
final boolean hardcoded = extended && !account.getHostname().isEmpty();
|
||||||
if (hardcoded) {
|
if (hardcoded) {
|
||||||
result = Resolver.fromHardCoded(account.getHostname(), account.getPort());
|
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);
|
result = Resolver.resolve(domain);
|
||||||
}
|
}
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
|
@ -322,6 +328,9 @@ public class XmppConnection implements Runnable {
|
||||||
localSocket.setSoTimeout(Config.SOCKET_TIMEOUT * 1000);
|
localSocket.setSoTimeout(Config.SOCKET_TIMEOUT * 1000);
|
||||||
if (startXmpp(localSocket)) {
|
if (startXmpp(localSocket)) {
|
||||||
localSocket.setSoTimeout(0); //reset to 0; once the connection is established we don’t want this
|
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
|
// successfully connected to server that speaks xmpp
|
||||||
} else {
|
} else {
|
||||||
FileBackend.close(localSocket);
|
FileBackend.close(localSocket);
|
||||||
|
|
Loading…
Reference in New Issue