Sfoglia il codice sorgente

Tunnel: move state change into interface

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
master
Jason A. Donenfeld 4 anni fa
parent
commit
d9e9dd04af
6 ha cambiato i file con 23 aggiunte e 65 eliminazioni
  1. +0
    -18
      app/src/main/java/com/wireguard/android/backend/Backend.java
  2. +2
    -18
      app/src/main/java/com/wireguard/android/backend/GoBackend.java
  3. +13
    -0
      app/src/main/java/com/wireguard/android/backend/Tunnel.java
  4. +1
    -14
      app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
  5. +6
    -1
      app/src/main/java/com/wireguard/android/model/ObservableTunnel.java
  6. +1
    -14
      app/src/main/java/com/wireguard/android/model/TunnelManager.java

+ 0
- 18
app/src/main/java/com/wireguard/android/backend/Backend.java Vedi File

@@ -60,22 +60,4 @@ public interface Backend {
* @return The updated state of the tunnel.
*/
Tunnel.State setState(Tunnel tunnel, Tunnel.State state, @Nullable Config config) throws Exception;

interface TunnelStateChangeNotificationReceiver {
void tunnelStateChange(Tunnel tunnel, Tunnel.State state);
}

/**
* Register a state change notification callback.
*
* @param receiver The receiver object to receive the notification.
*/
void registerStateChangeNotification(TunnelStateChangeNotificationReceiver receiver);

/**
* Unregister a state change notification callback.
*
* @param receiver The receiver object to no longer receive the notification.
*/
void unregisterStateChangeNotification(TunnelStateChangeNotificationReceiver receiver);
}

+ 2
- 18
app/src/main/java/com/wireguard/android/backend/GoBackend.java Vedi File

@@ -5,7 +5,6 @@

package com.wireguard.android.backend;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
@@ -25,7 +24,6 @@ import com.wireguard.crypto.KeyFormatException;

import java.net.InetAddress;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -48,8 +46,6 @@ public final class GoBackend implements Backend {
@Nullable private Config currentConfig;
private int currentTunnelHandle = -1;

private final Set<TunnelStateChangeNotificationReceiver> notifiers = new HashSet<>();

public GoBackend(final Context context) {
SharedLibraryLoader.loadSharedLibrary(context, "wg-go");
this.context = context;
@@ -240,8 +236,7 @@ public final class GoBackend implements Backend {
currentConfig = null;
}

for (final TunnelStateChangeNotificationReceiver notifier : notifiers)
notifier.tunnelStateChange(tunnel, state);
tunnel.onStateChange(state);
}

private void startVpnService() {
@@ -249,16 +244,6 @@ public final class GoBackend implements Backend {
context.startService(new Intent(context, VpnService.class));
}

@Override
public void registerStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
notifiers.add(receiver);
}

@Override
public void unregisterStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
notifiers.remove(receiver);
}

public static class VpnService extends android.net.VpnService {
@Nullable private GoBackend owner;

@@ -286,8 +271,7 @@ public final class GoBackend implements Backend {
owner.currentTunnel = null;
owner.currentTunnelHandle = -1;
owner.currentConfig = null;
for (final TunnelStateChangeNotificationReceiver notifier : owner.notifiers)
notifier.tunnelStateChange(tunnel, State.DOWN);
tunnel.onStateChange(State.DOWN);
}
}
vpnService = vpnService.newIncompleteFuture();


+ 13
- 0
app/src/main/java/com/wireguard/android/backend/Tunnel.java Vedi File

@@ -29,5 +29,18 @@ public interface Tunnel {
return !NAME_PATTERN.matcher(name).matches();
}

/**
* Get the name of the tunnel, which should always pass the !isNameInvalid test.
*
* @return The name of the tunnel.
*/
String getName();

/**
* React to a change in state of the tunnel. Should only be directly called by Backend.
*
* @param newState The new state of the tunnel.
* @return The new state of the tunnel.
*/
void onStateChange(State newState);
}

+ 1
- 14
app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java Vedi File

@@ -23,7 +23,6 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -45,7 +44,6 @@ public final class WgQuickBackend implements Backend {
private final ToolsInstaller toolsInstaller;
private final File localTemporaryDir;
private final Map<Tunnel, Config> runningConfigs = new HashMap<>();
private final Set<TunnelStateChangeNotificationReceiver> notifiers = new HashSet<>();

public WgQuickBackend(final Context context, final RootShell rootShell, final ToolsInstaller toolsInstaller) {
localTemporaryDir = new File(context.getCacheDir(), "tmp");
@@ -155,17 +153,6 @@ public final class WgQuickBackend implements Backend {
else
runningConfigs.remove(tunnel);

for (final TunnelStateChangeNotificationReceiver notifier : notifiers)
notifier.tunnelStateChange(tunnel, state);
}

@Override
public void registerStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
notifiers.add(receiver);
}

@Override
public void unregisterStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
notifiers.remove(receiver);
tunnel.onStateChange(state);
}
}

+ 6
- 1
app/src/main/java/com/wireguard/android/model/ObservableTunnel.java Vedi File

@@ -96,7 +96,7 @@ public class ObservableTunnel extends BaseObservable implements Keyed<String>, T
return config;
}

public String onNameChanged(final String name) {
String onNameChanged(final String name) {
this.name = name;
notifyPropertyChanged(BR.name);
return name;
@@ -110,6 +110,11 @@ public class ObservableTunnel extends BaseObservable implements Keyed<String>, T
return state;
}

@Override
public void onStateChange(final State newState) {
onStateChanged(state);
}

@Nullable
Statistics onStatisticsChanged(@Nullable final Statistics statistics) {
this.statistics = statistics;


+ 1
- 14
app/src/main/java/com/wireguard/android/model/TunnelManager.java Vedi File

@@ -15,7 +15,6 @@ import androidx.annotation.Nullable;
import com.wireguard.android.Application;
import com.wireguard.android.BR;
import com.wireguard.android.R;
import com.wireguard.android.backend.Backend.TunnelStateChangeNotificationReceiver;
import com.wireguard.android.configStore.ConfigStore;
import com.wireguard.android.backend.Tunnel;
import com.wireguard.android.backend.Tunnel.State;
@@ -40,7 +39,7 @@ import java9.util.stream.StreamSupport;
* Maintains and mediates changes to the set of available WireGuard tunnels,
*/

public final class TunnelManager extends BaseObservable implements TunnelStateChangeNotificationReceiver {
public final class TunnelManager extends BaseObservable {
private static final Comparator<String> COMPARATOR = Comparators.<String>thenComparing(
String.CASE_INSENSITIVE_ORDER, Comparators.naturalOrder());
private static final String KEY_LAST_USED_TUNNEL = "last_used_tunnel";
@@ -57,13 +56,6 @@ public final class TunnelManager extends BaseObservable implements TunnelStateCh

public TunnelManager(final ConfigStore configStore) {
this.configStore = configStore;
Application.getBackendAsync().thenAccept(backend -> backend.registerStateChangeNotification(this));
}

@Override
protected void finalize() throws Throwable {
Application.getBackendAsync().thenAccept(backend -> backend.unregisterStateChangeNotification(this));
super.finalize();
}

static CompletionStage<State> getTunnelState(final ObservableTunnel tunnel) {
@@ -266,11 +258,6 @@ public final class TunnelManager extends BaseObservable implements TunnelStateCh
});
}

@Override
public void tunnelStateChange(final Tunnel tunnel, final State state) {
((ObservableTunnel)tunnel).onStateChanged(state);
}

public static final class IntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, @Nullable final Intent intent) {


Caricamento…
Annulla
Salva