backup restore

This commit is contained in:
arrase 2016-11-27 18:50:20 +01:00
parent 130b89d846
commit 5f02561f9d
7 changed files with 88 additions and 25 deletions

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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) {

View File

@ -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();

View File

@ -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);
}
});

View File

@ -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;

View File

@ -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>