diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2017-12-19 02:42:00 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2017-12-19 02:42:00 +0100 |
commit | 9b92f4d3c5ed263533844d95031d497e3ba7a0ca (patch) | |
tree | 1869827cbad898087c5458a3d7fd9ef4bcce34c7 /app/src/main/java/com/wireguard/android | |
parent | ecc2a1c7591d1749c95d9fe687ec00753ea0eae2 (diff) |
SettingsActivity: allow for installing command line tools
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android')
3 files changed, 87 insertions, 12 deletions
diff --git a/app/src/main/java/com/wireguard/android/SettingsActivity.java b/app/src/main/java/com/wireguard/android/SettingsActivity.java index bb617b63..bc6d71f2 100644 --- a/app/src/main/java/com/wireguard/android/SettingsActivity.java +++ b/app/src/main/java/com/wireguard/android/SettingsActivity.java @@ -2,9 +2,14 @@ package com.wireguard.android; import android.app.Activity; import android.app.FragmentTransaction; +import android.content.Context; +import android.os.AsyncTask; import android.os.Bundle; +import android.preference.Preference; import android.preference.PreferenceFragment; +import com.wireguard.android.backends.RootShell; + public class SettingsActivity extends Activity { @Override protected void onCreate(final Bundle savedInstanceState) { @@ -22,6 +27,77 @@ public class SettingsActivity extends Activity { addPreferencesFromResource(R.xml.preferences); if (getArguments() != null && getArguments().getBoolean("showQuickTile")) ((ConfigListPreference) findPreference("primary_config")).show(); + + final Preference installTools = findPreference("install_cmd_line_tools"); + installTools.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + public boolean onPreferenceClick(Preference preference) { + new ToolsInstaller(installTools).execute(); + return true; + } + }); + } + } + + private static class ToolsInstaller extends AsyncTask<Void, Void, Integer> { + Preference installTools; + + public ToolsInstaller(Preference installTools) { + this.installTools = installTools; + installTools.setEnabled(false); + installTools.setSummary(installTools.getContext().getString(R.string.install_cmd_line_tools_progress)); + } + + private static final String[][] libraryNamedExecutables = { + { "libwg.so", "wg" }, + { "libwg-quick.so", "wg-quick" } + }; + + @Override + protected Integer doInBackground(final Void... voids) { + final Context context = installTools.getContext(); + final String libDir = context.getApplicationInfo().nativeLibraryDir; + final StringBuilder cmd = new StringBuilder(); + + cmd.append("set -ex;"); + + for (final String[] libraryNamedExecutable : libraryNamedExecutables) { + final String arg1 = "'" + libDir + "/" + libraryNamedExecutable[0] + "'"; + final String arg2 = "'/system/xbin/" + libraryNamedExecutable[1] + "'"; + + cmd.append(String.format("diff %s %s && ", arg1, arg2)); + } + cmd.append("exit 114;"); + + cmd.append("trap 'mount -o remount,ro /system' EXIT;"); + cmd.append("mount -o remount,rw /system;"); + + for (final String[] libraryNamedExecutable : libraryNamedExecutables) { + final String arg1 = "'" + libDir + "/" + libraryNamedExecutable[0] + "'"; + final String arg2 = "'/system/xbin/" + libraryNamedExecutable[1] + "'"; + cmd.append(String.format("cp %s %s; chmod 755 %s;", arg1, arg2, arg2)); + } + + return new RootShell(context).run(null, cmd.toString()); + } + + @Override + protected void onPostExecute(final Integer ret) { + final Context context = installTools.getContext(); + String status; + + switch (ret) { + case 0: + status = context.getString(R.string.install_cmd_line_tools_success); + break; + case 114 /* OsConstants.EALREADY */: + status = context.getString(R.string.install_cmd_line_tools_already); + break; + default: + status = context.getString(R.string.install_cmd_line_tools_failure); + break; + } + installTools.setSummary(status); + installTools.setEnabled(true); } } } diff --git a/app/src/main/java/com/wireguard/android/backends/RootShell.java b/app/src/main/java/com/wireguard/android/backends/RootShell.java index 44b9dd8c..cab39730 100644 --- a/app/src/main/java/com/wireguard/android/backends/RootShell.java +++ b/app/src/main/java/com/wireguard/android/backends/RootShell.java @@ -18,7 +18,7 @@ import java.util.regex.Matcher; * Helper class for running commands as root. */ -class RootShell { +public class RootShell { /** * Setup commands that are run at the beginning of each root shell. The trap command ensures * access to the return value of the last command, since su itself always exits with 0. @@ -33,23 +33,23 @@ class RootShell { private final String preamble; - RootShell(final Context context) { + public RootShell(final Context context) { final String binDir = context.getCacheDir().getPath() + "/bin"; final String tmpDir = context.getCacheDir().getPath() + "/tmp"; + final String libDir = context.getApplicationInfo().nativeLibraryDir; new File(binDir).mkdirs(); new File(tmpDir).mkdirs(); - preamble = String.format("export PATH=\"%s:$PATH\" TMPDIR=\"%s\";", binDir, tmpDir); - - final String libDir = context.getApplicationInfo().nativeLibraryDir; - String symlinkCommand = "set -ex;"; + StringBuilder builder = new StringBuilder(); for (final String[] libraryNamedExecutable : libraryNamedExecutables) { - final String args = "'" + libDir + "/" + libraryNamedExecutable[0] + "' '" + binDir + "/" + libraryNamedExecutable[1] + "'"; - symlinkCommand += "ln -f " + args + " || ln -sf " + args + ";"; + final String arg1 = "'" + libDir + "/" + libraryNamedExecutable[0] + "'"; + final String arg2 = "'" + binDir + "/" + libraryNamedExecutable[1] + "'"; + builder.append(String.format("[ %s -ef %s ] || ln -sf %s %s || exit 31;", arg1, arg2, arg1, arg2)); } - if (run(null, symlinkCommand) != 0) - Log.e(TAG, "Unable to establish symlinks for important executables."); + builder.append(String.format("export PATH=\"%s:$PATH\" TMPDIR=\"%s\";", binDir, tmpDir)); + + preamble = builder.toString(); } /** @@ -60,7 +60,7 @@ class RootShell { * @param command Command to run as root. * @return The exit value of the last command run, or -1 if there was an internal error. */ - int run(final List<String> output, final String command) { + public int run(final List<String> output, final String command) { int exitValue = -1; try { final ProcessBuilder builder = new ProcessBuilder(); diff --git a/app/src/main/java/com/wireguard/android/backends/VpnService.java b/app/src/main/java/com/wireguard/android/backends/VpnService.java index ad16fb36..16596721 100644 --- a/app/src/main/java/com/wireguard/android/backends/VpnService.java +++ b/app/src/main/java/com/wireguard/android/backends/VpnService.java @@ -14,7 +14,6 @@ import android.os.IBinder; import android.preference.PreferenceManager; import android.provider.OpenableColumns; import android.service.quicksettings.TileService; -import android.system.ErrnoException; import android.system.OsConstants; import android.util.Log; import android.widget.Toast; |