summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config/Attribute.java
blob: 4ee4e9f52e2494c46905f6eb75ce2617d8fd77e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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"),
    PRE_SHARED_KEY("PresharedKey"),
    PRIVATE_KEY("PrivateKey"),
    PUBLIC_KEY("PublicKey");

    private static final Map<String, Attribute> map;

    static {
        map = new HashMap<>(Attribute.values().length);
        for (final Attribute key : Attribute.values())
            map.put(key.getToken(), key);
    }

    public static Attribute match(final String line) {
        return map.get(line.split("\\s|=")[0]);
    }

    private final String token;
    private final Pattern pattern;

    Attribute(final String token) {
        pattern = Pattern.compile(token + "\\s*=\\s*(\\S.*)");
        this.token = token;
    }

    public String composeWith(final String value) {
        return token + " = " + value + "\n";
    }

    public String getToken() {
        return token;
    }

    public String parseFrom(final String line) {
        final Matcher matcher = pattern.matcher(line);
        if (matcher.matches())
            return matcher.group(1);
        return null;
    }
}