2017-08-13 14:24:03 +02:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
|
|
|
import android.app.Service;
|
2017-08-17 12:45:40 +02:00
|
|
|
import android.content.ComponentName;
|
2017-08-13 14:24:03 +02:00
|
|
|
import android.content.Intent;
|
2017-08-16 11:28:41 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-08-13 14:24:03 +02:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Binder;
|
2017-08-17 12:45:40 +02:00
|
|
|
import android.os.Build;
|
2017-08-13 14:24:03 +02:00
|
|
|
import android.os.IBinder;
|
2017-08-16 11:28:41 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2017-08-17 12:45:40 +02:00
|
|
|
import android.service.quicksettings.TileService;
|
2017-08-13 14:24:03 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.wireguard.config.Config;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.FilenameFilter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Collections;
|
2017-08-16 11:36:12 +02:00
|
|
|
import java.util.HashSet;
|
2017-08-13 14:24:03 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-08-16 11:36:12 +02:00
|
|
|
import java.util.Set;
|
2017-08-13 14:24:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Service that handles config state coordination and all background processing for the application.
|
|
|
|
*/
|
|
|
|
|
2017-08-16 11:28:41 +02:00
|
|
|
public class VpnService extends Service
|
|
|
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2017-08-17 11:50:41 +02:00
|
|
|
public static final String KEY_ENABLED_CONFIGS = "enabled_configs";
|
|
|
|
public static final String KEY_PRIMARY_CONFIG = "primary_config";
|
|
|
|
public static final String KEY_RESTORE_ON_BOOT = "restore_on_boot";
|
2017-08-13 14:24:03 +02:00
|
|
|
private static final String TAG = "VpnService";
|
|
|
|
|
|
|
|
private static VpnService instance;
|
|
|
|
|
|
|
|
public static VpnService getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private final IBinder binder = new Binder();
|
2017-08-17 09:43:15 +02:00
|
|
|
private final ObservableTreeMap<String, Config> configurations = new ObservableTreeMap<>();
|
2017-08-16 11:36:12 +02:00
|
|
|
private final Set<String> enabledConfigs = new HashSet<>();
|
2017-08-16 11:28:41 +02:00
|
|
|
private SharedPreferences preferences;
|
2017-08-17 11:50:41 +02:00
|
|
|
private String primaryName;
|
2017-08-13 14:24:03 +02:00
|
|
|
private RootShell rootShell;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new configuration to the set of known configurations. The configuration will initially
|
|
|
|
* be disabled. The configuration's name must be unique within the set of known configurations.
|
|
|
|
*
|
|
|
|
* @param config The configuration to add.
|
|
|
|
*/
|
|
|
|
public void add(final Config config) {
|
|
|
|
new ConfigUpdater(null, config, false).execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to disable and tear down an interface for this configuration. The configuration's
|
|
|
|
* enabled state will be updated the operation is successful. If this configuration is already
|
|
|
|
* disconnected, or it is not a known configuration, no changes will be made.
|
|
|
|
*
|
|
|
|
* @param name The name of the configuration (in the set of known configurations) to disable.
|
|
|
|
*/
|
|
|
|
public void disable(final String name) {
|
|
|
|
final Config config = configurations.get(name);
|
|
|
|
if (config == null || !config.isEnabled())
|
|
|
|
return;
|
|
|
|
new ConfigDisabler(config).execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to set up and enable an interface for this configuration. The configuration's enabled
|
|
|
|
* state will be updated if the operation is successful. If this configuration is already
|
|
|
|
* enabled, or it is not a known configuration, no changes will be made.
|
|
|
|
*
|
|
|
|
* @param name The name of the configuration (in the set of known configurations) to enable.
|
|
|
|
*/
|
|
|
|
public void enable(final String name) {
|
|
|
|
final Config config = configurations.get(name);
|
|
|
|
if (config == null || config.isEnabled())
|
|
|
|
return;
|
|
|
|
new ConfigEnabler(config).execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a configuration known and managed by this service. The returned object must not be
|
|
|
|
* modified directly.
|
|
|
|
*
|
|
|
|
* @param name The name of the configuration (in the set of known configurations) to retrieve.
|
|
|
|
* @return An object representing the configuration. This object must not be modified.
|
|
|
|
*/
|
|
|
|
public Config get(final String name) {
|
|
|
|
return configurations.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the set of configurations known and managed by the service. Configurations in this
|
|
|
|
* set must not be modified directly. If a configuration is to be updated, first create a copy
|
|
|
|
* of it by calling getCopy().
|
|
|
|
*
|
|
|
|
* @return The set of known configurations.
|
|
|
|
*/
|
2017-08-17 09:43:15 +02:00
|
|
|
public ObservableSortedMap<String, Config> getConfigs() {
|
2017-08-13 14:24:03 +02:00
|
|
|
return configurations;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(final Intent intent) {
|
|
|
|
instance = this;
|
|
|
|
return binder;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
// Ensure the service sticks around after being unbound. This only needs to happen once.
|
|
|
|
startService(new Intent(this, getClass()));
|
|
|
|
rootShell = new RootShell(this);
|
|
|
|
new ConfigLoader().execute(getFilesDir().listFiles(new FilenameFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(final File dir, final String name) {
|
|
|
|
return name.endsWith(".conf");
|
|
|
|
}
|
|
|
|
}));
|
2017-08-16 11:28:41 +02:00
|
|
|
preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
}
|
|
|
|
|
2017-08-17 11:50:41 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
preferences.unregisterOnSharedPreferenceChangeListener(this);
|
|
|
|
}
|
|
|
|
|
2017-08-16 11:28:41 +02:00
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(final SharedPreferences preferences,
|
|
|
|
final String key) {
|
|
|
|
if (!KEY_PRIMARY_CONFIG.equals(key))
|
|
|
|
return;
|
2017-08-17 12:45:40 +02:00
|
|
|
boolean changed = false;
|
2017-08-17 11:50:41 +02:00
|
|
|
final String newName = preferences.getString(key, null);
|
|
|
|
if (primaryName != null && !primaryName.equals(newName)) {
|
|
|
|
final Config oldConfig = configurations.get(primaryName);
|
|
|
|
if (oldConfig != null)
|
|
|
|
oldConfig.setIsPrimary(false);
|
2017-08-17 12:45:40 +02:00
|
|
|
changed = true;
|
2017-08-16 11:28:41 +02:00
|
|
|
}
|
2017-08-17 11:50:41 +02:00
|
|
|
if (newName != null && !newName.equals(primaryName)) {
|
|
|
|
final Config newConfig = configurations.get(newName);
|
|
|
|
if (newConfig != null)
|
|
|
|
newConfig.setIsPrimary(true);
|
|
|
|
else
|
|
|
|
preferences.edit().remove(KEY_PRIMARY_CONFIG).apply();
|
2017-08-17 12:45:40 +02:00
|
|
|
changed = true;
|
2017-08-16 11:28:41 +02:00
|
|
|
}
|
2017-08-17 11:50:41 +02:00
|
|
|
primaryName = newName;
|
2017-08-17 12:45:40 +02:00
|
|
|
if (changed)
|
|
|
|
updateTile();
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(final Intent intent, final int flags, final int startId) {
|
|
|
|
instance = this;
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a configuration from being managed by the service. If it is currently enabled, the
|
|
|
|
* the configuration will be disabled before removal. If successful, the configuration will be
|
|
|
|
* removed from persistent storage. If the configuration is not known to the service, no changes
|
|
|
|
* will be made.
|
|
|
|
*
|
|
|
|
* @param name The name of the configuration (in the set of known configurations) to remove.
|
|
|
|
*/
|
|
|
|
public void remove(final String name) {
|
|
|
|
final Config config = configurations.get(name);
|
|
|
|
if (config == null)
|
|
|
|
return;
|
|
|
|
if (config.isEnabled())
|
|
|
|
new ConfigDisabler(config).execute();
|
|
|
|
new ConfigRemover(config).execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the attributes of the named configuration. If the configuration is currently enabled,
|
|
|
|
* it will be disabled before the update, and the service will attempt to re-enable it
|
|
|
|
* afterward. If successful, the updated configuration will be saved to persistent storage.
|
|
|
|
*
|
|
|
|
* @param name The name of an existing configuration to update.
|
|
|
|
* @param config A copy of the configuration, with updated attributes.
|
|
|
|
*/
|
|
|
|
public void update(final String name, final Config config) {
|
|
|
|
if (name == null)
|
|
|
|
return;
|
|
|
|
if (configurations.containsValue(config))
|
|
|
|
throw new IllegalArgumentException("Config " + config.getName() + " modified directly");
|
|
|
|
final Config oldConfig = configurations.get(name);
|
|
|
|
if (oldConfig == null)
|
|
|
|
return;
|
|
|
|
final boolean wasEnabled = oldConfig.isEnabled();
|
|
|
|
if (wasEnabled)
|
|
|
|
new ConfigDisabler(oldConfig).execute();
|
|
|
|
new ConfigUpdater(oldConfig, config, wasEnabled).execute();
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:45:40 +02:00
|
|
|
private void updateTile() {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
|
|
|
return;
|
|
|
|
Log.v(TAG, "Requesting quick tile update");
|
|
|
|
TileService.requestListeningState(this, new ComponentName(this, QuickTileService.class));
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
private class ConfigDisabler extends AsyncTask<Void, Void, Boolean> {
|
|
|
|
private final Config config;
|
|
|
|
|
|
|
|
private ConfigDisabler(final Config config) {
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(final Void... voids) {
|
|
|
|
Log.i(TAG, "Running wg-quick down for " + config.getName());
|
|
|
|
final File configFile = new File(getFilesDir(), config.getName() + ".conf");
|
|
|
|
return rootShell.run(null, "wg-quick down '" + configFile.getPath() + "'") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(final Boolean result) {
|
|
|
|
if (!result)
|
|
|
|
return;
|
2017-08-16 07:57:27 +02:00
|
|
|
config.setIsEnabled(false);
|
2017-08-16 11:36:12 +02:00
|
|
|
enabledConfigs.remove(config.getName());
|
|
|
|
preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply();
|
2017-08-17 12:45:40 +02:00
|
|
|
if (config.getName().equals(primaryName))
|
|
|
|
updateTile();
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ConfigEnabler extends AsyncTask<Void, Void, Boolean> {
|
|
|
|
private final Config config;
|
|
|
|
|
|
|
|
private ConfigEnabler(final Config config) {
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(final Void... voids) {
|
|
|
|
Log.i(TAG, "Running wg-quick up for " + config.getName());
|
|
|
|
final File configFile = new File(getFilesDir(), config.getName() + ".conf");
|
|
|
|
return rootShell.run(null, "wg-quick up '" + configFile.getPath() + "'") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(final Boolean result) {
|
|
|
|
if (!result)
|
|
|
|
return;
|
2017-08-16 07:57:27 +02:00
|
|
|
config.setIsEnabled(true);
|
2017-08-16 11:36:12 +02:00
|
|
|
enabledConfigs.add(config.getName());
|
|
|
|
preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply();
|
2017-08-17 12:45:40 +02:00
|
|
|
if (config.getName().equals(primaryName))
|
|
|
|
updateTile();
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ConfigLoader extends AsyncTask<File, Void, List<Config>> {
|
|
|
|
@Override
|
|
|
|
protected List<Config> doInBackground(final File... files) {
|
|
|
|
final List<Config> configs = new LinkedList<>();
|
|
|
|
final List<String> interfaces = new LinkedList<>();
|
|
|
|
final String command = "wg show interfaces";
|
|
|
|
if (rootShell.run(interfaces, command) == 0 && interfaces.size() == 1) {
|
|
|
|
// wg puts all interface names on the same line. Split them into separate elements.
|
|
|
|
final String nameList = interfaces.get(0);
|
|
|
|
Collections.addAll(interfaces, nameList.split(" "));
|
|
|
|
interfaces.remove(0);
|
|
|
|
} else {
|
|
|
|
interfaces.clear();
|
|
|
|
Log.w(TAG, "No existing WireGuard interfaces found. Maybe they are all disabled?");
|
|
|
|
}
|
|
|
|
for (final File file : files) {
|
|
|
|
if (isCancelled())
|
|
|
|
return null;
|
|
|
|
final String fileName = file.getName();
|
|
|
|
final String configName = fileName.substring(0, fileName.length() - 5);
|
|
|
|
Log.v(TAG, "Attempting to load config " + configName);
|
|
|
|
try {
|
|
|
|
final Config config = new Config();
|
|
|
|
config.parseFrom(openFileInput(fileName));
|
2017-08-16 07:57:27 +02:00
|
|
|
config.setIsEnabled(interfaces.contains(configName));
|
2017-08-13 14:24:03 +02:00
|
|
|
config.setName(configName);
|
|
|
|
configs.add(config);
|
|
|
|
} catch (IllegalArgumentException | IOException e) {
|
|
|
|
Log.w(TAG, "Failed to load config from " + fileName, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return configs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(final List<Config> configs) {
|
|
|
|
if (configs == null)
|
|
|
|
return;
|
|
|
|
for (final Config config : configs)
|
|
|
|
configurations.put(config.getName(), config);
|
2017-08-17 11:50:41 +02:00
|
|
|
// Run the handler to avoid duplicating the code here.
|
|
|
|
onSharedPreferenceChanged(preferences, KEY_PRIMARY_CONFIG);
|
2017-08-16 11:36:12 +02:00
|
|
|
if (preferences.getBoolean(KEY_RESTORE_ON_BOOT, false)) {
|
|
|
|
final Set<String> configsToEnable =
|
|
|
|
preferences.getStringSet(KEY_ENABLED_CONFIGS, null);
|
|
|
|
if (configsToEnable != null) {
|
|
|
|
for (final String name : configsToEnable) {
|
|
|
|
final Config config = configurations.get(name);
|
|
|
|
if (config != null && !config.isEnabled())
|
|
|
|
new ConfigEnabler(config).execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ConfigRemover extends AsyncTask<Void, Void, Boolean> {
|
|
|
|
private final Config config;
|
|
|
|
|
|
|
|
private ConfigRemover(final Config config) {
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(final Void... voids) {
|
|
|
|
Log.i(TAG, "Removing config " + config.getName());
|
|
|
|
final File configFile = new File(getFilesDir(), config.getName() + ".conf");
|
|
|
|
if (configFile.delete()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Could not delete configuration for config " + config.getName());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(final Boolean result) {
|
|
|
|
if (!result)
|
|
|
|
return;
|
|
|
|
configurations.remove(config.getName());
|
2017-08-17 11:50:41 +02:00
|
|
|
if (config.getName().equals(primaryName)) {
|
|
|
|
// This will get picked up by the preference change listener.
|
|
|
|
preferences.edit().remove(KEY_PRIMARY_CONFIG).apply();
|
|
|
|
}
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ConfigUpdater extends AsyncTask<Void, Void, Boolean> {
|
2017-08-24 01:04:45 +02:00
|
|
|
private Config knownConfig;
|
|
|
|
private final Config newConfig;
|
2017-08-13 14:24:03 +02:00
|
|
|
private final String newName;
|
|
|
|
private final String oldName;
|
|
|
|
private final Boolean shouldConnect;
|
|
|
|
|
2017-08-24 01:04:45 +02:00
|
|
|
private ConfigUpdater(final Config knownConfig, final Config newConfig,
|
2017-08-13 14:24:03 +02:00
|
|
|
final Boolean shouldConnect) {
|
2017-08-24 01:04:45 +02:00
|
|
|
this.knownConfig = knownConfig;
|
|
|
|
this.newConfig = newConfig.copy();
|
2017-08-13 14:24:03 +02:00
|
|
|
newName = newConfig.getName();
|
2017-08-16 07:56:53 +02:00
|
|
|
// When adding a config, "old file" and "new file" are the same thing.
|
2017-08-24 01:04:45 +02:00
|
|
|
oldName = knownConfig != null ? knownConfig.getName() : newName;
|
|
|
|
this.shouldConnect = shouldConnect;
|
2017-08-16 07:56:53 +02:00
|
|
|
if (isAddOrRename() && configurations.containsKey(newName))
|
2017-08-13 14:24:03 +02:00
|
|
|
throw new IllegalStateException("Config " + newName + " already exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(final Void... voids) {
|
2017-08-24 01:04:45 +02:00
|
|
|
Log.i(TAG, (knownConfig == null ? "Adding" : "Updating") + " config " + newName);
|
2017-08-13 14:24:03 +02:00
|
|
|
final File newFile = new File(getFilesDir(), newName + ".conf");
|
|
|
|
final File oldFile = new File(getFilesDir(), oldName + ".conf");
|
2017-08-16 07:56:53 +02:00
|
|
|
if (isAddOrRename() && newFile.exists()) {
|
2017-08-13 14:24:03 +02:00
|
|
|
Log.w(TAG, "Refusing to overwrite existing config configuration");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
final FileOutputStream stream = openFileOutput(oldFile.getName(), MODE_PRIVATE);
|
|
|
|
stream.write(newConfig.toString().getBytes(StandardCharsets.UTF_8));
|
|
|
|
stream.close();
|
|
|
|
} catch (final IOException e) {
|
|
|
|
Log.e(TAG, "Could not save configuration for config " + oldName, e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isRename() && !oldFile.renameTo(newFile)) {
|
|
|
|
Log.e(TAG, "Could not rename " + oldFile.getName() + " to " + newFile.getName());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-16 07:56:53 +02:00
|
|
|
private boolean isAddOrRename() {
|
2017-08-24 01:04:45 +02:00
|
|
|
return knownConfig == null || !newName.equals(oldName);
|
2017-08-16 07:56:53 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
private boolean isRename() {
|
2017-08-24 01:04:45 +02:00
|
|
|
return knownConfig != null && !newName.equals(oldName);
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(final Boolean result) {
|
|
|
|
if (!result)
|
|
|
|
return;
|
2017-08-24 01:04:45 +02:00
|
|
|
if (knownConfig != null)
|
2017-08-13 14:24:03 +02:00
|
|
|
configurations.remove(oldName);
|
2017-08-24 01:04:45 +02:00
|
|
|
if (knownConfig == null)
|
|
|
|
knownConfig = new Config();
|
|
|
|
knownConfig.copyFrom(newConfig);
|
|
|
|
knownConfig.setIsEnabled(false);
|
|
|
|
knownConfig.setIsPrimary(oldName != null && oldName.equals(primaryName));
|
|
|
|
configurations.put(newName, knownConfig);
|
|
|
|
if (isRename() && oldName != null && oldName.equals(primaryName))
|
2017-08-17 11:50:41 +02:00
|
|
|
preferences.edit().putString(KEY_PRIMARY_CONFIG, newName).apply();
|
2017-08-13 14:24:03 +02:00
|
|
|
if (shouldConnect)
|
2017-08-24 01:04:45 +02:00
|
|
|
new ConfigEnabler(knownConfig).execute();
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|