Custodian closes tcp socket following error

This commit is contained in:
ameba23
2021-04-21 15:19:36 +02:00
parent 5420204703
commit 741c9b63d9
2 changed files with 12 additions and 21 deletions

View File

@@ -225,7 +225,7 @@ class OwnerReturnShardViewModel extends AndroidViewModel
} }
} }
// TODO figure out how to actually use a set for these objects // TODO figure out how to actually use a hash set for these objects
public boolean addToShardSet(ReturnShardPayload toAdd) { public boolean addToShardSet(ReturnShardPayload toAdd) {
boolean found = false; boolean found = false;
for (ReturnShardPayload returnShardPayload : recoveredShards) { for (ReturnShardPayload returnShardPayload : recoveredShards) {

View File

@@ -5,8 +5,6 @@ import org.briarproject.bramble.api.crypto.AgreementPublicKey;
import org.briarproject.bramble.api.crypto.AuthenticatedCipher; import org.briarproject.bramble.api.crypto.AuthenticatedCipher;
import org.briarproject.bramble.api.crypto.CryptoComponent; import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.data.BdfList; import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.transport.StreamReaderFactory;
import org.briarproject.bramble.api.transport.StreamWriterFactory;
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask; import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
import java.io.DataInputStream; import java.io.DataInputStream;
@@ -34,20 +32,15 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
private Socket socket; private Socket socket;
private final AuthenticatedCipher cipher; private final AuthenticatedCipher cipher;
private byte[] payload; private byte[] payload;
// private final StreamReaderFactory streamReaderFactory;
// private final StreamWriterFactory streamWriterFactory;
private static final Logger LOG = private static final Logger LOG =
getLogger(CustodianTaskImpl.class.getName()); getLogger(CustodianTaskImpl.class.getName());
@Inject @Inject
CustodianTaskImpl(CryptoComponent crypto, ClientHelper clientHelper, CustodianTaskImpl(CryptoComponent crypto, ClientHelper clientHelper,
AuthenticatedCipher cipher, StreamReaderFactory streamReaderFactory, AuthenticatedCipher cipher) {
StreamWriterFactory streamWriterFactory) {
super(cipher, crypto); super(cipher, crypto);
this.clientHelper = clientHelper; this.clientHelper = clientHelper;
// this.streamReaderFactory = streamReaderFactory;
// this.streamWriterFactory = streamWriterFactory;
this.cipher = cipher; this.cipher = cipher;
} }
@@ -62,12 +55,10 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
@Override @Override
public void cancel() { public void cancel() {
cancelled = true; cancelled = true;
if (socket != null) { if (socket != null && !socket.isClosed()) {
try { try {
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
// The reason here is OTHER rather than NO_CONNECTION because
// the socket could fail to close because it is already closed
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.OTHER)); State.Failure.Reason.OTHER));
} }
@@ -122,11 +113,6 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
outputStream.write(payloadEncrypted); outputStream.write(payloadEncrypted);
// OutputStream encryptedOutputStream = streamWriterFactory
// .createContactExchangeStreamWriter(outputStream,
// sharedSecret).getOutputStream();
// encryptedOutputStream.write(payload);
LOG.info("Written payload"); LOG.info("Written payload");
observer.onStateChanged(new CustodianTask.State.ReceivingAck()); observer.onStateChanged(new CustodianTask.State.ReceivingAck());
@@ -140,12 +126,13 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
LOG.warning("IO Error connecting to secret owner " + e.getMessage()); LOG.warning("IO Error connecting to secret owner " + e.getMessage());
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.QR_CODE_INVALID)); State.Failure.Reason.QR_CODE_INVALID));
closeSocket();
return; return;
// }
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
LOG.warning("Security error "+ e.getMessage()); LOG.warning("Security error "+ e.getMessage());
observer.onStateChanged(new CustodianTask.State.Failure( observer.onStateChanged(new CustodianTask.State.Failure(
State.Failure.Reason.OTHER)); State.Failure.Reason.OTHER));
closeSocket();
return; return;
} }
receiveAck(); receiveAck();
@@ -154,9 +141,6 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
private void receiveAck() { private void receiveAck() {
try { try {
DataInputStream inputStream = new DataInputStream(socket.getInputStream()); DataInputStream inputStream = new DataInputStream(socket.getInputStream());
// InputStream inputStream = streamReaderFactory
// .createContactExchangeStreamReader(socket.getInputStream(),
// sharedSecret);
byte[] ackNonce = read(inputStream, NONCE_LENGTH); byte[] ackNonce = read(inputStream, NONCE_LENGTH);
byte[] ackMessageEncrypted = byte[] ackMessageEncrypted =
read(inputStream, 3 + cipher.getMacBytes()); read(inputStream, 3 + cipher.getMacBytes());
@@ -177,4 +161,11 @@ public class CustodianTaskImpl extends ReturnShardTaskImpl
State.Failure.Reason.OTHER)); State.Failure.Reason.OTHER));
} }
} }
private void closeSocket() {
if (socket.isClosed()) return;
try {
socket.close();
} catch (IOException ignored) {}
}
} }