aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/config/Peer.java
blob: 718a5c3ce9e22bdc432329a7fe50952dc1ea7fc8 (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
package com.wireguard.config;

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

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

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

public class Peer extends BaseObservable implements Copyable<Peer>, Observable, Parcelable {
    public static final Parcelable.Creator<Peer> CREATOR = new Parcelable.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 String allowedIPs;
    private final Config config;
    private String endpoint;
    private String persistentKeepalive;
    private String preSharedKey;
    private String publicKey;

    public Peer(final Config config) {
        this.config = config;
    }

    protected Peer(final Parcel in) {
        allowedIPs = in.readString();
        config = null;
        endpoint = in.readString();
        persistentKeepalive = in.readString();
        preSharedKey = in.readString();
        publicKey = in.readString();
    }

    @Override
    public Peer copy() {
        return copy(config);
    }

    public Peer copy(final Config config) {
        final Peer copy = new Peer(config);
        copy.copyFrom(this);
        return copy;
    }

    @Override
    public void copyFrom(final Peer source) {
        allowedIPs = source.allowedIPs;
        endpoint = source.endpoint;
        persistentKeepalive = source.persistentKeepalive;
        preSharedKey = source.preSharedKey;
        publicKey = source.publicKey;
        notifyChange();
    }

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

    @Bindable
    public String getAllowedIPs() {
        return allowedIPs;
    }

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

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

    @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)
            setAllowedIPs(key.parseFrom(line));
        else if (key == Attribute.ENDPOINT)
            setEndpoint(key.parseFrom(line));
        else if (key == Attribute.PERSISTENT_KEEPALIVE)
            setPersistentKeepalive(key.parseFrom(line));
        else if (key == Attribute.PRE_SHARED_KEY)
            setPreSharedKey(key.parseFrom(line));
        else if (key == Attribute.PUBLIC_KEY)
            setPublicKey(key.parseFrom(line));
        else
            throw new IllegalArgumentException(line);
    }

    public void removeSelf() {
        if (!config.getPeers().remove(this))
            throw new IllegalStateException("This peer was already removed from its config");
    }

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

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

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

    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);
    }

    public String toString() {
        final StringBuilder sb = new StringBuilder().append("[Peer]\n");
        if (allowedIPs != null)
            sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPs));
        if (endpoint != null)
            sb.append(Attribute.ENDPOINT.composeWith(endpoint));
        if (persistentKeepalive != null)
            sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
        if (preSharedKey != null)
            sb.append(Attribute.PRE_SHARED_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.writeString(allowedIPs);
        dest.writeString(endpoint);
        dest.writeString(persistentKeepalive);
        dest.writeString(preSharedKey);
        dest.writeString(publicKey);
    }
}