From f9b06df544c8134b5982b76f2d24aa93289f6d71 Mon Sep 17 00:00:00 2001 From: auth Date: Sat, 11 Jul 2020 17:09:27 +0200 Subject: Added blacklist implementation on server. Overall code cleanup and optimization. --- server/src/client/blacklist.h | 54 +++++++++++++++++++++++++++++++++++++++++++ server/src/client/client.h | 11 +++++---- 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 server/src/client/blacklist.h (limited to 'server/src/client') diff --git a/server/src/client/blacklist.h b/server/src/client/blacklist.h new file mode 100644 index 0000000..ea9e261 --- /dev/null +++ b/server/src/client/blacklist.h @@ -0,0 +1,54 @@ +#pragma once + +namespace tcp { + +struct blacklist_data { + std::string ip; + std::string hwid; +}; + +class blacklist { + +nlohmann::json m_data; +std::string m_name; + +public: + void init(const std::string_view file = "blacklist") { + m_name = file; + + std::string data; + if(!io::read_file(file, data)) + return; + + if(!nlohmann::json::accept(data)) { + io::logger->error("blacklist file isnt valid json."); + return; + } + + m_data = nlohmann::json::parse(data); + } + + void add(const blacklist_data &data) { + m_data["ips"].emplace_back(data.ip); + m_data["hwids"].emplace_back(data.hwid); + + save(); + } + + void save() { + std::ofstream o(m_name, std::ios::trunc); + o << std::setw(4) << m_data; + o.close(); + } + + bool find(const std::string &key) { + for(auto &item : m_data["ips"]) { + if(item.get() == key) { + return true; + } + } + return false; + } +}; + +}; \ No newline at end of file diff --git a/server/src/client/client.h b/server/src/client/client.h index 141ac67..a3d558d 100644 --- a/server/src/client/client.h +++ b/server/src/client/client.h @@ -24,8 +24,10 @@ class client { void cleanup() { close(m_socket); - SSL_shutdown(m_ssl); - SSL_free(m_ssl); + if (m_ssl) { + SSL_shutdown(m_ssl); + SSL_free(m_ssl); + } m_socket = -1; } @@ -63,8 +65,9 @@ class client { int& get_socket() { return m_socket; } auto& get_ip() { return m_ip; } - auto& get_session() { return m_session_id; } - operator bool() const { return m_ssl && m_socket > 0; } + operator bool() const { return m_socket > 0; } + auto &operator()() { return m_session_id; } + }; }; // namespace tcp \ No newline at end of file -- cgit v1.2.3