From 5f02561f9d00b238c880d0a77bcd63f41c556bc3 Mon Sep 17 00:00:00 2001 From: arrase Date: Sun, 27 Nov 2016 18:50:20 +0100 Subject: [PATCH] backup restore --- .../torproject/android/OrbotMainActivity.java | 3 +- .../ui/hiddenservices/backup/BackupUtils.java | 77 ++++++++++++++++--- .../ui/hiddenservices/backup/ZipIt.java | 11 ++- .../hiddenservices/dialogs/HSDataDialog.java | 8 +- .../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 9a5bfb5f..e08ddfa5 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 07a9600c..d446190d 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 42e0bf9b..130b56bb 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 f8ce1fdc..329d6a8c 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 5ca41580..0c0d9438 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 2270f5d1..fe6dbf16 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 d2057ff3..656a3dd0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -343,6 +343,7 @@ Backup Service Delete Service Backup saved at external storage + Backup restored Filemanager not available Please grant permissions for external storage Permission granted