aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/android/viewmodel/InterfaceProxy.java
blob: cc9f2dd847a7f59b5ad3fc7fa9f4d269fe08eca7 (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
/*
 * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.viewmodel;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import android.os.Parcel;
import android.os.Parcelable;

import com.wireguard.android.BR;
import com.wireguard.config.Attribute;
import com.wireguard.config.BadConfigException;
import com.wireguard.config.Interface;
import com.wireguard.crypto.Key;
import com.wireguard.crypto.KeyFormatException;
import com.wireguard.crypto.KeyPair;

import java.net.InetAddress;
import java.util.List;

import java9.util.stream.Collectors;
import java9.util.stream.StreamSupport;

public class InterfaceProxy extends BaseObservable implements Parcelable {
    public static final Parcelable.Creator<InterfaceProxy> CREATOR = new InterfaceProxyCreator();

    private final ObservableList<String> excludedApplications = new ObservableArrayList<>();
    private String addresses;
    private String dnsServers;
    private String listenPort;
    private String mtu;
    private String privateKey;
    private String publicKey;

    private InterfaceProxy(final Parcel in) {
        addresses = in.readString();
        dnsServers = in.readString();
        in.readStringList(excludedApplications);
        listenPort = in.readString();
        mtu = in.readString();
        privateKey = in.readString();
        publicKey = in.readString();
    }

    public InterfaceProxy(final Interface other) {
        addresses = Attribute.join(other.getAddresses());
        final List<String> dnsServerStrings = StreamSupport.stream(other.getDnsServers())
                .map(InetAddress::getHostAddress)
                .collect(Collectors.toUnmodifiableList());
        dnsServers = Attribute.join(dnsServerStrings);
        excludedApplications.addAll(other.getExcludedApplications());
        listenPort = other.getListenPort().map(String::valueOf).orElse("");
        mtu = other.getMtu().map(String::valueOf).orElse("");
        final KeyPair keyPair = other.getKeyPair();
        privateKey = keyPair.getPrivateKey().toBase64();
        publicKey = keyPair.getPublicKey().toBase64();
    }

    public InterfaceProxy() {
        addresses = "";
        dnsServers = "";
        listenPort = "";
        mtu = "";
        privateKey = "";
        publicKey = "";
    }

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

    public void generateKeyPair() {
        final KeyPair keyPair = new KeyPair();
        privateKey = keyPair.getPrivateKey().toBase64();
        publicKey = keyPair.getPublicKey().toBase64();
        notifyPropertyChanged(BR.privateKey);
        notifyPropertyChanged(BR.publicKey);
    }

    @Bindable
    public String getAddresses() {
        return addresses;
    }

    @Bindable
    public String getDnsServers() {
        return dnsServers;
    }

    public ObservableList<String> getExcludedApplications() {
        return excludedApplications;
    }

    @Bindable
    public String getListenPort() {
        return listenPort;
    }

    @Bindable
    public String getMtu() {
        return mtu;
    }

    @Bindable
    public String getPrivateKey() {
        return privateKey;
    }

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

    public Interface resolve() throws BadConfigException {
        final Interface.Builder builder = new Interface.Builder();
        if (!addresses.isEmpty())
            builder.parseAddresses(addresses);
        if (!dnsServers.isEmpty())
            builder.parseDnsServers(dnsServers);
        if (!excludedApplications.isEmpty())
            builder.excludeApplications(excludedApplications);
        if (!listenPort.isEmpty())
            builder.parseListenPort(listenPort);
        if (!mtu.isEmpty())
            builder.parseMtu(mtu);
        if (!privateKey.isEmpty())
            builder.parsePrivateKey(privateKey);
        return builder.build();
    }

    public void setAddresses(final String addresses) {
        this.addresses = addresses;
        notifyPropertyChanged(BR.addresses);
    }

    public void setDnsServers(final String dnsServers) {
        this.dnsServers = dnsServers;
        notifyPropertyChanged(BR.dnsServers);
    }

    public void setListenPort(final String listenPort) {
        this.listenPort = listenPort;
        notifyPropertyChanged(BR.listenPort);
    }

    public void setMtu(final String mtu) {
        this.mtu = mtu;
        notifyPropertyChanged(BR.mtu);
    }

    public void setPrivateKey(final String privateKey) {
        this.privateKey = privateKey;
        try {
            publicKey = new KeyPair(Key.fromBase64(privateKey)).getPublicKey().toBase64();
        } catch (final KeyFormatException ignored) {
            publicKey = "";
        }
        notifyPropertyChanged(BR.privateKey);
        notifyPropertyChanged(BR.publicKey);
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeString(addresses);
        dest.writeString(dnsServers);
        dest.writeStringList(excludedApplications);
        dest.writeString(listenPort);
        dest.writeString(mtu);
        dest.writeString(privateKey);
        dest.writeString(publicKey);
    }

    private static class InterfaceProxyCreator implements Parcelable.Creator<InterfaceProxy> {
        @Override
        public InterfaceProxy createFromParcel(final Parcel in) {
            return new InterfaceProxy(in);
        }

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