bigsnitch/session.h

116 lines
5.8 KiB
C
Raw Normal View History

2020-08-08 11:44:16 +00:00
#pragma once
#include <QObject>
2020-08-09 12:07:14 +00:00
#include <includes.h>
2020-08-13 03:35:22 +00:00
#include <httpflow.h>
2020-08-18 15:14:48 +00:00
#include <optional>
2020-08-08 11:44:16 +00:00
class Session : public QObject
{
Q_OBJECT
private:
2020-08-13 21:25:04 +00:00
const char* create_request_tbl = R"rstr( CREATE TABLE request(
id INTEGER PRIMARY KEY AUTOINCREMENT,
server_conn_id TEXT,
server_ip_address TEXT,
tls INTEGER,
content TEXT,
scheme TEXT,
method TEXT,
host TEXT,
address TEXT,
port INTEGER,
http_version TEXT,
path TEXT,
timestamp_start REAL,
timestamp_end REAL,
error TEXT
); )rstr";
const char* create_request_header_tbl = R"rstr( CREATE TABLE request_header(
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT,
value TEXT,
request_id INTEGER,
FOREIGN KEY(request_id) REFERENCES request(referer_id) ON UPDATE CASCADE ON DELETE CASCADE
); )rstr";
const char* create_response_tbl = R"rstr( CREATE TABLE response(
id INTEGER PRIMARY KEY AUTOINCREMENT,
status_code INTEGER,
http_version TEXT,
reason TEXT,
content TEXT,
timestamp_start REAL,
timestamp_end REAL
); )rstr";
const char* create_response_header_tbl = R"rstr( CREATE TABLE response_header(
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT,
value TEXT,
response_id INTEGER,
FOREIGN KEY(response_id) REFERENCES response(referer_id) ON UPDATE CASCADE ON DELETE CASCADE
); )rstr";
const char* create_flow_tbl = R"rstr( CREATE TABLE flow(
id INTEGER PRIMARY KEY AUTOINCREMENT,
2020-08-14 16:54:43 +00:00
uid TEXT UNIQUE NOT NULL,
2020-08-13 21:25:04 +00:00
request_id INTEGER,
response_id INTEGER,
FOREIGN KEY(request_id) REFERENCES request(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY(response_id) REFERENCES response(id) ON UPDATE CASCADE ON DELETE CASCADE
); )rstr";
2020-08-18 15:14:48 +00:00
const char* create_setting_tbl = R"rstr( CREATE TABLE setting(
2020-08-13 21:25:04 +00:00
key TEXT UNIQUE NOT NULL PRIMARY KEY,
value TEXT
); )rstr";
2020-08-18 15:14:48 +00:00
const char* get_tbl = R"rstr( SELECT name FROM sqlite_master WHERE type='table' AND name=?; )rstr";
const char* get_setting = R"rstr( SELECT value FROM setting WHERE key=?; )rstr";
const char* insert_request = R"rstr( INSERT INTO request (server_ip_address, tls, content, scheme, method, host, address, port, http_version, path, timestamp_start, timestamp_end, error) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?); )rstr";
2020-08-13 21:25:04 +00:00
const char* insert_request_header = R"rstr( INSERT INTO request_header (key, value, request_id) VALUES (?,?,?); )rstr";
const char* insert_response = R"rstr( INSERT INTO response (status_code, http_version, reason, content, timestamp_start, timestamp_end) VALUES (?,?,?,?,?,?); )rstr";
const char* insert_response_header = R"rstr( INSERT INTO response_header (key, value, response_id) VALUES (?,?,?); )rstr";
const char* insert_flow = R"rstr( INSERT INTO flow (uid, request_id, response_id) VALUES (?,?,?); )rstr";
2020-08-18 15:14:48 +00:00
const char* insert_setting = R"rstr( INSERT INTO setting (key, value) VALUES (?,?); )rstr";
const char* update_setting = R"rstr( UPDATE setting SET value=? WHERE key=?; )rstr";
2020-08-13 21:25:04 +00:00
2020-08-09 12:07:14 +00:00
sqlite3* db = nullptr;
bool loaded = false;
2020-08-13 21:25:04 +00:00
2020-08-18 15:14:48 +00:00
sqlite3_stmt* stmt_get_setting = nullptr;
2020-08-13 21:25:04 +00:00
sqlite3_stmt* stmt_insert_request = nullptr;
sqlite3_stmt* stmt_insert_request_header = nullptr;
sqlite3_stmt* stmt_insert_response = nullptr;
sqlite3_stmt* stmt_insert_response_header = nullptr;
sqlite3_stmt* stmt_insert_flow = nullptr;
2020-08-18 15:14:48 +00:00
sqlite3_stmt* stmt_insert_setting = nullptr;
sqlite3_stmt* stmt_update_setting = nullptr;
2020-08-13 21:25:04 +00:00
void open_db(std::filesystem::path path);
void prepare_tables();
void prepare(const char* query, sqlite3_stmt** stmt);
bool prepare_and_exec(const char* query);
void bind_text(sqlite3_stmt* stmt, int id, std::string text);
2020-08-18 15:14:48 +00:00
bool table_exists(std::string name);
2020-08-08 11:44:16 +00:00
public:
explicit Session(QObject *parent = nullptr);
2020-08-09 12:07:14 +00:00
~Session();
2020-08-13 21:25:04 +00:00
2020-08-09 12:07:14 +00:00
void load(std::filesystem::path path);
void unload();
bool isLoaded();
2020-08-18 15:14:48 +00:00
std::optional<std::string> getSetting(std::string key);
2020-08-09 12:07:14 +00:00
public slots:
2020-08-14 16:54:43 +00:00
int saveRequest(http::Flow flow);
int saveRequestHeader(std::string key, std::string value, int id);
2020-08-14 16:54:43 +00:00
int saveResponse(http::Flow flow);
int saveResponseHeader(std::string key, std::string value, int id);
int saveFlow(http::Flow flow);
2020-08-18 15:14:48 +00:00
bool saveSetting(std::string key, std::string value);
2020-08-08 11:44:16 +00:00
signals:
};