From c4198854347ae97edfc4be1517c2720eacc552f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 17 Jun 2015 15:26:43 +0200 Subject: Cleanup tor code --- .../keychain/ui/CreateYubiKeyImportFragment.java | 2 +- .../keychain/ui/DecryptFragment.java | 2 +- .../keychain/ui/ImportKeysActivity.java | 2 +- .../keychain/ui/ImportKeysProxyActivity.java | 2 +- .../keychain/ui/KeyListFragment.java | 9 +- .../keychain/ui/SettingsActivity.java | 2 +- .../keychain/ui/UploadKeyActivity.java | 2 +- .../keychain/ui/ViewKeyActivity.java | 2 +- .../keychain/ui/ViewKeyTrustFragment.java | 2 +- .../ui/dialog/AddEditKeyserverDialogFragment.java | 16 +- .../ui/dialog/OrbotStartDialogFragment.java | 2 +- .../keychain/util/orbot/OrbotHelper.java | 177 --------------------- .../keychain/util/orbot/TorServiceUtils.java | 153 ------------------ .../keychain/util/tor/OrbotHelper.java | 175 ++++++++++++++++++++ .../keychain/util/tor/TorServiceUtils.java | 144 +++++++++++++++++ 15 files changed, 346 insertions(+), 346 deletions(-) delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java index 93614db56..ec74ca7c8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateYubiKeyImportFragment.java @@ -43,7 +43,7 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; public class CreateYubiKeyImportFragment diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java index adb15111d..e2cc21464 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -60,7 +60,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; public abstract class DecryptFragment extends Fragment implements LoaderManager.LoaderCallbacks { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index e2b90056d..c64fc5a2a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -43,7 +43,7 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.io.IOException; import java.util.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java index 1a03b8102..358dc108c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java @@ -46,7 +46,7 @@ import org.sufficientlysecure.keychain.util.IntentIntegratorSupportV4; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.util.ArrayList; import java.util.Locale; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index da181711e..35f76be5e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -83,6 +83,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; /** * Public key list with sticky list headers. It does _not_ exte /** @@ -472,13 +473,11 @@ public class KeyListFragment extends LoaderFragment return true; case R.id.menu_key_list_export: - ); + mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true); return true; - case R.id.menu_key_list_debug -_cons return true; - - consolidate(case R.id.menu_key_list_debug_cons:); + case R.id.menu_key_list_update_all_keys: + updateAllKeys(); return true; case R.id.menu_key_list_debug_read: diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java index 81e4b9eda..bdf10b065 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SettingsActivity.java @@ -42,7 +42,7 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.widget.IntegerListPreference; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.util.List; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java index ee37b4b98..fc6585bbb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/UploadKeyActivity.java @@ -39,7 +39,7 @@ import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; /** * Sends the selected public key to a keyserver diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index ce8935bce..29fbbb3c8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -86,7 +86,7 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.NfcHelper; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.io.IOException; import java.util.ArrayList; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java index f6b580b01..8bcfd6111 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyTrustFragment.java @@ -53,7 +53,7 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.util.ArrayList; import java.util.Hashtable; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java index e5ab3a228..c409cbb7c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddEditKeyserverDialogFragment.java @@ -55,6 +55,7 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.TlsHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; import java.io.IOException; import java.net.HttpURLConnection; @@ -221,8 +222,19 @@ public class AddEditKeyserverDialogFragment extends DialogFragment implements On // behaviour same for edit and add String keyserverUrl = mKeyserverEditText.getText().toString(); if (mVerifyKeyserverCheckBox.isChecked()) { - // TODO: PHILIP Implement proxy - verifyConnection(keyserverUrl); + final Preferences.ProxyPrefs proxyPrefs = Preferences.getPreferences(getActivity()) + .getProxyPrefs(); + Runnable ignoreTor = new Runnable() { + @Override + public void run() { + verifyConnection(keyserverUrl, null); + } + }; + + if (OrbotHelper.isOrbotInRequiredState(R.string.orbot_ignore_tor, ignoreTor, proxyPrefs, + getActivity())) { + verifyConnection(keyserverUrl, proxyPrefs.parcelableProxy.getProxy()); + } } else { dismiss(); // return unverified keyserver back to activity diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java index 4736eddca..fdbab3b20 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/OrbotStartDialogFragment.java @@ -29,7 +29,7 @@ import android.support.v4.app.DialogFragment; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; +import org.sufficientlysecure.keychain.util.tor.OrbotHelper; /** * displays a dialog asking the user to enable Tor diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java deleted file mode 100644 index f57496767..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotHelper.java +++ /dev/null @@ -1,177 +0,0 @@ -/* This is the license for Orlib, a free software project to - provide anonymity on the Internet from a Google Android smartphone. - - For more information about Orlib, see https://guardianproject.info/ - - If you got this file as a part of a larger bundle, there may be other - license terms that you should be aware of. - =============================================================================== - Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - ***** - Orlib contains a binary distribution of the JSocks library: - http://code.google.com/p/jsocks-mirror/ - which is licensed under the GNU Lesser General Public License: - http://www.gnu.org/licenses/lgpl.html - - ***** -*/ - -package org.sufficientlysecure.keychain.util.orbot; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.Handler; -import android.os.Message; -import android.os.Messenger; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentActivity; -import android.support.v4.app.FragmentManager; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; -import org.sufficientlysecure.keychain.util.Preferences; - -/** - * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ - */ -public class OrbotHelper { - - public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; - public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; - - public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; - - public static boolean isOrbotRunning() { - int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); - - return (procId != -1); - } - - public static boolean isOrbotInstalled(Context context) { - return isAppInstalled(ORBOT_PACKAGE_NAME, context); - } - - private static boolean isAppInstalled(String uri, Context context) { - PackageManager pm = context.getPackageManager(); - boolean installed = false; - try { - pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); - installed = true; - } catch (PackageManager.NameNotFoundException e) { - installed = false; - } - return installed; - } - - /** - * hack to get around the fact that PreferenceActivity still supports only android.app.DialogFragment - * - * @return - */ - public static android.app.DialogFragment getPreferenceInstallDialogFragment() { - return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); - } - - public static DialogFragment getInstallDialogFragment() { - return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); - } - - public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { - return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, - R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); - } - - public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { - return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string - .orbot_start_dialog_content, - middleButton); - } - - public static Intent getOrbotStartIntent() { - Intent intent = new Intent(ACTION_START_TOR); - intent.setPackage(ORBOT_PACKAGE_NAME); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return intent; - } - - /** - * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. - * - * @param middleButton resourceId of string to display as the middle button of install and enable dialogs - * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button - * @param proxyPrefs - * @param fragmentActivity - * @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false - */ - public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable, - Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) { - Handler ignoreTorHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - // every message received by this handler will mean the middle button was pressed - middleButtonRunnable.run(); - } - }; - - if (!proxyPrefs.torEnabled) { - return true; - } - - if (!OrbotHelper.isOrbotInstalled(fragmentActivity)) { - - OrbotHelper.getInstallDialogFragmentWithThirdButton( - new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor - ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); - - return false; - } else if (!OrbotHelper.isOrbotRunning()) { - - OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), - R.string.orbot_install_dialog_ignore_tor) - .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); - - return false; - } else { - return true; - } - } -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java deleted file mode 100644 index b1d8327fb..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/TorServiceUtils.java +++ /dev/null @@ -1,153 +0,0 @@ -/* This is the license for Orlib, a free software project to - provide anonymity on the Internet from a Google Android smartphone. - - For more information about Orlib, see https://guardianproject.info/ - - If you got this file as a part of a larger bundle, there may be other - license terms that you should be aware of. - =============================================================================== - Orlib is distributed under this license (aka the 3-clause BSD license) - - Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following disclaimer - in the documentation and/or other materials provided with the - distribution. - - * Neither the names of the copyright owners nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - ***** - Orlib contains a binary distribution of the JSocks library: - http://code.google.com/p/jsocks-mirror/ - which is licensed under the GNU Lesser General Public License: - http://www.gnu.org/licenses/lgpl.html - - ***** -*/ - -package org.sufficientlysecure.keychain.util.orbot; - -import android.util.Log; - -import java.io.BufferedReader; -import java.io.File; -import java.io.InputStreamReader; -import java.net.URLEncoder; -import java.util.StringTokenizer; - -/** - * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ - */ - -public class TorServiceUtils { - - private final static String TAG = "TorUtils"; - // various console cmds - public final static String SHELL_CMD_PS = "ps"; - public final static String SHELL_CMD_PIDOF = "pidof"; - - public static int findProcessId(String command) { - int procId = -1; - - try { - procId = findProcessIdWithPidOf(command); - - if (procId == -1) - procId = findProcessIdWithPS(command); - } catch (Exception e) { - try { - procId = findProcessIdWithPS(command); - } catch (Exception e2) { - Log.e(TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); - } - } - - return procId; - } - - // use 'pidof' command - public static int findProcessIdWithPidOf(String command) throws Exception { - - int procId = -1; - - Runtime r = Runtime.getRuntime(); - - Process procPs = null; - - String baseName = new File(command).getName(); - // fix contributed my mikos on 2010.12.10 - procPs = r.exec(new String[]{ - SHELL_CMD_PIDOF, baseName - }); - // procPs = r.exec(SHELL_CMD_PIDOF); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; - - while ((line = reader.readLine()) != null) { - - try { - // this line should just be the process id - procId = Integer.parseInt(line.trim()); - break; - } catch (NumberFormatException e) { - Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); - } - } - - return procId; - - } - - // use 'ps' command - public static int findProcessIdWithPS(String command) throws Exception { - - int procId = -1; - - Runtime r = Runtime.getRuntime(); - - Process procPs = null; - - procPs = r.exec(SHELL_CMD_PS); - - BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); - String line = null; - - while ((line = reader.readLine()) != null) { - if (line.indexOf(' ' + command) != -1) { - - StringTokenizer st = new StringTokenizer(line, " "); - st.nextToken(); // proc owner - - procId = Integer.parseInt(st.nextToken().trim()); - - break; - } - } - - return procId; - - } -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java new file mode 100644 index 000000000..84e4f36ab --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/OrbotHelper.java @@ -0,0 +1,175 @@ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. + + For more information about Orlib, see https://guardianproject.info/ + + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html + + ***** +*/ + +package org.sufficientlysecure.keychain.util.tor; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; + +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.dialog.InstallDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.PreferenceInstallDialogFragment; +import org.sufficientlysecure.keychain.util.Preferences; + +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ +public class OrbotHelper { + + public final static String ORBOT_PACKAGE_NAME = "org.torproject.android"; + public final static String TOR_BIN_PATH = "/data/data/org.torproject.android/app_bin/tor"; + + public final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; + + public static boolean isOrbotRunning() { + int procId = TorServiceUtils.findProcessId(TOR_BIN_PATH); + + return (procId != -1); + } + + public static boolean isOrbotInstalled(Context context) { + return isAppInstalled(ORBOT_PACKAGE_NAME, context); + } + + private static boolean isAppInstalled(String uri, Context context) { + PackageManager pm = context.getPackageManager(); + + boolean installed; + try { + pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); + installed = true; + } catch (PackageManager.NameNotFoundException e) { + installed = false; + } + return installed; + } + + /** + * hack to get around the fact that PreferenceActivity still supports only android.app.DialogFragment + * + * @return + */ + public static android.app.DialogFragment getPreferenceInstallDialogFragment() { + return PreferenceInstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); + } + + public static DialogFragment getInstallDialogFragment() { + return InstallDialogFragment.newInstance(R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME); + } + + public static DialogFragment getInstallDialogFragmentWithThirdButton(Messenger messenger, int middleButton) { + return InstallDialogFragment.newInstance(messenger, R.string.orbot_install_dialog_title, + R.string.orbot_install_dialog_content, ORBOT_PACKAGE_NAME, middleButton, true); + } + + public static DialogFragment getOrbotStartDialogFragment(Messenger messenger, int middleButton) { + return OrbotStartDialogFragment.newInstance(messenger, R.string.orbot_start_dialog_title, R.string + .orbot_start_dialog_content, + middleButton); + } + + public static Intent getOrbotStartIntent() { + Intent intent = new Intent(ACTION_START_TOR); + intent.setPackage(ORBOT_PACKAGE_NAME); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + return intent; + } + + /** + * checks if Tor is enabled and if it is, that Orbot is installed and runnign. Generates appropriate dialogs. + * + * @param middleButton resourceId of string to display as the middle button of install and enable dialogs + * @param middleButtonRunnable runnable to be executed if the user clicks on the middle button + * @param proxyPrefs + * @param fragmentActivity + * @return true if Tor is not enabled or Tor is enabled and Orbot is installed and running, else false + */ + public static boolean isOrbotInRequiredState(int middleButton, final Runnable middleButtonRunnable, + Preferences.ProxyPrefs proxyPrefs, FragmentActivity fragmentActivity) { + Handler ignoreTorHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + // every message received by this handler will mean the middle button was pressed + middleButtonRunnable.run(); + } + }; + + if (!proxyPrefs.torEnabled) { + return true; + } + + if (!OrbotHelper.isOrbotInstalled(fragmentActivity)) { + + OrbotHelper.getInstallDialogFragmentWithThirdButton( + new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor + ).show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotInstallDialog"); + + return false; + } else if (!OrbotHelper.isOrbotRunning()) { + + OrbotHelper.getOrbotStartDialogFragment(new Messenger(ignoreTorHandler), + R.string.orbot_install_dialog_ignore_tor) + .show(fragmentActivity.getSupportFragmentManager(), "OrbotHelperOrbotStartDialog"); + + return false; + } else { + return true; + } + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java new file mode 100644 index 000000000..48adc9528 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/tor/TorServiceUtils.java @@ -0,0 +1,144 @@ +/* This is the license for Orlib, a free software project to + provide anonymity on the Internet from a Google Android smartphone. + + For more information about Orlib, see https://guardianproject.info/ + + If you got this file as a part of a larger bundle, there may be other + license terms that you should be aware of. + =============================================================================== + Orlib is distributed under this license (aka the 3-clause BSD license) + + Copyright (c) 2009-2010, Nathan Freitas, The Guardian Project + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + * Neither the names of the copyright owners nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + ***** + Orlib contains a binary distribution of the JSocks library: + http://code.google.com/p/jsocks-mirror/ + which is licensed under the GNU Lesser General Public License: + http://www.gnu.org/licenses/lgpl.html + + ***** +*/ + +package org.sufficientlysecure.keychain.util.tor; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.util.Log; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.net.URLEncoder; +import java.util.StringTokenizer; + +/** + * This class is taken from the NetCipher library: https://github.com/guardianproject/NetCipher/ + */ +public class TorServiceUtils { + // various console cmds + public final static String SHELL_CMD_PS = "ps"; + public final static String SHELL_CMD_PIDOF = "pidof"; + + public static int findProcessId(String command) { + int procId = -1; + + try { + procId = findProcessIdWithPidOf(command); + + if (procId == -1) { + procId = findProcessIdWithPS(command); + } + } catch (Exception e) { + try { + procId = findProcessIdWithPS(command); + } catch (Exception e2) { + Log.e(Constants.TAG, "Unable to get proc id for command: " + URLEncoder.encode(command), e2); + } + } + + return procId; + } + + // use 'pidof' command + public static int findProcessIdWithPidOf(String command) throws Exception { + + int procId = -1; + Runtime r = Runtime.getRuntime(); + Process procPs; + + String baseName = new File(command).getName(); + // fix contributed my mikos on 2010.12.10 + procPs = r.exec(new String[]{ + SHELL_CMD_PIDOF, baseName + }); + // procPs = r.exec(SHELL_CMD_PIDOF); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line; + + while ((line = reader.readLine()) != null) { + + try { + // this line should just be the process id + procId = Integer.parseInt(line.trim()); + break; + } catch (NumberFormatException e) { + Log.e("TorServiceUtils", "unable to parse process pid: " + line, e); + } + } + + return procId; + } + + // use 'ps' command + public static int findProcessIdWithPS(String command) throws Exception { + int procId = -1; + Runtime r = Runtime.getRuntime(); + Process procPs; + + procPs = r.exec(SHELL_CMD_PS); + + BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + if (line.contains(' ' + command)) { + + StringTokenizer st = new StringTokenizer(line, " "); + st.nextToken(); // proc owner + + procId = Integer.parseInt(st.nextToken().trim()); + + break; + } + } + + return procId; + } +} -- cgit v1.2.3