TLS almost connects. Needs read and write logic finished.

This commit is contained in:
Brad Arant 2025-02-20 15:33:28 -08:00
parent e0c0e2c07e
commit 5feabd0fde
10 changed files with 45 additions and 27 deletions

View File

@ -5,7 +5,7 @@
namespace core {
ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) : TCPServer(ePoll, address, " ", 10, "Console Server") {
ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo) : TCPServer(ePoll, address, tlsInfo, " ", 10, "Console Server") {
coreutils::Log(this);
}

View File

@ -1,10 +1,11 @@
#ifndef __ConsoleServer_h__
#define __ConsoleServer_h__
# define __ConsoleServer_h__
#include "TCPServer.h"
#include "Command.h"
#include "EPoll.h"
#include "LogListener.h"
# include "TCPServer.h"
# include "Command.h"
# include "EPoll.h"
# include "LogListener.h"
# include "TLSInfo.h"
namespace core {
@ -23,7 +24,7 @@ namespace core {
//
//
ConsoleServer(EPoll &ePoll, IPAddress address);
ConsoleServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo);
//
//

View File

@ -70,7 +70,7 @@ namespace core
bool Socket::eventReceived(struct epoll_event event, long long eventId) {
coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor();
if(inHandler)
coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true.";
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true.";
inHandler = true;
if(event.events & EPOLLRDHUP) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP";

View File

@ -5,21 +5,21 @@
#include "TCPSession.h"
namespace core {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text)
: TCPSocket(ePoll, text), commands(delimiter, depth) {
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
int yes = 1;
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(getDescriptor(), address.getPointer(), address.addressLength) < 0)
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
if (listen(getDescriptor(), 20) < 0)
throw coreutils::Exception("Error on listen to socket");
}
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo, std::string delimiter, int depth, std::string text)
: TCPSocket(ePoll, tlsInfo, text), commands(delimiter, depth) {
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
int yes = 1;
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(getDescriptor(), address.getPointer(), address.addressLength) < 0)
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
if (listen(getDescriptor(), 20) < 0)
throw coreutils::Exception("Error on listen to socket");
}
TCPServer::~TCPServer() {
coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << ".";
close(getDescriptor());
@ -31,6 +31,10 @@ namespace core {
TCPSession *session = accept();
if (session)
sessions.push_back(session);
if(true) {
registerSocket(session->getDescriptor());
acceptSocket();
}
lock.unlock();
}

View File

@ -8,6 +8,7 @@
# include "SubscriptionManager.h"
# include "TCPSession.h"
# include "TCPSocket.h"
# include "TLSInfo.h"
namespace core {
@ -36,7 +37,7 @@ namespace core {
/// @param commandName the name of the command used to invoke the status display for this object.
///
TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter = " ", int depth = 10, std::string text = "");
TCPServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo, std::string delimiter = " ", int depth = 10, std::string text = "");
///
/// The destructor for this object.

View File

@ -10,7 +10,7 @@ namespace core {
TCPSocket::TCPSocket(EPoll &ePoll, std::string text) : Socket(ePoll, text) {}
TCPSocket::TCPSocket(EPoll &ePoll, TLSInfo *tlsInfo, std::string text) : Socket(ePoll, text), TLS(tlsInfo) {}
TCPSocket::TCPSocket(EPoll &ePoll, TLSInfo *tlsInfo, std::string text) : Socket(ePoll, text), TLS(tlsInfo), tlsInfo(tlsInfo) {}
TCPSocket::TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text) : Socket(ePoll, text), TLS(ctx) {}
@ -28,6 +28,11 @@ namespace core {
}
void TCPSocket::onDataReceived(coreutils::ZString &data) {
if(tlsInfo) {
}
if (data.getLength() > 0) {
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength());
memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength());

View File

@ -106,6 +106,7 @@ namespace core {
protected:
bool term = false;
TLSInfo *tlsInfo;
private:
char *lineBuffer = NULL;

View File

@ -85,7 +85,7 @@ namespace core {
if(!SSL_CTX_load_verify_locations(ctx, tlsInfo->cACertificate.c_str(), NULL))
throw coreutils::Exception("Cannot verify locations.");
SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(tlsInfo->cACertificate.c_str()));
coreutils::Log(coreutils::LOG_DEBUG_1) << "Server key authenticated.";
coreutils::Log(coreutils::LOG_INFO) << "Server key authenticated.";
}
}

Binary file not shown.

View File

@ -4,6 +4,7 @@
#include "File.h"
#include "Log.h"
#include "IPAddress.h"
#include "TLSInfo.h"
#include <iostream>
int main(int argc, char **argv) {
@ -16,8 +17,13 @@ int main(int argc, char **argv) {
std::string ipAddress = "0.0.0.0";
core::EPoll ePoll;
core::TLSInfo tlsInfo;
tlsInfo.cACertificate = "certs/cert.pem";
tlsInfo.certificate = "certs/cert.pem";
tlsInfo.key = "certs/key.pem";
core::TCPServer console(ePoll, core::IPAddress(ipAddress, 1027));
core::TCPServer console(ePoll, core::IPAddress(ipAddress, 1027), &tlsInfo);
console.commands.add(ePoll, "threads");
console.commands.add(console, "consoles");