Interface: Rework private key handling
This works much better with a data-bound UI -- no confusing erasing of the text box, and no crashes, either! Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
8bf12f3f55
commit
cb48a7be3a
@ -18,6 +18,7 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
|
|||||||
private String listenPort;
|
private String listenPort;
|
||||||
private Keypair keypair;
|
private Keypair keypair;
|
||||||
private String mtu;
|
private String mtu;
|
||||||
|
private String privateKey;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Interface copy() {
|
public Interface copy() {
|
||||||
@ -31,12 +32,13 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
|
|||||||
address = source.address;
|
address = source.address;
|
||||||
dns = source.dns;
|
dns = source.dns;
|
||||||
listenPort = source.listenPort;
|
listenPort = source.listenPort;
|
||||||
keypair = source.keypair;
|
setPrivateKey(source.privateKey);
|
||||||
mtu = source.mtu;
|
mtu = source.mtu;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateKeypair() {
|
public void generateKeypair() {
|
||||||
keypair = new Keypair();
|
keypair = new Keypair();
|
||||||
|
privateKey = keypair.getPrivateKey();
|
||||||
notifyPropertyChanged(BR.privateKey);
|
notifyPropertyChanged(BR.privateKey);
|
||||||
notifyPropertyChanged(BR.publicKey);
|
notifyPropertyChanged(BR.publicKey);
|
||||||
}
|
}
|
||||||
@ -63,7 +65,7 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
|
|||||||
|
|
||||||
@Bindable
|
@Bindable
|
||||||
public String getPrivateKey() {
|
public String getPrivateKey() {
|
||||||
return keypair != null ? keypair.getPrivateKey() : null;
|
return privateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bindable
|
@Bindable
|
||||||
@ -82,7 +84,7 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
|
|||||||
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)
|
||||||
keypair = new Keypair(key.parseFrom(line));
|
setPrivateKey(key.parseFrom(line));
|
||||||
else
|
else
|
||||||
throw new IllegalArgumentException(line);
|
throw new IllegalArgumentException(line);
|
||||||
}
|
}
|
||||||
@ -115,12 +117,16 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
|
|||||||
notifyPropertyChanged(BR.mtu);
|
notifyPropertyChanged(BR.mtu);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPrivateKey(final String privateKey) {
|
public void setPrivateKey(String privateKey) {
|
||||||
if (privateKey != null && !privateKey.isEmpty()) {
|
if (privateKey != null && privateKey.isEmpty())
|
||||||
// Avoid exceptions from Keypair while the user is typing.
|
privateKey = null;
|
||||||
if (privateKey.length() != KeyEncoding.KEY_LENGTH_BASE64)
|
this.privateKey = privateKey;
|
||||||
return;
|
if (privateKey != null && privateKey.length() == KeyEncoding.KEY_LENGTH_BASE64) {
|
||||||
keypair = new Keypair(privateKey);
|
try {
|
||||||
|
keypair = new Keypair(privateKey);
|
||||||
|
} catch (final IllegalArgumentException ignored) {
|
||||||
|
keypair = null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
keypair = null;
|
keypair = null;
|
||||||
}
|
}
|
||||||
@ -139,8 +145,8 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
|
|||||||
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 (keypair != null)
|
if (privateKey != null)
|
||||||
sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey()));
|
sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user