Compare commits

..

1 Commits

Author SHA1 Message Date
NickVs2015 4941fcad23 feat: add edge-to-edge android 15 view 2026-03-24 13:12:45 +03:00
7 changed files with 66 additions and 92 deletions
@@ -54,19 +54,13 @@ public final class InetEndpoint {
}
if (uri.getPort() < 0 || uri.getPort() > 65535)
throw new ParseException(InetEndpoint.class, endpoint, "Missing/invalid port number");
String host = uri.getHost();
if (host == null)
throw new ParseException(InetEndpoint.class, endpoint, "Missing host");
// URI.getHost() wraps IPv6 literals in brackets; strip them so the numeric parser accepts it.
if (host.length() > 1 && host.charAt(0) == '[' && host.charAt(host.length() - 1) == ']')
host = host.substring(1, host.length() - 1);
try {
InetAddresses.parse(host);
InetAddresses.parse(uri.getHost());
// Parsing ths host as a numeric address worked, so we don't need to do DNS lookups.
return new InetEndpoint(host, true, uri.getPort());
return new InetEndpoint(uri.getHost(), true, uri.getPort());
} catch (final ParseException ignored) {
// Failed to parse the host as a numeric address, so it must be a DNS hostname/FQDN.
return new InetEndpoint(host, false, uri.getPort());
return new InetEndpoint(uri.getHost(), false, uri.getPort());
}
}
@@ -1,51 +0,0 @@
/*
* Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package org.amnezia.awg.config;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class InetEndpointTest {
@Test
public void ipv6_literal_is_parsed_as_canonical_numeric_host() throws ParseException {
final InetEndpoint endpoint = InetEndpoint.parse("[2001:db8::1]:51820");
// URI.getHost() yields the bracketed form; the stored host must be the bare, canonical one.
assertEquals("Host is stored without brackets", "2001:db8::1", endpoint.getHost());
assertEquals("Port is parsed", 51820, endpoint.getPort());
// toString() must re-add brackets so the userspace string is valid for amneziawg-go.
assertEquals("toString re-brackets the literal", "[2001:db8::1]:51820", endpoint.toString());
}
@Test
public void ipv4_endpoint_is_unchanged() throws ParseException {
final InetEndpoint endpoint = InetEndpoint.parse("192.0.2.1:51820");
assertEquals("192.0.2.1", endpoint.getHost());
assertEquals(51820, endpoint.getPort());
assertEquals("192.0.2.1:51820", endpoint.toString());
}
@Test
public void hostname_endpoint_is_preserved() throws ParseException {
final InetEndpoint endpoint = InetEndpoint.parse("vpn.example.com:51820");
assertEquals("vpn.example.com", endpoint.getHost());
assertEquals(51820, endpoint.getPort());
}
@Test
public void ipv6_endpoint_round_trips_to_userspace_string() throws BadConfigException {
final Peer peer = new Peer.Builder()
.parsePublicKey("vBN7qyUTb5lJtWYJ8LhbPio1Z4RcyBPGnqFBGn6O6Qg=")
.parseEndpoint("[2001:db8::1]:51820")
.build();
assertTrue(
"Userspace string keeps the bracketed IPv6 endpoint",
peer.toAwgUserspaceString().contains("endpoint=[2001:db8::1]:51820")
);
}
}
+1
View File
@@ -78,6 +78,7 @@
<activity
android:name=".activity.SettingsActivity"
android:label="@string/settings"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".activity.MainActivity" />
<activity
@@ -29,9 +29,12 @@ import kotlinx.coroutines.withContext
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (supportFragmentManager.findFragmentById(android.R.id.content) == null) {
setContentView(R.layout.activity_settings)
setSupportActionBar(findViewById(R.id.toolbar))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
if (supportFragmentManager.findFragmentById(R.id.settings_container) == null) {
supportFragmentManager.commit {
add(android.R.id.content, SettingsFragment())
add(R.id.settings_container, SettingsFragment())
}
}
}
@@ -63,11 +63,11 @@ class PeerProxy : BaseObservable, Parcelable {
@get:Bindable
val isAbleToExcludePrivateIps: Boolean
get() = allowedIpsState == AllowedIpsState.CONTAINS_PUBLIC_NETWORKS || allowedIpsState == AllowedIpsState.CONTAINS_WILDCARD
get() = allowedIpsState == AllowedIpsState.CONTAINS_IPV4_PUBLIC_NETWORKS || allowedIpsState == AllowedIpsState.CONTAINS_IPV4_WILDCARD
@get:Bindable
val isExcludingPrivateIps: Boolean
get() = allowedIpsState == AllowedIpsState.CONTAINS_PUBLIC_NETWORKS
get() = allowedIpsState == AllowedIpsState.CONTAINS_IPV4_PUBLIC_NETWORKS
private constructor(parcel: Parcel) {
allowedIps = parcel.readString() ?: ""
@@ -106,11 +106,12 @@ class PeerProxy : BaseObservable, Parcelable {
// the above sets of (valid) *networks*. We are not checking for a superset based on
// the individual addresses in each set.
val networkStrings: Collection<String> = getAllowedIpsSet()
// A wildcard in either family means private IPs can still be excluded.
if (networkStrings.containsAll(IPV4_WILDCARD) || networkStrings.containsAll(IPV6_WILDCARD))
AllowedIpsState.CONTAINS_WILDCARD
else if (networkStrings.containsAll(IPV4_PUBLIC_NETWORKS) || networkStrings.containsAll(IPV6_PUBLIC_NETWORKS))
AllowedIpsState.CONTAINS_PUBLIC_NETWORKS
// If allowedIps contains both the wildcard and the public networks, then private
// networks aren't excluded!
if (networkStrings.containsAll(IPV4_WILDCARD))
AllowedIpsState.CONTAINS_IPV4_WILDCARD
else if (networkStrings.containsAll(IPV4_PUBLIC_NETWORKS))
AllowedIpsState.CONTAINS_IPV4_PUBLIC_NETWORKS
else
AllowedIpsState.OTHER
} else {
@@ -127,29 +128,31 @@ class PeerProxy : BaseObservable, Parcelable {
private fun getAllowedIpsSet() = setOf(*Attribute.split(allowedIps))
// For each family, replace the first instance of the wildcard with its public network list, or vice versa.
// Replace the first instance of the wildcard with the public network list, or vice versa.
// DNS servers only need to handled specially when we're excluding private IPs.
fun setExcludingPrivateIps(excludingPrivateIps: Boolean) {
if (!isAbleToExcludePrivateIps || isExcludingPrivateIps == excludingPrivateIps) return
val replacements = listOf(IPV4_WILDCARD to IPV4_PUBLIC_NETWORKS, IPV6_WILDCARD to IPV6_PUBLIC_NETWORKS)
.map { (wildcard, public) -> if (excludingPrivateIps) wildcard to public else public to wildcard }
val oldNetworks = if (excludingPrivateIps) IPV4_WILDCARD else IPV4_PUBLIC_NETWORKS
val newNetworks = if (excludingPrivateIps) IPV4_PUBLIC_NETWORKS else IPV4_WILDCARD
val input: Collection<String> = getAllowedIpsSet()
val output: MutableCollection<String?> = LinkedHashSet(input.size)
val replaced = BooleanArray(replacements.size)
val outputSize = input.size - oldNetworks.size + newNetworks.size
val output: MutableCollection<String?> = LinkedHashSet(outputSize)
var replaced = false
// Replace the first instance of the wildcard with the public network list, or vice versa.
for (network in input) {
val family = replacements.indexOfFirst { it.first.contains(network) }
if (family >= 0) {
if (!replaced[family]) {
for (replacement in replacements[family].second) if (!output.contains(replacement)) output.add(replacement)
replaced[family] = true
if (oldNetworks.contains(network)) {
if (!replaced) {
for (replacement in newNetworks) if (!output.contains(replacement)) output.add(replacement)
replaced = true
}
} else if (!output.contains(network)) {
output.add(network)
}
}
// DNS servers only need to be handled specially when we're excluding private IPs.
// DNS servers only need to handled specially when we're excluding private IPs.
if (excludingPrivateIps) output.addAll(dnsRoutes) else output.removeAll(dnsRoutes)
allowedIps = Attribute.join(output)
allowedIpsState = if (excludingPrivateIps) AllowedIpsState.CONTAINS_PUBLIC_NETWORKS else AllowedIpsState.CONTAINS_WILDCARD
allowedIpsState = if (excludingPrivateIps) AllowedIpsState.CONTAINS_IPV4_PUBLIC_NETWORKS else AllowedIpsState.CONTAINS_IPV4_WILDCARD
notifyPropertyChanged(BR.allowedIps)
notifyPropertyChanged(BR.excludingPrivateIps)
}
@@ -166,10 +169,8 @@ class PeerProxy : BaseObservable, Parcelable {
}
private fun setInterfaceDns(dnsServers: CharSequence) {
val newDnsRoutes = Attribute.split(dnsServers)
.filter { it.contains(":") || it.matches(Regex("""^\d{1,3}(\.\d{1,3}){3}$""")) }
.map { if (it.contains(":")) "$it/128" else "$it/32" }
if (allowedIpsState == AllowedIpsState.CONTAINS_PUBLIC_NETWORKS) {
val newDnsRoutes = Attribute.split(dnsServers).filter { !it.contains(":") }.map { "$it/32" }
if (allowedIpsState == AllowedIpsState.CONTAINS_IPV4_PUBLIC_NETWORKS) {
val input = getAllowedIpsSet()
// Yes, this is quadratic in the number of DNS servers, but most users have 1 or 2.
val output = input.filter { !dnsRoutes.contains(it) || newDnsRoutes.contains(it) }.plus(newDnsRoutes).distinct()
@@ -208,7 +209,7 @@ class PeerProxy : BaseObservable, Parcelable {
}
private enum class AllowedIpsState {
CONTAINS_PUBLIC_NETWORKS, CONTAINS_WILDCARD, INVALID, OTHER
CONTAINS_IPV4_PUBLIC_NETWORKS, CONTAINS_IPV4_WILDCARD, INVALID, OTHER
}
private class InterfaceDnsListener constructor(peerProxy: PeerProxy) : OnPropertyChangedCallback() {
@@ -289,11 +290,5 @@ class PeerProxy : BaseObservable, Parcelable {
"193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4"
)
private val IPV4_WILDCARD = setOf("0.0.0.0/0")
// ::/0 minus the private ranges fc00::/7 (unique local) and fe80::/10 (link-local).
private val IPV6_PUBLIC_NETWORKS = setOf(
"::/1", "8000::/2", "c000::/3", "e000::/4", "f000::/5", "f800::/6",
"fe00::/9", "fec0::/10", "ff00::/8"
)
private val IPV6_WILDCARD = setOf("::/0")
}
}
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
+5
View File
@@ -15,6 +15,11 @@
<!-- Various additional API-specific features in values-v*/styles.xml -->
<style name="AppTheme" parent="AppThemeBase" />
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AmneziaWgTheme.MaterialCardView" parent="Widget.Material3.CardView.Elevated">
<item name="cornerRadius">4dp</item>
<item name="contentPadding">8dp</item>