2018-05-02 17:29:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Samuel Holland <samuel@sholland.org>
|
2018-05-02 21:40:01 +02:00
|
|
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-07-06 04:09:48 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-05-02 17:29:58 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
2018-08-01 07:55:08 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2018-07-13 02:10:35 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2018-04-17 05:20:17 +02:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-07-04 23:47:55 +02:00
|
|
|
public enum Attribute {
|
2017-07-30 00:30:33 +02:00
|
|
|
ADDRESS("Address"),
|
|
|
|
ALLOWED_IPS("AllowedIPs"),
|
|
|
|
DNS("DNS"),
|
2018-07-04 23:47:55 +02:00
|
|
|
EXCLUDED_APPLICATIONS("ExcludedApplications"),
|
2017-07-30 00:30:33 +02:00
|
|
|
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-04-30 18:37:52 +02:00
|
|
|
private static final String[] EMPTY_LIST = new String[0];
|
2018-01-01 09:06:37 +01:00
|
|
|
private static final Map<String, Attribute> KEY_MAP;
|
2018-04-30 18:37:52 +02:00
|
|
|
private static final Pattern LIST_SEPARATOR_PATTERN = Pattern.compile("\\s*,\\s*");
|
2018-01-01 09:06:37 +01:00
|
|
|
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()) {
|
2018-05-03 15:41:44 +02:00
|
|
|
KEY_MAP.put(key.token.toLowerCase(), key);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
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-04-30 18:37:52 +02:00
|
|
|
public static <T> String iterableToString(final Iterable<T> iterable) {
|
|
|
|
return TextUtils.join(", ", iterable);
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 19:35:54 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
public static Attribute match(final CharSequence line) {
|
2018-05-03 15:41:44 +02:00
|
|
|
return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0].toLowerCase());
|
2018-04-27 18:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
public static String[] stringToList(@Nullable final String string) {
|
2018-07-04 23:47:55 +02:00
|
|
|
if (TextUtils.isEmpty(string))
|
2018-04-30 18:37:52 +02:00
|
|
|
return EMPTY_LIST;
|
|
|
|
return LIST_SEPARATOR_PATTERN.split(string.trim());
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 07:55:08 +02:00
|
|
|
@SuppressLint("DefaultLocale")
|
2018-07-13 02:10:35 +02:00
|
|
|
public String composeWith(@Nullable final Object value) {
|
2018-01-01 09:06:37 +01:00
|
|
|
return String.format("%s = %s%n", token, value);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 07:55:08 +02:00
|
|
|
@SuppressLint("DefaultLocale")
|
2018-04-18 05:28:31 +02:00
|
|
|
public String composeWith(final int value) {
|
2018-07-24 10:09:50 +02:00
|
|
|
return String.format("%s = %d%n", token, value);
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public <T> String composeWith(final Iterable<T> value) {
|
|
|
|
return String.format("%s = %s%n", token, iterableToString(value));
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
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
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
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
|
|
|
}
|