summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config/Interface.java
blob: b81b6d9962bc4638e42c3640ccfd8767b631e816 (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
package com.wireguard.config;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.Observable;

import com.wireguard.android.BR;
import com.wireguard.crypto.Keypair;
import com.wireguard.crypto.KeyEncoding;

/**
 * Represents the configuration for a WireGuard interface (an [Interface] block).
 */

public class Interface extends BaseObservable implements Copyable<Interface>, Observable {
    private String address;
    private String dns;
    private String listenPort;
    private Keypair keypair;
    private String mtu;
    private String privateKey;

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

    @Override
    public void copyFrom(final Interface source) {
        if (source != null) {
            address = source.address;
            dns = source.dns;
            listenPort = source.listenPort;
            mtu = source.mtu;
            setPrivateKey(source.privateKey);
        } else {
            address = null;
            dns = null;
            listenPort = null;
            mtu = null;
            setPrivateKey(null);
        }
    }

    public void generateKeypair() {
        keypair = new Keypair();
        privateKey = keypair.getPrivateKey();
        notifyPropertyChanged(BR.privateKey);
        notifyPropertyChanged(BR.publicKey);
    }

    @Bindable
    public String getAddress() {
        return address;
    }

    @Bindable
    public String getDns() {
        return dns;
    }

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

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

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

    @Bindable
    public String getPublicKey() {
        return keypair != null ? keypair.getPublicKey() : null;
    }

    public void parseFrom(final String line) {
        final Attribute key = Attribute.match(line);
        if (key == Attribute.ADDRESS)
            address = key.parseFrom(line);
        else if (key == Attribute.DNS)
            dns = key.parseFrom(line);
        else if (key == Attribute.LISTEN_PORT)
            listenPort = key.parseFrom(line);
        else if (key == Attribute.MTU)
            mtu = key.parseFrom(line);
        else if (key == Attribute.PRIVATE_KEY)
            setPrivateKey(key.parseFrom(line));
        else
            throw new IllegalArgumentException(line);
    }

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

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

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

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

    public void setPrivateKey(String privateKey) {
        if (privateKey != null && privateKey.isEmpty())
            privateKey = null;
        this.privateKey = privateKey;
        if (privateKey != null && privateKey.length() == KeyEncoding.KEY_LENGTH_BASE64) {
            try {
                keypair = new Keypair(privateKey);
            } catch (final IllegalArgumentException ignored) {
                keypair = null;
            }
        } else {
            keypair = null;
        }
        notifyPropertyChanged(BR.privateKey);
        notifyPropertyChanged(BR.publicKey);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder().append("[Interface]\n");
        if (address != null)
            sb.append(Attribute.ADDRESS.composeWith(address));
        if (dns != null)
            sb.append(Attribute.DNS.composeWith(dns));
        if (listenPort != null)
            sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
        if (mtu != null)
            sb.append(Attribute.MTU.composeWith(mtu));
        if (privateKey != null)
            sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey));
        return sb.toString();
    }
}