2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
private static final Map<String, Attribute> map;
|
|
|
|
|
|
|
|
static {
|
|
|
|
map = new HashMap<>(Attribute.values().length);
|
2017-08-13 14:24:03 +02:00
|
|
|
for (final Attribute key : Attribute.values())
|
2017-07-30 00:30:33 +02:00
|
|
|
map.put(key.getToken(), key);
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
public static Attribute match(final String line) {
|
2017-07-30 00:30:33 +02:00
|
|
|
return map.get(line.split("\\s|=")[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private final String token;
|
|
|
|
private final Pattern pattern;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
public String composeWith(final String value) {
|
2017-07-30 00:30:33 +02:00
|
|
|
return token + " = " + value + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken() {
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
public String parseFrom(final String line) {
|
2017-07-30 00:30:33 +02:00
|
|
|
final Matcher matcher = pattern.matcher(line);
|
|
|
|
if (matcher.matches())
|
|
|
|
return matcher.group(1);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|