aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/config/Attribute.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/wireguard/config/Attribute.java')
-rw-r--r--app/src/main/java/com/wireguard/config/Attribute.java91
1 files changed, 23 insertions, 68 deletions
diff --git a/app/src/main/java/com/wireguard/config/Attribute.java b/app/src/main/java/com/wireguard/config/Attribute.java
index d4bdb6c8..d61cc744 100644
--- a/app/src/main/java/com/wireguard/config/Attribute.java
+++ b/app/src/main/java/com/wireguard/config/Attribute.java
@@ -1,94 +1,49 @@
/*
- * Copyright © 2017-2018 WireGuard LLC. All Rights Reserved.
+ * Copyright © 2018 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.config;
-import android.annotation.SuppressLint;
-import android.support.annotation.Nullable;
import android.text.TextUtils;
-import java.util.HashMap;
-import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-/**
- * The set of valid attributes for an interface or peer in a WireGuard configuration file.
- */
-
-public enum Attribute {
- ADDRESS("Address"),
- ALLOWED_IPS("AllowedIPs"),
- DNS("DNS"),
- EXCLUDED_APPLICATIONS("ExcludedApplications"),
- ENDPOINT("Endpoint"),
- LISTEN_PORT("ListenPort"),
- MTU("MTU"),
- PERSISTENT_KEEPALIVE("PersistentKeepalive"),
- PRESHARED_KEY("PresharedKey"),
- PRIVATE_KEY("PrivateKey"),
- PUBLIC_KEY("PublicKey");
-
- private static final String[] EMPTY_LIST = new String[0];
- private static final Map<String, Attribute> KEY_MAP;
- private static final Pattern LIST_SEPARATOR_PATTERN = Pattern.compile("\\s*,\\s*");
- private static final Pattern SEPARATOR_PATTERN = Pattern.compile("\\s|=");
-
- static {
- KEY_MAP = new HashMap<>(Attribute.values().length);
- for (final Attribute key : Attribute.values()) {
- KEY_MAP.put(key.token.toLowerCase(), key);
- }
- }
+import java9.util.Optional;
- private final Pattern pattern;
- private final String token;
+public final class Attribute {
+ private static final Pattern LINE_PATTERN = Pattern.compile("(\\w+)\\s*=\\s*([^\\s#][^#]*)");
+ private static final Pattern LIST_SEPARATOR = Pattern.compile("\\s*,\\s*");
- Attribute(final String token) {
- pattern = Pattern.compile(token + "\\s*=\\s*(\\S.*)");
- this.token = token;
- }
-
- public static <T> String iterableToString(final Iterable<T> iterable) {
- return TextUtils.join(", ", iterable);
- }
-
- @Nullable
- public static Attribute match(final CharSequence line) {
- return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0].toLowerCase());
- }
+ private final String key;
+ private final String value;
- public static String[] stringToList(@Nullable final String string) {
- if (TextUtils.isEmpty(string))
- return EMPTY_LIST;
- return LIST_SEPARATOR_PATTERN.split(string.trim());
+ private Attribute(final String key, final String value) {
+ this.key = key;
+ this.value = value;
}
- @SuppressLint("DefaultLocale")
- public String composeWith(@Nullable final Object value) {
- return String.format("%s = %s%n", token, value);
+ public static String join(final Iterable<?> values) {
+ return TextUtils.join(", ", values);
}
- @SuppressLint("DefaultLocale")
- public String composeWith(final int value) {
- return String.format("%s = %d%n", token, value);
+ public static Optional<Attribute> parse(final CharSequence line) {
+ final Matcher matcher = LINE_PATTERN.matcher(line);
+ if (!matcher.matches())
+ return Optional.empty();
+ return Optional.of(new Attribute(matcher.group(1), matcher.group(2)));
}
- public <T> String composeWith(final Iterable<T> value) {
- return String.format("%s = %s%n", token, iterableToString(value));
+ public static String[] split(final CharSequence value) {
+ return LIST_SEPARATOR.split(value);
}
- @Nullable
- public String parse(final CharSequence line) {
- final Matcher matcher = pattern.matcher(line);
- return matcher.matches() ? matcher.group(1) : null;
+ public String getKey() {
+ return key;
}
- @Nullable
- public String[] parseList(final CharSequence line) {
- final Matcher matcher = pattern.matcher(line);
- return matcher.matches() ? stringToList(matcher.group(1)) : null;
+ public String getValue() {
+ return value;
}
}