[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [orbot/master] restore backup from intent



commit 0fd59bc78d22924ecc306899e6672332815f1cfe
Author: arrase <arrase@xxxxxxxxx>
Date:   Sun Nov 20 23:56:17 2016 +0100

    restore backup from intent
---
 .../org/torproject/android/OrbotMainActivity.java  | 79 +++++++++++-----------
 .../android/hsutils/HiddenServiceUtils.java        |  5 +-
 .../java/org/torproject/android/hsutils/ZipIt.java | 48 +++++++++++++
 .../android/ui/hs/dialogs/HSActionsDialog.java     |  3 +-
 4 files changed, 93 insertions(+), 42 deletions(-)

diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
index 2e4a4e9..b28c3c4 100644
--- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java
+++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
@@ -525,15 +525,15 @@ public class OrbotMainActivity extends AppCompatActivity
          }
          else if (item.getItemId() == R.id.menu_hidden_services)
          {
-			 if(usesRuntimePermissions()){
-				 postPermissionsAction = new GrantedPermissionsAction() {
-					 @Override
-					 public void run(Context context, boolean granted) {
-						 startActivity(new Intent(context, HiddenServicesActivity.class));
-					 }
-				 };
-
-				 checkPermissions();
+			 if(usesRuntimePermissions() && !hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
+					 postPermissionsAction = new GrantedPermissionsAction() {
+						 @Override
+						 public void run(Context context, boolean granted) {
+							 startActivity(new Intent(context, HiddenServicesActivity.class));
+						 }
+					 };
+
+					 checkPermissions();
 			 } else {
 				 startActivity(new Intent(this, HiddenServicesActivity.class));
 			 }
@@ -615,7 +615,7 @@ public class OrbotMainActivity extends AppCompatActivity
             stopVpnService();
     }
 	
-	private void enableHiddenServicePort (String hsName, final int hsPort, int hsRemotePort, final boolean doBackup) throws RemoteException, InterruptedException
+	private void enableHiddenServicePort (String hsName, final int hsPort, int hsRemotePort, final boolean doBackup, final String keyZipPath) throws RemoteException, InterruptedException
 	{
 		String onionHostname = null;
 
@@ -664,8 +664,15 @@ public class OrbotMainActivity extends AppCompatActivity
 						Cursor onion = getContentResolver().query(HSContentProvider.CONTENT_URI, mProjection, "port=" + hsPort, null, null);
 						if(onion != null) {
 							hostname = onion.getString(onion.getColumnIndex(HSContentProvider.HiddenService.NAME));
-							if(doBackup) {
-								HiddenServiceUtils hsutils = new HiddenServiceUtils(getApplicationContext());
+							HiddenServiceUtils hsutils = new HiddenServiceUtils(getApplicationContext());
+							if(keyZipPath != null && keyZipPath.length() > 0)
+							{
+								hsutils.restoreOnionBackup(hsPort, keyZipPath);
+								requestTorRereadConfig();
+							}
+
+							if(doBackup)
+							{
 								backupPath = hsutils.createOnionBackup(hsPort);
 							}
 							onion.close();
@@ -686,7 +693,6 @@ public class OrbotMainActivity extends AppCompatActivity
 
 		Intent nResult = new Intent();
 		nResult.putExtra("hs_host", onionHostname);
-		// TODO: Add key
 		setResult(RESULT_OK, nResult);
 		finish();
 	
@@ -713,23 +719,22 @@ public class OrbotMainActivity extends AppCompatActivity
         	final int hiddenServiceRemotePort = intent.getIntExtra("hs_onion_port", -1);
         	final String hiddenServiceName = intent.getStringExtra("hs_name");
         	final Boolean createBackup = intent.getBooleanExtra("hs_backup",false);
-
-			if(createBackup && usesRuntimePermissions())
-				checkPermissions();
+        	final String keyZipPath = intent.getStringExtra("hs_key_zip_path");
 
 			DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
 			    
 			    public void onClick(DialogInterface dialog, int which) {
 			        switch (which){
 			        case DialogInterface.BUTTON_POSITIVE:
-						if(createBackup && usesRuntimePermissions()){
+						if(createBackup && usesRuntimePermissions()
+								&& !hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
 							postPermissionsAction = new GrantedPermissionsAction() {
 								@Override
 								public void run(Context context, boolean granted) {
 									try {
 										enableHiddenServicePort (
 												hiddenServiceName, hiddenServicePort,
-												hiddenServiceRemotePort, createBackup
+												hiddenServiceRemotePort, createBackup, keyZipPath
 										);
 									} catch (RemoteException e) {
 										// TODO Auto-generated catch block
@@ -745,7 +750,7 @@ public class OrbotMainActivity extends AppCompatActivity
 							try {
 								enableHiddenServicePort (
 										hiddenServiceName, hiddenServicePort,
-										hiddenServiceRemotePort, createBackup
+										hiddenServiceRemotePort, createBackup, keyZipPath
 								);
 							} catch (RemoteException e) {
 								// TODO Auto-generated catch block
@@ -1605,25 +1610,23 @@ public class OrbotMainActivity extends AppCompatActivity
 	}
 
 	private void checkPermissions() {
-		if (!hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
-			if (ActivityCompat.shouldShowRequestPermissionRationale
-					(OrbotMainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
-				Snackbar.make(findViewById(android.R.id.content),
-						R.string.please_grant_permissions_for_external_storage,
-						Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
-						new View.OnClickListener() {
-							@Override
-							public void onClick(View v) {
-								ActivityCompat.requestPermissions(OrbotMainActivity.this,
-										new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
-										PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
-							}
-						}).show();
-			} else {
-				ActivityCompat.requestPermissions(OrbotMainActivity.this,
-						new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
-						PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
-			}
+		if (ActivityCompat.shouldShowRequestPermissionRationale
+				(OrbotMainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
+			Snackbar.make(findViewById(android.R.id.content),
+					R.string.please_grant_permissions_for_external_storage,
+					Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
+					new View.OnClickListener() {
+						@Override
+						public void onClick(View v) {
+							ActivityCompat.requestPermissions(OrbotMainActivity.this,
+									new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
+									PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
+						}
+					}).show();
+		} else {
+			ActivityCompat.requestPermissions(OrbotMainActivity.this,
+					new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
+					PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
 		}
 	}
 
diff --git a/app/src/main/java/org/torproject/android/hsutils/HiddenServiceUtils.java b/app/src/main/java/org/torproject/android/hsutils/HiddenServiceUtils.java
index 2eb434c..0e96756 100644
--- a/app/src/main/java/org/torproject/android/hsutils/HiddenServiceUtils.java
+++ b/app/src/main/java/org/torproject/android/hsutils/HiddenServiceUtils.java
@@ -37,7 +37,8 @@ public class HiddenServiceUtils {
         return zip_path;
     }
 
-    public void restoreOnionBackup(String path) {
-
+    public void restoreOnionBackup(Integer port, String path) {
+        ZipIt zip = new ZipIt(null, path);
+        zip.unzip(appCacheHome + "/hs" + port);
     }
 }
diff --git a/app/src/main/java/org/torproject/android/hsutils/ZipIt.java b/app/src/main/java/org/torproject/android/hsutils/ZipIt.java
index 39544ec..25d74a5 100644
--- a/app/src/main/java/org/torproject/android/hsutils/ZipIt.java
+++ b/app/src/main/java/org/torproject/android/hsutils/ZipIt.java
@@ -3,9 +3,13 @@ package org.torproject.android.hsutils;
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
 public class ZipIt {
@@ -47,4 +51,48 @@ public class ZipIt {
 
         return true;
     }
+
+    public boolean unzip(String output_path) {
+        InputStream is;
+        ZipInputStream zis;
+
+        try {
+            String filename;
+            is = new FileInputStream(_zipFile);
+            zis = new ZipInputStream(new BufferedInputStream(is));
+            ZipEntry ze;
+            byte[] buffer = new byte[1024];
+            int count;
+
+            while ((ze = zis.getNextEntry()) != null) {
+                // zapis do souboru
+                filename = ze.getName();
+
+                // Need to create directories if not exists, or
+                // it will generate an Exception...
+                if (ze.isDirectory()) {
+                    File fmd = new File(output_path + filename);
+                    fmd.mkdirs();
+                    continue;
+                }
+
+                FileOutputStream fout = new FileOutputStream(output_path + filename);
+
+                // cteni zipu a zapis
+                while ((count = zis.read(buffer)) != -1) {
+                    fout.write(buffer, 0, count);
+                }
+
+                fout.close();
+                zis.closeEntry();
+            }
+
+            zis.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+            return false;
+        }
+
+        return true;
+    }
 }
\ No newline at end of file
diff --git a/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java b/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java
index 8d6ad37..97361b7 100644
--- a/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java
+++ b/app/src/main/java/org/torproject/android/ui/hs/dialogs/HSActionsDialog.java
@@ -53,11 +53,10 @@ public class HSActionsDialog extends DialogFragment {
 
                 Toast.makeText(mContext, R.string.done, Toast.LENGTH_LONG).show();
 
-                Uri selectedUri = Uri.parse(backupPath);
+                Uri selectedUri = Uri.parse(backupPath.substring(0, backupPath.lastIndexOf("/")));
                 Intent intent = new Intent(Intent.ACTION_VIEW);
                 intent.setDataAndType(selectedUri, "resource/folder");
 
-
                 if (intent.resolveActivityInfo(mContext.getPackageManager(), 0) != null) {
                     startActivity(intent);
                 } else {



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits