Write config.json

This commit is contained in:
arrase 2016-11-27 17:15:15 +01:00
parent fca54cfcdc
commit 130b89d846
3 changed files with 99 additions and 30 deletions

View File

@ -26,16 +26,8 @@ import org.torproject.android.ui.hiddenservices.storage.PermissionManager;
public class HiddenServicesActivity extends AppCompatActivity { public class HiddenServicesActivity extends AppCompatActivity {
public final int WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR = 1; public final int WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR = 1;
private ContentResolver mCR; private ContentResolver mResolver;
private OnionListAdapter mAdapter; private OnionListAdapter mAdapter;
private Toolbar toolbar;
private String[] mProjection = new String[]{
HSContentProvider.HiddenService._ID,
HSContentProvider.HiddenService.NAME,
HSContentProvider.HiddenService.PORT,
HSContentProvider.HiddenService.DOMAIN,
HSContentProvider.HiddenService.CREATED_BY_USER
};
private String mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=1"; private String mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=1";
@ -44,11 +36,11 @@ public class HiddenServicesActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.layout_hs_list_view); setContentView(R.layout.layout_hs_list_view);
toolbar = (Toolbar) findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mCR = getContentResolver(); mResolver = getContentResolver();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@ -61,11 +53,13 @@ public class HiddenServicesActivity extends AppCompatActivity {
mAdapter = new OnionListAdapter( mAdapter = new OnionListAdapter(
this, this,
mCR.query(HSContentProvider.CONTENT_URI, mProjection, mWhere, null, null), mResolver.query(
HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null
),
0 0
); );
mCR.registerContentObserver( mResolver.registerContentObserver(
HSContentProvider.CONTENT_URI, true, new HSObserver(new Handler()) HSContentProvider.CONTENT_URI, true, new HSObserver(new Handler())
); );
@ -143,8 +137,8 @@ public class HiddenServicesActivity extends AppCompatActivity {
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
mAdapter.changeCursor(mCR.query( mAdapter.changeCursor(mResolver.query(
HSContentProvider.CONTENT_URI, mProjection, mWhere, null, null HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null
)); ));
} }
} }

View File

