2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
2018-04-17 05:20:17 +02:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
import java.util.HashMap;
|
2018-04-18 05:28:31 +02:00
|
|
|
import java.util.List;
|
2017-07-30 00:30:33 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum Attribute {
|
|
|
|
ADDRESS("Address"),
|
|
|
|
ALLOWED_IPS("AllowedIPs"),
|
|
|
|
DNS("DNS"),
|
|
|
|
ENDPOINT("Endpoint"),
|
|
|
|
LISTEN_PORT("ListenPort"),
|
|
|
|
MTU("MTU"),
|
|
|
|
PERSISTENT_KEEPALIVE("PersistentKeepalive"),
|
2017-11-26 23:45:41 +01:00
|
|
|
PRESHARED_KEY("PresharedKey"),
|
2017-07-30 00:30:33 +02:00
|
|
|
PRIVATE_KEY("PrivateKey"),
|
|
|
|
PUBLIC_KEY("PublicKey");
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private static final Map<String, Attribute> KEY_MAP;
|
|
|
|
private static final Pattern SEPARATOR_PATTERN = Pattern.compile("\\s|=");
|
2017-07-30 00:30:33 +02:00
|
|
|
|
|
|
|
static {
|
2018-01-01 09:06:37 +01:00
|
|
|
KEY_MAP = new HashMap<>(Attribute.values().length);
|
|
|
|
for (final Attribute key : Attribute.values()) {
|
|
|
|
KEY_MAP.put(key.token, key);
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private final Pattern pattern;
|
2018-01-01 09:06:37 +01:00
|
|
|
private final String token;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
Attribute(final String token) {
|
|
|
|
pattern = Pattern.compile(token + "\\s*=\\s*(\\S.*)");
|
2017-07-30 00:30:33 +02:00
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public static Attribute match(final CharSequence line) {
|
|
|
|
return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0]);
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public static <T> String listToString(final List<T> list) {
|
2018-04-17 05:20:17 +02:00
|
|
|
return TextUtils.join(", ", list);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String[] stringToList(final String string) {
|
2018-04-18 05:28:31 +02:00
|
|
|
return string.trim().split("\\s*,\\s*", -1);
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public String composeWith(final Object value) {
|
|
|
|
return String.format("%s = %s%n", token, value);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public String composeWith(final int value) {
|
|
|
|
return String.format("%s = %d%n", token, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T> String composeWith(final List<T> value) {
|
2018-04-17 05:20:17 +02:00
|
|
|
return String.format("%s = %s%n", token, listToString(value));
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
public String getToken() {
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public String parse(final CharSequence line) {
|
2017-07-30 00:30:33 +02:00
|
|
|
final Matcher matcher = pattern.matcher(line);
|
2018-01-01 09:06:37 +01:00
|
|
|
return matcher.matches() ? matcher.group(1) : null;
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
2018-04-17 05:20:17 +02:00
|
|
|
|
|
|
|
public String[] parseList(final CharSequence line) {
|
|
|
|
final Matcher matcher = pattern.matcher(line);
|
|
|
|
return matcher.matches() ? stringToList(matcher.group(1)) : null;
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|