Modified By Vim Firstly.
This commit is contained in:
parent
2d15bc935e
commit
31aa47cac6
6
.gitignore
vendored
6
.gitignore
vendored
@ -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
|
||||||
|
@ -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
67
compile_commands.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
@ -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,6 +13,10 @@
|
|||||||
|
|
||||||
namespace Net {
|
namespace Net {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TCP Server using
|
||||||
|
*
|
||||||
|
*/
|
||||||
class TCPServer {
|
class TCPServer {
|
||||||
public:
|
public:
|
||||||
TCPServer(int port, int max_connection);
|
TCPServer(int port, int max_connection);
|
||||||
@ -32,7 +35,6 @@ namespace Net {
|
|||||||
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
|
||||||
|
@ -7,9 +7,9 @@
|
|||||||
|
|
||||||
#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"
|
||||||
@ -18,7 +18,6 @@ namespace Net {
|
|||||||
|
|
||||||
class AESCBCEncryptor {
|
class AESCBCEncryptor {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
AESCBCEncryptor();
|
AESCBCEncryptor();
|
||||||
|
|
||||||
string getKeyData() const;
|
string getKeyData() const;
|
||||||
@ -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
|
||||||
|
@ -2,39 +2,44 @@
|
|||||||
// 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)
|
||||||
|
throw std::runtime_error(strerror(errno));
|
||||||
else {
|
else {
|
||||||
boost::thread accept_thread(TCPServer::accept, connect_fd, &server->buff_mutex, &server->recv_buff, &server->status);
|
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();
|
||||||
@ -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;
|
||||||
@ -66,7 +72,6 @@ Net::TCPServer::TCPServer(int port, int max_connection) {
|
|||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
|
||||||
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user