diff options
| author | auth <[email protected]> | 2020-07-05 11:25:10 +0200 |
|---|---|---|
| committer | auth <[email protected]> | 2020-07-05 11:25:10 +0200 |
| commit | e1ca40bf09255ea74602a6b31e0e17ae611e3e3b (patch) | |
| tree | d163ae07461108a2667107153b72180f69981a9c | |
| parent | Replaced std::byte by uint8_t (diff) | |
| download | loader-e1ca40bf09255ea74602a6b31e0e17ae611e3e3b.tar.xz loader-e1ca40bf09255ea74602a6b31e0e17ae611e3e3b.zip | |
Added support for packet actions.
Added support for client hwid handling.
Removed timeout client message.
| -rw-r--r-- | client/src/assembler/opcodes.h | 2 | ||||
| -rw-r--r-- | client/src/client/client.cpp | 2 | ||||
| -rw-r--r-- | client/src/client/client.h | 1 | ||||
| -rw-r--r-- | client/src/client/packet.h | 17 | ||||
| -rw-r--r-- | client/src/main.cpp | 40 | ||||
| -rw-r--r-- | server/src/client/client.h | 11 | ||||
| -rw-r--r-- | server/src/main.cpp | 30 | ||||
| -rw-r--r-- | server/src/server/packet.h | 11 |
8 files changed, 62 insertions, 52 deletions
diff --git a/client/src/assembler/opcodes.h b/client/src/assembler/opcodes.h index cc21ef0..5268b45 100644 --- a/client/src/assembler/opcodes.h +++ b/client/src/assembler/opcodes.h @@ -4,6 +4,6 @@ namespace assembler { enum opcodes : uint8_t { nop = 0x90, - ret = 0xc3S + ret = 0xc3 }; };
\ No newline at end of file diff --git a/client/src/client/client.cpp b/client/src/client/client.cpp index 32a5b37..43ee6c7 100644 --- a/client/src/client/client.cpp +++ b/client/src/client/client.cpp @@ -45,7 +45,7 @@ void tcp::client::start(const std::string_view server_ip, const uint16_t port) { m_active = true; - io::logger->info("connected."); + connect_event.call(); } int tcp::client::read_stream(std::vector<char>& out) { diff --git a/client/src/client/client.h b/client/src/client/client.h index 9bc0eb2..7618913 100644 --- a/client/src/client/client.h +++ b/client/src/client/client.h @@ -21,6 +21,7 @@ class client { public: std::string session_id; event<packet_t&> receive_event; + event<> connect_event; client() : m_socket{-1}, m_active{false} {} diff --git a/client/src/client/packet.h b/client/src/client/packet.h index 992bee7..f574625 100644 --- a/client/src/client/packet.h +++ b/client/src/client/packet.h @@ -9,14 +9,18 @@ constexpr size_t message_len = 512; enum packet_type : int { write = 0, read }; +enum packet_action : uint8_t { message = 0, hwid = 1, session }; + struct packet_t { std::string message; std::string session_id; + uint8_t act; int id; packet_t() {} - packet_t(const std::string_view msg, const packet_type& type, - std::string_view session = "") { + packet_t(const std::string_view msg, const packet_type &type, + std::string_view session = "", + const packet_action &action = packet_action::message) { if (type == read) { ++id; @@ -26,14 +30,16 @@ struct packet_t { auto json = nlohmann::json::parse(message); message = json["message"]; session_id = json["session_id"]; - + act = json["action"]; } else { nlohmann::json json; + json["action"] = action; json["session_id"] = session; json["message"] = msg.data(); message = json.dump(); session_id = session; + act = action; enc::encrypt_message(message); } @@ -42,9 +48,12 @@ struct packet_t { ~packet_t() { message.clear(); session_id.clear(); + act = -1; } - operator bool() const { return !message.empty() && !session_id.empty(); } + operator bool() const { + return !message.empty() && !session_id.empty() && act != -1; + } auto &operator()() { return message; } }; }; // namespace tcp diff --git a/client/src/main.cpp b/client/src/main.cpp index f0508ce..727e3a0 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -6,15 +6,14 @@ int main(int argc, char* argv[]) { io::init(); - assembler::assembler a; + /*assembler::assembler a; a.push({1, 2, 3, 7, 9}); a.end(); for(auto &b : a()) { io::logger->info("{:x}", b); } + std::cin.get();*/ - - std::cin.get(); tcp::client client; std::thread t{tcp::client::monitor, std::ref(client)}; @@ -22,35 +21,36 @@ int main(int argc, char* argv[]) { client.start("127.0.0.1", 6666); + client.connect_event.add([&]() { io::logger->info("connected."); }); + client.receive_event.add([&](tcp::packet_t& packet) { if (!packet) return; auto message = packet(); + auto action = packet.act; + + // move ? + int ret = -1; - // first packet is the session id and current version - if (packet.id == 1) { + if (action == tcp::packet_action::session) { client.session_id = packet.session_id; tcp::version_t v{0, 1, 0}; auto version = fmt::format("{}.{}.{}", v.major, v.minor, v.patch); - if(version != message) { - io::logger->error("please update your client"); + if (version != message) { + io::logger->error("please update your client."); client.shutdown(); } - return; - } - if (message == "timedout") { - io::logger->warn("connection timeout."); - client.shutdown(); + int ret = client.write(tcp::packet_t("hwid", tcp::packet_type::write, + client.session_id, + tcp::packet_action::hwid)); + if (ret <= 0) { + io::logger->error("failed to send hwid."); + client.shutdown(); + } } - io::logger->info("{}:{}->{}", packet.id, packet.session_id, message); - - std::string imports; - client.read_stream(imports); - - auto json = nlohmann::json::parse(imports); - std::ofstream o("o"); - o << std::setw(4) << json; + io::logger->info("{}:{}->{} {}", packet.id, packet.session_id, message, + packet.act); }); while (client) { diff --git a/server/src/client/client.h b/server/src/client/client.h index 52e0c42..4dad02e 100644 --- a/server/src/client/client.h +++ b/server/src/client/client.h @@ -13,6 +13,9 @@ class client { std::string m_session_id; public: + std::string hwid; + + client() : m_socket{-1} {}; client(const int& socket, const std::string_view ip) : m_socket{std::move(socket)}, m_ip{ip}, m_ssl{nullptr} {} @@ -27,7 +30,7 @@ class client { } void reset() { std::time(&m_time); } - bool timeout() { return std::difftime(std::time(nullptr), m_time) >= 30; } + bool timeout() { return std::difftime(std::time(nullptr), m_time) >= 300; } int write(const packet_t& packet) { if (!packet) return 0; @@ -57,8 +60,8 @@ class client { void gen_session(); - int get_socket() { return m_socket; } - auto get_ip() { return m_ip; } - auto get_session() { return m_session_id; } + int &get_socket() { return m_socket; } + auto &get_ip() { return m_ip; } + auto &get_session() { return m_session_id; } }; }; // namespace tcp
\ No newline at end of file diff --git a/server/src/main.cpp b/server/src/main.cpp index 138f733..77269a9 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -18,8 +18,9 @@ int main(int argc, char* argv[]) { client_server.connect_event.add([&](tcp::client& client) { auto ip = client.get_ip(); client.gen_session(); - client.write(tcp::packet_t(client_version, - tcp::packet_type::write, client.get_session())); + client.write(tcp::packet_t(client_version, tcp::packet_type::write, + client.get_session(), + tcp::packet_action::session)); io::logger->info("{} connected", ip); }); @@ -41,6 +42,7 @@ int main(int argc, char* argv[]) { auto packet_session = packet.session_id; auto ip = client.get_ip(); auto message = packet(); + auto action = packet.act; if (!packet) { io::logger->info("{} sent invalid packet", ip); @@ -54,29 +56,17 @@ int main(int argc, char* argv[]) { io::logger->info("{} : {}", packet_session, message); - client.write(tcp::packet_t(message, tcp::packet_type::write, - client.get_session())); + if(action == tcp::packet_action::hwid) { + client.hwid = message; - /*auto imports = image.get_json_imports(); - client.stream(imports);*/ - - /*std::vector<char> t; - io::read_file("test.dll", t); - float tot; - for(int i = 0; i < 100; i++) { - float dur; - client.stream(t, &dur); - tot += dur; + io::logger->info("got hwid from {} : {}", ip, message); } - float avg = tot / 100.f; - io::logger->info("average time {}", avg);*/ - + + //client.write(tcp::packet_t(message, tcp::packet_type::write, + //client.get_session())); }); client_server.timeout_event.add([&](tcp::client& client) { - client.write(tcp::packet_t("timedout", tcp::packet_type::write, - client.get_session())); - io::logger->info("{} timed out.", client.get_ip()); }); diff --git a/server/src/server/packet.h b/server/src/server/packet.h index 661b0df..279d140 100644 --- a/server/src/server/packet.h +++ b/server/src/server/packet.h @@ -9,13 +9,17 @@ constexpr size_t message_len = 512; enum packet_type : int { write = 0, read }; +enum packet_action : uint8_t { message = 0, hwid = 1, session }; + struct packet_t { std::string message; std::string session_id; + uint8_t act; packet_t() {} - packet_t(const std::string_view msg, const packet_type& type, - std::string_view session = "") { + packet_t(const std::string_view msg, const packet_type &type, + std::string_view session = "", + const packet_action &action = packet_action::message) { if (type == read) { message = msg; enc::decrypt_message(message); @@ -28,8 +32,10 @@ struct packet_t { auto json = nlohmann::json::parse(message); message = json["message"]; session_id = json["session_id"]; + act = json["action"]; } else { nlohmann::json json; + json["action"] = action; json["session_id"] = session; json["message"] = msg.data(); @@ -43,6 +49,7 @@ struct packet_t { ~packet_t() { message.clear(); session_id.clear(); + act = -1; } operator bool() const { return !message.empty() && !session_id.empty(); } |