commit 1060c69623c4c43092a8d278df7fe3156d1f895f Author: arrase arrase@gmail.com Date: Sun Nov 27 20:57:47 2016 +0100
intent api: restore key --- .../org/torproject/android/OrbotMainActivity.java | 20 ++++++++++------- .../ui/hiddenservices/backup/BackupUtils.java | 25 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java index d7222a9..4ceb0a0 100644 --- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java +++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java @@ -70,6 +70,7 @@ import org.torproject.android.ui.ImageProgressView; import org.torproject.android.ui.PromoAppsActivity; import org.torproject.android.ui.Rotate3dAnimation; import org.torproject.android.ui.hiddenservices.HiddenServicesActivity; +import org.torproject.android.ui.hiddenservices.backup.BackupUtils; import org.torproject.android.ui.hiddenservices.providers.HSContentProvider; import org.torproject.android.vpn.VPNEnableActivity;
@@ -559,7 +560,11 @@ public class OrbotMainActivity extends AppCompatActivity stopVpnService(); }
- private void enableHiddenServicePort(String hsName, final int hsPort, int hsRemotePort, final String backupToPackage, final String keyZipPath) throws RemoteException, InterruptedException { + private void enableHiddenServicePort( + String hsName, final int hsPort, int hsRemotePort, + final String backupToPackage, final Uri hsKeyPath + ) throws RemoteException, InterruptedException { + String onionHostname = null;
if (hsName == null) @@ -620,13 +625,12 @@ public class OrbotMainActivity extends AppCompatActivity if (onion != null && onion.getCount() > 0) { hostname = onion.getString(onion.getColumnIndex(HSContentProvider.HiddenService.NAME)); nResult.putExtra("hs_host", hostname); - /* TODO - BackupUtils hsutils = new BackupUtils(getApplicationContext()); - if (keyZipPath != null && keyZipPath.length() > 0) { - // hsutils.restoreZipBackup(hsPort, keyZipPath); + + if (hsKeyPath != null) { + BackupUtils hsutils = new BackupUtils(getApplicationContext()); + hsutils.restoreKeyBackup(hsPort, hsKeyPath); requestTorRereadConfig(); } - */
if (backupToPackage != null && backupToPackage.length() > 0) { String servicePath = getFilesDir() + "/" + TorServiceConstants.HIDDEN_SERVICES_DIR + "/hs" + hsPort; @@ -682,7 +686,7 @@ public class OrbotMainActivity extends AppCompatActivity final int hiddenServiceRemotePort = intent.getIntExtra("hs_onion_port", -1); final String hiddenServiceName = intent.getStringExtra("hs_name"); final String backupToPackage = intent.getStringExtra("hs_backup_to_package"); - final String keyZipPath = intent.getStringExtra("hs_key_path"); + final Uri mKeyUri = intent.getData();
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@@ -692,7 +696,7 @@ public class OrbotMainActivity extends AppCompatActivity try { enableHiddenServicePort( hiddenServiceName, hiddenServicePort, - hiddenServiceRemotePort, backupToPackage, keyZipPath + hiddenServiceRemotePort, backupToPackage, mKeyUri ); } catch (RemoteException e) { // TODO Auto-generated catch block diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java index 4e2e30c..dbaf7e3 100644 --- a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java +++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/BackupUtils.java @@ -4,6 +4,8 @@ import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.net.Uri; +import android.os.ParcelFileDescriptor; import android.widget.Toast;
import org.json.JSONException; @@ -15,8 +17,11 @@ import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; @@ -204,4 +209,24 @@ public class BackupUtils {
Toast.makeText(mContext, R.string.backup_restored, Toast.LENGTH_LONG).show(); } + + public void restoreKeyBackup(int hsPort, Uri hsKeyPath) { + String keyFilePath = mHSBasePath + "/hs" + hsPort + "/private_key"; + + try { + ParcelFileDescriptor mInputPFD = mContext.getContentResolver().openFileDescriptor(hsKeyPath, "r"); + InputStream fileStream = new FileInputStream(mInputPFD.getFileDescriptor()); + OutputStream file = new FileOutputStream(keyFilePath); + + byte[] buffer = new byte[1024]; + int length; + while ((length = fileStream.read(buffer)) > 0) { + file.write(buffer, 0, length); + } + file.close(); + + } catch (IOException | NullPointerException e) { + e.printStackTrace(); + } + } }
tor-commits@lists.torproject.org