@ -1,18 +1,26 @@
package org.torproject.android.ui.hiddenservices.backup; package org.torproject.android.ui.hiddenservices.backup;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.widget.Toast; import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import org.torproject.android.service.R; import org.torproject.android.service.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.storage.ExternalStorage; import org.torproject.android.ui.hiddenservices.storage.ExternalStorage;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
public class BackupUtils { public class BackupUtils {
private File mHSBasePath; private File mHSBasePath;
private Context mContext; private Context mContext;
private ContentResolver mResolver;
private final String configFileName = "config.json";
public BackupUtils(Context context) { public BackupUtils(Context context) {
mContext = context; mContext = context;
@ -20,26 +28,86 @@ public class BackupUtils {
mContext.getFilesDir().getAbsolutePath(), mContext.getFilesDir().getAbsolutePath(),
TorServiceConstants.HIDDEN_SERVICES_DIR TorServiceConstants.HIDDEN_SERVICES_DIR
); );
mResolver = mContext.getContentResolver();
} }
public String createZipBackup(Integer port) { public String createZipBackup(Integer port) {
String configFilePath = mHSBasePath + "/hs" + port + "/" + configFileName;
String hostnameFilePath = mHSBasePath + "/hs" + port + "/hostname";
String keyFilePath = mHSBasePath + "/hs" + port + "/private_key";
File storage_path = ExternalStorage.getOrCreateBackupDir(); File storage_path = ExternalStorage.getOrCreateBackupDir();
if (storage_path == null) if (storage_path == null)
return null; return null;
Cursor portData = mResolver.query(
HSContentProvider.CONTENT_URI,
HSContentProvider.PROJECTION,
HSContentProvider.HiddenService.PORT + "=" + port,
null,
null
);
JSONObject config = new JSONObject();
try {
if (portData.getCount() != 1)
return null;
portData.moveToNext();
config.put(
HSContentProvider.HiddenService.NAME,
portData.getString(portData.getColumnIndex(HSContentProvider.HiddenService.NAME))
);
config.put(
HSContentProvider.HiddenService.PORT,
portData.getInt(portData.getColumnIndex(HSContentProvider.HiddenService.PORT))
);
config.put(
HSContentProvider.HiddenService.ONION_PORT,
portData.getInt(portData.getColumnIndex(HSContentProvider.HiddenService.ONION_PORT))
);
config.put(
HSContentProvider.HiddenService.DOMAIN,
portData.getString(portData.getColumnIndex(HSContentProvider.HiddenService.DOMAIN))
);
config.put(
HSContentProvider.HiddenService.CREATED_BY_USER,
portData.getInt(portData.getColumnIndex(HSContentProvider.HiddenService.CREATED_BY_USER))
);
} catch (JSONException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
try {
FileWriter file = new FileWriter(configFilePath);
file.write(config.toString());
file.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
portData.close();
String zip_path = storage_path.getAbsolutePath() + "/hs" + port + ".zip"; String zip_path = storage_path.getAbsolutePath() + "/hs" + port + ".zip";
String files[] = { String files[] = {hostnameFilePath, keyFilePath, configFilePath};
mHSBasePath + "/hs" + port + "/hostname",
mHSBasePath + "/hs" + port + "/private_key"
};
ZipIt zip = new ZipIt(files, zip_path); ZipIt zip = new ZipIt(files, zip_path);
if (!zip.zip()) { if (!zip.zip())
return null; return null;
}
return zip_path; return zip_path;
} }

View File

@ -19,39 +19,47 @@ public class HSContentProvider extends ContentProvider {
private static final String AUTH = "org.torproject.android.ui.hiddenservices.providers"; private static final String AUTH = "org.torproject.android.ui.hiddenservices.providers";
public static final Uri CONTENT_URI = public static final Uri CONTENT_URI =
Uri.parse("content://" + AUTH + "/hs"); Uri.parse("content://" + AUTH + "/hs");
public static final String[] PROJECTION = new String[]{
HiddenService._ID,
HiddenService.NAME,
HiddenService.PORT,
HiddenService.DOMAIN,
HiddenService.ONION_PORT,
HiddenService.CREATED_BY_USER
};
//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;
private static final UriMatcher uriMatcher; private static final UriMatcher uriMatcher;
//Inicializamos el UriMatcher
static { static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTH, "hs", ONIONS); uriMatcher.addURI(AUTH, "hs", ONIONS);
uriMatcher.addURI(AUTH, "hs/#", ONION_ID); uriMatcher.addURI(AUTH, "hs/#", ONION_ID);
} }
private HSDatabase mServerDB; private HSDatabase mServervices;
private Context mContext; private Context mContext;
@Override @Override
public boolean onCreate() { public boolean onCreate() {
mContext = getContext(); mContext = getContext();
mServerDB = new HSDatabase(mContext); mServervices = new HSDatabase(mContext);
return true; return true;
} }
@Nullable @Nullable
@Override @Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
//Si es una consulta a un ID concreto construimos el WHERE
String where = selection; String where = selection;
if (uriMatcher.match(uri) == ONION_ID) { if (uriMatcher.match(uri) == ONION_ID) {
where = "_id=" + uri.getLastPathSegment(); where = "_id=" + uri.getLastPathSegment();
} }
SQLiteDatabase db = mServerDB.getReadableDatabase(); SQLiteDatabase db = mServervices.getReadableDatabase();
return db.query(HSDatabase.HS_DATA_TABLE_NAME, projection, where, return db.query(HSDatabase.HS_DATA_TABLE_NAME, projection, where,
selectionArgs, null, null, sortOrder); selectionArgs, null, null, sortOrder);
@ -77,7 +85,7 @@ public class HSContentProvider extends ContentProvider {
public Uri insert(@NonNull Uri uri, ContentValues values) { public Uri insert(@NonNull Uri uri, ContentValues values) {
long regId; long regId;
SQLiteDatabase db = mServerDB.getWritableDatabase(); SQLiteDatabase db = mServervices.getWritableDatabase();
regId = db.insert(HSDatabase.HS_DATA_TABLE_NAME, null, values); regId = db.insert(HSDatabase.HS_DATA_TABLE_NAME, null, values);
@ -89,13 +97,12 @@ public class HSContentProvider extends ContentProvider {
@Override @Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) { public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
//Si es una consulta a un ID concreto construimos el WHERE
String where = selection; String where = selection;
if (uriMatcher.match(uri) == ONION_ID) { if (uriMatcher.match(uri) == ONION_ID) {
where = "_id=" + uri.getLastPathSegment(); where = "_id=" + uri.getLastPathSegment();
} }
SQLiteDatabase db = mServerDB.getWritableDatabase(); SQLiteDatabase db = mServervices.getWritableDatabase();
Integer rows = db.delete(HSDatabase.HS_DATA_TABLE_NAME, where, selectionArgs); Integer rows = db.delete(HSDatabase.HS_DATA_TABLE_NAME, where, selectionArgs);
@ -107,7 +114,7 @@ public class HSContentProvider extends ContentProvider {
@Override @Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) { public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = mServerDB.getWritableDatabase(); SQLiteDatabase db = mServervices.getWritableDatabase();
String where = selection; String where = selection;
if (uriMatcher.match(uri) == ONION_ID) { if (uriMatcher.match(uri) == ONION_ID) {