bigsnitch/session.cpp

378 lines
11 KiB
C++
Raw Normal View History

2020-08-08 11:44:16 +00:00
#include "session.h"
Session::Session(QObject *parent) : QObject(parent)
{
}
Session::~Session() {
unload();
}
2020-08-13 21:25:04 +00:00
void Session::open_db(std::filesystem::path path) {
2020-08-08 11:44:16 +00:00
if(db) {
unload();
}
2020-08-13 21:25:04 +00:00
2020-08-08 11:44:16 +00:00
auto rc = sqlite3_open(path.c_str(), &db);
2020-08-13 21:25:04 +00:00
if(rc != SQLITE_OK) {
2020-08-08 11:44:16 +00:00
qDebug("cannot open database");
2020-08-09 12:07:14 +00:00
qDebug() << sqlite3_errmsg(db);
2020-08-08 11:44:16 +00:00
unload();
throw;
}
2020-08-13 21:25:04 +00:00
}
void Session::prepare(const char* query, sqlite3_stmt** stmt) {
auto rc = sqlite3_prepare_v2(db, query, strlen(query), stmt, 0);
if(rc != SQLITE_OK) {
qDebug() << "cannot prepare statement";
qDebug() << query;
qDebug() << sqlite3_errmsg(db);
unload();
throw;
}
}
bool Session::prepare_and_exec(const char* query) {
sqlite3_stmt* res;
prepare(query, &res);
bool result = sqlite3_step(res)==SQLITE_OK;
sqlite3_finalize(res);
return result;
}
2020-08-18 15:14:48 +00:00
bool Session::table_exists(std::string name) {
2020-08-13 21:25:04 +00:00
sqlite3_stmt* res;
2020-08-18 15:14:48 +00:00
prepare(get_tbl, &res);
bind_text(res, 1, name);
2020-08-13 21:25:04 +00:00
auto rc = sqlite3_step(res);
sqlite3_finalize(res);
2020-08-18 15:14:48 +00:00
return rc == SQLITE_ROW;
}
2020-09-01 20:14:49 +00:00
bool Session::reset(sqlite3_stmt *stmt)
{
auto res = sqlite3_reset(stmt);
if(res != SQLITE_ROW ||
res != SQLITE_DONE ||
res != SQLITE_OK) {
qDebug() << "cannot reset statement";
qDebug() << sqlite3_errmsg(db);
unload();
throw;
return false;
}
return true;
}
2020-08-18 15:14:48 +00:00
void Session::prepare_tables() {
if(table_exists("setting")) {
prepare(get_setting, &stmt_get_setting);
auto v = getSetting("version");
if(v) {
if(std::stoi(*v) != LITTLESNITCH_VERSION) {
qDebug() << "version " << v.value().c_str() << " is not supported!";
2020-08-13 21:25:04 +00:00
unload();
throw;
}
2020-08-18 15:14:48 +00:00
qDebug() << "loaded session with version " << v.value().c_str();
2020-08-13 21:25:04 +00:00
} else {
2020-08-18 15:14:48 +00:00
qDebug() << "setting table but no version found, aborting";
2020-08-13 21:25:04 +00:00
unload();
throw;
}
2020-08-18 15:14:48 +00:00
} else {
prepare_and_exec(create_request_tbl);
prepare_and_exec(create_request_header_tbl);
prepare_and_exec(create_response_tbl);
prepare_and_exec(create_response_header_tbl);
prepare_and_exec(create_flow_tbl);
prepare_and_exec(create_setting_tbl);
prepare(get_setting, &stmt_get_setting);
2020-08-13 21:25:04 +00:00
}
2020-08-19 00:15:08 +00:00
prepare(get_all_history, &stmt_get_all_history);
2020-09-01 20:14:49 +00:00
prepare(get_request, &stmt_get_request);
prepare(get_request_headers, &stmt_get_request_headers);
prepare(get_response, &stmt_get_response);
prepare(get_response_headers, &stmt_get_response_headers);
2020-08-18 15:14:48 +00:00
prepare(update_setting, &stmt_update_setting);
2020-08-13 21:25:04 +00:00
prepare(insert_request, &stmt_insert_request);
prepare(insert_request_header, &stmt_insert_request_header);
prepare(insert_response, &stmt_insert_response);
prepare(insert_response_header, &stmt_insert_response_header);
prepare(insert_flow, &stmt_insert_flow);
2020-08-18 15:14:48 +00:00
prepare(insert_setting, &stmt_insert_setting);
saveSetting("version", std::to_string(LITTLESNITCH_VERSION));
2020-08-13 21:25:04 +00:00
}
void Session::load(std::filesystem::path path)
{
open_db(path);
prepare_tables();
loaded = true;
2020-08-08 11:44:16 +00:00
}
void Session::unload() {
if(db) {
sqlite3_close(db);
}
2020-08-13 21:25:04 +00:00
loaded = false;
}
bool Session::isLoaded() {
return loaded;
}
2020-08-18 15:14:48 +00:00
std::optional<std::string> Session::getSetting(std::string key) {
2020-09-01 20:14:49 +00:00
reset(stmt_get_setting);
2020-08-18 15:14:48 +00:00
int j = 1;
bind_text(stmt_get_setting, j++, key);
auto rc = sqlite3_step(stmt_get_setting);
if(rc == SQLITE_ROW) {
auto value = std::string((const char*)sqlite3_column_text(stmt_get_setting, 0));
return value;
} else if(rc == SQLITE_DONE) {
return std::nullopt;
} else {
qDebug() << "getting setting failed " << rc;
qDebug() << sqlite3_errmsg(db);
return std::nullopt;
}
return std::nullopt;
}
2020-08-26 18:58:36 +00:00
std::vector<HistoryItem>* Session::getHistoryItems() {
2020-08-28 23:09:41 +00:00
qDebug() << "history items generated";
2020-08-26 18:58:36 +00:00
// todo check reset return code
2020-09-01 20:14:49 +00:00
reset(stmt_get_all_history);
2020-08-19 00:15:08 +00:00
2020-08-26 18:58:36 +00:00
std::vector<HistoryItem>* result = new std::vector<HistoryItem>();
2020-08-19 00:15:08 +00:00
auto rc = sqlite3_step(stmt_get_all_history);
while(rc == SQLITE_ROW) {
int j = 0;
2020-08-26 18:58:36 +00:00
HistoryItem i;
2020-08-19 00:15:08 +00:00
i.id = sqlite3_column_int(stmt_get_all_history, j++);
i.timestamp = sqlite3_column_double(stmt_get_all_history, j++);
i.method = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++));
i.scheme = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++));
i.host = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++));
i.port = sqlite3_column_int(stmt_get_all_history, j++);
i.path = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++));
i.status_code = sqlite3_column_int(stmt_get_all_history, j++);
i.reason = std::string((const char*)sqlite3_column_text(stmt_get_all_history, j++));
i.rtt = sqlite3_column_double(stmt_get_all_history, j++);
i.size = sqlite3_column_int(stmt_get_all_history, j++);
result->push_back(i);
rc = sqlite3_step(stmt_get_all_history);
}
if(rc != SQLITE_DONE) {
qDebug() << "error getting history items" << rc;
qDebug() << sqlite3_errmsg(db);
delete result;
return nullptr;
}
return result;
}
2020-09-01 20:14:49 +00:00
std::optional<std::tuple<std::string, std::string>> Session::getRequest(int id)
{
}
std::optional<std::map<std::string, std::string>> Session::getRequestHeader(int id)
{
}
std::optional<std::tuple<std::string, std::string>> Session::getResponse(int id)
{
}
std::optional<std::map<std::string, std::string>> Session::getResponseHeader(int id)
{
}
2020-08-13 21:25:04 +00:00
void Session::bind_text(sqlite3_stmt* stmt, int id, std::string text) {
//qDebug() << id << " " << text.c_str() << " " << strlen(text.c_str());
2020-08-13 21:25:04 +00:00
sqlite3_bind_text(stmt, id, text.c_str(), -1, SQLITE_TRANSIENT);
2020-08-08 11:44:16 +00:00
}
2020-08-09 12:07:14 +00:00
int Session::saveRequest(http::Flow flow) {
2020-09-01 20:14:49 +00:00
reset(stmt_insert_request);
2020-08-14 16:54:43 +00:00
int j = 1;
bind_text(stmt_insert_request, j++, flow.request.server_ip_address);
sqlite3_bind_int(stmt_insert_request, j++, flow.request.tls);
bind_text(stmt_insert_request, j++, flow.request.content);
bind_text(stmt_insert_request, j++, flow.request.scheme);
bind_text(stmt_insert_request, j++, flow.request.method);
bind_text(stmt_insert_request, j++, flow.request.host);
sqlite3_bind_int(stmt_insert_request, j++, flow.request.port);
bind_text(stmt_insert_request, j++, flow.request.http_version);
bind_text(stmt_insert_request, j++, flow.request.path);
sqlite3_bind_double(stmt_insert_request, j++, flow.request.timestamp_start);
sqlite3_bind_double(stmt_insert_request, j++, flow.request.timestamp_end);
bind_text(stmt_insert_request, j++, flow.request.error);
2020-08-13 21:25:04 +00:00
auto rc = sqlite3_step(stmt_insert_request);
if(rc != SQLITE_DONE) {
qDebug() << "inserting request failed" << rc;
qDebug() << sqlite3_errmsg(db);
2020-08-14 16:54:43 +00:00
return -1;
2020-08-13 21:25:04 +00:00
}
int id = sqlite3_last_insert_rowid(db);
for(auto&[k,v] : flow.request.headers) {
saveRequestHeader(k,v,id);
}
return id;
}
2020-08-14 16:54:43 +00:00
int Session::saveRequestHeader(std::string key, std::string value, int id) {
2020-09-01 20:14:49 +00:00
reset(stmt_insert_request_header);
2020-08-14 16:54:43 +00:00
int j = 1;
bind_text(stmt_insert_request_header, j++, key);
bind_text(stmt_insert_request_header, j++, value);
sqlite3_bind_int(stmt_insert_request_header, j++, id);
auto rc = sqlite3_step(stmt_insert_request_header);
if(rc != SQLITE_DONE) {
qDebug() << "inserting request header failed" << rc;
qDebug() << sqlite3_errmsg(db);
return -1;
}
return sqlite3_last_insert_rowid(db);
2020-08-09 12:07:14 +00:00
}
2020-08-14 01:37:33 +00:00
2020-08-14 16:54:43 +00:00
int Session::saveResponse(http::Flow flow) {
2020-09-01 20:14:49 +00:00
reset(stmt_insert_response);
int j = 1;
sqlite3_bind_int(stmt_insert_response, j++, flow.response.status_code);
bind_text(stmt_insert_response, j++, flow.response.http_version);
bind_text(stmt_insert_response, j++, flow.response.reason);
bind_text(stmt_insert_response, j++, flow.response.content);
sqlite3_bind_double(stmt_insert_response, j++, flow.response.timestamp_start);
sqlite3_bind_double(stmt_insert_response, j++, flow.response.timestamp_end);
2020-08-14 16:54:43 +00:00
auto rc = sqlite3_step(stmt_insert_response);
if(rc != SQLITE_DONE) {
qDebug() << "inserting response failed" << rc;
qDebug() << sqlite3_errmsg(db);
return -1;
}
int id = sqlite3_last_insert_rowid(db);
for(auto&[k,v] : flow.response.headers) {
saveResponseHeader(k,v,id);
}
return id;
}
int Session::saveResponseHeader(std::string key, std::string value, int id) {
2020-09-01 20:14:49 +00:00
reset(stmt_insert_response_header);
int j = 1;
bind_text(stmt_insert_response_header, j++, key);
bind_text(stmt_insert_response_header, j++, value);
sqlite3_bind_int(stmt_insert_response_header, j++, id);
auto rc = sqlite3_step(stmt_insert_response_header);
if(rc != SQLITE_DONE) {
qDebug() << "inserting response header failed" << rc;
qDebug() << sqlite3_errmsg(db);
return -1;
}
return sqlite3_last_insert_rowid(db);
}
int Session::saveFlow(http::Flow flow) {
2020-09-01 20:14:49 +00:00
reset(stmt_insert_flow);
2020-08-18 15:14:48 +00:00
int request_id = saveRequest(flow);
int response_id = saveResponse(flow);
int j = 1;
bind_text(stmt_insert_flow, j++, flow.uid);
sqlite3_bind_int(stmt_insert_flow, j++, request_id);
sqlite3_bind_int(stmt_insert_flow, j++, response_id);
auto rc = sqlite3_step(stmt_insert_flow);
if(rc != SQLITE_DONE) {
qDebug() << "inserting flow failed" << rc;
qDebug() << sqlite3_errmsg(db);
return -1;
}
2020-08-28 23:09:41 +00:00
qDebug() << "saved flow";
return sqlite3_last_insert_rowid(db);
2020-08-14 01:37:33 +00:00
}
2020-08-18 15:14:48 +00:00
bool Session::saveSetting(std::string key, std::string value) {
auto old_value = getSetting(key);
if(!old_value) {
2020-09-01 20:14:49 +00:00
reset(stmt_insert_setting);
2020-08-18 15:14:48 +00:00
int j = 1;
bind_text(stmt_insert_setting, j++, key);
bind_text(stmt_insert_setting, j++, value);
auto rc = sqlite3_step(stmt_insert_setting);
if(rc != SQLITE_DONE) {
qDebug() << "inserting setting failed" << rc;
qDebug() << sqlite3_errmsg(db);
return false;
}
return sqlite3_last_insert_rowid(db);
} else {
2020-09-01 20:14:49 +00:00
reset(stmt_update_setting);
2020-08-18 15:14:48 +00:00
int j = 1;
bind_text(stmt_update_setting, j++, value);
bind_text(stmt_insert_setting, j++, key);
auto rc = sqlite3_step(stmt_update_setting);
if(rc != SQLITE_DONE) {
qDebug() << "updating setting failed" << rc;
qDebug() << sqlite3_errmsg(db);
return false;
}
return true;
}
return false;
}