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

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



commit 5f02561f9d00b238c880d0a77bcd63f41c556bc3
Author: arrase <arrase@xxxxxxxxx>
Date:   Sun Nov 27 18:50:20 2016 +0100

    backup restore
---
 .../org/torproject/android/OrbotMainActivity.java  |  3 +-
 .../ui/hiddenservices/backup/BackupUtils.java      | 77 +++++++++++++++++++---
 .../android/ui/hiddenservices/backup/ZipIt.java    | 11 ++--
 .../ui/hiddenservices/dialogs/HSDataDialog.java    |  8 +--
 .../hiddenservices/dialogs/SelectBackupDialog.java |  5 +-
 .../providers/HSContentProvider.java               |  8 +--
 app/src/main/res/values/strings.xml                |  1 +
 7 files changed, 88 insertions(+), 25 deletions(-)

diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
index 9a5bfb5..e08ddfa 100644
--- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java
+++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
@@ -649,7 +649,8 @@ public class OrbotMainActivity extends AppCompatActivity
 							BackupUtils hsutils = new BackupUtils(getApplicationContext());
 							if(keyZipPath != null && keyZipPath.length() > 0)
 							{
-								hsutils.restoreZipBackup(hsPort, keyZipPath);
+								// TODO
+								// hsutils.restoreZipBackup(hsPort, keyZipPath);
 								requestTorRereadConfig();
 							}
 
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 07a9600..d446190 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
@@ -1,26 +1,31 @@
 package org.torproject.android.ui.hiddenservices.backup;
 
 import android.content.ContentResolver;
+import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.widget.Toast;
 
 import org.json.JSONException;
 import org.json.JSONObject;
