Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
v1.2.0

See merge request paker_wreah/Inspector/Inspector!7
  • Loading branch information
pakerwreah committed Dec 16, 2019
2 parents 114c1f3 + cfd55a6 commit c7f1d80
Show file tree
Hide file tree
Showing 33 changed files with 275 additions and 225,157 deletions.
6 changes: 3 additions & 3 deletions Inspector.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |spec|
spec.name = "Inspector"
spec.version = "1.1.1"
spec.version = "1.2.0"
spec.summary = "Library to inspect databases in mobile apps"
spec.homepage = "https://gitlab.com/paker_wreah/Inspector/Inspector"
spec.license = 'LGPL'
spec.authors = { "Carlos César Neves Enumo" => "[email protected]" }

spec.source = { :git => "[email protected]:paker_wreah/Inspector/Inspector.git" }

spec.source_files = 'cpp/**/*.{h,hpp,c,cpp}', 'ios/Inspector/IOSInspector.{h,mm}'
spec.public_header_files = 'ios/Inspector/IOSInspector.h'
spec.library = 'z'

spec.dependency 'SQLCipher'
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public void onCreate() {
super.onCreate();

Inspector.initializeWith(this, 30000);

Inspector.setCipherKey("database.db", "123456", 3);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void run() {
Log.d("Inspector", resp);
}
}
}).start();
});//.start();
}

}
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.10.0"
}
}
Expand Down
3 changes: 2 additions & 1 deletion android/lib/inspector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publishing {
aar(MavenPublication) {
groupId 'br.newm.inspector'
artifactId 'inspector'
version '1.1.1'
version '1.2.0'

artifact("$buildDir/outputs/aar/inspector-release.aar")
}
Expand Down Expand Up @@ -51,4 +51,5 @@ android {

dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.2.2'
implementation "net.zetetic:android-database-sqlcipher:4.2.0@aar"
}
4 changes: 4 additions & 0 deletions android/lib/inspector/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ find_library( # Sets the name of the path variable.
# you want CMake to locate.
log)

set(LIBSQLITE ${PROJECT_SOURCE_DIR}/sqlcipher/${ANDROID_ABI}/libsqlcipher.so)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
Expand All @@ -45,6 +47,8 @@ target_link_libraries( # Specifies the target library.
inspector

inspector-cpp

${LIBSQLITE}
# Links the target library to the log library
# included in the NDK.
${log-lib})
5 changes: 4 additions & 1 deletion android/lib/inspector/src/main/cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ extern "C" JNIEXPORT void JNICALL
Java_br_newm_inspector_Inspector_initialize(JNIEnv *env, jobject, jint port) {
// No emulador precisa executar: ./adb forward tcp:30000 tcp:30000
inspector->bind(port);
}

inspector->preselectDB();
extern "C" JNIEXPORT void JNICALL
Java_br_newm_inspector_Inspector_setCipherKeyJNI(JNIEnv *env, jobject, jstring database, jstring password, jint version) {
inspector->setCipherKey(readString(env, database), readString(env, password), version);
}

extern "C" JNIEXPORT bool JNICALL
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package br.newm.inspector;

import android.annotation.SuppressLint;
import android.content.Context;

@SuppressWarnings("unused")
public class Inspector {
@SuppressLint("StaticFieldLeak")
private static Context context;

static {
Expand All @@ -24,5 +26,12 @@ private static String[] databaseList() {
return paths;
}

// alias to avoid argument obfuscation of public method
public static void setCipherKey(String database, String password, int version) {
setCipherKeyJNI(database, password, version);
}

private static native void setCipherKeyJNI(String database, String password, int version);

private static native void initialize(int port);
}
7 changes: 5 additions & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ project(inspector)

set(
sources
src/libs/sqlite3.c
src/libs/picohttpparser.c
src/libs/base64.cpp
src/libs/sha1.cpp
Expand All @@ -31,7 +30,7 @@ set(
add_library( # Sets the name of the library.
inspector-cpp

# Sets the library as a shared library.
# Sets the library as a static library.
STATIC

# Provides a relative path to your source file(s).
Expand All @@ -44,6 +43,10 @@ target_link_libraries(inspector-cpp z)
# build script, prebuilt third-party libraries, or system libraries.

if (NOT DEFINED ANDROID_NDK)
find_library(LIBSQLITE sqlcipher)

target_link_libraries(inspector-cpp ${LIBSQLITE})

add_executable(inspector-test src/main.cpp)

target_link_libraries(inspector-test inspector-cpp)
Expand Down
15 changes: 11 additions & 4 deletions cpp/src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
// Created by Paker on 2019-10-23.
//

#include <cassert>
#include "Database.h"

Database::Database(string path) {
Database::Database(string path, string password, int version) {
if (!path.length()) {
throw runtime_error("Database path not selected");
}
if (path.find("file://") == 0) {
path = path.substr(7);
}
auto flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
auto flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_WAL | SQLITE_OPEN_SHAREDCACHE;
auto err = sqlite3_open_v2(path.c_str(), &db, flags, nullptr);
if (err != SQLITE_OK) {
throw runtime_error("Error opening database (" + to_string(err) + "): " + path);
}

if (password.length()) {
execute("PRAGMA cipher_memory_security = OFF");
execute("PRAGMA key = '" + password + "'");

if (version) {
execute("PRAGMA cipher_compatibility = " + to_string(version));
}
}

execute("PRAGMA foreign_keys = ON");
execute("PRAGMA journal_mode = WAL");
}

Database::~Database() {
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ResultSet;
class Database {
sqlite3 *db;
public:
Database(string path);
Database(string path, string password = "", int version = 0);

~Database();

Expand Down
63 changes: 53 additions & 10 deletions cpp/src/DatabasePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,49 @@
#include "Database.h"
#include "HttpServer.h"
#include "util.h"
#include <thread>
#include <unistd.h>

using namespace util;

static int close_token;

shared_ptr<Database> DatabasePlugin::open() {
if (db_path.empty()) {
try {
selectDB(0);
} catch (out_of_range &ex) {
throw runtime_error("No databases available");
}
}

if (!db_con) {
auto name = split(db_path, '/').back();
if (cipher.count(name)) {
SQLCipher config = cipher[name];
db_con = make_shared<Database>(db_path, config.password, config.version);
} else {
db_con = make_shared<Database>(db_path);
}
}

thread([this]() {
auto token = ++close_token;
sleep(5);
if (token == close_token) {
db_con = nullptr;
}
}).detach();

return db_con;
}

DatabasePlugin::DatabasePlugin(HttpServer *server, DatabaseProvider *_provider) {
this->provider = _provider;

server->get("/database/list", [this](Request req) {
int index = 0;
auto paths = provider->databaseList();
auto paths = databaseList();
auto names = json::array();
for (int i = 0; i < paths.size(); i++) {
names += split(paths[i], '/').back();
Expand All @@ -36,7 +70,7 @@ DatabasePlugin::DatabasePlugin(HttpServer *server, DatabaseProvider *_provider)

selectDB(index);

Database db(db_path);
open();
} catch (out_of_range &ex) {
return Response(ex.what(), 400);
} catch (exception &ex) {
Expand All @@ -49,11 +83,11 @@ DatabasePlugin::DatabasePlugin(HttpServer *server, DatabaseProvider *_provider)
auto sql = req.body;

try {
Database db(db_path);
auto db = open();

auto start = timestamp();

auto res = db.query(sql);
auto res = db->query(sql);

auto duration = benchmark(start);

Expand Down Expand Up @@ -96,13 +130,13 @@ DatabasePlugin::DatabasePlugin(HttpServer *server, DatabaseProvider *_provider)

server->post("/database/execute", [this](Request req) {
try {
Database db(db_path);
auto db = open();

auto start = timestamp();

db.transaction();
db.execute(req.body);
db.commit();
db->transaction();
db->execute(req.body);
db->commit();

auto duration = benchmark(start);

Expand All @@ -115,11 +149,20 @@ DatabasePlugin::DatabasePlugin(HttpServer *server, DatabaseProvider *_provider)
});
}

vector<string> DatabasePlugin::databaseList() {
return filter<string>(provider->databaseList(), [](const string &item) { return !endsWith(item, "-journal"); });
}

void DatabasePlugin::selectDB(int index) {
auto list = provider->databaseList();
auto list = databaseList();
if (index >= 0 && index < list.size()) {
db_path = list[index];
db_con = nullptr;
} else {
throw out_of_range("Index out of range");
}
}
}

void DatabasePlugin::setCipherKey(string database, string password, int version) {
cipher[database] = {password, version};
}
19 changes: 18 additions & 1 deletion cpp/src/DatabasePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,41 @@
#ifndef INSPECTOR_DATABASEPLUGIN_H
#define INSPECTOR_DATABASEPLUGIN_H

#include <map>
#include <string>

class HttpServer;

class Database;

using namespace std;

class DatabaseProvider {
public:
virtual vector<string> databaseList() = 0;
};

struct SQLCipher {
string password;
int version;
};

class DatabasePlugin {
string db_path;
DatabaseProvider *provider;
map<string, SQLCipher> cipher;
shared_ptr<Database> db_con;

shared_ptr<Database> open();

vector<string> databaseList();

void selectDB(int index);

public:
DatabasePlugin(HttpServer *server, DatabaseProvider *provider);

void selectDB(int index);
void setCipherKey(string database, string password, int version);
};


Expand Down
Loading

0 comments on commit c7f1d80

Please sign in to comment.