mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
get the actual ip address on local wifi and add it to qr code
This commit is contained in:
@@ -2,8 +2,11 @@ package org.briarproject.briar.android.socialbackup.recover;
|
||||
|
||||
import android.app.Application;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.util.DisplayMetrics;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
@@ -12,17 +15,23 @@ import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static android.content.Context.WIFI_SERVICE;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@@ -45,10 +54,10 @@ class OwnerReturnShardViewModel extends AndroidViewModel implements SecretOwnerT
|
||||
new MutableLiveEvent<>();
|
||||
private final MutableLiveData<SecretOwnerTask.State> state =
|
||||
new MutableLiveData<>();
|
||||
|
||||
private boolean wasContinueClicked = false;
|
||||
private boolean isActivityResumed = false;
|
||||
private Bitmap qrCodeBitmap;
|
||||
private WifiManager wifiManager;
|
||||
|
||||
@Inject
|
||||
OwnerReturnShardViewModel(Application app,
|
||||
@@ -59,10 +68,36 @@ class OwnerReturnShardViewModel extends AndroidViewModel implements SecretOwnerT
|
||||
this.androidExecutor = androidExecutor;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.task = task;
|
||||
wifiManager = (WifiManager) app.getSystemService(WIFI_SERVICE);
|
||||
|
||||
// IntentFilter filter = new IntentFilter(ACTION_SCAN_MODE_CHANGED);
|
||||
startListening();
|
||||
}
|
||||
|
||||
private InetAddress getWifiIpv4Address() {
|
||||
if (wifiManager == null) return null;
|
||||
// If we're connected to a wifi network, return its address
|
||||
WifiInfo info = wifiManager.getConnectionInfo();
|
||||
if (info != null && info.getIpAddress() != 0) {
|
||||
return intToInetAddress(info.getIpAddress());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private InetAddress intToInetAddress(int ip) {
|
||||
byte[] ipBytes = new byte[4];
|
||||
ipBytes[0] = (byte) (ip & 0xFF);
|
||||
ipBytes[1] = (byte) ((ip >> 8) & 0xFF);
|
||||
ipBytes[2] = (byte) ((ip >> 16) & 0xFF);
|
||||
ipBytes[3] = (byte) ((ip >> 24) & 0xFF);
|
||||
try {
|
||||
return InetAddress.getByAddress(ipBytes);
|
||||
} catch (UnknownHostException e) {
|
||||
// Should only be thrown if address has illegal length
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
@@ -87,7 +122,7 @@ class OwnerReturnShardViewModel extends AndroidViewModel implements SecretOwnerT
|
||||
|
||||
@UiThread
|
||||
private void startListening() {
|
||||
task.start(this);
|
||||
task.start(this, getWifiIpv4Address());
|
||||
// KeyAgreementTask oldTask = task;
|
||||
// KeyAgreementTask newTask = keyAgreementTaskProvider.get();
|
||||
// task = newTask;
|
||||
|
||||
@@ -3,12 +3,13 @@ package org.briarproject.briar.api.socialbackup.recovery;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface SecretOwnerTask {
|
||||
|
||||
void start(Observer observer);
|
||||
void start(Observer observer, InetAddress inetAddress);
|
||||
|
||||
void cancel();
|
||||
|
||||
|
||||
@@ -2,9 +2,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.AgreementPublicKey;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.security.PublicKey;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class CustodianTaskImpl implements CustodianTask {
|
||||
@@ -12,6 +17,7 @@ public class CustodianTaskImpl implements CustodianTask {
|
||||
private boolean cancelled = false;
|
||||
private Observer observer;
|
||||
private ClientHelper clientHelper;
|
||||
private InetSocketAddress remoteSocketAddress;
|
||||
|
||||
@Inject
|
||||
CustodianTaskImpl(ClientHelper clientHelper) {
|
||||
@@ -33,12 +39,13 @@ public class CustodianTaskImpl implements CustodianTask {
|
||||
public void qrCodeDecoded(byte[] qrCodePayloadRaw) {
|
||||
try {
|
||||
BdfList qrCodePayload = clientHelper.toList(qrCodePayloadRaw);
|
||||
byte[] publicKeyRaw = qrCodePayload.getRaw(0);
|
||||
AgreementPublicKey publicKey = new AgreementPublicKey(qrCodePayload.getRaw(0));
|
||||
byte[] addressRaw = qrCodePayload.getRaw(1);
|
||||
Long port = qrCodePayload.getLong(2);
|
||||
System.out.println(" Qr code decoded " + publicKeyRaw.length + " " + addressRaw.length + " "+ port);
|
||||
int port = qrCodePayload.getLong(2).intValue();
|
||||
remoteSocketAddress = new InetSocketAddress(InetAddress.getByAddress(addressRaw), port);
|
||||
System.out.println(" Qr code decoded " + publicKey.getEncoded().length + " " + remoteSocketAddress);
|
||||
observer.onStateChanged(new CustodianTask.State.SendingShard());
|
||||
} catch (FormatException e) {
|
||||
} catch (Exception e) {
|
||||
observer.onStateChanged(new CustodianTask.State.Failure(State.Failure.Reason.QR_CODE_INVALID));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,13 +33,14 @@ public class SecretOwnerTaskImpl implements SecretOwnerTask {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Observer observer) {
|
||||
// TODO use the actual ip address on local network
|
||||
byte[] hostBytes = { (byte) 192, (byte) 168, 1,1};
|
||||
// TODO add version number
|
||||
public void start(Observer observer, InetAddress inetAddress) {
|
||||
if (inetAddress == null) observer.onStateChanged(new State.Failure());
|
||||
System.out.println("InetAddress is " + inetAddress);
|
||||
socketAddress = new InetSocketAddress(inetAddress, 3002);
|
||||
// TODO start listening on socketAddress
|
||||
try {
|
||||
// TODO add version number
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user