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

8
.gitignore vendored
View File

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

View File

@ -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)
add_subdirectory(test)

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
#define NET_TCP_SERVER_H
// 基础依赖
#include <project.h>
@ -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<uint8_t> recv_buff;
boost::mutex buff_mutex;
boost::thread *p_accept_manager_thread;
int fd;
int status = 0;
struct sockaddr_in server_addr;
std::queue<uint8_t> 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<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,41 +7,39 @@
#include <cassert>
#include <cstdint>
#include <string>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <string>
#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

View File

@ -1,3 +1,3 @@
add_subdirectory(utils)
add_subdirectory(debug_tools)
add_subdirectory(communicate)
add_subdirectory(communicate)

View File

@ -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<uint8_t> *recv_buff, const int *status) {
Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection.");
uint8_t buff[1024];
int len;
std::vector<uint8_t> 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<uint8_t> *recv_buff, const int *status) {
Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection.");
uint8_t buff[1024];
int len;
std::vector<uint8_t> 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;
}

View File

@ -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<uint8_t *>(malloc(c_len));
auto *encrypt_buffer = reinterpret_cast<uint8_t *>(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<const unsigned char *>(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<const unsigned char *>(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<const char *>(encrypt_buffer), len);
encrypted_data.append(reinterpret_cast<const char *>(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<uint8_t *>(malloc(p_len));
int p_len = encrypt_data.length(), f_len = 0;
auto *plain_buffer = static_cast<uint8_t *>(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<const unsigned char *>(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<const unsigned char *>(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<const char *>(plain_buffer), len);
data.append(reinterpret_cast<const char *>(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<const char *>(p_data), 32);
key_data.append(reinterpret_cast<const char *>(p_data), 32);
}
void Net::AESCBCEncryptor::aes_init(string &key_data) {
int i =
EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), nullptr,
reinterpret_cast<const unsigned char *>(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<const unsigned char *>(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);
}