diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-01 01:12:59 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-01 01:12:59 -0500 |
commit | 92d9b8a5fd28a9661c2cca8e359076539bf016a3 (patch) | |
tree | e73b27e0026373b68799c8a119ee9a98a6044930 /app/src/main/java/com | |
parent | 0501d0427bcbb3dc8041bea61c4170f74d99b057 (diff) |
Interface: Convert to using Keypair class
This allows retrieving the public key and generating keypairs, both of
which will be exposed in the UI.
Diffstat (limited to 'app/src/main/java/com')
-rw-r--r-- | app/src/main/java/com/wireguard/android/ProfileService.java | 2 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Interface.java | 28 |
2 files changed, 23 insertions, 7 deletions
diff --git a/app/src/main/java/com/wireguard/android/ProfileService.java b/app/src/main/java/com/wireguard/android/ProfileService.java index 93d2049f..5bc0593c 100644 --- a/app/src/main/java/com/wireguard/android/ProfileService.java +++ b/app/src/main/java/com/wireguard/android/ProfileService.java @@ -156,7 +156,7 @@ public class ProfileService extends Service { profile.parseFrom(openFileInput(fileName)); profile.setIsConnected(interfaceNames.contains(profileName)); loadedProfiles.add(profile); - } catch (IOException e) { + } catch (IOException | IndexOutOfBoundsException e) { Log.w(TAG, "Failed to load profile from " + fileName, e); } } diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java index 828f2c47..35185cf9 100644 --- a/app/src/main/java/com/wireguard/config/Interface.java +++ b/app/src/main/java/com/wireguard/config/Interface.java @@ -5,6 +5,7 @@ import android.databinding.Bindable; import android.databinding.Observable; import com.wireguard.android.BR; +import com.wireguard.crypto.Keypair; /** * 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 dns; private String listenPort; + private Keypair keypair; private String mtu; - private String privateKey; + + public void generateKeypair() { + keypair = new Keypair(); + notifyPropertyChanged(BR.privateKey); + notifyPropertyChanged(BR.publicKey); + } @Bindable public String getAddress() { @@ -39,7 +46,12 @@ public class Interface extends BaseObservable implements Observable { @Bindable 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) { @@ -53,7 +65,7 @@ public class Interface extends BaseObservable implements Observable { else if (key == Attribute.MTU) mtu = key.parseFrom(line); else if (key == Attribute.PRIVATE_KEY) - privateKey = key.parseFrom(line); + keypair = new Keypair(key.parseFrom(line)); } public void setAddress(String address) { @@ -77,8 +89,12 @@ public class Interface extends BaseObservable implements Observable { } 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.publicKey); } @Override @@ -92,8 +108,8 @@ public class Interface extends BaseObservable implements Observable { sb.append(Attribute.LISTEN_PORT.composeWith(listenPort)); if (mtu != null) sb.append(Attribute.MTU.composeWith(mtu)); - if (privateKey != null) - sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey)); + if (keypair != null) + sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey())); return sb.toString(); } } |