backup restore
This commit is contained in:
parent
130b89d846
commit
5f02561f9d
|
@ -649,7 +649,8 @@ public class OrbotMainActivity extends AppCompatActivity
|
||||||
BackupUtils hsutils = new BackupUtils(getApplicationContext());
|
BackupUtils hsutils = new BackupUtils(getApplicationContext());
|
||||||
if(keyZipPath != null && keyZipPath.length() > 0)
|
if(keyZipPath != null && keyZipPath.length() > 0)
|
||||||
{
|
{
|
||||||
hsutils.restoreZipBackup(hsPort, keyZipPath);
|
// TODO
|
||||||
|
// hsutils.restoreZipBackup(hsPort, keyZipPath);
|
||||||
requestTorRereadConfig();
|
requestTorRereadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1,31 @@
|
||||||
package org.torproject.android.ui.hiddenservices.backup;
|
package org.torproject.android.ui.hiddenservices.backup;
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
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.service.TorServiceConstants;
|
||||||
import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
|
import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
|
||||||
import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
|
import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.MappedByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
public class BackupUtils {
|
public class BackupUtils {
|
||||||
|
private final String configFileName = "config.json";
|
||||||
private File mHSBasePath;
|
private File mHSBasePath;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private ContentResolver mResolver;
|
private ContentResolver mResolver;
|
||||||
private final String configFileName = "config.json";
|
|
||||||
|
|
||||||
public BackupUtils(Context context) {
|
public BackupUtils(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
@ -112,18 +117,70 @@ public class BackupUtils {
|
||||||
return zip_path;
|
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 {
|
try {
|
||||||
File hsPath = new File(mHSBasePath.getCanonicalPath(), "/hs" + port);
|
stream = new FileInputStream(config);
|
||||||
if (hsPath.mkdirs()) {
|
FileChannel fc = stream.getChannel();
|
||||||
ZipIt zip = new ZipIt(null, path);
|
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
|
||||||
zip.unzip(hsPath.getCanonicalPath());
|
jString = Charset.defaultCharset().decode(bb).toString();
|
||||||
return;
|
stream.close();
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.error, Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Toast.makeText(mContext, R.string.backup_restored, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package org.torproject.android.ui.hiddenservices.backup;
|
package org.torproject.android.ui.hiddenservices.backup;
|
||||||
|
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -18,14 +21,14 @@ public class ZipIt {
|
||||||
private String[] _files;
|
private String[] _files;
|
||||||
private String _zipFile;
|
private String _zipFile;
|
||||||
|
|
||||||
public ZipIt(String[] files, String zipFile) {
|
public ZipIt(@Nullable String[] files, @NonNull String zipFile) {
|
||||||
_files = files;
|
_files = files;
|
||||||
_zipFile = zipFile;
|
_zipFile = zipFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean zip() {
|
public boolean zip() {
|
||||||
try {
|
try {
|
||||||
BufferedInputStream origin = null;
|
BufferedInputStream origin;
|
||||||
FileOutputStream dest = new FileOutputStream(_zipFile);
|
FileOutputStream dest = new FileOutputStream(_zipFile);
|
||||||
|
|
||||||
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
|
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
|
||||||
|
@ -71,12 +74,12 @@ public class ZipIt {
|
||||||
// Need to create directories if not exists, or
|
// Need to create directories if not exists, or
|
||||||
// it will generate an Exception...
|
// it will generate an Exception...
|
||||||
if (ze.isDirectory()) {
|
if (ze.isDirectory()) {
|
||||||
File fmd = new File(output_path + filename);
|
File fmd = new File(output_path + "/" + filename);
|
||||||
fmd.mkdirs();
|
fmd.mkdirs();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileOutputStream fout = new FileOutputStream(output_path + filename);
|
FileOutputStream fout = new FileOutputStream(output_path + "/" + filename);
|
||||||
|
|
||||||
// cteni zipu a zapis
|
// cteni zipu a zapis
|
||||||
while ((count = zis.read(buffer)) != -1) {
|
while ((count = zis.read(buffer)) != -1) {
|
||||||
|
|
|
@ -82,10 +82,10 @@ public class HSDataDialog extends DialogFragment {
|
||||||
|
|
||||||
private void saveData(String name, Integer local, Integer remote) {
|
private void saveData(String name, Integer local, Integer remote) {
|
||||||
ContentValues fields = new ContentValues();
|
ContentValues fields = new ContentValues();
|
||||||
fields.put("name", name);
|
fields.put(HSContentProvider.HiddenService.NAME, name);
|
||||||
fields.put("port", local);
|
fields.put(HSContentProvider.HiddenService.PORT, local);
|
||||||
fields.put("onion_port", remote);
|
fields.put(HSContentProvider.HiddenService.ONION_PORT, remote);
|
||||||
fields.put("created_by_user", 1);
|
fields.put(HSContentProvider.HiddenService.CREATED_BY_USER, 1);
|
||||||
|
|
||||||
ContentResolver cr = getContext().getContentResolver();
|
ContentResolver cr = getContext().getContentResolver();
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import android.widget.ListView;
|
||||||
|
|
||||||
import org.torproject.android.R;
|
import org.torproject.android.R;
|
||||||
import org.torproject.android.ui.hiddenservices.adapters.BackupAdapter;
|
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 org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -72,7 +73,9 @@ public class SelectBackupDialog extends DialogFragment {
|
||||||
backups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
backups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,6 @@ import org.torproject.android.ui.hiddenservices.database.HSDatabase;
|
||||||
|
|
||||||
|
|
||||||
public class HSContentProvider extends ContentProvider {
|
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[]{
|
public static final String[] PROJECTION = new String[]{
|
||||||
HiddenService._ID,
|
HiddenService._ID,
|
||||||
HiddenService.NAME,
|
HiddenService.NAME,
|
||||||
|
@ -28,7 +24,9 @@ public class HSContentProvider extends ContentProvider {
|
||||||
HiddenService.ONION_PORT,
|
HiddenService.ONION_PORT,
|
||||||
HiddenService.CREATED_BY_USER
|
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
|
//UriMatcher
|
||||||
private static final int ONIONS = 1;
|
private static final int ONIONS = 1;
|
||||||
private static final int ONION_ID = 2;
|
private static final int ONION_ID = 2;
|
||||||
|
|
|
@ -343,6 +343,7 @@
|
||||||
<string name="backup_service">Backup Service</string>
|
<string name="backup_service">Backup Service</string>
|
||||||
<string name="delete_service">Delete 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_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="filemanager_not_available">Filemanager not available</string>
|
||||||
<string name="please_grant_permissions_for_external_storage">Please grant permissions for external storage</string>
|
<string name="please_grant_permissions_for_external_storage">Please grant permissions for external storage</string>
|
||||||
<string name="permission_granted">Permission granted</string>
|
<string name="permission_granted">Permission granted</string>
|
||||||
|
|
Loading…
Reference in New Issue