mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 14:49:53 +01:00
Run contact exchange task on IO executor.
This commit is contained in:
@@ -38,10 +38,15 @@ public interface ContactExchangeTask {
|
|||||||
*/
|
*/
|
||||||
String BOB_NONCE_LABEL = "org.briarproject.bramble.contact/BOB_NONCE";
|
String BOB_NONCE_LABEL = "org.briarproject.bramble.contact/BOB_NONCE";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Label for signing key binding nonces.
|
||||||
|
*/
|
||||||
|
String SIGNING_LABEL = "org.briarproject.briar.contact/EXCHANGE";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exchanges contact information with a remote peer.
|
* Exchanges contact information with a remote peer.
|
||||||
*/
|
*/
|
||||||
void startExchange(LocalAuthor localAuthor, SecretKey masterKey,
|
void exchangeContacts(LocalAuthor localAuthor, SecretKey masterKey,
|
||||||
DuplexTransportConnection conn, TransportId transportId,
|
DuplexTransportConnection conn, TransportId transportId,
|
||||||
boolean alice);
|
boolean alice);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ import java.util.logging.Logger;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.contact.RecordTypes.CONTACT_INFO;
|
import static org.briarproject.bramble.api.contact.RecordTypes.CONTACT_INFO;
|
||||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
|
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
@@ -54,13 +55,10 @@ import static org.briarproject.bramble.util.ValidationUtils.checkSize;
|
|||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
class ContactExchangeTaskImpl implements ContactExchangeTask {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(ContactExchangeTaskImpl.class.getName());
|
getLogger(ContactExchangeTaskImpl.class.getName());
|
||||||
|
|
||||||
private static final String SIGNING_LABEL_EXCHANGE =
|
|
||||||
"org.briarproject.briar.contact/EXCHANGE";
|
|
||||||
|
|
||||||
// Accept records with current protocol version, known record type
|
// Accept records with current protocol version, known record type
|
||||||
private static final Predicate<Record> ACCEPT = r ->
|
private static final Predicate<Record> ACCEPT = r ->
|
||||||
@@ -89,12 +87,6 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
private final StreamReaderFactory streamReaderFactory;
|
private final StreamReaderFactory streamReaderFactory;
|
||||||
private final StreamWriterFactory streamWriterFactory;
|
private final StreamWriterFactory streamWriterFactory;
|
||||||
|
|
||||||
private volatile LocalAuthor localAuthor;
|
|
||||||
private volatile DuplexTransportConnection conn;
|
|
||||||
private volatile TransportId transportId;
|
|
||||||
private volatile SecretKey masterKey;
|
|
||||||
private volatile boolean alice;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ContactExchangeTaskImpl(DatabaseComponent db, ClientHelper clientHelper,
|
ContactExchangeTaskImpl(DatabaseComponent db, ClientHelper clientHelper,
|
||||||
RecordReaderFactory recordReaderFactory,
|
RecordReaderFactory recordReaderFactory,
|
||||||
@@ -119,19 +111,9 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startExchange(LocalAuthor localAuthor, SecretKey masterKey,
|
public void exchangeContacts(LocalAuthor localAuthor,
|
||||||
DuplexTransportConnection conn, TransportId transportId,
|
SecretKey masterKey, DuplexTransportConnection conn,
|
||||||
boolean alice) {
|
TransportId transportId, boolean alice) {
|
||||||
this.localAuthor = localAuthor;
|
|
||||||
this.conn = conn;
|
|
||||||
this.transportId = transportId;
|
|
||||||
this.masterKey = masterKey;
|
|
||||||
this.alice = alice;
|
|
||||||
start();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
// Get the transport connection's input and output streams
|
// Get the transport connection's input and output streams
|
||||||
InputStream in;
|
InputStream in;
|
||||||
OutputStream out;
|
OutputStream out;
|
||||||
@@ -195,13 +177,11 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
if (alice) {
|
if (alice) {
|
||||||
sendContactInfo(recordWriter, localAuthor, localProperties,
|
sendContactInfo(recordWriter, localAuthor, localProperties,
|
||||||
localSignature, localTimestamp);
|
localSignature, localTimestamp);
|
||||||
recordWriter.flush();
|
|
||||||
remoteInfo = receiveContactInfo(recordReader);
|
remoteInfo = receiveContactInfo(recordReader);
|
||||||
} else {
|
} else {
|
||||||
remoteInfo = receiveContactInfo(recordReader);
|
remoteInfo = receiveContactInfo(recordReader);
|
||||||
sendContactInfo(recordWriter, localAuthor, localProperties,
|
sendContactInfo(recordWriter, localAuthor, localProperties,
|
||||||
localSignature, localTimestamp);
|
localSignature, localTimestamp);
|
||||||
recordWriter.flush();
|
|
||||||
}
|
}
|
||||||
// Send EOF on the outgoing stream
|
// Send EOF on the outgoing stream
|
||||||
streamWriter.sendEndOfStream();
|
streamWriter.sendEndOfStream();
|
||||||
@@ -227,8 +207,8 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Add the contact
|
// Add the contact
|
||||||
ContactId contactId = addContact(remoteInfo.author, timestamp,
|
ContactId contactId = addContact(remoteInfo.author, localAuthor,
|
||||||
remoteInfo.properties);
|
masterKey, timestamp, alice, remoteInfo.properties);
|
||||||
// Reuse the connection as a transport connection
|
// Reuse the connection as a transport connection
|
||||||
connectionManager.manageOutgoingConnection(contactId, transportId,
|
connectionManager.manageOutgoingConnection(contactId, transportId,
|
||||||
conn);
|
conn);
|
||||||
@@ -250,8 +230,7 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
|
|
||||||
private byte[] sign(LocalAuthor author, byte[] nonce) {
|
private byte[] sign(LocalAuthor author, byte[] nonce) {
|
||||||
try {
|
try {
|
||||||
return crypto.sign(SIGNING_LABEL_EXCHANGE, nonce,
|
return crypto.sign(SIGNING_LABEL, nonce, author.getPrivateKey());
|
||||||
author.getPrivateKey());
|
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException e) {
|
||||||
throw new AssertionError();
|
throw new AssertionError();
|
||||||
}
|
}
|
||||||
@@ -259,8 +238,8 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
|
|
||||||
private boolean verify(Author author, byte[] nonce, byte[] signature) {
|
private boolean verify(Author author, byte[] nonce, byte[] signature) {
|
||||||
try {
|
try {
|
||||||
return crypto.verifySignature(signature, SIGNING_LABEL_EXCHANGE,
|
return crypto.verifySignature(signature, SIGNING_LABEL, nonce,
|
||||||
nonce, author.getPublicKey());
|
author.getPublicKey());
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -274,6 +253,7 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
BdfList payload = BdfList.of(authorList, props, signature, timestamp);
|
BdfList payload = BdfList.of(authorList, props, signature, timestamp);
|
||||||
recordWriter.writeRecord(new Record(PROTOCOL_VERSION, CONTACT_INFO,
|
recordWriter.writeRecord(new Record(PROTOCOL_VERSION, CONTACT_INFO,
|
||||||
clientHelper.toByteArray(payload)));
|
clientHelper.toByteArray(payload)));
|
||||||
|
recordWriter.flush();
|
||||||
LOG.info("Sent contact info");
|
LOG.info("Sent contact info");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,7 +275,8 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
|
|||||||
return new ContactInfo(author, properties, signature, timestamp);
|
return new ContactInfo(author, properties, signature, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ContactId addContact(Author remoteAuthor, long timestamp,
|
private ContactId addContact(Author remoteAuthor, LocalAuthor localAuthor,
|
||||||
|
SecretKey masterKey, long timestamp, boolean alice,
|
||||||
Map<TransportId, TransportProperties> remoteProperties)
|
Map<TransportId, TransportProperties> remoteProperties)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
return db.transactionWithResult(false, txn -> {
|
return db.transactionWithResult(false, txn -> {
|
||||||
|
|||||||
@@ -14,11 +14,13 @@ import org.briarproject.bramble.api.identity.Author;
|
|||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -36,6 +38,10 @@ public class ContactExchangeActivity extends KeyAgreementActivity implements
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(ContactExchangeActivity.class.getName());
|
Logger.getLogger(ContactExchangeActivity.class.getName());
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@IoExecutor
|
||||||
|
Executor ioExecutor;
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
// Fields that are accessed from background threads must be volatile
|
||||||
@Inject
|
@Inject
|
||||||
volatile ContactExchangeTask contactExchangeTask;
|
volatile ContactExchangeTask contactExchangeTask;
|
||||||
@@ -63,12 +69,12 @@ public class ContactExchangeActivity extends KeyAgreementActivity implements
|
|||||||
@Override
|
@Override
|
||||||
protected void onStop() {
|
protected void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
// Stop listen to updates from contactExchangeTask
|
// Stop listening to updates from contactExchangeTask
|
||||||
eventBus.addListener(this);
|
eventBus.addListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startContactExchange(KeyAgreementResult result) {
|
private void startContactExchange(KeyAgreementResult result) {
|
||||||
runOnDbThread(() -> {
|
ioExecutor.execute(() -> {
|
||||||
LocalAuthor localAuthor;
|
LocalAuthor localAuthor;
|
||||||
// Load the local pseudonym
|
// Load the local pseudonym
|
||||||
try {
|
try {
|
||||||
@@ -78,9 +84,8 @@ public class ContactExchangeActivity extends KeyAgreementActivity implements
|
|||||||
contactExchangeFailed();
|
contactExchangeFailed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exchange contact details
|
// Exchange contact details
|
||||||
contactExchangeTask.startExchange(localAuthor,
|
contactExchangeTask.exchangeContacts(localAuthor,
|
||||||
result.getMasterKey(), result.getConnection(),
|
result.getMasterKey(), result.getConnection(),
|
||||||
result.getTransportId(), result.wasAlice());
|
result.getTransportId(), result.wasAlice());
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user