@@ -7,6 +7,14 @@ import android.os.Parcelable;
|
||||
|
||||
import com.android.databinding.library.baseAdapters.BR;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Represents the configuration for a WireGuard peer (a [Peer] block).
|
||||
*/
|
||||
@@ -24,22 +32,29 @@ public class Peer extends BaseObservable implements Parcelable {
|
||||
}
|
||||
};
|
||||
|
||||
private String[] allowedIPList;
|
||||
private String endpoint;
|
||||
private String persistentKeepalive;
|
||||
private List<IPCidr> allowedIPsList;
|
||||
private InetSocketAddress endpoint;
|
||||
private int persistentKeepalive;
|
||||
private String preSharedKey;
|
||||
private String publicKey;
|
||||
|
||||
public Peer() {
|
||||
allowedIPList = new String[0];
|
||||
allowedIPsList = new LinkedList<>();
|
||||
}
|
||||
|
||||
private Peer(final Parcel in) {
|
||||
allowedIPList = in.createStringArray();
|
||||
endpoint = in.readString();
|
||||
persistentKeepalive = in.readString();
|
||||
allowedIPsList = in.createTypedArrayList(IPCidr.CREATOR);
|
||||
String host = in.readString();
|
||||
int port = in.readInt();
|
||||
if (host != null && !host.isEmpty() && port > 0)
|
||||
endpoint = InetSocketAddress.createUnresolved(host, port);
|
||||
persistentKeepalive = in.readInt();
|
||||
preSharedKey = in.readString();
|
||||
if (preSharedKey != null && preSharedKey.isEmpty())
|
||||
preSharedKey = null;
|
||||
publicKey = in.readString();
|
||||
if (publicKey != null && publicKey.isEmpty())
|
||||
publicKey = null;
|
||||
}
|
||||
|
||||
public static Peer newInstance() {
|
||||
@@ -53,23 +68,53 @@ public class Peer extends BaseObservable implements Parcelable {
|
||||
|
||||
|
||||
@Bindable
|
||||
public String getAllowedIPsString() { return Attribute.listToString(allowedIPList); }
|
||||
|
||||
@Bindable
|
||||
public String[] getAllowedIPs() {
|
||||
return allowedIPList;
|
||||
public String getAllowedIPsString() {
|
||||
if (allowedIPsList.isEmpty())
|
||||
return null;
|
||||
return Attribute.listToString(allowedIPsList);
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public String getEndpoint() {
|
||||
public IPCidr[] getAllowedIPs() {
|
||||
return allowedIPsList.toArray(new IPCidr[allowedIPsList.size()]);
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public InetSocketAddress getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public String getPersistentKeepalive() {
|
||||
public String getEndpointString() {
|
||||
if (endpoint == null)
|
||||
return null;
|
||||
return String.format("%s:%d", endpoint.getHostString(), endpoint.getPort());
|
||||
}
|
||||
|
||||
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("[%s]:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
|
||||
return String.format("%s:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public int getPersistentKeepalive() {
|
||||
return persistentKeepalive;
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public String getPersistentKeepaliveString() {
|
||||
if (persistentKeepalive == 0)
|
||||
return null;
|
||||
return new Integer(persistentKeepalive).toString();
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public String getPreSharedKey() {
|
||||
return preSharedKey;
|
||||
@@ -80,14 +125,14 @@ public class Peer extends BaseObservable implements Parcelable {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
public void parse(final String line) {
|
||||
public void parse(final String line) throws UnknownHostException {
|
||||
final Attribute key = Attribute.match(line);
|
||||
if (key == Attribute.ALLOWED_IPS)
|
||||
addAllowedIPs(key.parseList(line));
|
||||
else if (key == Attribute.ENDPOINT)
|
||||
setEndpoint(key.parse(line));
|
||||
setEndpointString(key.parse(line));
|
||||
else if (key == Attribute.PERSISTENT_KEEPALIVE)
|
||||
setPersistentKeepalive(key.parse(line));
|
||||
setPersistentKeepaliveString(key.parse(line));
|
||||
else if (key == Attribute.PRESHARED_KEY)
|
||||
setPreSharedKey(key.parse(line));
|
||||
else if (key == Attribute.PUBLIC_KEY)
|
||||
@@ -96,38 +141,60 @@ public class Peer extends BaseObservable implements Parcelable {
|
||||
throw new IllegalArgumentException(line);
|
||||
}
|
||||
|
||||
public void addAllowedIPs(String[] allowedIPs) {
|
||||
if (allowedIPs == null || allowedIPs.length == 0)
|
||||
return;
|
||||
String[] both = new String[allowedIPs.length + this.allowedIPList.length];
|
||||
System.arraycopy(this.allowedIPList, 0, both, 0, this.allowedIPList.length);
|
||||
System.arraycopy(allowedIPs, 0, both, this.allowedIPList.length, allowedIPs.length);
|
||||
setAllowedIPs(both);
|
||||
}
|
||||
|
||||
public void setAllowedIPs(String[] allowedIPs) {
|
||||
if (allowedIPs == null)
|
||||
allowedIPs = new String[0];
|
||||
this.allowedIPList = allowedIPs;
|
||||
public void addAllowedIPs(String[] allowedIPs) throws UnknownHostException {
|
||||
if (allowedIPs != null && allowedIPs.length > 0) {
|
||||
for (final String allowedIP : allowedIPs) {
|
||||
if (allowedIP.isEmpty())
|
||||
throw new UnknownHostException("{empty}");
|
||||
this.allowedIPsList.add(new IPCidr(allowedIP));
|
||||
}
|
||||
}
|
||||
notifyPropertyChanged(BR.allowedIPs);
|
||||
notifyPropertyChanged(BR.allowedIPsString);
|
||||
}
|
||||
|
||||
public void setAllowedIPsString(String allowedIPsString) {
|
||||
setAllowedIPs(Attribute.stringToList(allowedIPsString));
|
||||
public void setAllowedIPsString(final String allowedIPsString) {
|
||||
try {
|
||||
this.allowedIPsList.clear();
|
||||
addAllowedIPs(Attribute.stringToList(allowedIPsString));
|
||||
} catch (Exception e) {
|
||||
this.allowedIPsList.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
if (endpoint != null && endpoint.isEmpty())
|
||||
endpoint = null;
|
||||
public void setEndpoint(InetSocketAddress endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
notifyPropertyChanged(BR.endpoint);
|
||||
notifyPropertyChanged(BR.endpointString);
|
||||
}
|
||||
|
||||
public void setPersistentKeepalive(String persistentKeepalive) {
|
||||
if (persistentKeepalive != null && persistentKeepalive.isEmpty())
|
||||
persistentKeepalive = null;
|
||||
public void setEndpointString(final String endpoint) {
|
||||
if (endpoint != null && !endpoint.isEmpty()) {
|
||||
InetSocketAddress constructedEndpoint;
|
||||
try {
|
||||
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
|
||||
throw new Exception();
|
||||
URI uri = new URI("wg://" + endpoint);
|
||||
constructedEndpoint = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
|
||||
} catch (Exception e) {
|
||||
return; /* XXX: Uh oh. */
|
||||
}
|
||||
setEndpoint(constructedEndpoint);
|
||||
} else
|
||||
setEndpoint(null);
|
||||
}
|
||||
|
||||
public void setPersistentKeepalive(int persistentKeepalive) {
|
||||
this.persistentKeepalive = persistentKeepalive;
|
||||
notifyPropertyChanged(BR.persistentKeepalive);
|
||||
notifyPropertyChanged(BR.persistentKeepaliveString);
|
||||
}
|
||||
|
||||
public void setPersistentKeepaliveString(String persistentKeepalive) {
|
||||
if (persistentKeepalive != null && !persistentKeepalive.isEmpty())
|
||||
setPersistentKeepalive(Integer.parseInt(persistentKeepalive, 10));
|
||||
else
|
||||
setPersistentKeepalive(0);
|
||||
}
|
||||
|
||||
public void setPreSharedKey(String preSharedKey) {
|
||||
@@ -147,11 +214,11 @@ public class Peer extends BaseObservable implements Parcelable {
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder().append("[Peer]\n");
|
||||
if (allowedIPList != null)
|
||||
sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPList));
|
||||
if (!allowedIPsList.isEmpty())
|
||||
sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPsList));
|
||||
if (endpoint != null)
|
||||
sb.append(Attribute.ENDPOINT.composeWith(endpoint));
|
||||
if (persistentKeepalive != null)
|
||||
sb.append(Attribute.ENDPOINT.composeWith(getEndpointString()));
|
||||
if (persistentKeepalive != 0)
|
||||
sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
|
||||
if (preSharedKey != null)
|
||||
sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey));
|
||||
@@ -162,9 +229,10 @@ public class Peer extends BaseObservable implements Parcelable {
|
||||
|
||||
@Override
|
||||
public void writeToParcel(final Parcel dest, final int flags) {
|
||||
dest.writeStringArray(allowedIPList);
|
||||
dest.writeString(endpoint);
|
||||
dest.writeString(persistentKeepalive);
|
||||
dest.writeTypedList(allowedIPsList);
|
||||
dest.writeString(endpoint == null ? null : endpoint.getHostString());
|
||||
dest.writeInt(endpoint == null ? 0 : endpoint.getPort());
|
||||
dest.writeInt(persistentKeepalive);
|
||||
dest.writeString(preSharedKey);
|
||||
dest.writeString(publicKey);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user