Update application state based on wg-quick

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-04-23 16:37:42 +02:00
parent a1c6f4b3b7
commit 88cf839c90
6 changed files with 38 additions and 3 deletions

View File

@ -43,13 +43,18 @@
android:parentActivityName=".activity.MainActivity" /> android:parentActivityName=".activity.MainActivity" />
<receiver android:name=".BootShutdownReceiver"> <receiver android:name=".BootShutdownReceiver">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" /> <action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name=".backend.WgQuickBackend$WgQuickChangeReceiver">
<intent-filter>
<action android:name="com.wireguard.android.WGQUICK_CHANGE" />
</intent-filter>
</receiver>
<service <service
android:name=".backend.GoBackend$VpnService" android:name=".backend.GoBackend$VpnService"
android:permission="android.permission.BIND_VPN_SERVICE" > android:permission="android.permission.BIND_VPN_SERVICE" >

View File

@ -1,8 +1,11 @@
package com.wireguard.android.backend; package com.wireguard.android.backend;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.util.Log; import android.util.Log;
import com.wireguard.android.Application;
import com.wireguard.android.model.Tunnel; import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State; import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.model.Tunnel.Statistics; import com.wireguard.android.model.Tunnel.Statistics;
@ -111,4 +114,12 @@ public final class WgQuickBackend implements Backend {
if (result != 0) if (result != 0)
throw new Exception("Unable to configure tunnel (wg-quick returned " + result + ')'); throw new Exception("Unable to configure tunnel (wg-quick returned " + result + ')');
} }
public final static class WgQuickChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Refreshing tunnel states");
Application.getComponent().getTunnelManager().refreshTunnelStates();
}
}
} }

View File

@ -128,6 +128,15 @@ public final class TunnelManager extends BaseObservable {
return tunnels; return tunnels;
} }
public void refreshTunnelStates() {
asyncWorker.supplyAsync(backend::enumerate)
.thenAccept(running -> {
for (final Tunnel tunnel : tunnels)
tunnel.onStateChanged(running.contains(tunnel.getName()) ? State.UP : State.DOWN);
})
.whenComplete(ExceptionLoggers.E);
}
public void onCreate() { public void onCreate() {
asyncWorker.supplyAsync(configStore::enumerate) asyncWorker.supplyAsync(configStore::enumerate)
.thenAcceptBoth(asyncWorker.supplyAsync(backend::enumerate), this::onTunnelsLoaded) .thenAcceptBoth(asyncWorker.supplyAsync(backend::enumerate), this::onTunnelsLoaded)

View File

@ -44,7 +44,7 @@ public class RootShell {
final File cacheDir = context.getCacheDir(); final File cacheDir = context.getCacheDir();
localBinaryDir = new File(cacheDir, "bin"); localBinaryDir = new File(cacheDir, "bin");
localTemporaryDir = new File(cacheDir, "tmp"); localTemporaryDir = new File(cacheDir, "tmp");
preamble = String.format("export PATH=\"%s:$PATH\" TMPDIR='%s'; id -u\n", preamble = String.format("export CALLING_PACKAGE=com.wireguard.android; export PATH=\"%s:$PATH\" TMPDIR='%s'; id -u\n",
localBinaryDir, localTemporaryDir); localBinaryDir, localTemporaryDir);
} }

View File

@ -446,6 +446,14 @@ static void set_config(const char *iface, const char *config)
exit(WIFEXITED(ret) ? WEXITSTATUS(ret) : EIO); exit(WIFEXITED(ret) ? WEXITSTATUS(ret) : EIO);
} }
static void broadcast_change(void)
{
const char *pkg = getenv("CALLING_PACKAGE");
if (!pkg || strcmp(pkg, "com.wireguard.android"))
cmd("am broadcast -a com.wireguard.android.WGQUICK_CHANGE com.wireguard.android");
}
static void print_search_paths(FILE *file, const char *prefix) static void print_search_paths(FILE *file, const char *prefix)
{ {
_cleanup_free_ char *paths = strdup(WG_CONFIG_SEARCH_PATHS); _cleanup_free_ char *paths = strdup(WG_CONFIG_SEARCH_PATHS);
@ -502,6 +510,7 @@ static void cmd_up(const char *iface, const char *config, unsigned int mtu, cons
set_dnses(netid, dnses); set_dnses(netid, dnses);
set_routes(iface, netid); set_routes(iface, netid);
set_mtu(iface, mtu); set_mtu(iface, mtu);
broadcast_change();
free(cleanup_iface); free(cleanup_iface);
cleanup_iface = NULL; cleanup_iface = NULL;
@ -528,6 +537,7 @@ static void cmd_down(const char *iface)
} }
del_if(iface); del_if(iface);
broadcast_change();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

@ -1 +1 @@
Subproject commit 14c6c9a7a4427bd5f07276b501e7f61ed3dbeb65 Subproject commit 0715bdf353dd670bf7c3af332a8dabbe30187a1d