summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config/Peer.java
blob: c2b23e9fb2aeb05419cde21d5b722ad2905ae294 (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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.wireguard.config;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.databinding.library.baseAdapters.BR;

import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;

/**
 * Represents the configuration for a WireGuard peer (a [Peer] block).
 */

public class Peer extends BaseObservable implements Parcelable {
    public static final Creator<Peer> CREATOR = new Creator<Peer>() {
        @Override
        public Peer createFromParcel(final Parcel in) {
            return new Peer(in);
        }

        @Override
        public Peer[] newArray(final int size) {
            return new Peer[size];
        }
    };

    private List<IPCidr> allowedIPsList;
    private InetSocketAddress endpoint;
    private int persistentKeepalive;
    private String preSharedKey;
    private String publicKey;

    public Peer() {
        allowedIPsList = new LinkedList<>();
    }

    private Peer(final Parcel in) {
        allowedIPsList = in.createTypedArrayList(IPCidr.CREATOR);
        String host = in.readString();
        int port = in.readInt();
        if (host != null && !host.isEmpty() && port > 0)
            endpoint = InetSocketAddress.createUnresolved(host, port);
        persistentKeepalive = in.readInt();
        preSharedKey = in.readString();
        if (preSharedKey != null && preSharedKey.isEmpty())
            preSharedKey = null;
        publicKey = in.readString();
        if (publicKey != null && publicKey.isEmpty())
            publicKey = null;
    }

    public static Peer newInstance() {
        return new Peer();
    }

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


    @Bindable
    public String getAllowedIPsString() {
        if (allowedIPsList.isEmpty())
            return null;
        return Attribute.listToString(allowedIPsList);
    }

    @Bindable
    public IPCidr[] getAllowedIPs() {
        return allowedIPsList.toArray(new IPCidr[allowedIPsList.size()]);
    }

    @Bindable
    public InetSocketAddress getEndpoint() {
        return endpoint;
    }

    @Bindable
    public String getEndpointString() {
        if (endpoint == null)
            return null;
        return String.format("%s:%d", endpoint.getHostString(), endpoint.getPort());
    }

    public String getResolvedEndpointString() throws UnknownHostException {
        if (endpoint == null)
            throw new UnknownHostException("{empty}");
        if (endpoint.isUnresolved())
            endpoint = new InetSocketAddress(endpoint.getHostString(), endpoint.getPort());
        if (endpoint.isUnresolved())
            throw new UnknownHostException(endpoint.getHostString());
        if (endpoint.getAddress() instanceof Inet6Address)
            return String.format("[%s]:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
        return String.format("%s:%d",  endpoint.getAddress().getHostAddress(), endpoint.getPort());
    }

    @Bindable
    public int getPersistentKeepalive() {
        return persistentKeepalive;
    }

    @Bindable
    public String getPersistentKeepaliveString() {
        if (persistentKeepalive == 0)
            return null;
        return new Integer(persistentKeepalive).toString();
    }

    @Bindable
    public String getPreSharedKey() {
        return preSharedKey;
    }

    @Bindable
    public String getPublicKey() {
        return publicKey;
    }

    public void parse(final String line) {
        final Attribute key = Attribute.match(line);
        if (key == Attribute.ALLOWED_IPS)
            addAllowedIPs(key.parseList(line));
        else if (key == Attribute.ENDPOINT)
            setEndpointString(key.parse(line));
        else if (key == Attribute.PERSISTENT_KEEPALIVE)
            setPersistentKeepaliveString(key.parse(line));
        else if (key == Attribute.PRESHARED_KEY)
            setPreSharedKey(key.parse(line));
        else if (key == Attribute.PUBLIC_KEY)
            setPublicKey(key.parse(line));
        else
            throw new IllegalArgumentException(line);
    }

    public void addAllowedIPs(String[] allowedIPs) {
        if (allowedIPs != null && allowedIPs.length > 0) {
            for (final String allowedIP : allowedIPs) {
                if (allowedIP.isEmpty())
                    throw new IllegalArgumentException("AllowedIP is empty");
                this.allowedIPsList.add(new IPCidr(allowedIP));
            }
        }
        notifyPropertyChanged(BR.allowedIPs);
        notifyPropertyChanged(BR.allowedIPsString);
    }

    public void setAllowedIPsString(final String allowedIPsString) {
        this.allowedIPsList.clear();
        addAllowedIPs(Attribute.stringToList(allowedIPsString));
    }

    public void setEndpoint(InetSocketAddress endpoint) {
        this.endpoint = endpoint;
        notifyPropertyChanged(BR.endpoint);
        notifyPropertyChanged(BR.endpointString);
    }

    public void setEndpointString(final String endpoint) {
        if (endpoint != null && !endpoint.isEmpty()) {
            InetSocketAddress constructedEndpoint;
            if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
                    throw new IllegalArgumentException("Forbidden characters in endpoint");
            URI uri;
            try {
                uri = new URI("wg://" + endpoint);
            } catch (URISyntaxException e) {
                throw new IllegalArgumentException(e);
            }
            constructedEndpoint = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
            setEndpoint(constructedEndpoint);
        } else
            setEndpoint(null);
    }

    public void setPersistentKeepalive(int persistentKeepalive) {
        this.persistentKeepalive = persistentKeepalive;
        notifyPropertyChanged(BR.persistentKeepalive);
        notifyPropertyChanged(BR.persistentKeepaliveString);
    }

    public void setPersistentKeepaliveString(String persistentKeepalive) {
        if (persistentKeepalive != null && !persistentKeepalive.isEmpty())
            setPersistentKeepalive(Integer.parseInt(persistentKeepalive, 10));
        else
            setPersistentKeepalive(0);
    }

    public void setPreSharedKey(String preSharedKey) {
        if (preSharedKey != null && preSharedKey.isEmpty())
            preSharedKey = null;
        this.preSharedKey = preSharedKey;
        notifyPropertyChanged(BR.preSharedKey);
    }

    public void setPublicKey(String publicKey) {
        if (publicKey != null && publicKey.isEmpty())
            publicKey = null;
        this.publicKey = publicKey;
        notifyPropertyChanged(BR.publicKey);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder().append("[Peer]\n");
        if (!allowedIPsList.isEmpty())
            sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPsList));
        if (endpoint != null)
            sb.append(Attribute.ENDPOINT.composeWith(getEndpointString()));
        if (persistentKeepalive != 0)
            sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
        if (preSharedKey != null)
            sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey));
        if (publicKey != null)
            sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
        return sb.toString();
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeTypedList(allowedIPsList);
        dest.writeString(endpoint == null ? null : endpoint.getHostString());
        dest.writeInt(endpoint == null ? 0 : endpoint.getPort());
        dest.writeInt(persistentKeepalive);
        dest.writeString(preSharedKey);
        dest.writeString(publicKey);
    }
}