aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/config/Config.java
blob: a39353025a57ec34da8a59213464371c4800bcb6 (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.wireguard.config;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.Observable;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import com.wireguard.android.BR;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Represents a wg-quick configuration file, its name, and its connection state.
 */

public class Config extends BaseObservable
        implements Comparable<Config>, Copyable<Config>, Observable, Parcelable {
    public static final Parcelable.Creator<Config> CREATOR = new Parcelable.Creator<Config>() {
        @Override
        public Config createFromParcel(final Parcel in) {
            return new Config(in);
        }

        @Override
        public Config[] newArray(final int size) {
            return new Config[size];
        }
    };
    public static final int NAME_MAX_LENGTH = 16;
    private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_=+.-]{1,16}$");

    public static boolean isNameValid(final String name) {
        return name.length() <= NAME_MAX_LENGTH && PATTERN.matcher(name).matches();
    }

    private final Interface iface;
    private boolean isEnabled;
    private boolean isPrimary;
    private String name;
    private final ObservableList<Peer> peers = new ObservableArrayList<>();

    public Config() {
        iface = new Interface();
    }

    protected Config(final Parcel in) {
        iface = in.readParcelable(Interface.class.getClassLoader());
        name = in.readString();
        // The flattened peers must be recreated to associate them with this config.
        final List<Peer> flattenedPeers = new LinkedList<>();
        in.readTypedList(flattenedPeers, Peer.CREATOR);
        for (final Peer peer : flattenedPeers)
            addPeer(peer);
    }

    public Peer addPeer() {
        final Peer peer = new Peer(this);
        peers.add(peer);
        return peer;
    }

    private Peer addPeer(final Peer peer) {
        final Peer copy = peer.copy(this);
        peers.add(copy);
        return copy;
    }

    @Override
    public int compareTo(@NonNull final Config config) {
        return getName().compareTo(config.getName());
    }

    @Override
    public Config copy() {
        final Config copy = new Config();
        copy.copyFrom(this);
        return copy;
    }

    @Override
    public void copyFrom(final Config source) {
        if (source != null) {
            iface.copyFrom(source.iface);
            name = source.name;
            peers.clear();
            for (final Peer peer : source.peers)
                addPeer(peer);
        } else {
            iface.copyFrom(null);
            name = null;
            peers.clear();
        }
        notifyChange();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public Interface getInterface() {
        return iface;
    }

    @Bindable
    public String getName() {
        return name;
    }

    public ObservableList<Peer> getPeers() {
        return peers;
    }

    @Bindable
    public boolean isEnabled() {
        return isEnabled;
    }

    @Bindable
    public boolean isPrimary() {
        return isPrimary;
    }

    public void parseFrom(final InputStream stream)
            throws IOException {
        peers.clear();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(stream, StandardCharsets.UTF_8))) {
            Peer currentPeer = null;
            String line;
            boolean inInterfaceSection = false;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty() || line.startsWith("#"))
                    continue;
                if ("[Interface]".equals(line)) {
                    currentPeer = null;
                    inInterfaceSection = true;
                } else if ("[Peer]".equals(line)) {
                    currentPeer = addPeer();
                    inInterfaceSection = false;
                } else if (inInterfaceSection) {
                    iface.parse(line);
                } else if (currentPeer != null) {
                    currentPeer.parse(line);
                } else {
                    throw new IllegalArgumentException("Invalid configuration line: " + line);
                }
            }
            if (!inInterfaceSection && currentPeer == null) {
                throw new IllegalArgumentException("Could not find any config information");
            }
        }
    }

    public void setIsEnabled(final boolean isEnabled) {
        this.isEnabled = isEnabled;
        notifyPropertyChanged(BR.enabled);
    }

    public void setIsPrimary(final boolean isPrimary) {
        this.isPrimary = isPrimary;
        notifyPropertyChanged(BR.primary);
    }

    public void setName(final String name) {
        if (name != null && !name.isEmpty() && !isNameValid(name))
            throw new IllegalArgumentException();
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder().append(iface);
        for (final Peer peer : peers)
            sb.append('\n').append(peer);
        return sb.toString();
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeParcelable(iface, flags);
        dest.writeString(name);
        dest.writeTypedList(peers);
    }
}