working commit
This commit is contained in:
+39
-12
@@ -11,18 +11,21 @@ extern "C" {
|
||||
|
||||
#include <tservice.hpp>
|
||||
#include <uxlogger.hpp>
|
||||
#include <networkaux.hpp>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TCPService::TCPService(int svcport) {
|
||||
port = svcport;
|
||||
TunService::TunService(int svcport, std::string itunnelnet, std::vector<std::string> ilocalnets) {
|
||||
listenport = svcport;
|
||||
tunnelnet = itunnelnet;
|
||||
localnets = ilocalnets;
|
||||
}
|
||||
|
||||
TCPService::~TCPService() {
|
||||
close(port);
|
||||
TunService::~TunService() {
|
||||
close(listenport);
|
||||
}
|
||||
|
||||
std::expected<void, std::string> TCPService::Bind(void) {
|
||||
std::expected<void, std::string> TunService::Bind(void) {
|
||||
struct sockaddr_in address;
|
||||
int srvsock;
|
||||
if ((srvsock = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||
@@ -38,7 +41,7 @@ std::expected<void, std::string> TCPService::Bind(void) {
|
||||
}
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
address.sin_port = htons(port);
|
||||
address.sin_port = htons(listenport);
|
||||
if (bind(srvsock, (struct sockaddr *)&address, sizeof(address)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
@@ -53,7 +56,7 @@ std::expected<void, std::string> TCPService::Bind(void) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, std::string> TCPService::Listen(void) {
|
||||
std::expected<void, std::string> TunService::Listen(void) {
|
||||
struct sockaddr_in address;
|
||||
int addrlen = sizeof(address);
|
||||
int newsock = 0;
|
||||
@@ -63,18 +66,42 @@ std::expected<void, std::string> TCPService::Listen(void) {
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Accept error: " + error);
|
||||
}
|
||||
std::jthread t(&TCPService::Handle, this, newsock);
|
||||
|
||||
std::jthread t(&TunService::Handle, this, newsock);
|
||||
t.detach();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
void TCPService::Handle(int sock) {
|
||||
uxlogger.Log("Start socker handler");
|
||||
void TunService::Handle(int sock) {
|
||||
auto prefixRes = netprefix(tunnelnet);
|
||||
if (!prefixRes) {
|
||||
uxlogger.Error(prefixRes.error());
|
||||
return;
|
||||
}
|
||||
auto networkRes = network(tunnelnet);
|
||||
if (!networkRes) {
|
||||
uxlogger.Error(networkRes.error());
|
||||
return;
|
||||
}
|
||||
auto localaddrRes = nethost(networkRes.value(), prefixRes.value(), sock);
|
||||
if (!networkRes) {
|
||||
uxlogger.Error(networkRes.error());
|
||||
return;
|
||||
}
|
||||
auto remoteaddrRes = nethost(networkRes.value(), prefixRes.value(), sock + 1);
|
||||
if (!remoteaddrRes) {
|
||||
uxlogger.Error(remoteaddrRes.error());
|
||||
return;
|
||||
}
|
||||
uxlogger.Debug("Start socker handler");
|
||||
SocketHandler handler;
|
||||
handler.Handle(sock);
|
||||
uxlogger.Log("Stop socker handler");
|
||||
std::string laddr = localaddrRes.value();
|
||||
std::string raddr = remoteaddrRes.value();
|
||||
auto prefix = prefixRes.value();
|
||||
handler.Handle(sock, laddr, raddr, prefix);
|
||||
uxlogger.Debug("Stop socker handler");
|
||||
close(sock);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user