Modified By Vim Firstly.

This commit is contained in:
Saturneric 2020-11-25 02:23:51 +08:00
parent 2d15bc935e
commit 31aa47cac6
8 changed files with 227 additions and 143 deletions

6
.gitignore vendored
View File

@ -40,6 +40,12 @@
*.cbp *.cbp
*.marks *.marks
# Vim
*.swp
# Project Files # Project Files
cmake-build-debug cmake-build-debug
.idea .idea
.cache
build
.DS_Store

View File

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.13)
project(Net) project(Net)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(include/) include_directories(include/)
include_directories(utils/) include_directories(utils/)

67
compile_commands.json Normal file
View File

@ -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"
}
]

View File

@ -5,7 +5,6 @@
#ifndef NET_TCP_SERVER_H #ifndef NET_TCP_SERVER_H
#define NET_TCP_SERVER_H #define NET_TCP_SERVER_H
// 基础依赖 // 基础依赖
#include <project.h> #include <project.h>
@ -14,25 +13,28 @@
namespace Net { namespace Net {
class TCPServer { /**
public: * TCP Server using
*
*/
class TCPServer {
public:
TCPServer(int port, int max_connection); TCPServer(int port, int max_connection);
~TCPServer(){ ~TCPServer() {
stop(); stop();
close(fd); close(fd);
} }
void stop(){ void stop() {
if(p_accept_manager_thread != nullptr) if (p_accept_manager_thread != nullptr)
p_accept_manager_thread->interrupt(); p_accept_manager_thread->interrupt();
this->status = -1; this->status = -1;
} }
uint8_t readByte(); uint8_t readByte();
private: private:
int fd; int fd;
int status = 0; int status = 0;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
@ -42,13 +44,14 @@ namespace Net {
void cycle(); void cycle();
static void accept_manager(TCPServer *server); static void connection_manager(TCPServer *server);
static void accept(int fd, boost::mutex *buff_mutex, std::queue<uint8_t> *recv_buff, const int *status); static void accept(int fd, boost::mutex *buff_mutex,
std::queue<uint8_t> *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

View File

@ -7,18 +7,17 @@
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <string>
#include <openssl/aes.h> #include <openssl/aes.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <string>
#include "debug_tools/print_tools.h" #include "debug_tools/print_tools.h"
#include "random_generator.h" #include "random_generator.h"
namespace Net { namespace Net {
class AESCBCEncryptor { class AESCBCEncryptor {
public: public:
AESCBCEncryptor(); AESCBCEncryptor();
string getKeyData() const; string getKeyData() const;
@ -27,7 +26,7 @@ namespace Net {
void decrypt(std::string &data, const std::string &encrypt_data); void decrypt(std::string &data, const std::string &encrypt_data);
private: private:
const int nrounds = 8; const int nrounds = 8;
uint8_t key[32], iv[32]; uint8_t key[32], iv[32];
@ -39,9 +38,8 @@ namespace Net {
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

View File

@ -2,43 +2,48 @@
// Created by 胡宇 on 2020/7/7. // Created by 胡宇 on 2020/7/7.
// //
#include "debug_tools/print_tools.h"
#include "communicate/tcp_server.h" #include "communicate/tcp_server.h"
#include "debug_tools/print_tools.h"
extern int errno; extern int errno;
void Net::TCPServer::cycle() { void Net::TCPServer::cycle() {
boost::thread accept_manager_thread(TCPServer::accept_manager, this); boost::thread accept_manager_thread(TCPServer::connection_manager, this);
this->p_accept_manager_thread = &accept_manager_thread; this->p_accept_manager_thread = &accept_manager_thread;
accept_manager_thread.detach(); accept_manager_thread.detach();
} }
void Net::TCPServer::accept_manager(Net::TCPServer *server) { void Net::TCPServer::connection_manager(Net::TCPServer *server) {
Net::PrintTools::debugPrintSuccess("AcceptManager Started."); Net::PrintTools::debugPrintSuccess("AcceptManager Started.");
while(true){ while (true) {
int connect_fd = ::accept(server->fd, nullptr, nullptr); int connect_fd = ::accept(server->fd, nullptr, nullptr);
Net::PrintTools::debugPrintSuccess("New Connection."); Net::PrintTools::debugPrintSuccess("New Connection.");
if(connect_fd < 0) throw std::runtime_error(strerror(errno)); if (connect_fd < 0)
else{ throw std::runtime_error(strerror(errno));
boost::thread accept_thread(TCPServer::accept, connect_fd, &server->buff_mutex, &server->recv_buff, &server->status); else {
boost::thread accept_thread(TCPServer::accept, connect_fd,
&server->buff_mutex, &server->recv_buff,
&server->status);
accept_thread.detach(); accept_thread.detach();
} }
} }
} }
void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, std::queue<uint8_t> *recv_buff, const int *status) { void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex,
std::queue<uint8_t> *recv_buff, const int *status) {
Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection."); Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection.");
uint8_t buff[1024]; uint8_t buff[1024];
int len; int len;
std::vector<uint8_t> accept_buff; std::vector<uint8_t> accept_buff;
while((len = recv(fd, buff, sizeof(buff), 0)) > 0 && *status == 0){ while ((len = recv(fd, buff, sizeof(buff), 0)) > 0 && *status == 0) {
Net::PrintTools::debugPrintSuccess("Received."); Net::PrintTools::debugPrintSuccess("Received.");
for(int i = 0; i < len; ++i) accept_buff.push_back(buff[i]); for (int i = 0; i < len; ++i)
accept_buff.push_back(buff[i]);
} }
if(*status == 0) { if (*status == 0) {
buff_mutex->lock(); buff_mutex->lock();
for(unsigned char & i : accept_buff) for (unsigned char &i : accept_buff)
recv_buff->push(i); recv_buff->push(i);
buff_mutex->unlock(); buff_mutex->unlock();
} }
@ -47,7 +52,8 @@ void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, std::queue<uint8_t
} }
void Net::TCPServer::create_socket(int port) { 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)); std::memset(&server_addr, 0, sizeof(struct sockaddr_in));
this->server_addr.sin_family = AF_INET; this->server_addr.sin_family = AF_INET;
@ -60,20 +66,19 @@ Net::TCPServer::TCPServer(int port, int max_connection) {
// 创建 socket // 创建 socket
create_socket(port); create_socket(port);
if(bind(fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) if (bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
throw std::runtime_error(strerror(errno)); throw std::runtime_error(strerror(errno));
if(listen(fd, max_connection) < 0) if (listen(fd, max_connection) < 0)
throw std::runtime_error(strerror(errno)); throw std::runtime_error(strerror(errno));
cycle(); cycle();
} }
uint8_t Net::TCPServer::readByte() { uint8_t Net::TCPServer::readByte() {
uint8_t byte = '\0'; uint8_t byte = '\0';
buff_mutex.try_lock(); buff_mutex.try_lock();
if(!recv_buff.empty()) { if (!recv_buff.empty()) {
byte = recv_buff.front(); byte = recv_buff.front();
recv_buff.pop(); recv_buff.pop();
} }

View File

@ -4,6 +4,7 @@
#include "utils/aes_cbc_encryptor.h" #include "utils/aes_cbc_encryptor.h"
#include "utils/sha256_generator.h"
void Net::AESCBCEncryptor::encrypt(const string &data, string &encrypted_data) { 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;
@ -12,12 +13,14 @@ void Net::AESCBCEncryptor::encrypt(const string &data, string &encrypted_data) {
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
EVP_EncryptUpdate(e_ctx, encrypt_buffer, &c_len, EVP_EncryptUpdate(e_ctx, encrypt_buffer, &c_len,
reinterpret_cast<const unsigned char *>(data.data()), data.length()); reinterpret_cast<const unsigned char *>(data.data()),
data.length());
EVP_EncryptFinal_ex(e_ctx, encrypt_buffer + c_len, &f_len); 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<const char *>(encrypt_buffer), len); encrypted_data.append(reinterpret_cast<const char *>(encrypt_buffer), len);
@ -29,13 +32,16 @@ void Net::AESCBCEncryptor::decrypt(string &data, const string &encrypt_data) {
auto *plain_buffer = static_cast<uint8_t *>(malloc(p_len)); auto *plain_buffer = static_cast<uint8_t *>(malloc(p_len));
EVP_DecryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); EVP_DecryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
EVP_DecryptUpdate(e_ctx, plain_buffer, &p_len, EVP_DecryptUpdate(
reinterpret_cast<const unsigned char *>(encrypt_data.data()), encrypt_data.length()); e_ctx, plain_buffer, &p_len,
reinterpret_cast<const unsigned char *>(encrypt_data.data()),
encrypt_data.length());
EVP_DecryptFinal_ex(e_ctx, plain_buffer + p_len, &f_len); 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<const char *>(plain_buffer), len); data.append(reinterpret_cast<const char *>(plain_buffer), len);
@ -46,7 +52,7 @@ void Net::AESCBCEncryptor::generate_random_key_data() {
Rand::UniformUInt rand(0, UINT32_MAX); Rand::UniformUInt rand(0, UINT32_MAX);
uint32_t p_data[8]; uint32_t p_data[8];
for(unsigned int & i : p_data){ for (unsigned int &i : p_data) {
i = rand.generate(); i = rand.generate();
} }
@ -55,10 +61,10 @@ void Net::AESCBCEncryptor::generate_random_key_data() {
void Net::AESCBCEncryptor::aes_init(string &key_data) { void Net::AESCBCEncryptor::aes_init(string &key_data) {
int i =
int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), nullptr, EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), nullptr,
reinterpret_cast<const unsigned char *>(key_data.c_str()), key_data.length(), reinterpret_cast<const unsigned char *>(key_data.c_str()),
nrounds, key, iv); key_data.length(), nrounds, key, iv);
if (i != 32) { if (i != 32) {
throw std::runtime_error("key data must equal 256 bits."); throw std::runtime_error("key data must equal 256 bits.");
} }
@ -67,9 +73,7 @@ void Net::AESCBCEncryptor::aes_init(string &key_data) {
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
} }
string Net::AESCBCEncryptor::getKeyData() const { string Net::AESCBCEncryptor::getKeyData() const { return key_data; }
return key_data;
}
Net::AESCBCEncryptor::AESCBCEncryptor() { Net::AESCBCEncryptor::AESCBCEncryptor() {
generate_random_key_data(); generate_random_key_data();