Compare commits
No commits in common. "4fa74d0cfb3f492381a7236179d7a29e03e70b22" and "2cd8ef55580f370483a9a03d1d88e32533341e64" have entirely different histories.
4fa74d0cfb
...
2cd8ef5558
@ -16,7 +16,6 @@
|
|||||||
#define DATA_FILE_NAME "DATA.bin"
|
#define DATA_FILE_NAME "DATA.bin"
|
||||||
#define VECTOR_FILE_NAME "VECTOR.bin"
|
#define VECTOR_FILE_NAME "VECTOR.bin"
|
||||||
#define GEN_FILE_PATTRN "gen/%04d.bin"
|
#define GEN_FILE_PATTRN "gen/%04d.bin"
|
||||||
#define HOST_NAME "petrovv.com"
|
|
||||||
|
|
||||||
void DnaStore::load(DnaManagerData *data)
|
void DnaStore::load(DnaManagerData *data)
|
||||||
{
|
{
|
||||||
@ -150,24 +149,24 @@ void DnaStore::saveGen(DnaManagerData *data)
|
|||||||
sync();
|
sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
void client(std::string_view prefix);
|
void client(std::string prefix);
|
||||||
|
|
||||||
void DnaStore::sync()
|
void DnaStore::sync()
|
||||||
{
|
{
|
||||||
const char *prefix = sys::transformFilePath("");
|
const char *prefix = sys::transformFilePath("");
|
||||||
std::string_view prefixs = prefix;
|
std::string prefixs = prefix;
|
||||||
|
|
||||||
std::thread t(client, prefixs);
|
std::thread t(client, prefixs);
|
||||||
t.detach();
|
t.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
void client(std::string_view prefix)
|
void client(std::string prefix)
|
||||||
{
|
{
|
||||||
constexpr int extra_buff = 22; // len of 2**31 -> 2147483648 plus the pattern size
|
constexpr int extra_buff = 22; // len of 2**31 -> 2147483648 plus the pattern size
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
buffer.resize(prefix.size() + extra_buff);
|
buffer.resize(prefix.size() + extra_buff);
|
||||||
|
|
||||||
int sock = TcpSocket::connectt(HOST_NAME, keyPort);
|
int sock = TcpSocket::connectt("petrovv.com", keyPort);
|
||||||
if (sock < 0)
|
if (sock < 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -178,7 +177,7 @@ void client(std::string_view prefix)
|
|||||||
|
|
||||||
TcpSocket::closet(sock);
|
TcpSocket::closet(sock);
|
||||||
|
|
||||||
sock = TcpSocket::connectt(HOST_NAME, serverPort);
|
sock = TcpSocket::connectt("petrovv.com", serverPort);
|
||||||
|
|
||||||
if (sock < 0)
|
if (sock < 0)
|
||||||
{
|
{
|
||||||
|
@ -6,94 +6,12 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <unordered_map>
|
|
||||||
#include <mutex>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
// use pthread rw_lock to lock db so you can safy clone .db file
|
// use pthread rw_lock to lock db so you can safy clone .db file
|
||||||
|
|
||||||
class BlockedList
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
std::mutex m;
|
|
||||||
|
|
||||||
sqlite3 *db;
|
|
||||||
sqlite3_stmt *sel_stmt;
|
|
||||||
sqlite3_stmt *ins_stmt;
|
|
||||||
sqlite3_stmt *upd_stmt;
|
|
||||||
|
|
||||||
public:
|
|
||||||
void init()
|
|
||||||
{
|
|
||||||
int rc = sql::open(DB_NAME, &db);
|
|
||||||
if (rc)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Can't open database: %s\n", sql::errmsg(db));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sql::prepare_v2(db, get_warnings, -1, &sel_stmt, NULL);
|
|
||||||
sql::prepare_v2(db, insert_warnings, -1, &ins_stmt, NULL);
|
|
||||||
sql::prepare_v2(db, set_warnings, -1, &upd_stmt, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isBlocked(uint32_t id)
|
|
||||||
{
|
|
||||||
std::scoped_lock lock(m);
|
|
||||||
sql::bind_int64(sel_stmt, 1, id);
|
|
||||||
int64_t found = 0;
|
|
||||||
if (sql::step(sel_stmt) == SQLITE_ROW)
|
|
||||||
{
|
|
||||||
int type = sql::column_type(sel_stmt, 0);
|
|
||||||
if (type != SQL_NULL)
|
|
||||||
found = sql::column_int64(sel_stmt, 0);
|
|
||||||
}
|
|
||||||
sql::reset(sel_stmt);
|
|
||||||
|
|
||||||
return found >= 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addWarning(uint32_t id)
|
|
||||||
{
|
|
||||||
std::scoped_lock lock(m);
|
|
||||||
sql::bind_int64(sel_stmt, 1, id);
|
|
||||||
int64_t found = 0;
|
|
||||||
int res = sql::step(sel_stmt);
|
|
||||||
if (res == SQLITE_ROW)
|
|
||||||
{
|
|
||||||
int type = sql::column_type(sel_stmt, 0);
|
|
||||||
if (type != SQL_NULL)
|
|
||||||
{
|
|
||||||
found = sql::column_int64(sel_stmt, 0);
|
|
||||||
}
|
|
||||||
sql::reset(sel_stmt);
|
|
||||||
}
|
|
||||||
else if (res == SQL_DONE)
|
|
||||||
{
|
|
||||||
sql::bind_int64(ins_stmt, 1, id);
|
|
||||||
sql::step(ins_stmt);
|
|
||||||
sql::reset(ins_stmt);
|
|
||||||
sql::reset(sel_stmt);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sql::bind_int64(upd_stmt, 1, found + 1);
|
|
||||||
sql::bind_int64(upd_stmt, 2, id);
|
|
||||||
sql::step(upd_stmt);
|
|
||||||
sql::reset(upd_stmt);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
BlockedList blockedList;
|
|
||||||
|
|
||||||
// When a new client connected:
|
// When a new client connected:
|
||||||
void call(int sock, sockaddr_in newSocketInfo)
|
void call(int sock, sockaddr_in newSocketInfo)
|
||||||
{
|
{
|
||||||
if (blockedList.isBlocked(newSocketInfo.sin_addr.s_addr))
|
|
||||||
{
|
|
||||||
TcpSocket::closet(sock);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
std::string add = TcpSocket::remoteAddress(newSocketInfo);
|
std::string add = TcpSocket::remoteAddress(newSocketInfo);
|
||||||
printf("new: %s\n", add.c_str());
|
printf("new: %s\n", add.c_str());
|
||||||
int64_t conf, id;
|
int64_t conf, id;
|
||||||
@ -106,7 +24,6 @@ void call(int sock, sockaddr_in newSocketInfo)
|
|||||||
{
|
{
|
||||||
printf("StartHeader ERROR\n");
|
printf("StartHeader ERROR\n");
|
||||||
TcpSocket::closet(sock);
|
TcpSocket::closet(sock);
|
||||||
blockedList.addWarning(newSocketInfo.sin_addr.s_addr);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +31,6 @@ void call(int sock, sockaddr_in newSocketInfo)
|
|||||||
{
|
{
|
||||||
printf("ID ERROR\n");
|
printf("ID ERROR\n");
|
||||||
TcpSocket::closet(sock);
|
TcpSocket::closet(sock);
|
||||||
blockedList.addWarning(newSocketInfo.sin_addr.s_addr);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,6 +51,8 @@ void call(int sock, sockaddr_in newSocketInfo)
|
|||||||
int64_t gen = 0;
|
int64_t gen = 0;
|
||||||
while (sql::step(max_gen_stmt) != SQL_DONE)
|
while (sql::step(max_gen_stmt) != SQL_DONE)
|
||||||
{
|
{
|
||||||
|
int num_cols = sql::column_count(max_gen_stmt);
|
||||||
|
|
||||||
int type = sql::column_type(max_gen_stmt, 0);
|
int type = sql::column_type(max_gen_stmt, 0);
|
||||||
|
|
||||||
if (type == SQL_NULL)
|
if (type == SQL_NULL)
|
||||||
@ -215,7 +133,6 @@ int main()
|
|||||||
{
|
{
|
||||||
sql::init();
|
sql::init();
|
||||||
sql::create_tables();
|
sql::create_tables();
|
||||||
blockedList.init();
|
|
||||||
std::thread t(checker);
|
std::thread t(checker);
|
||||||
std::thread l(listen_key);
|
std::thread l(listen_key);
|
||||||
// Bind the server to a port.
|
// Bind the server to a port.
|
||||||
|
@ -4,12 +4,6 @@ const char create_like_table[] = "CREATE TABLE IF NOT EXISTS like_table ( ID INT
|
|||||||
|
|
||||||
const char create_user_table[] = "CREATE TABLE IF NOT EXISTS user_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, CHECKED INTEGER);";
|
const char create_user_table[] = "CREATE TABLE IF NOT EXISTS user_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, CHECKED INTEGER);";
|
||||||
|
|
||||||
const char create_blocked_ip_table[] = "CREATE TABLE IF NOT EXISTS blocked_ip_table ( ID INTEGER PRIMARY KEY, WARNINGS INTEGER);";
|
|
||||||
|
|
||||||
const char get_warnings[] = "SELECT WARNINGS FROM blocked_ip_table WHERE ID = ?;";
|
|
||||||
const char insert_warnings[] = "INSERT INTO blocked_ip_table (ID, WARNINGS) VALUES(?, 1);";
|
|
||||||
const char set_warnings[] = "UPDATE blocked_ip_table SET WARNINGS = ? WHERE ID = ?;";
|
|
||||||
|
|
||||||
const char max_gen[] = "SELECT MAX(GEN) FROM user_table WHERE USER_ID = ?;";
|
const char max_gen[] = "SELECT MAX(GEN) FROM user_table WHERE USER_ID = ?;";
|
||||||
|
|
||||||
const char insert_user_data[] = "INSERT INTO user_table (USER_ID, GEN, CHECKED) VALUES(?, ?, 0);";
|
const char insert_user_data[] = "INSERT INTO user_table (USER_ID, GEN, CHECKED) VALUES(?, ?, 0);";
|
||||||
@ -33,10 +27,8 @@ constexpr char DB_NAME[] = "data.db";
|
|||||||
struct sqlite3;
|
struct sqlite3;
|
||||||
struct sqlite3_stmt;
|
struct sqlite3_stmt;
|
||||||
|
|
||||||
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
|
|
||||||
#define SQL_DONE 101
|
#define SQL_DONE 101
|
||||||
#define SQL_NULL 5
|
#define SQL_NULL 5
|
||||||
#define SQLITE_OK 0
|
|
||||||
|
|
||||||
namespace sql
|
namespace sql
|
||||||
{
|
{
|
||||||
|
@ -50,17 +50,6 @@ namespace sql
|
|||||||
fprintf(stdout, "user_table created successfully\n");
|
fprintf(stdout, "user_table created successfully\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = sqlite3_exec(db, create_blocked_ip_table, nullptr, 0, &zErrMsg);
|
|
||||||
if (rc != SQLITE_OK)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
|
||||||
sqlite3_free(zErrMsg);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(stdout, "blocked_ip_table created successfully\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user