2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
|
|
|
import android.databinding.BaseObservable;
|
|
|
|
import android.databinding.Bindable;
|
2017-08-24 06:47:44 +02:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
|
|
|
import com.android.databinding.library.baseAdapters.BR;
|
2018-04-28 04:45:17 +02:00
|
|
|
import com.wireguard.crypto.KeyEncoding;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
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;
|
2018-04-27 18:33:39 +02:00
|
|
|
import java.util.Locale;
|
2018-04-18 05:28:31 +02:00
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
/**
|
|
|
|
* Represents the configuration for a WireGuard peer (a [Peer] block).
|
|
|
|
*/
|
|
|
|
|
2018-04-30 05:00:51 +02:00
|
|
|
public class Peer {
|
2018-04-18 05:28:31 +02:00
|
|
|
private List<IPCidr> allowedIPsList;
|
|
|
|
private InetSocketAddress endpoint;
|
|
|
|
private int persistentKeepalive;
|
2017-08-24 09:00:53 +02:00
|
|
|
private String preSharedKey;
|
2017-07-30 00:30:33 +02:00
|
|
|
private String publicKey;
|
2018-01-01 09:06:37 +01:00
|
|
|
public Peer() {
|
2018-04-18 05:28:31 +02:00
|
|
|
allowedIPsList = new LinkedList<>();
|
2017-08-20 01:50:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:39:12 +02:00
|
|
|
private void addAllowedIPs(String[] allowedIPs) {
|
|
|
|
if (allowedIPs != null && allowedIPs.length > 0) {
|
|
|
|
for (final String allowedIP : allowedIPs) {
|
|
|
|
this.allowedIPsList.add(new IPCidr(allowedIP));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IPCidr[] getAllowedIPs() {
|
|
|
|
return allowedIPsList.toArray(new IPCidr[allowedIPsList.size()]);
|
|
|
|
}
|
2018-04-17 05:20:17 +02:00
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private String getAllowedIPsString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (allowedIPsList.isEmpty())
|
|
|
|
return null;
|
|
|
|
return Attribute.listToString(allowedIPsList);
|
|
|
|
}
|
2018-04-17 05:20:17 +02:00
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public InetSocketAddress getEndpoint() {
|
2017-07-30 00:30:33 +02:00
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private String getEndpointString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (endpoint == null)
|
|
|
|
return null;
|
2018-04-27 18:33:39 +02:00
|
|
|
return String.format(Locale.getDefault(), "%s:%d", endpoint.getHostString(), endpoint.getPort());
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getPersistentKeepalive() {
|
2017-07-30 00:30:33 +02:00
|
|
|
return persistentKeepalive;
|
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private String getPersistentKeepaliveString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (persistentKeepalive == 0)
|
|
|
|
return null;
|
2018-04-27 18:33:39 +02:00
|
|
|
return Integer.valueOf(persistentKeepalive).toString();
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:00:53 +02:00
|
|
|
public String getPreSharedKey() {
|
|
|
|
return preSharedKey;
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
public String getPublicKey() {
|
|
|
|
return publicKey;
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:39:12 +02:00
|
|
|
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(Locale.getDefault(),
|
|
|
|
"[%s]:%d",
|
|
|
|
endpoint.getAddress().getHostAddress(),
|
|
|
|
endpoint.getPort());
|
|
|
|
return String.format(Locale.getDefault(),
|
|
|
|
"%s:%d",
|
|
|
|
endpoint.getAddress().getHostAddress(),
|
|
|
|
endpoint.getPort());
|
|
|
|
}
|
|
|
|
|
2018-04-24 03:31:10 +02:00
|
|
|
public void parse(final String line) {
|
2017-07-30 00:30:33 +02:00
|
|
|
final Attribute key = Attribute.match(line);
|
|
|
|
if (key == Attribute.ALLOWED_IPS)
|
2018-04-17 05:20:17 +02:00
|
|
|
addAllowedIPs(key.parseList(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.ENDPOINT)
|
2018-04-18 05:28:31 +02:00
|
|
|
setEndpointString(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.PERSISTENT_KEEPALIVE)
|
2018-04-18 05:28:31 +02:00
|
|
|
setPersistentKeepaliveString(key.parse(line));
|
2017-11-26 23:45:41 +01:00
|
|
|
else if (key == Attribute.PRESHARED_KEY)
|
2018-01-01 09:06:37 +01:00
|
|
|
setPreSharedKey(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.PUBLIC_KEY)
|
2018-01-01 09:06:37 +01:00
|
|
|
setPublicKey(key.parse(line));
|
2017-08-13 14:24:03 +02:00
|
|
|
else
|
|
|
|
throw new IllegalArgumentException(line);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setAllowedIPsString(final String allowedIPsString) {
|
2018-04-24 03:31:10 +02:00
|
|
|
this.allowedIPsList.clear();
|
|
|
|
addAllowedIPs(Attribute.stringToList(allowedIPsString));
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setEndpoint(InetSocketAddress endpoint) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.endpoint = endpoint;
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setEndpointString(final String endpoint) {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (endpoint != null && !endpoint.isEmpty()) {
|
|
|
|
InetSocketAddress constructedEndpoint;
|
2018-04-24 03:31:10 +02:00
|
|
|
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
|
2018-04-30 18:39:12 +02:00
|
|
|
throw new IllegalArgumentException("Forbidden characters in endpoint");
|
2018-04-24 03:31:10 +02:00
|
|
|
URI uri;
|
2018-04-18 05:28:31 +02:00
|
|
|
try {
|
2018-04-24 03:31:10 +02:00
|
|
|
uri = new URI("wg://" + endpoint);
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
2018-04-24 03:31:10 +02:00
|
|
|
constructedEndpoint = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
|
2018-04-18 05:28:31 +02:00
|
|
|
setEndpoint(constructedEndpoint);
|
|
|
|
} else
|
|
|
|
setEndpoint(null);
|
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setPersistentKeepalive(int persistentKeepalive) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.persistentKeepalive = persistentKeepalive;
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setPersistentKeepaliveString(String persistentKeepalive) {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (persistentKeepalive != null && !persistentKeepalive.isEmpty())
|
|
|
|
setPersistentKeepalive(Integer.parseInt(persistentKeepalive, 10));
|
|
|
|
else
|
|
|
|
setPersistentKeepalive(0);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setPreSharedKey(String preSharedKey) {
|
2017-08-24 09:00:53 +02:00
|
|
|
if (preSharedKey != null && preSharedKey.isEmpty())
|
|
|
|
preSharedKey = null;
|
2018-04-28 04:45:17 +02:00
|
|
|
if (preSharedKey != null)
|
|
|
|
KeyEncoding.keyFromBase64(preSharedKey);
|
2017-08-24 09:00:53 +02:00
|
|
|
this.preSharedKey = preSharedKey;
|
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setPublicKey(String publicKey) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (publicKey != null && publicKey.isEmpty())
|
|
|
|
publicKey = null;
|
2018-04-28 04:45:17 +02:00
|
|
|
if (publicKey != null)
|
|
|
|
KeyEncoding.keyFromBase64(publicKey);
|
2017-07-30 00:30:33 +02:00
|
|
|
this.publicKey = publicKey;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:51:32 +01:00
|
|
|
@Override
|
2017-07-30 00:30:33 +02:00
|
|
|
public String toString() {
|
|
|
|
final StringBuilder sb = new StringBuilder().append("[Peer]\n");
|
2018-04-18 05:28:31 +02:00
|
|
|
if (!allowedIPsList.isEmpty())
|
|
|
|
sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPsList));
|
2017-07-30 00:30:33 +02:00
|
|
|
if (endpoint != null)
|
2018-04-18 05:28:31 +02:00
|
|
|
sb.append(Attribute.ENDPOINT.composeWith(getEndpointString()));
|
|
|
|
if (persistentKeepalive != 0)
|
2017-07-30 00:30:33 +02:00
|
|
|
sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
|
2017-08-24 09:00:53 +02:00
|
|
|
if (preSharedKey != null)
|
2017-11-26 23:45:41 +01:00
|
|
|
sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey));
|
2017-07-30 00:30:33 +02:00
|
|
|
if (publicKey != null)
|
|
|
|
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2018-04-30 18:39:12 +02:00
|
|
|
|
|
|
|
public static class Observable extends BaseObservable implements Parcelable {
|
|
|
|
public static final Creator<Observable> CREATOR = new Creator<Observable>() {
|
|
|
|
@Override
|
|
|
|
public Observable createFromParcel(final Parcel in) {
|
|
|
|
return new Observable(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Observable[] newArray(final int size) {
|
|
|
|
return new Observable[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private String allowedIPs;
|
|
|
|
private String endpoint;
|
|
|
|
private String persistentKeepalive;
|
|
|
|
private String preSharedKey;
|
|
|
|
private String publicKey;
|
|
|
|
|
|
|
|
public Observable(Peer parent) {
|
|
|
|
loadData(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Observable(final Parcel in) {
|
|
|
|
allowedIPs = in.readString();
|
|
|
|
endpoint = in.readString();
|
|
|
|
persistentKeepalive = in.readString();
|
|
|
|
preSharedKey = in.readString();
|
|
|
|
publicKey = in.readString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Observable newInstance() {
|
|
|
|
return new Observable(new Peer());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void commitData(Peer parent) {
|
|
|
|
parent.setAllowedIPsString(this.allowedIPs);
|
|
|
|
parent.setEndpointString(this.endpoint);
|
|
|
|
parent.setPersistentKeepaliveString(this.persistentKeepalive);
|
|
|
|
parent.setPreSharedKey(this.preSharedKey);
|
|
|
|
parent.setPublicKey(this.publicKey);
|
|
|
|
if (parent.getPublicKey() == null)
|
|
|
|
throw new IllegalArgumentException("Peer public key may not be empty");
|
|
|
|
loadData(parent);
|
|
|
|
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 loadData(Peer parent) {
|
|
|
|
this.allowedIPs = parent.getAllowedIPsString();
|
|
|
|
this.endpoint = parent.getEndpointString();
|
|
|
|
this.persistentKeepalive = parent.getPersistentKeepaliveString();
|
|
|
|
this.preSharedKey = parent.getPreSharedKey();
|
|
|
|
this.publicKey = parent.getPublicKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAllowedIPs(String allowedIPs) {
|
|
|
|
this.allowedIPs = allowedIPs;
|
|
|
|
notifyPropertyChanged(BR.allowedIPs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEndpoint(String endpoint) {
|
|
|
|
this.endpoint = endpoint;
|
|
|
|
notifyPropertyChanged(BR.endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPersistentKeepalive(String persistentKeepalive) {
|
|
|
|
this.persistentKeepalive = persistentKeepalive;
|
|
|
|
notifyPropertyChanged(BR.persistentKeepalive);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPreSharedKey(String preSharedKey) {
|
|
|
|
this.preSharedKey = preSharedKey;
|
|
|
|
notifyPropertyChanged(BR.preSharedKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPublicKey(String publicKey) {
|
|
|
|
this.publicKey = publicKey;
|
|
|
|
notifyPropertyChanged(BR.publicKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|