2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
|
|
|
import android.databinding.BaseObservable;
|
|
|
|
import android.databinding.Bindable;
|
|
|
|
import android.databinding.Observable;
|
|
|
|
|
|
|
|
import com.wireguard.android.BR;
|
2017-08-01 08:12:59 +02:00
|
|
|
import com.wireguard.crypto.Keypair;
|
2017-08-08 18:22:41 +02:00
|
|
|
import com.wireguard.crypto.KeyEncoding;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the configuration for a WireGuard interface (an [Interface] block).
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class Interface extends BaseObservable implements Observable {
|
|
|
|
private String address;
|
|
|
|
private String dns;
|
|
|
|
private String listenPort;
|
2017-08-01 08:12:59 +02:00
|
|
|
private Keypair keypair;
|
2017-07-30 00:30:33 +02:00
|
|
|
private String mtu;
|
2017-08-01 08:12:59 +02:00
|
|
|
|
|
|
|
public void generateKeypair() {
|
|
|
|
keypair = new Keypair();
|
|
|
|
notifyPropertyChanged(BR.privateKey);
|
|
|
|
notifyPropertyChanged(BR.publicKey);
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
|
|
|
|
@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() {
|
2017-08-01 08:12:59 +02:00
|
|
|
return keypair != null ? keypair.getPrivateKey() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getPublicKey() {
|
|
|
|
return keypair != null ? keypair.getPublicKey() : null;
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void parseFrom(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)
|
2017-08-01 08:12:59 +02:00
|
|
|
keypair = new Keypair(key.parseFrom(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setAddress(String address) {
|
|
|
|
this.address = address;
|
|
|
|
notifyPropertyChanged(BR.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDns(String dns) {
|
|
|
|
this.dns = dns;
|
|
|
|
notifyPropertyChanged(BR.dns);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setListenPort(String listenPort) {
|
|
|
|
this.listenPort = listenPort;
|
|
|
|
notifyPropertyChanged(BR.listenPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMtu(String mtu) {
|
|
|
|
this.mtu = mtu;
|
|
|
|
notifyPropertyChanged(BR.mtu);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPrivateKey(String privateKey) {
|
2017-08-09 09:51:54 +02:00
|
|
|
if (privateKey != null && !privateKey.isEmpty()) {
|
|
|
|
// Avoid exceptions from Keypair while the user is typing.
|
|
|
|
if (privateKey.length() != KeyEncoding.KEY_LENGTH_BASE64)
|
|
|
|
return;
|
|
|
|
keypair = new Keypair(privateKey);
|
|
|
|
} else {
|
|
|
|
keypair = null;
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
notifyPropertyChanged(BR.privateKey);
|
2017-08-01 08:12:59 +02:00
|
|
|
notifyPropertyChanged(BR.publicKey);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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));
|
2017-08-01 08:12:59 +02:00
|
|
|
if (keypair != null)
|
|
|
|
sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey()));
|
2017-07-30 00:30:33 +02:00
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|