diff --git a/Redirector/DNS.cpp b/Redirector/DNS.cpp deleted file mode 100644 index 26bd00a0..00000000 --- a/Redirector/DNS.cpp +++ /dev/null @@ -1,231 +0,0 @@ -#include "DNS.h" - -#include "Data.h" -#include "Utils.h" - -#include - -#include -#include -#include - -using namespace std; - -extern string dnsHost; -extern USHORT dnsPort; - -typedef struct _DNSPKT { - ENDPOINT_ID ID; - PBYTE Target; - ULONG TargetLength; - PCHAR Buffer; - ULONG BufferLength; - PNF_UDP_OPTIONS Option; -} DNSPKT, * PDNSPKT; - -BOOL dnsInited = FALSE; -HANDLE dnsLock = NULL; -list dnsList; - -SOCKET dns_createSocket() -{ - sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); - addr.sin_port = 0; - - auto client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (client == INVALID_SOCKET) - { - printf("[Redirector][DNS][CreateSocket] Unable to create socket: %d\n", WSAGetLastError()); - return NULL; - } - - if (bind(client, (PSOCKADDR)&addr, sizeof(sockaddr_in)) == SOCKET_ERROR) - { - printf("[Redirector][DNS][CreateSocket] Unable to bind socket: %d\n", WSAGetLastError()); - return NULL; - } - - return client; -} - -void dns_freePacket(PDNSPKT i) -{ - if (i) - { - if (i->Target) - { - free(i->Target); - } - - if (i->Buffer) - { - free(i->Buffer); - } - - if (i->Option) - { - free(i->Option); - } - - free(i); - } - - i = NULL; -} - -void dns_work() -{ - sockaddr_in addr; - memset(&addr, 0, sizeof(sockaddr_in)); - addr.sin_addr.S_un.S_addr = inet_addr(dnsHost.c_str()); - addr.sin_port = htons(dnsPort); - - while (dnsInited) - { - auto client = dns_createSocket(); - if (NULL == client) - { - Sleep(100); - continue; - } - - WaitForSingleObject(dnsLock, INFINITE); - if (!dnsList.size()) - { - closesocket(client); - ReleaseMutex(dnsLock); - - Sleep(1); - continue; - } - - auto data = dnsList.front(); - dnsList.remove(data); - ReleaseMutex(dnsLock); - - if (data->BufferLength != (ULONG)sendto(client, (PCHAR)data->Buffer, data->BufferLength, NULL, (PSOCKADDR)&addr, sizeof(sockaddr_in))) - { - closesocket(client); - dns_freePacket(data); - - printf("[Redirector][DNS][dnsWorker] Unable to send packet: %d\n", WSAGetLastError()); - continue; - } - - char buffer[1500]; - auto length = recvfrom(client, buffer, sizeof(buffer), NULL, NULL, NULL); - if (!length) - { - closesocket(client); - dns_freePacket(data); - - printf("[Redirector][DNS][dnsWorker] Unable to receive packet: %d\n", WSAGetLastError()); - continue; - } - - nf_udpPostReceive(data->ID, data->Target, buffer, length, data->Option); - closesocket(client); - dns_freePacket(data); - } -} - -void dns_init() -{ - if (!dnsLock) - { - dnsLock = CreateMutex(NULL, FALSE, NULL); - } - - dnsInited = TRUE; - dnsDelete(); - - for (DWORD i = 0; i < 4; i++) - { - thread(dns_work).detach(); - } -} - -void dns_free() -{ - dnsInited = FALSE; - Sleep(10); - - if (dnsLock) - { - dnsDelete(); - - CloseHandle(dnsLock); - dnsLock = NULL; - } -} - -void dnsCreate(ENDPOINT_ID id, PBYTE target, ULONG targetLength, PCHAR buffer, ULONG bufferLength, PNF_UDP_OPTIONS option) -{ - if (!dnsInited) - { - return; - } - - auto data = (PDNSPKT)malloc(sizeof(DNSPKT)); - if (!data) - { - puts("[Redirector][DNS][dnsCreate] Unable to allocate memory"); - return; - } - data->ID = id; - - data->Target = (PBYTE)malloc(targetLength); - data->TargetLength = targetLength; - if (!data->Target) - { - free(data); - - puts("[Redirector][DNS][dnsCreate] Unable to allocate memory"); - return; - } - - data->Buffer = (PCHAR)malloc(bufferLength); - data->BufferLength = bufferLength; - if (!data->Buffer) - { - free(data->Target); - free(data); - - puts("[Redirector][DNS][dnsCreate] Unable to allocate memory"); - return; - } - - data->Option = (PNF_UDP_OPTIONS)malloc(sizeof(NF_UDP_OPTIONS)); - if (!data->Option) - { - free(data->Target); - free(data->Buffer); - free(data); - - puts("[Redirector][DNS][dnsCreate] Unable to allocate memory"); - return; - } - - memcpy(data->Target, target, targetLength); - memcpy(data->Buffer, buffer, bufferLength); - memcpy(data->Option, option, sizeof(NF_UDP_OPTIONS)); - - WaitForSingleObject(dnsLock, INFINITE); - dnsList.emplace_back(data); - ReleaseMutex(dnsLock); -} - -void dnsDelete() -{ - WaitForSingleObject(dnsLock, INFINITE); - - for (auto i : dnsList) - { - dns_freePacket(i); - } - dnsList.clear(); - - ReleaseMutex(dnsLock); -} diff --git a/Redirector/DNS.h b/Redirector/DNS.h deleted file mode 100644 index 003e4897..00000000 --- a/Redirector/DNS.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#ifndef DNS_H -#define DNS_H -#include - -#include - -void dns_init(); -void dns_free(); -void dnsCreate(ENDPOINT_ID id, PBYTE target, ULONG targetLength, PCHAR buffer, ULONG bufferLength, PNF_UDP_OPTIONS options); -void dnsDelete(); - -#endif diff --git a/Redirector/DNSHandler.cpp b/Redirector/DNSHandler.cpp new file mode 100644 index 00000000..11b50f8a --- /dev/null +++ b/Redirector/DNSHandler.cpp @@ -0,0 +1,90 @@ +#include "DNSHandler.h" + +#include "Data.h" +#include "Utils.h" + +#include + +#include + +DNSHandler::DNSHandler(string dnsHost, USHORT dnsPort) +{ + lock_guard lg(this->DNSLock); + + this->Started = TRUE; + this->DNSHost = dnsHost; + this->DNSPort = dnsPort; + + for (int i = 0; i < 4; i++) + { + thread(&DNSHandler::Worker, this).detach(); + } +} + +DNSHandler::~DNSHandler() +{ + lock_guard lg(this->DNSLock); + + this->Started = FALSE; + for (PDNSPKT i : this->DNSList) + { + this->Delete(i); + } + this->DNSList.clear(); +} + +void DNSHandler::Create(ENDPOINT_ID id, PBYTE target, ULONG targetLength, PCHAR buffer, ULONG bufferLength, PNF_UDP_OPTIONS option) +{ + if (!this->Started) + { + return; + } + + auto data = new DNSPKT(); + data->ID = id; + data->Target = new BYTE[targetLength](); + data->TargetLength = targetLength; + data->Buffer = new CHAR[bufferLength](); + data->BufferLength = bufferLength; + data->Option = new NF_UDP_OPTIONS(); + + memcpy(data->Target, target, targetLength); + memcpy(data->Buffer, buffer, bufferLength); + memcpy(data->Option, option, sizeof(NF_UDP_OPTIONS)); + + lock_guard lg(this->DNSLock); + this->DNSList.emplace_back(data); +} + +void DNSHandler::Delete(PDNSPKT i) +{ + if (i) + { + if (i->Target) + { + delete[] i->Target; + } + + if (i->Buffer) + { + delete[] i->Buffer; + } + + if (i->Option) + { + delete i->Option; + } + + delete i; + } + + i = NULL; +} + +void DNSHandler::Worker() +{ + while (this->Started) + { + + } +} diff --git a/Redirector/DNSHandler.h b/Redirector/DNSHandler.h new file mode 100644 index 00000000..82e8e9da --- /dev/null +++ b/Redirector/DNSHandler.h @@ -0,0 +1,43 @@ +#pragma once +#ifndef DNSHANDLER_H +#define DNSHANDLER_H +#include + +#include +#include +#include + +#include + +using namespace std; + +typedef struct _DNSPKT { + ENDPOINT_ID ID; + PBYTE Target; + ULONG TargetLength; + PCHAR Buffer; + ULONG BufferLength; + PNF_UDP_OPTIONS Option; +} DNSPKT, * PDNSPKT; + +typedef class DNSHandler +{ +public: + DNSHandler(string dnsHost, USHORT dnsPort); + ~DNSHandler(); + + void Create(ENDPOINT_ID id, PBYTE target, ULONG targetLength, PCHAR buffer, ULONG bufferLength, PNF_UDP_OPTIONS options); +private: + void Delete(PDNSPKT i); + void Worker(); + + BOOL Started = FALSE; + + string DNSHost; + USHORT DNSPort = 0; + + mutex DNSLock; + vector DNSList; +} *PDNSHandler; + +#endif diff --git a/Redirector/EventHandler.cpp b/Redirector/EventHandler.cpp index abc954f1..0d131cf5 100644 --- a/Redirector/EventHandler.cpp +++ b/Redirector/EventHandler.cpp @@ -1,18 +1,23 @@ #include "EventHandler.h" -#include "DNS.h" #include "Data.h" +#include "DNSHandler.h" +#include "TCPHandler.h" +#include "UDPHandler.h" #include #include #include +#include #include #include using namespace std; extern BOOL dnsHook; +extern string dnsHost; +extern USHORT dnsPort; extern USHORT tcpLisn; extern USHORT udpLisn; @@ -28,11 +33,14 @@ typedef struct _UDPINFO { vector handleList; vector bypassList; -HANDLE TCPLock = NULL; -HANDLE UDPLock = NULL; +mutex TCPLock; +mutex UDPLock; map TCPContext; map UDPContext; +PDNSHandler dnsHandler = NULL; +PTCPHandler tcpHandler = NULL; + wstring getProcessName(DWORD id) { if (id == 0) @@ -95,23 +103,21 @@ BOOL checkHandleName(DWORD id) void eh_init() { - if (!TCPLock) + if (dnsHandler == NULL) { - TCPLock = CreateMutex(NULL, FALSE, NULL); + dnsHandler = new DNSHandler(dnsHost, dnsPort); } - if (!UDPLock) + if (tcpHandler == NULL) { - UDPLock = CreateMutex(NULL, FALSE, NULL); + tcpHandler = new TCPHandler(); } - - dns_init(); } void eh_free() { - WaitForSingleObject(TCPLock, INFINITE); - WaitForSingleObject(UDPLock, INFINITE); + lock_guard tlg(TCPLock); + lock_guard ulg(UDPLock); for (auto& [k, v] : TCPContext) { @@ -133,16 +139,17 @@ void eh_free() } UDPContext.clear(); - ReleaseMutex(TCPLock); - ReleaseMutex(UDPLock); + if (dnsHandler != NULL) + { + delete dnsHandler; + dnsHandler = NULL; + } - CloseHandle(TCPLock); - CloseHandle(UDPLock); - - TCPLock = NULL; - UDPLock = NULL; - - dns_free(); + if (tcpHandler != NULL) + { + delete tcpHandler; + tcpHandler = NULL; + } } void threadStart() @@ -189,7 +196,7 @@ void tcpReceive(ENDPOINT_ID id, const char* buffer, int length) void tcpClosed(ENDPOINT_ID id, PNF_TCP_CONN_INFO info) { - + } void udpCreated(ENDPOINT_ID id, PNF_UDP_CONN_INFO info) diff --git a/Redirector/Redirector.vcxproj b/Redirector/Redirector.vcxproj index 41f43389..1ad57081 100644 --- a/Redirector/Redirector.vcxproj +++ b/Redirector/Redirector.vcxproj @@ -97,17 +97,21 @@ - + + + - + + + diff --git a/Redirector/Redirector.vcxproj.filters b/Redirector/Redirector.vcxproj.filters index 826799bd..7968361c 100644 --- a/Redirector/Redirector.vcxproj.filters +++ b/Redirector/Redirector.vcxproj.filters @@ -26,7 +26,13 @@ Source - + + Source + + + Source + + Source @@ -43,7 +49,13 @@ Header - + + Header + + + Header + + Header diff --git a/Redirector/TCPHandler.cpp b/Redirector/TCPHandler.cpp new file mode 100644 index 00000000..898ba724 --- /dev/null +++ b/Redirector/TCPHandler.cpp @@ -0,0 +1,21 @@ +#include "TCPHandler.h" + +TCPHandler::TCPHandler() +{ + +} + +TCPHandler::~TCPHandler() +{ + +} + +BOOL TCPHandler::init() +{ + return FALSE; +} + +void TCPHandler::free() +{ + +} diff --git a/Redirector/TCPHandler.h b/Redirector/TCPHandler.h new file mode 100644 index 00000000..e92cd02d --- /dev/null +++ b/Redirector/TCPHandler.h @@ -0,0 +1,16 @@ +#pragma once +#ifndef TCPHANDLER_H +#define TCPHANDLER_H +#include + +typedef class TCPHandler +{ +public: + TCPHandler(); + ~TCPHandler(); + + BOOL init(); + void free(); +} *PTCPHandler; + +#endif diff --git a/Redirector/UDPHandler.cpp b/Redirector/UDPHandler.cpp new file mode 100644 index 00000000..1e8da97e --- /dev/null +++ b/Redirector/UDPHandler.cpp @@ -0,0 +1 @@ +#include "UDPHandler.h" diff --git a/Redirector/UDPHandler.h b/Redirector/UDPHandler.h new file mode 100644 index 00000000..fa8cbfa8 --- /dev/null +++ b/Redirector/UDPHandler.h @@ -0,0 +1,5 @@ +#pragma once +#ifndef UDPHANDLER_H +#define UDPHANDLER_H + +#endif diff --git a/Redirector/Utils.cpp b/Redirector/Utils.cpp index 2c58ad03..1d1ccbfa 100644 --- a/Redirector/Utils.cpp +++ b/Redirector/Utils.cpp @@ -2,14 +2,6 @@ #include "Data.h" -DWORD GetCPUCount() -{ - SYSTEM_INFO info; - GetSystemInfo(&info); - - return info.dwNumberOfProcessors; -} - USHORT IPv4Checksum(PBYTE buffer, ULONG64 size) { UINT32 sum = 0; diff --git a/Redirector/Utils.h b/Redirector/Utils.h index 51151698..37031eca 100644 --- a/Redirector/Utils.h +++ b/Redirector/Utils.h @@ -3,7 +3,6 @@ #define UTILS_H #include -DWORD GetCPUCount(); USHORT IPv4Checksum(PBYTE buffer, ULONG64 size); USHORT ICMPChecksum(PBYTE buffer, ULONG64 size);