-import org.torproject.android.service.R;
+import org.torproject.android.R;
 import org.torproject.android.service.TorServiceConstants;
 import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
 import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
 
 public class BackupUtils {
+    private final String configFileName = "config.json";
     private File mHSBasePath;
     private Context mContext;
     private ContentResolver mResolver;
-    private final String configFileName = "config.json";
 
     public BackupUtils(Context context) {
         mContext = context;
@@ -112,18 +117,70 @@ public class BackupUtils {
         return zip_path;
     }
 
-    public void restoreZipBackup(Integer port, String path) {
+    public void restoreZipBackup(File backup) {
+        String backupName = backup.getName();
+        String hsDir = backupName.substring(0, backupName.lastIndexOf('.'));
+        String configFilePath = mHSBasePath + "/" + hsDir + "/" + configFileName;
+        String jString = null;
+
+        File hsPath = new File(mHSBasePath.getAbsolutePath(), hsDir);
+        if (!hsPath.isDirectory())
+            hsPath.mkdirs();
+
+        ZipIt zip = new ZipIt(null, backup.getAbsolutePath());
+        zip.unzip(hsPath.getAbsolutePath());
+
+        File config = new File(configFilePath);
+        FileInputStream stream = null;
+
         try {
-            File hsPath = new File(mHSBasePath.getCanonicalPath(), "/hs" + port);
-            if (hsPath.mkdirs()) {
-                ZipIt zip = new ZipIt(null, path);
-                zip.unzip(hsPath.getCanonicalPath());
-                return;
-            }
+            stream = new FileInputStream(config);
+            FileChannel fc = stream.getChannel();
+            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
+            jString = Charset.defaultCharset().decode(bb).toString();
+            stream.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
 
-        Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show();
+        if (jString == null)
+            Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show();
+
+        try {
+            JSONObject savedValues = new JSONObject(jString);
+            ContentValues fields = new ContentValues();
+
+            fields.put(
+                    HSContentProvider.HiddenService.NAME,
+                    savedValues.getString(HSContentProvider.HiddenService.NAME)
+            );
+
+            fields.put(
+                    HSContentProvider.HiddenService.PORT,
+                    savedValues.getInt(HSContentProvider.HiddenService.PORT)
+            );
+
+            fields.put(
+                    HSContentProvider.HiddenService.ONION_PORT,
+                    savedValues.getInt(HSContentProvider.HiddenService.ONION_PORT)
+            );
+
+            fields.put(
+                    HSContentProvider.HiddenService.DOMAIN,
+                    savedValues.getString(HSContentProvider.HiddenService.DOMAIN)
+            );
+
+            fields.put(
+                    HSContentProvider.HiddenService.CREATED_BY_USER,
+                    savedValues.getInt(HSContentProvider.HiddenService.CREATED_BY_USER)
+            );
+
+            mResolver.insert(HSContentProvider.CONTENT_URI, fields);
+        } catch (JSONException e) {
+            e.printStackTrace();
+            Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show();
+        }
+
+        Toast.makeText(mContext, R.string.backup_restored, Toast.LENGTH_LONG).show();
     }
 }
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipIt.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipIt.java
index 42e0bf9..130b56b 100644
--- a/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipIt.java
+++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/backup/ZipIt.java
@@ -1,6 +1,9 @@
 package org.torproject.android.ui.hiddenservices.backup;
 
 
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
@@ -18,14 +21,14 @@ public class ZipIt {
     private String[] _files;
     private String _zipFile;
 
-    public ZipIt(String[] files, String zipFile) {
+    public ZipIt(@Nullable String[] files, @NonNull String zipFile) {
         _files = files;
         _zipFile = zipFile;
     }
 
     public boolean zip() {
         try {
-            BufferedInputStream origin = null;
+            BufferedInputStream origin;
             FileOutputStream dest = new FileOutputStream(_zipFile);
 
             ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
@@ -71,12 +74,12 @@ public class ZipIt {
                 // Need to create directories if not exists, or
                 // it will generate an Exception...
                 if (ze.isDirectory()) {
-                    File fmd = new File(output_path + filename);
+                    File fmd = new File(output_path + "/" + filename);
                     fmd.mkdirs();
                     continue;
                 }
 
-                FileOutputStream fout = new FileOutputStream(output_path + filename);
+                FileOutputStream fout = new FileOutputStream(output_path + "/" + filename);
 
                 // cteni zipu a zapis
                 while ((count = zis.read(buffer)) != -1) {
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDataDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDataDialog.java
index f8ce1fd..329d6a8 100644
--- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDataDialog.java
+++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/HSDataDialog.java
@@ -82,10 +82,10 @@ public class HSDataDialog extends DialogFragment {
 
     private void saveData(String name, Integer local, Integer remote) {
         ContentValues fields = new ContentValues();
-        fields.put("name", name);
-        fields.put("port", local);
-        fields.put("onion_port", remote);
-        fields.put("created_by_user", 1);
+        fields.put(HSContentProvider.HiddenService.NAME, name);
+        fields.put(HSContentProvider.HiddenService.PORT, local);
+        fields.put(HSContentProvider.HiddenService.ONION_PORT, remote);
+        fields.put(HSContentProvider.HiddenService.CREATED_BY_USER, 1);
 
         ContentResolver cr = getContext().getContentResolver();
 
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java
index 5ca4158..0c0d943 100644
--- a/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java
+++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/dialogs/SelectBackupDialog.java
@@ -12,6 +12,7 @@ import android.widget.ListView;
 
 import org.torproject.android.R;
 import org.torproject.android.ui.hiddenservices.adapters.BackupAdapter;
+import org.torproject.android.ui.hiddenservices.backup.BackupUtils;
 import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
 
 import java.io.File;
@@ -72,7 +73,9 @@ public class SelectBackupDialog extends DialogFragment {
         backups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                // TODO
+                BackupUtils backupUtils = new BackupUtils(view.getContext().getApplicationContext());
+                File p = (File) parent.getItemAtPosition(position);
+                backupUtils.restoreZipBackup(p);
             }
         });
 
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/providers/HSContentProvider.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/providers/HSContentProvider.java
index 2270f5d..fe6dbf1 100644
--- a/app/src/main/java/org/torproject/android/ui/hiddenservices/providers/HSContentProvider.java
+++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/providers/HSContentProvider.java
@@ -16,10 +16,6 @@ import org.torproject.android.ui.hiddenservices.database.HSDatabase;
 
 
 public class HSContentProvider extends ContentProvider {
-    private static final String AUTH = "org.torproject.android.ui.hiddenservices.providers";
-    public static final Uri CONTENT_URI =
-            Uri.parse("content://" + AUTH + "/hs");
-
     public static final String[] PROJECTION = new String[]{
             HiddenService._ID,
             HiddenService.NAME,
@@ -28,7 +24,9 @@ public class HSContentProvider extends ContentProvider {
             HiddenService.ONION_PORT,
             HiddenService.CREATED_BY_USER
     };
-
+    private static final String AUTH = "org.torproject.android.ui.hiddenservices.providers";
+    public static final Uri CONTENT_URI =
+            Uri.parse("content://" + AUTH + "/hs");
     //UriMatcher
     private static final int ONIONS = 1;
     private static final int ONION_ID = 2;
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index d2057ff..656a3dd 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -343,6 +343,7 @@
     <string name="backup_service">Backup Service</string>
     <string name="delete_service">Delete Service</string>
     <string name="backup_saved_at_external_storage">Backup saved at external storage</string>
+    <string name="backup_restored">Backup restored</string>
     <string name="filemanager_not_available">Filemanager not available</string>
     <string name="please_grant_permissions_for_external_storage">Please grant permissions for external storage</string>
     <string name="permission_granted">Permission granted</string>



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