Interface: Convert to using Keypair class
This allows retrieving the public key and generating keypairs, both of which will be exposed in the UI. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
19e8087642
commit
874db0b95e
@ -156,7 +156,7 @@ public class ProfileService extends Service {
|
|||||||
profile.parseFrom(openFileInput(fileName));
|
profile.parseFrom(openFileInput(fileName));
|
||||||
profile.setIsConnected(interfaceNames.contains(profileName));
|
profile.setIsConnected(interfaceNames.contains(profileName));
|
||||||
loadedProfiles.add(profile);
|
loadedProfiles.add(profile);
|
||||||
} catch (IOException e) {
|
} catch (IOException | IndexOutOfBoundsException e) {
|
||||||
Log.w(TAG, "Failed to load profile from " + fileName, e);
|
Log.w(TAG, "Failed to load profile from " + fileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import android.databinding.Bindable;
|
|||||||
import android.databinding.Observable;
|
import android.databinding.Observable;
|
||||||
|
|
||||||
import com.wireguard.android.BR;
|
import com.wireguard.android.BR;
|
||||||
|
import com.wireguard.crypto.Keypair;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the configuration for a WireGuard interface (an [Interface] block).
|
* Represents the configuration for a WireGuard interface (an [Interface] block).
|
||||||
@ -14,8 +15,14 @@ public class Interface extends BaseObservable implements Observable {
|
|||||||
private String address;
|
private String address;
|
||||||
private String dns;
|
private String dns;
|
||||||
private String listenPort;
|
private String listenPort;
|
||||||
|
private Keypair keypair;
|
||||||
private String mtu;
|
private String mtu;
|
||||||
private String privateKey;
|
|
||||||
|
public void generateKeypair() {
|
||||||
|
keypair = new Keypair();
|
||||||
|
notifyPropertyChanged(BR.privateKey);
|
||||||
|
notifyPropertyChanged(BR.publicKey);
|
||||||
|
}
|
||||||
|
|
||||||
@Bindable
|
@Bindable
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
@ -39,7 +46,12 @@ public class Interface extends BaseObservable implements Observable {
|
|||||||
|
|
||||||
@Bindable
|
@Bindable
|
||||||
public String getPrivateKey() {
|
public String getPrivateKey() {
|
||||||
return privateKey;
|
return keypair != null ? keypair.getPrivateKey() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bindable
|
||||||
|
public String getPublicKey() {
|
||||||
|
return keypair != null ? keypair.getPublicKey() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void parseFrom(String line) {
|
public void parseFrom(String line) {
|
||||||
@ -53,7 +65,7 @@ public class Interface extends BaseObservable implements Observable {
|
|||||||
else if (key == Attribute.MTU)
|
else if (key == Attribute.MTU)
|
||||||
mtu = key.parseFrom(line);
|
mtu = key.parseFrom(line);
|
||||||
else if (key == Attribute.PRIVATE_KEY)
|
else if (key == Attribute.PRIVATE_KEY)
|
||||||
privateKey = key.parseFrom(line);
|
keypair = new Keypair(key.parseFrom(line));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAddress(String address) {
|
public void setAddress(String address) {
|
||||||
@ -77,8 +89,12 @@ public class Interface extends BaseObservable implements Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPrivateKey(String privateKey) {
|
public void setPrivateKey(String privateKey) {
|
||||||
this.privateKey = privateKey;
|
// Avoid exceptions from Keypair while the user is typing.
|
||||||
|
if (privateKey.length() != Keypair.KEY_STRING_LENGTH)
|
||||||
|
return;
|
||||||
|
keypair = new Keypair(privateKey);
|
||||||
notifyPropertyChanged(BR.privateKey);
|
notifyPropertyChanged(BR.privateKey);
|
||||||
|
notifyPropertyChanged(BR.publicKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -92,8 +108,8 @@ public class Interface extends BaseObservable implements Observable {
|
|||||||
sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
|
sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
|
||||||
if (mtu != null)
|
if (mtu != null)
|
||||||
sb.append(Attribute.MTU.composeWith(mtu));
|
sb.append(Attribute.MTU.composeWith(mtu));
|
||||||
if (privateKey != null)
|
if (keypair != null)
|
||||||
sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey));
|
sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey()));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user