mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
Qr code payload contains socket address and public key
This commit is contained in:
@@ -7,6 +7,7 @@ import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.socialbackup.CustodianRecoveryModeExplainerFragment;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -55,15 +56,15 @@ public class CustodianReturnShardActivity extends BriarActivity
|
||||
if (state == null) {
|
||||
showInitialFragment(new CustodianRecoveryModeExplainerFragment());
|
||||
}
|
||||
// viewModel.getCheckPermissions().observeEvent(this, check ->
|
||||
// permissionManager.checkPermissions());
|
||||
// viewModel.getRequestBluetoothDiscoverable().observeEvent(this, r ->
|
||||
// requestBluetoothDiscoverable()); // never false
|
||||
viewModel.getShowCameraFragment().observeEvent(this, show -> {
|
||||
if (show) showCameraFragment();
|
||||
});
|
||||
// viewModel.getState()
|
||||
// .observe(this, this::onReturnShardStateChanged);
|
||||
viewModel.getState()
|
||||
.observe(this, this::onReturnShardStateChanged);
|
||||
}
|
||||
|
||||
private void onReturnShardStateChanged(CustodianTask.State state) {
|
||||
|
||||
}
|
||||
|
||||
private void showCameraFragment() {
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.briarproject.briar.android.contact.add.nearby.QrCodeDecoder;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -36,7 +37,7 @@ import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
public class CustodianReturnShardViewModel extends AndroidViewModel
|
||||
implements QrCodeDecoder.ResultCallback {
|
||||
implements QrCodeDecoder.ResultCallback, CustodianTask.Observer {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(CustodianReturnShardViewModel.class.getName());
|
||||
@@ -49,6 +50,7 @@ public class CustodianReturnShardViewModel extends AndroidViewModel
|
||||
private final MutableLiveData<CustodianTask.State> state =
|
||||
new MutableLiveData<>();
|
||||
final QrCodeDecoder qrCodeDecoder;
|
||||
private final CustodianTask task;
|
||||
|
||||
@SuppressWarnings("CharsetObjectCanBeUsed") // Requires minSdkVersion >= 19
|
||||
private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
|
||||
@@ -57,12 +59,15 @@ public class CustodianReturnShardViewModel extends AndroidViewModel
|
||||
public CustodianReturnShardViewModel(
|
||||
@NonNull Application application,
|
||||
@IoExecutor Executor ioExecutor,
|
||||
CustodianTask task,
|
||||
AndroidExecutor androidExecutor) {
|
||||
super(application);
|
||||
|
||||
this.androidExecutor = androidExecutor;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.task = task;
|
||||
qrCodeDecoder = new QrCodeDecoder(androidExecutor, ioExecutor, this);
|
||||
task.start(this);
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
@@ -75,17 +80,12 @@ public class CustodianReturnShardViewModel extends AndroidViewModel
|
||||
byte[] payloadBytes = result.getText().getBytes(ISO_8859_1);
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Remote payload is " + payloadBytes.length + " bytes");
|
||||
// Payload remotePayload = payloadParser.parse(payloadBytes);
|
||||
// gotRemotePayload = true;
|
||||
// requireNonNull(task).connectAndRunProtocol(remotePayload);
|
||||
// state.postValue(new ReturnShardState.QrCodeScanned());
|
||||
task.qrCodeDecoded(payloadBytes);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.log(WARNING, "QR Code Invalid", e);
|
||||
androidExecutor.runOnUiThread(() -> Toast.makeText(getApplication(),
|
||||
R.string.qr_code_invalid, LENGTH_LONG).show());
|
||||
// resetPayloadFlags();
|
||||
state.postValue(new CustodianTask.State.Failure(
|
||||
CustodianTask.State.Failure.Reason.QR_CODE_INVALID));
|
||||
task.qrCodeDecoded(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,4 +107,12 @@ public class CustodianReturnShardViewModel extends AndroidViewModel
|
||||
LiveData<CustodianTask.State> getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStateChanged(CustodianTask.State state) {
|
||||
// Connecting, SendingShard, ReceivingAck, Success, Failure
|
||||
if (state instanceof CustodianTask.State.SendingShard) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ public interface CustodianTask {
|
||||
|
||||
void cancel();
|
||||
|
||||
void qrCodeDecoded(byte[] qrCodePayload);
|
||||
|
||||
interface Observer {
|
||||
void onStateChanged(State state);
|
||||
}
|
||||
|
||||
@@ -20,24 +20,10 @@ public interface SecretOwnerTask {
|
||||
|
||||
public static class Listening extends State {
|
||||
|
||||
private final PublicKey publicKey;
|
||||
private final InetSocketAddress socketAddress;
|
||||
private final byte[] localPayload;
|
||||
|
||||
public Listening(PublicKey publicKey,
|
||||
InetSocketAddress socketAddress) {
|
||||
this.publicKey = publicKey;
|
||||
this.socketAddress = socketAddress;
|
||||
// TODO this should also include the socket address
|
||||
this.localPayload = publicKey.getEncoded();
|
||||
}
|
||||
|
||||
public PublicKey getPublicKey() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
public InetSocketAddress getSocketAddress() {
|
||||
return socketAddress;
|
||||
public Listening(byte[] localPayload) {
|
||||
this.localPayload = localPayload;
|
||||
}
|
||||
|
||||
public byte[] getLocalPayload() {
|
||||
|
||||
@@ -10,7 +10,9 @@ import org.briarproject.bramble.api.versioning.ClientVersioningManager;
|
||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackupExchangeManager;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
|
||||
import org.briarproject.briar.socialbackup.recovery.CustodianTaskImpl;
|
||||
import org.briarproject.briar.socialbackup.recovery.SecretOwnerTaskImpl;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -103,4 +105,9 @@ public class SocialBackupModule {
|
||||
SecretOwnerTask secretOwnerTask(SecretOwnerTaskImpl secretOwnerTask) {
|
||||
return secretOwnerTask;
|
||||
}
|
||||
|
||||
@Provides
|
||||
CustodianTask custodianTask(CustodianTaskImpl custodianTask) {
|
||||
return custodianTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,45 @@
|
||||
package org.briarproject.briar.socialbackup.recovery;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class CustodianTaskImpl implements CustodianTask {
|
||||
|
||||
private boolean cancelled = false;
|
||||
private Observer observer;
|
||||
private ClientHelper clientHelper;
|
||||
|
||||
@Inject
|
||||
CustodianTaskImpl(ClientHelper clientHelper) {
|
||||
this.clientHelper = clientHelper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Observer observer) {
|
||||
|
||||
this.observer = observer;
|
||||
observer.onStateChanged(new CustodianTask.State.Connecting());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
cancelled = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void qrCodeDecoded(byte[] qrCodePayloadRaw) {
|
||||
try {
|
||||
BdfList qrCodePayload = clientHelper.toList(qrCodePayloadRaw);
|
||||
byte[] publicKeyRaw = qrCodePayload.getRaw(0);
|
||||
byte[] addressRaw = qrCodePayload.getRaw(1);
|
||||
Long port = qrCodePayload.getLong(2);
|
||||
System.out.println(" Qr code decoded " + publicKeyRaw.length + " " + addressRaw.length + " "+ port);
|
||||
observer.onStateChanged(new CustodianTask.State.SendingShard());
|
||||
} catch (FormatException e) {
|
||||
observer.onStateChanged(new CustodianTask.State.Failure(State.Failure.Reason.QR_CODE_INVALID));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package org.briarproject.briar.socialbackup.recovery;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@@ -16,20 +20,34 @@ public class SecretOwnerTaskImpl implements SecretOwnerTask {
|
||||
private final Executor ioExecutor;
|
||||
private final KeyPair localKeyPair;
|
||||
private boolean cancelled = false;
|
||||
private InetSocketAddress socketAddress;
|
||||
private ClientHelper clientHelper;
|
||||
|
||||
@Inject
|
||||
SecretOwnerTaskImpl(CryptoComponent crypto,
|
||||
@IoExecutor Executor ioExecutor) {
|
||||
@IoExecutor Executor ioExecutor, ClientHelper clientHelper) {
|
||||
this.crypto = crypto;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.clientHelper = clientHelper;
|
||||
localKeyPair = crypto.generateAgreementKeyPair();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Observer observer) {
|
||||
// TODO use the actual ip address on local network
|
||||
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("192.168.1.1", 1234);
|
||||
observer.onStateChanged(new State.Listening(localKeyPair.getPublic(), socketAddress));
|
||||
byte[] hostBytes = { (byte) 192, (byte) 168, 1,1};
|
||||
// TODO add version number
|
||||
try {
|
||||
BdfList payloadList = new BdfList();
|
||||
socketAddress = new InetSocketAddress(InetAddress.getByAddress(hostBytes), 1234);
|
||||
payloadList.add(localKeyPair.getPublic().getEncoded());
|
||||
payloadList.add(socketAddress.getAddress().getAddress());
|
||||
payloadList.add(socketAddress.getPort());
|
||||
observer.onStateChanged(
|
||||
new State.Listening(clientHelper.toByteArray(payloadList)));
|
||||
} catch (Exception e) {
|
||||
observer.onStateChanged(new State.Failure());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user