Peer: Add a field for the optional pre-shared key

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-24 02:00:53 -05:00
parent 881ad4fd47
commit 3d6109e6d9
2 changed files with 21 additions and 0 deletions

View File

@ -17,6 +17,7 @@ enum Attribute {
LISTEN_PORT("ListenPort"), LISTEN_PORT("ListenPort"),
MTU("MTU"), MTU("MTU"),
PERSISTENT_KEEPALIVE("PersistentKeepalive"), PERSISTENT_KEEPALIVE("PersistentKeepalive"),
PRE_SHARED_KEY("PresharedKey"),
PRIVATE_KEY("PrivateKey"), PRIVATE_KEY("PrivateKey"),
PUBLIC_KEY("PublicKey"); PUBLIC_KEY("PublicKey");

View File

@ -29,6 +29,7 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
private final Config config; private final Config config;
private String endpoint; private String endpoint;
private String persistentKeepalive; private String persistentKeepalive;
private String preSharedKey;
private String publicKey; private String publicKey;
public Peer(final Config config) { public Peer(final Config config) {
@ -40,6 +41,7 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
config = null; config = null;
endpoint = in.readString(); endpoint = in.readString();
persistentKeepalive = in.readString(); persistentKeepalive = in.readString();
preSharedKey = in.readString();
publicKey = in.readString(); publicKey = in.readString();
} }
@ -59,6 +61,7 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
allowedIPs = source.allowedIPs; allowedIPs = source.allowedIPs;
endpoint = source.endpoint; endpoint = source.endpoint;
persistentKeepalive = source.persistentKeepalive; persistentKeepalive = source.persistentKeepalive;
preSharedKey = source.preSharedKey;
publicKey = source.publicKey; publicKey = source.publicKey;
notifyChange(); notifyChange();
} }
@ -83,6 +86,11 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
return persistentKeepalive; return persistentKeepalive;
} }
@Bindable
public String getPreSharedKey() {
return preSharedKey;
}
@Bindable @Bindable
public String getPublicKey() { public String getPublicKey() {
return publicKey; return publicKey;
@ -96,6 +104,8 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
setEndpoint(key.parseFrom(line)); setEndpoint(key.parseFrom(line));
else if (key == Attribute.PERSISTENT_KEEPALIVE) else if (key == Attribute.PERSISTENT_KEEPALIVE)
setPersistentKeepalive(key.parseFrom(line)); setPersistentKeepalive(key.parseFrom(line));
else if (key == Attribute.PRE_SHARED_KEY)
setPreSharedKey(key.parseFrom(line));
else if (key == Attribute.PUBLIC_KEY) else if (key == Attribute.PUBLIC_KEY)
setPublicKey(key.parseFrom(line)); setPublicKey(key.parseFrom(line));
else else
@ -128,6 +138,13 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
notifyPropertyChanged(BR.persistentKeepalive); notifyPropertyChanged(BR.persistentKeepalive);
} }
public void setPreSharedKey(String preSharedKey) {
if (preSharedKey != null && preSharedKey.isEmpty())
preSharedKey = null;
this.preSharedKey = preSharedKey;
notifyPropertyChanged(BR.preSharedKey);
}
public void setPublicKey(String publicKey) { public void setPublicKey(String publicKey) {
if (publicKey != null && publicKey.isEmpty()) if (publicKey != null && publicKey.isEmpty())
publicKey = null; publicKey = null;
@ -143,6 +160,8 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
sb.append(Attribute.ENDPOINT.composeWith(endpoint)); sb.append(Attribute.ENDPOINT.composeWith(endpoint));
if (persistentKeepalive != null) if (persistentKeepalive != null)
sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive)); sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
if (preSharedKey != null)
sb.append(Attribute.PRE_SHARED_KEY.composeWith(preSharedKey));
if (publicKey != null) if (publicKey != null)
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey)); sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
return sb.toString(); return sb.toString();
@ -153,6 +172,7 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
dest.writeString(allowedIPs); dest.writeString(allowedIPs);
dest.writeString(endpoint); dest.writeString(endpoint);
dest.writeString(persistentKeepalive); dest.writeString(persistentKeepalive);
dest.writeString(preSharedKey);
dest.writeString(publicKey); dest.writeString(publicKey);
} }
} }