From 31aa47cac6afb9cc8c4ba8a4fae449ee1890b7e2 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 25 Nov 2020 02:23:51 +0800 Subject: [PATCH] Modified By Vim Firstly. --- .gitignore | 8 ++- CMakeLists.txt | 3 +- compile_commands.json | 67 ++++++++++++++++++ include/communicate/tcp_server.h | 61 +++++++++-------- include/utils/aes_cbc_encryptor.h | 36 +++++----- src/CMakeLists.txt | 2 +- src/communicate/tcp_server.cpp | 109 ++++++++++++++++-------------- src/utils/aes_cbc_encryptor.cpp | 84 ++++++++++++----------- 8 files changed, 227 insertions(+), 143 deletions(-) create mode 100644 compile_commands.json diff --git a/.gitignore b/.gitignore index cf14ce8..11dda0d 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,12 @@ *.cbp *.marks +# Vim +*.swp + # Project Files cmake-build-debug -.idea \ No newline at end of file +.idea +.cache +build +.DS_Store diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b6c392..859e68b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.13) project(Net) set(CMAKE_CXX_STANDARD 14) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) include_directories(include/) include_directories(utils/) @@ -26,4 +27,4 @@ enable_testing() add_subdirectory(src) -add_subdirectory(test) \ No newline at end of file +add_subdirectory(test) diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..7845a4e --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,67 @@ +[ +{ + "directory": "/home/eric/NetCpp/build/src/utils", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/aes_cbc_encryptor.cpp.o -c /home/eric/NetCpp/src/utils/aes_cbc_encryptor.cpp", + "file": "/home/eric/NetCpp/src/utils/aes_cbc_encryptor.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/src/utils", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/random_generator.cpp.o -c /home/eric/NetCpp/src/utils/random_generator.cpp", + "file": "/home/eric/NetCpp/src/utils/random_generator.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/src/utils", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/rsa_key_chain.cpp.o -c /home/eric/NetCpp/src/utils/rsa_key_chain.cpp", + "file": "/home/eric/NetCpp/src/utils/rsa_key_chain.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/src/utils", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/sha256_generator.cpp.o -c /home/eric/NetCpp/src/utils/sha256_generator.cpp", + "file": "/home/eric/NetCpp/src/utils/sha256_generator.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/src/debug_tools", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/debugTools.dir/print_tools.cpp.o -c /home/eric/NetCpp/src/debug_tools/print_tools.cpp", + "file": "/home/eric/NetCpp/src/debug_tools/print_tools.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/src/communicate", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/commu.dir/tcp_client.cpp.o -c /home/eric/NetCpp/src/communicate/tcp_client.cpp", + "file": "/home/eric/NetCpp/src/communicate/tcp_client.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/src/communicate", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/commu.dir/tcp_server.cpp.o -c /home/eric/NetCpp/src/communicate/tcp_server.cpp", + "file": "/home/eric/NetCpp/src/communicate/tcp_server.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/src/communicate", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/commu.dir/tcp_session.cpp.o -c /home/eric/NetCpp/src/communicate/tcp_session.cpp", + "file": "/home/eric/NetCpp/src/communicate/tcp_session.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/test/test_util", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/main.cpp.o -c /home/eric/NetCpp/test/test_util/main.cpp", + "file": "/home/eric/NetCpp/test/test_util/main.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/test/test_util", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/test_aes.cpp.o -c /home/eric/NetCpp/test/test_util/test_aes.cpp", + "file": "/home/eric/NetCpp/test/test_util/test_aes.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/test/test_util", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/test_rsa.cpp.o -c /home/eric/NetCpp/test/test_util/test_rsa.cpp", + "file": "/home/eric/NetCpp/test/test_util/test_rsa.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/test/test_util", + "command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/test_sha.cpp.o -c /home/eric/NetCpp/test/test_util/test_sha.cpp", + "file": "/home/eric/NetCpp/test/test_util/test_sha.cpp" +}, +{ + "directory": "/home/eric/NetCpp/build/test/test_commu", + "command": "/usr/bin/c++ -DDEBUG -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testCommu.dir/main.cpp.o -c /home/eric/NetCpp/test/test_commu/main.cpp", + "file": "/home/eric/NetCpp/test/test_commu/main.cpp" +} +] \ No newline at end of file diff --git a/include/communicate/tcp_server.h b/include/communicate/tcp_server.h index c621772..83234ca 100644 --- a/include/communicate/tcp_server.h +++ b/include/communicate/tcp_server.h @@ -5,7 +5,6 @@ #ifndef NET_TCP_SERVER_H #define NET_TCP_SERVER_H - // 基础依赖 #include @@ -14,41 +13,45 @@ namespace Net { - class TCPServer { - public: - TCPServer(int port, int max_connection); +/** + * TCP Server using + * +*/ +class TCPServer { +public: + TCPServer(int port, int max_connection); - ~TCPServer(){ - stop(); - close(fd); - } + ~TCPServer() { + stop(); + close(fd); + } - void stop(){ - if(p_accept_manager_thread != nullptr) - p_accept_manager_thread->interrupt(); - this->status = -1; - } + void stop() { + if (p_accept_manager_thread != nullptr) + p_accept_manager_thread->interrupt(); + this->status = -1; + } - uint8_t readByte(); + uint8_t readByte(); - private: +private: + int fd; + int status = 0; + struct sockaddr_in server_addr; + std::queue recv_buff; + boost::mutex buff_mutex; + boost::thread *p_accept_manager_thread; - int fd; - int status = 0; - struct sockaddr_in server_addr; - std::queue recv_buff; - boost::mutex buff_mutex; - boost::thread *p_accept_manager_thread; + void cycle(); - void cycle(); + static void connection_manager(TCPServer *server); - static void accept_manager(TCPServer *server); + static void accept(int fd, boost::mutex *buff_mutex, + std::queue *recv_buff, const int *status); - static void accept(int fd, boost::mutex *buff_mutex, std::queue *recv_buff, const int *status); + void create_socket(int port); +}; - void create_socket(int port); - }; +} // namespace Net -} - -#endif //NET_TCP_SERVER_H +#endif // NET_TCP_SERVER_H diff --git a/include/utils/aes_cbc_encryptor.h b/include/utils/aes_cbc_encryptor.h index 8eb1a3b..e6632f2 100644 --- a/include/utils/aes_cbc_encryptor.h +++ b/include/utils/aes_cbc_encryptor.h @@ -7,41 +7,39 @@ #include #include -#include #include #include +#include #include "debug_tools/print_tools.h" #include "random_generator.h" namespace Net { - class AESCBCEncryptor { - public: +class AESCBCEncryptor { +public: + AESCBCEncryptor(); - AESCBCEncryptor(); + string getKeyData() const; - string getKeyData() const; + void encrypt(const std::string &data, std::string &encrypted_data); - void encrypt(const std::string &data, std::string &encrypted_data); + void decrypt(std::string &data, const std::string &encrypt_data); - void decrypt(std::string &data, const std::string &encrypt_data); +private: + const int nrounds = 8; - private: - const int nrounds = 8; + uint8_t key[32], iv[32]; - uint8_t key[32], iv[32]; + EVP_CIPHER_CTX *e_ctx = EVP_CIPHER_CTX_new(); - EVP_CIPHER_CTX *e_ctx = EVP_CIPHER_CTX_new(); + std::string key_data; - std::string key_data; + void generate_random_key_data(); - void generate_random_key_data(); + void aes_init(std::string &key_data); +}; - void aes_init(std::string &key_data); +} // namespace Net - }; - -} - -#endif //NET_AES_CBC_ENCRYPTOR_H +#endif // NET_AES_CBC_ENCRYPTOR_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f508e42..8f89025 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,3 @@ add_subdirectory(utils) add_subdirectory(debug_tools) -add_subdirectory(communicate) \ No newline at end of file +add_subdirectory(communicate) diff --git a/src/communicate/tcp_server.cpp b/src/communicate/tcp_server.cpp index f70336b..5e90441 100644 --- a/src/communicate/tcp_server.cpp +++ b/src/communicate/tcp_server.cpp @@ -2,81 +2,86 @@ // Created by 胡宇 on 2020/7/7. // -#include "debug_tools/print_tools.h" #include "communicate/tcp_server.h" +#include "debug_tools/print_tools.h" extern int errno; void Net::TCPServer::cycle() { - boost::thread accept_manager_thread(TCPServer::accept_manager, this); - this->p_accept_manager_thread = &accept_manager_thread; - accept_manager_thread.detach(); + boost::thread accept_manager_thread(TCPServer::connection_manager, this); + this->p_accept_manager_thread = &accept_manager_thread; + accept_manager_thread.detach(); } -void Net::TCPServer::accept_manager(Net::TCPServer *server) { - Net::PrintTools::debugPrintSuccess("AcceptManager Started."); - while(true){ - int connect_fd = ::accept(server->fd, nullptr, nullptr); - Net::PrintTools::debugPrintSuccess("New Connection."); +void Net::TCPServer::connection_manager(Net::TCPServer *server) { + Net::PrintTools::debugPrintSuccess("AcceptManager Started."); + while (true) { + int connect_fd = ::accept(server->fd, nullptr, nullptr); + Net::PrintTools::debugPrintSuccess("New Connection."); - if(connect_fd < 0) throw std::runtime_error(strerror(errno)); - else{ - boost::thread accept_thread(TCPServer::accept, connect_fd, &server->buff_mutex, &server->recv_buff, &server->status); - accept_thread.detach(); - } + if (connect_fd < 0) + throw std::runtime_error(strerror(errno)); + else { + boost::thread accept_thread(TCPServer::accept, connect_fd, + &server->buff_mutex, &server->recv_buff, + &server->status); + accept_thread.detach(); } + } } -void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, std::queue *recv_buff, const int *status) { - Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection."); - uint8_t buff[1024]; - int len; - std::vector accept_buff; - while((len = recv(fd, buff, sizeof(buff), 0)) > 0 && *status == 0){ - Net::PrintTools::debugPrintSuccess("Received."); - for(int i = 0; i < len; ++i) accept_buff.push_back(buff[i]); - } - if(*status == 0) { - buff_mutex->lock(); - for(unsigned char & i : accept_buff) - recv_buff->push(i); - buff_mutex->unlock(); - } - close(fd); - Net::PrintTools::debugPrintSuccess("Connection Closed."); +void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, + std::queue *recv_buff, const int *status) { + Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection."); + uint8_t buff[1024]; + int len; + std::vector accept_buff; + while ((len = recv(fd, buff, sizeof(buff), 0)) > 0 && *status == 0) { + Net::PrintTools::debugPrintSuccess("Received."); + for (int i = 0; i < len; ++i) + accept_buff.push_back(buff[i]); + } + if (*status == 0) { + buff_mutex->lock(); + for (unsigned char &i : accept_buff) + recv_buff->push(i); + buff_mutex->unlock(); + } + close(fd); + Net::PrintTools::debugPrintSuccess("Connection Closed."); } void Net::TCPServer::create_socket(int port) { - if((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) throw std::runtime_error(strerror(errno)); + if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) + throw std::runtime_error(strerror(errno)); - std::memset(&server_addr, 0, sizeof(struct sockaddr_in)); - this->server_addr.sin_family = AF_INET; - this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - this->server_addr.sin_port = htons(port); + std::memset(&server_addr, 0, sizeof(struct sockaddr_in)); + this->server_addr.sin_family = AF_INET; + this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + this->server_addr.sin_port = htons(port); } Net::TCPServer::TCPServer(int port, int max_connection) { - // 创建 socket - create_socket(port); + // 创建 socket + create_socket(port); - if(bind(fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) - throw std::runtime_error(strerror(errno)); + if (bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) + throw std::runtime_error(strerror(errno)); - if(listen(fd, max_connection) < 0) - throw std::runtime_error(strerror(errno)); + if (listen(fd, max_connection) < 0) + throw std::runtime_error(strerror(errno)); - - cycle(); + cycle(); } uint8_t Net::TCPServer::readByte() { - uint8_t byte = '\0'; - buff_mutex.try_lock(); - if(!recv_buff.empty()) { - byte = recv_buff.front(); - recv_buff.pop(); - } - buff_mutex.unlock(); - return byte; + uint8_t byte = '\0'; + buff_mutex.try_lock(); + if (!recv_buff.empty()) { + byte = recv_buff.front(); + recv_buff.pop(); + } + buff_mutex.unlock(); + return byte; } diff --git a/src/utils/aes_cbc_encryptor.cpp b/src/utils/aes_cbc_encryptor.cpp index 33c06e9..853215b 100644 --- a/src/utils/aes_cbc_encryptor.cpp +++ b/src/utils/aes_cbc_encryptor.cpp @@ -4,74 +4,78 @@ #include "utils/aes_cbc_encryptor.h" +#include "utils/sha256_generator.h" void Net::AESCBCEncryptor::encrypt(const string &data, string &encrypted_data) { - int c_len = data.length() + AES_BLOCK_SIZE, f_len = 0; + int c_len = data.length() + AES_BLOCK_SIZE, f_len = 0; - auto *encrypt_buffer = reinterpret_cast(malloc(c_len)); + auto *encrypt_buffer = reinterpret_cast(malloc(c_len)); - EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); - EVP_EncryptUpdate(e_ctx, encrypt_buffer, &c_len, - reinterpret_cast(data.data()), data.length()); - EVP_EncryptFinal_ex(e_ctx, encrypt_buffer + c_len, &f_len); + EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); + EVP_EncryptUpdate(e_ctx, encrypt_buffer, &c_len, + reinterpret_cast(data.data()), + data.length()); + EVP_EncryptFinal_ex(e_ctx, encrypt_buffer + c_len, &f_len); - int len = c_len + f_len; + int len = c_len + f_len; - if(!encrypted_data.empty()) encrypted_data.clear(); + if (!encrypted_data.empty()) + encrypted_data.clear(); - encrypted_data.append(reinterpret_cast(encrypt_buffer), len); + encrypted_data.append(reinterpret_cast(encrypt_buffer), len); - EVP_CIPHER_CTX_reset(e_ctx); + EVP_CIPHER_CTX_reset(e_ctx); } void Net::AESCBCEncryptor::decrypt(string &data, const string &encrypt_data) { - int p_len = encrypt_data.length(), f_len = 0; - auto *plain_buffer = static_cast(malloc(p_len)); + int p_len = encrypt_data.length(), f_len = 0; + auto *plain_buffer = static_cast(malloc(p_len)); - EVP_DecryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); - EVP_DecryptUpdate(e_ctx, plain_buffer, &p_len, - reinterpret_cast(encrypt_data.data()), encrypt_data.length()); - EVP_DecryptFinal_ex(e_ctx, plain_buffer + p_len, &f_len); + EVP_DecryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); + EVP_DecryptUpdate( + e_ctx, plain_buffer, &p_len, + reinterpret_cast(encrypt_data.data()), + encrypt_data.length()); + EVP_DecryptFinal_ex(e_ctx, plain_buffer + p_len, &f_len); - int len = p_len + f_len; + int len = p_len + f_len; - if(!data.empty()) data.clear(); + if (!data.empty()) + data.clear(); - data.append(reinterpret_cast(plain_buffer), len); + data.append(reinterpret_cast(plain_buffer), len); - EVP_CIPHER_CTX_reset(e_ctx); + EVP_CIPHER_CTX_reset(e_ctx); } void Net::AESCBCEncryptor::generate_random_key_data() { - Rand::UniformUInt rand(0, UINT32_MAX); + Rand::UniformUInt rand(0, UINT32_MAX); - uint32_t p_data[8]; - for(unsigned int & i : p_data){ - i = rand.generate(); - } + uint32_t p_data[8]; + for (unsigned int &i : p_data) { + i = rand.generate(); + } - key_data.append(reinterpret_cast(p_data), 32); + key_data.append(reinterpret_cast(p_data), 32); } void Net::AESCBCEncryptor::aes_init(string &key_data) { + int i = + EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), nullptr, + reinterpret_cast(key_data.c_str()), + key_data.length(), nrounds, key, iv); + if (i != 32) { + throw std::runtime_error("key data must equal 256 bits."); + } - int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), nullptr, - reinterpret_cast(key_data.c_str()), key_data.length(), - nrounds, key, iv); - if (i != 32) { - throw std::runtime_error("key data must equal 256 bits."); - } - - EVP_CIPHER_CTX_init(e_ctx); - EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); + EVP_CIPHER_CTX_init(e_ctx); + EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); } -string Net::AESCBCEncryptor::getKeyData() const { - return key_data; -} +string Net::AESCBCEncryptor::getKeyData() const { return key_data; } Net::AESCBCEncryptor::AESCBCEncryptor() { - generate_random_key_data(); - aes_init(key_data); + generate_random_key_data(); + aes_init(key_data); }