diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-14 13:53:47 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-14 13:53:58 -0500 |
commit | 83da2aa1990d534bb2531b01fa1993de6d225d69 (patch) | |
tree | 2fdd45faf39eb669cb18e9682470a4affba8fb92 /app/src/main/java/com/wireguard/android/ConfigEditFragment.java | |
parent | d5ede015cf39dea1487b2a2f79c5fed1b9911a47 (diff) |
EditFragment: Input filters for config name and private key
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/ConfigEditFragment.java')
-rw-r--r-- | app/src/main/java/com/wireguard/android/ConfigEditFragment.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/ConfigEditFragment.java b/app/src/main/java/com/wireguard/android/ConfigEditFragment.java index 395358f6..a8e37155 100644 --- a/app/src/main/java/com/wireguard/android/ConfigEditFragment.java +++ b/app/src/main/java/com/wireguard/android/ConfigEditFragment.java @@ -2,6 +2,10 @@ package com.wireguard.android; import android.content.Context; import android.os.Bundle; +import android.text.InputFilter; +import android.text.LoginFilter; +import android.text.SpannableStringBuilder; +import android.text.Spanned; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -9,9 +13,11 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; import com.wireguard.android.databinding.ConfigEditFragmentBinding; import com.wireguard.config.Config; +import com.wireguard.crypto.KeyEncoding; /** * Fragment for editing a WireGuard configuration. @@ -41,6 +47,50 @@ public class ConfigEditFragment extends BaseConfigFragment { final Bundle savedInstanceState) { final ConfigEditFragmentBinding binding = ConfigEditFragmentBinding.inflate(inflater, parent, false); + final EditText configNameText = binding.getRoot().findViewById(R.id.config_name_text); + configNameText.setFilters(new InputFilter[]{ + new InputFilter.LengthFilter(16), + new LoginFilter.UsernameFilterGeneric() { + @Override + public boolean isAllowed(final char c) { + return Character.isLetterOrDigit(c) || "_=+.-".indexOf(c) != -1; + } + } + }); + final EditText privateKeyText = binding.getRoot().findViewById(R.id.private_key_text); + privateKeyText.setFilters(new InputFilter[]{ + new InputFilter() { + @Override + public CharSequence filter(final CharSequence source, + final int sStart, final int sEnd, + final Spanned dest, + final int dStart, final int dEnd) { + SpannableStringBuilder replacement = null; + int rIndex = 0; + final int dLength = dest.length(); + for (int sIndex = sStart; sIndex < sEnd; ++sIndex) { + final char c = source.charAt(sIndex); + final int dIndex = dStart + (sIndex - sStart); + // Restrict characters to the base64 character set. + // Ensure adding this character does not push the length over the limit. + if (((dIndex + 1 < KeyEncoding.KEY_LENGTH_BASE64 && isAllowed(c)) || + (dIndex + 1 == KeyEncoding.KEY_LENGTH_BASE64 && c == '=')) && + dLength + (sIndex - sStart) < KeyEncoding.KEY_LENGTH_BASE64) { + ++rIndex; + } else { + if (replacement == null) + replacement = new SpannableStringBuilder(source, sStart, sEnd); + replacement.delete(rIndex, rIndex + 1); + } + } + return replacement; + } + + private boolean isAllowed(final char c) { + return Character.isLetterOrDigit(c) || c == '+' || c == '/'; + } + } + }); binding.setConfig(localConfig); return binding.getRoot(); } |