mirror of
https://github.com/netchx/netch.git
synced 2026-03-14 17:43:18 +08:00
Add files via upload
This commit is contained in:
23
Redirector/Based.h
Normal file
23
Redirector/Based.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#ifndef BASED_H
|
||||
#define BASED_H
|
||||
#include <stdio.h>
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <queue>
|
||||
#include <regex>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <ws2ipdef.h>
|
||||
#include <WS2tcpip.h>
|
||||
#include <Windows.h>
|
||||
|
||||
#include <nfapi.h>
|
||||
|
||||
using namespace std;
|
||||
#endif
|
||||
@@ -1,62 +1,8 @@
|
||||
#include "DNSHandler.h"
|
||||
|
||||
#include "Data.h"
|
||||
#include "Utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
DNSHandler::DNSHandler(string dnsHost, USHORT dnsPort)
|
||||
{
|
||||
lock_guard<mutex> 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<mutex> 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<mutex> lg(this->DNSLock);
|
||||
this->DNSList.emplace_back(data);
|
||||
}
|
||||
|
||||
void DNSHandler::Delete(PDNSPKT i)
|
||||
void dns_freePacket(PDNSPKT i)
|
||||
{
|
||||
if (i)
|
||||
{
|
||||
@@ -81,10 +27,151 @@ void DNSHandler::Delete(PDNSPKT i)
|
||||
i = NULL;
|
||||
}
|
||||
|
||||
void DNSHandler::Worker()
|
||||
DNSHandler::DNSHandler(string dnsHost, USHORT dnsPort)
|
||||
{
|
||||
while (this->Started)
|
||||
this->dnsHost = dnsHost;
|
||||
this->dnsPort = dnsPort;
|
||||
}
|
||||
|
||||
BOOL DNSHandler::init()
|
||||
{
|
||||
this->Started = TRUE;
|
||||
|
||||
WaitForSingleObject(this->dnsLock, INFINITE);
|
||||
for (size_t i = 0; i < 4; i++)
|
||||
{
|
||||
|
||||
this->dnsLoop[i] = thread(&DNSHandler::Thread, this);
|
||||
}
|
||||
ReleaseMutex(this->dnsLock);
|
||||
}
|
||||
|
||||
void DNSHandler::free()
|
||||
{
|
||||
if (this->dnsLock)
|
||||
{
|
||||
WaitForSingleObject(this->dnsLock, INFINITE);
|
||||
}
|
||||
|
||||
this->Started = FALSE;
|
||||
for (size_t i = 0; i < this->dnsLoop.size(); i++)
|
||||
{
|
||||
if (this->dnsLoop[i].joinable())
|
||||
{
|
||||
this->dnsLoop[i].join();
|
||||
}
|
||||
}
|
||||
this->dnsLoop.clear();
|
||||
|
||||
auto size = this->dnsList.size();
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
dns_freePacket(this->dnsList.front());
|
||||
}
|
||||
|
||||
if (this->dnsLock)
|
||||
{
|
||||
CloseHandle(this->dnsLock);
|
||||
this->dnsLock = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
WaitForSingleObject(this->dnsLock, INFINITE);
|
||||
this->dnsList.emplace(data);
|
||||
ReleaseMutex(this->dnsLock);
|
||||
}
|
||||
|
||||
void DNSHandler::Thread()
|
||||
{
|
||||
while (this->Started && this->dnsLock)
|
||||
{
|
||||
switch (WaitForSingleObject(this->dnsLock, 10))
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
this->Handle();
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DNSHandler::Handle()
|
||||
{
|
||||
SOCKADDR_IN addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(this->dnsPort);
|
||||
if (!inet_pton(AF_INET, this->dnsHost.c_str(), &addr.sin_addr))
|
||||
{
|
||||
ReleaseMutex(this->dnsLock);
|
||||
return;
|
||||
}
|
||||
|
||||
auto data = this->dnsList.front();
|
||||
this->dnsList.pop();
|
||||
ReleaseMutex(this->dnsLock);
|
||||
|
||||
auto client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (!client)
|
||||
{
|
||||
dns_freePacket(data);
|
||||
|
||||
printf("[Redirector][DNSHandler::Handle] Create socket failed: %d\n", WSAGetLastError());
|
||||
return;
|
||||
}
|
||||
|
||||
TIMEVAL tv;
|
||||
tv.tv_sec = 4;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
FD_SET fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(client, &fds);
|
||||
|
||||
if (sendto(client, data->Buffer, data->BufferLength, 0, (PSOCKADDR)&addr, sizeof(SOCKADDR_IN)))
|
||||
{
|
||||
if (select(0, &fds, 0, 0, &tv))
|
||||
{
|
||||
CHAR buffer[1024];
|
||||
auto length = recvfrom(client, buffer, sizeof(buffer), 0, NULL, NULL);
|
||||
|
||||
if (length)
|
||||
{
|
||||
nf_udpPostReceive(data->ID, data->Target, buffer, length, data->Option);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[Redirector][DNSHandler::Handle] Receive failed: %d\n", WSAGetLastError());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("[Redirector][DNSHandler::Handle] Receive timed out");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[Redirector][DNSHandler::Handle] Send failed: %d\n", WSAGetLastError());
|
||||
}
|
||||
|
||||
closesocket(client);
|
||||
dns_freePacket(data);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
#pragma once
|
||||
#ifndef DNSHANDLER_H
|
||||
#define DNSHANDLER_H
|
||||
#include <Windows.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <nfapi.h>
|
||||
|
||||
using namespace std;
|
||||
#include "Based.h"
|
||||
|
||||
typedef struct _DNSPKT {
|
||||
ENDPOINT_ID ID;
|
||||
@@ -24,20 +16,23 @@ typedef class DNSHandler
|
||||
{
|
||||
public:
|
||||
DNSHandler(string dnsHost, USHORT dnsPort);
|
||||
~DNSHandler();
|
||||
|
||||
BOOL init();
|
||||
void free();
|
||||
|
||||
void Create(ENDPOINT_ID id, PBYTE target, ULONG targetLength, PCHAR buffer, ULONG bufferLength, PNF_UDP_OPTIONS options);
|
||||
private:
|
||||
void Delete(PDNSPKT i);
|
||||
void Worker();
|
||||
void Thread();
|
||||
void Handle();
|
||||
|
||||
string dnsHost;
|
||||
USHORT dnsPort = 0;
|
||||
|
||||
HANDLE dnsLock;
|
||||
queue<PDNSPKT> dnsList;
|
||||
vector<thread> dnsLoop;
|
||||
|
||||
BOOL Started = FALSE;
|
||||
|
||||
string DNSHost;
|
||||
USHORT DNSPort = 0;
|
||||
|
||||
mutex DNSLock;
|
||||
vector<PDNSPKT> DNSList;
|
||||
} *PDNSHandler;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#include "Data.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
BOOL Started = FALSE;
|
||||
BOOL filterLoop = FALSE;
|
||||
BOOL filterICMP = TRUE;
|
||||
BOOL filterTCP = TRUE;
|
||||
BOOL filterUDP = TRUE;
|
||||
BOOL dnsHook = FALSE;
|
||||
string dnsHost = "";
|
||||
USHORT dnsPort = 0;
|
||||
USHORT tcpLisn = 0;
|
||||
USHORT udpLisn = 0;
|
||||
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef DATA_H
|
||||
#define DATA_H
|
||||
#include <Windows.h>
|
||||
|
||||
#include <nfapi.h>
|
||||
|
||||
typedef enum _AIO_TYPE {
|
||||
AIO_FILTERLOOP,
|
||||
AIO_FILTERICMP,
|
||||
AIO_FILTERTCP,
|
||||
AIO_FILTERUDP,
|
||||
|
||||
AIO_CLRNAME,
|
||||
AIO_ADDNAME,
|
||||
AIO_BYPNAME,
|
||||
|
||||
AIO_DNSHOOK,
|
||||
AIO_DNSHOST,
|
||||
AIO_DNSPORT,
|
||||
|
||||
AIO_TCPPORT,
|
||||
AIO_UDPPORT
|
||||
} AIO_TYPE;
|
||||
|
||||
#endif
|
||||
@@ -1,45 +1,31 @@
|
||||
#include "EventHandler.h"
|
||||
|
||||
#include "Data.h"
|
||||
#include "DNSHandler.h"
|
||||
#include "TCPHandler.h"
|
||||
#include "UDPHandler.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <map>
|
||||
#include <regex>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern BOOL dnsHook;
|
||||
extern string dnsHost;
|
||||
extern USHORT dnsPort;
|
||||
extern USHORT tcpLisn;
|
||||
extern USHORT udpLisn;
|
||||
|
||||
typedef struct _TCPINFO {
|
||||
DWORD PID;
|
||||
PBYTE Target;
|
||||
} TCPINFO, * PTCPINFO;
|
||||
|
||||
typedef struct _UDPINFO {
|
||||
SOCKET Socket;
|
||||
} UDPINFO, * PUDPINFO;
|
||||
BOOL Started = FALSE;
|
||||
BOOL filterLoop = FALSE;
|
||||
BOOL filterICMP = TRUE;
|
||||
BOOL filterTCP = TRUE;
|
||||
BOOL filterUDP = TRUE;
|
||||
BOOL dnsHook = FALSE;
|
||||
string dnsHost = "";
|
||||
USHORT dnsPort = 0;
|
||||
USHORT tcpLisn = 0;
|
||||
USHORT udpLisn = 0;
|
||||
|
||||
vector<wstring> handleList;
|
||||
vector<wstring> bypassList;
|
||||
|
||||
mutex TCPLock;
|
||||
mutex UDPLock;
|
||||
map<ENDPOINT_ID, PTCPINFO> TCPContext;
|
||||
map<ENDPOINT_ID, PUDPINFO> UDPContext;
|
||||
mutex tcpLock;
|
||||
mutex udpLock;
|
||||
map<ENDPOINT_ID, PTCPINFO> tcpContext;
|
||||
map<ENDPOINT_ID, PUDPINFO> udpContext;
|
||||
|
||||
PDNSHandler dnsHandler = NULL;
|
||||
PTCPHandler tcpHandler = NULL;
|
||||
PUDPHandler udpHandler = NULL;
|
||||
|
||||
wstring getProcessName(DWORD id)
|
||||
{
|
||||
@@ -62,10 +48,10 @@ wstring getProcessName(DWORD id)
|
||||
}
|
||||
}
|
||||
|
||||
wchar_t result[MAX_PATH];
|
||||
if (GetLongPathNameW(name, result, MAX_PATH))
|
||||
wchar_t data[MAX_PATH];
|
||||
if (GetLongPathNameW(name, data, MAX_PATH))
|
||||
{
|
||||
return result;
|
||||
return data;
|
||||
}
|
||||
|
||||
return name;
|
||||
@@ -101,35 +87,32 @@ BOOL checkHandleName(DWORD id)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void eh_init()
|
||||
BOOL eh_init()
|
||||
{
|
||||
if (dnsHandler == NULL)
|
||||
dnsHandler = new DNSHandler(dnsHost, dnsPort);
|
||||
tcpHandler = new TCPHandler(tcpLisn);
|
||||
udpHandler = new UDPHandler();
|
||||
|
||||
if (!tcpHandler->init())
|
||||
{
|
||||
dnsHandler = new DNSHandler(dnsHost, dnsPort);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (tcpHandler == NULL)
|
||||
{
|
||||
tcpHandler = new TCPHandler();
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void eh_free()
|
||||
{
|
||||
lock_guard<mutex> tlg(TCPLock);
|
||||
lock_guard<mutex> ulg(UDPLock);
|
||||
lock_guard<mutex> tlg(tcpLock);
|
||||
lock_guard<mutex> ulg(udpLock);
|
||||
|
||||
for (auto& [k, v] : TCPContext)
|
||||
for (auto& [k, v] : tcpContext)
|
||||
{
|
||||
if (v->Target)
|
||||
{
|
||||
free(v->Target);
|
||||
v->Target = NULL;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
TCPContext.clear();
|
||||
tcpContext.clear();
|
||||
|
||||
for (auto& [k, v] : UDPContext)
|
||||
for (auto& [k, v] : udpContext)
|
||||
{
|
||||
if (v->Socket)
|
||||
{
|
||||
@@ -137,19 +120,29 @@ void eh_free()
|
||||
v->Socket = NULL;
|
||||
}
|
||||
}
|
||||
UDPContext.clear();
|
||||
udpContext.clear();
|
||||
|
||||
if (dnsHandler != NULL)
|
||||
if (dnsHandler)
|
||||
{
|
||||
dnsHandler->free();
|
||||
|
||||
delete dnsHandler;
|
||||
dnsHandler = NULL;
|
||||
}
|
||||
|
||||
if (tcpHandler != NULL)
|
||||
if (tcpHandler)
|
||||
{
|
||||
tcpHandler->free();
|
||||
|
||||
delete tcpHandler;
|
||||
tcpHandler = NULL;
|
||||
}
|
||||
|
||||
if (udpHandler)
|
||||
{
|
||||
delete udpHandler;
|
||||
udpHandler = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void threadStart()
|
||||
@@ -164,7 +157,21 @@ void threadEnd()
|
||||
|
||||
void tcpConnectRequest(ENDPOINT_ID id, PNF_TCP_CONN_INFO info)
|
||||
{
|
||||
nf_tcpDisableFiltering(id);
|
||||
if (checkBypassName(info->processId))
|
||||
{
|
||||
nf_tcpDisableFiltering(id);
|
||||
|
||||
printf("[Redirector][EventHandler][tcpConnectRequest][%llu] this->checkBypassName\n", id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!checkHandleName(info->processId))
|
||||
{
|
||||
nf_tcpDisableFiltering(id);
|
||||
|
||||
printf("[Redirector][EventHandler][tcpConnectRequest][%llu] !this->checkHandleName\n", id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void tcpConnected(ENDPOINT_ID id, PNF_TCP_CONN_INFO info)
|
||||
@@ -196,7 +203,7 @@ void tcpReceive(ENDPOINT_ID id, const char* buffer, int length)
|
||||
|
||||
void tcpClosed(ENDPOINT_ID id, PNF_TCP_CONN_INFO info)
|
||||
{
|
||||
|
||||
printf("[Redirector][EventHandler][tcpClosed][%llu]\n", id);
|
||||
}
|
||||
|
||||
void udpCreated(ENDPOINT_ID id, PNF_UDP_CONN_INFO info)
|
||||
@@ -232,5 +239,11 @@ void udpReceive(ENDPOINT_ID id, const unsigned char* target, const char* buffer,
|
||||
|
||||
void udpClosed(ENDPOINT_ID id, PNF_UDP_CONN_INFO info)
|
||||
{
|
||||
printf("[Redirector][EventHandler][udpClosed][%llu]\n", id);
|
||||
|
||||
lock_guard<mutex> lg(udpLock);
|
||||
if (udpContext.find(id) != udpContext.end())
|
||||
{
|
||||
udpContext.erase(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
#pragma once
|
||||
#ifndef EVENTHANDLER_H
|
||||
#define EVENTHANDLER_H
|
||||
#include <Windows.h>
|
||||
#include "Based.h"
|
||||
|
||||
#include <nfapi.h>
|
||||
|
||||
void eh_init();
|
||||
BOOL eh_init();
|
||||
void eh_free();
|
||||
|
||||
void threadStart();
|
||||
|
||||
@@ -2,9 +2,45 @@
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
USHORT IPv4Checksum(PBYTE buffer, ULONG64 size)
|
||||
{
|
||||
UINT32 sum = 0;
|
||||
for (int i = 0; i < size; i += 2)
|
||||
{
|
||||
sum += (buffer[i] << 8) + buffer[i + 1];
|
||||
}
|
||||
|
||||
using namespace std;
|
||||
if ((size % 2) == 1)
|
||||
{
|
||||
sum += buffer[size - 1] << 8;
|
||||
}
|
||||
|
||||
while (sum > 0xffff)
|
||||
{
|
||||
sum = (sum >> 16) + (sum & 0xffff);
|
||||
}
|
||||
|
||||
return ~sum & 0xffff;
|
||||
}
|
||||
|
||||
USHORT ICMPChecksum(PBYTE buffer, ULONG64 size)
|
||||
{
|
||||
UINT32 sum = 0;
|
||||
for (int i = 0; i < size; i += 2)
|
||||
{
|
||||
sum += buffer[i] + (buffer[i + 1] << 8);
|
||||
}
|
||||
|
||||
if ((size % 2) == 1)
|
||||
{
|
||||
sum += buffer[size - 1];
|
||||
}
|
||||
|
||||
sum = (sum >> 16) + (sum & 0xffff);
|
||||
sum += (sum >> 16);
|
||||
|
||||
return ~sum & 0xffff;
|
||||
}
|
||||
|
||||
void ipSend(const char* buffer, int length, PNF_IP_PACKET_OPTIONS options)
|
||||
{
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
#pragma once
|
||||
#ifndef IPEVENTHANDLER_H
|
||||
#define IPEVENTHANDLER_H
|
||||
#include <Windows.h>
|
||||
|
||||
#include <nfapi.h>
|
||||
#include "Based.h"
|
||||
|
||||
void ipSend(const char* buffer, int length, PNF_IP_PACKET_OPTIONS options);
|
||||
void ipReceive(const char* buffer, int length, PNF_IP_PACKET_OPTIONS options);
|
||||
|
||||
@@ -1,16 +1,26 @@
|
||||
#include "Data.h"
|
||||
#include "Based.h"
|
||||
|
||||
#include "Utils.h"
|
||||
#include "EventHandler.h"
|
||||
#include "IPEventHandler.h"
|
||||
|
||||
#include <Windows.h>
|
||||
typedef enum _AIO_TYPE {
|
||||
AIO_FILTERLOOP,
|
||||
AIO_FILTERICMP,
|
||||
AIO_FILTERTCP,
|
||||
AIO_FILTERUDP,
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
AIO_CLRNAME,
|
||||
AIO_ADDNAME,
|
||||
AIO_BYPNAME,
|
||||
|
||||
#include <nfapi.h>
|
||||
AIO_DNSHOOK,
|
||||
AIO_DNSHOST,
|
||||
AIO_DNSPORT,
|
||||
|
||||
using namespace std;
|
||||
AIO_TCPPORT,
|
||||
AIO_UDPPORT
|
||||
} AIO_TYPE;
|
||||
|
||||
extern BOOL filterLoop;
|
||||
extern BOOL filterICMP;
|
||||
@@ -92,13 +102,13 @@ extern "C" {
|
||||
|
||||
__declspec(dllexport) BOOL __cdecl aio_init()
|
||||
{
|
||||
nf_adjustProcessPriviledges();
|
||||
|
||||
{
|
||||
WSADATA data;
|
||||
UNREFERENCED_PARAMETER(WSAStartup(MAKEWORD(2, 2), &data));
|
||||
}
|
||||
|
||||
nf_adjustProcessPriviledges();
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,6 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Data.cpp" />
|
||||
<ClCompile Include="DNSHandler.cpp" />
|
||||
<ClCompile Include="EventHandler.cpp" />
|
||||
<ClCompile Include="IPEventHandler.cpp" />
|
||||
@@ -106,10 +105,10 @@
|
||||
<ClCompile Include="Utils.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Data.h" />
|
||||
<ClInclude Include="DNSHandler.h" />
|
||||
<ClInclude Include="EventHandler.h" />
|
||||
<ClInclude Include="IPEventHandler.h" />
|
||||
<ClInclude Include="Based.h" />
|
||||
<ClInclude Include="TCPHandler.h" />
|
||||
<ClInclude Include="UDPHandler.h" />
|
||||
<ClInclude Include="Utils.h" />
|
||||
|
||||
@@ -17,9 +17,6 @@
|
||||
<ClCompile Include="EventHandler.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Data.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Utils.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
@@ -40,9 +37,6 @@
|
||||
<ClInclude Include="EventHandler.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Data.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Utils.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
@@ -58,6 +52,9 @@
|
||||
<ClInclude Include="UDPHandler.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Based.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="PROTOCOL.txt" />
|
||||
|
||||
@@ -1,21 +1,165 @@
|
||||
#include "TCPHandler.h"
|
||||
|
||||
TCPHandler::TCPHandler()
|
||||
TCPHandler::TCPHandler(USHORT tcpPort)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TCPHandler::~TCPHandler()
|
||||
{
|
||||
|
||||
this->tcpPort = tcpPort;
|
||||
}
|
||||
|
||||
BOOL TCPHandler::init()
|
||||
{
|
||||
return FALSE;
|
||||
{
|
||||
this->IPv4Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (!this->IPv4Socket)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::init][IPv4] Create socket failed: %d\n", WSAGetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SOCKADDR_IN addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = 0;
|
||||
|
||||
if (bind(this->IPv4Socket, (PSOCKADDR)&addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::init][IPv4] Bind socket failed: %d\n", WSAGetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (listen(this->IPv4Socket, 1024) == SOCKET_ERROR)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::init][IPv4] Listen socket failed: %d\n", WSAGetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
this->IPv6Socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (!this->IPv6Socket)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::init][IPv6] Create socket failed: %d\n", WSAGetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SOCKADDR_IN6 addr;
|
||||
addr.sin6_family = AF_INET6;
|
||||
addr.sin6_port = 0;
|
||||
|
||||
if (bind(this->IPv6Socket, (PSOCKADDR)&addr, sizeof(SOCKADDR_IN6)) == SOCKET_ERROR)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::init][IPv6] Bind socket failed: %d\n", WSAGetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (listen(this->IPv6Socket, 1024) == SOCKET_ERROR)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::init][IPv6] Listen socket failed: %d\n", WSAGetLastError());
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
this->tcpLock = CreateMutex(NULL, FALSE, NULL);
|
||||
if (!this->tcpLock)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
this->Started = TRUE;
|
||||
thread(&TCPHandler::IPv4, this).detach();
|
||||
thread(&TCPHandler::IPv6, this).detach();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void TCPHandler::free()
|
||||
{
|
||||
WaitForSingleObject(this->tcpLock, INFINITE);
|
||||
|
||||
if (this->IPv4Socket)
|
||||
{
|
||||
closesocket(this->IPv4Socket);
|
||||
this->IPv4Socket = NULL;
|
||||
}
|
||||
|
||||
if (this->IPv6Socket)
|
||||
{
|
||||
closesocket(this->IPv6Socket);
|
||||
this->IPv6Socket = NULL;
|
||||
}
|
||||
|
||||
for (auto& [k, v] : this->tcpContext)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
this->tcpContext.clear();
|
||||
|
||||
if (this->tcpLock)
|
||||
{
|
||||
CloseHandle(this->tcpLock);
|
||||
this->tcpLock = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void TCPHandler::IPv4()
|
||||
{
|
||||
while (this->Started && this->IPv4Socket)
|
||||
{
|
||||
SOCKADDR_IN addr;
|
||||
int addrLength = 0;
|
||||
|
||||
auto client = accept(this->IPv4Socket, (PSOCKADDR)&addr, &addrLength);
|
||||
if (!client)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::IPv4Handler] Accept client failed: %d\n", WSAGetLastError());
|
||||
return;
|
||||
}
|
||||
|
||||
WaitForSingleObject(this->tcpLock, INFINITE);
|
||||
if (this->tcpContext.find(addr.sin_port) == this->tcpContext.end())
|
||||
{
|
||||
ReleaseMutex(this->tcpLock);
|
||||
|
||||
closesocket(client);
|
||||
continue;
|
||||
}
|
||||
ReleaseMutex(this->tcpLock);
|
||||
|
||||
thread(&TCPHandler::Handle, this, client, addr.sin_port).detach();
|
||||
}
|
||||
}
|
||||
|
||||
void TCPHandler::IPv6()
|
||||
{
|
||||
while (this->Started && this->IPv6Socket)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void TCPHandler::Handle(SOCKET client, USHORT side)
|
||||
{
|
||||
SOCKADDR_IN addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.S_un.S_addr = htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = htons(this->tcpPort);
|
||||
|
||||
WaitForSingleObject(this->tcpLock, INFINITE);
|
||||
auto data = this->tcpContext[side];
|
||||
ReleaseMutex(this->tcpLock);
|
||||
|
||||
auto remote = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (!remote)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::Handle] Create socket failed: %d\n", WSAGetLastError());
|
||||
|
||||
closesocket(client);
|
||||
return;
|
||||
}
|
||||
|
||||
if (connect(remote, (PSOCKADDR)&addr, sizeof(SOCKADDR_IN)) != NO_ERROR)
|
||||
{
|
||||
printf("[Redirector][TCPHandler::Handle] Connect to remote failed: %d\n", WSAGetLastError());
|
||||
|
||||
closesocket(client);
|
||||
closesocket(remote);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,33 @@
|
||||
#pragma once
|
||||
#ifndef TCPHANDLER_H
|
||||
#define TCPHANDLER_H
|
||||
#include <Windows.h>
|
||||
#include "Based.h"
|
||||
|
||||
typedef struct _TCPINFO {
|
||||
DWORD PID;
|
||||
SOCKADDR Target;
|
||||
} TCPINFO, * PTCPINFO;
|
||||
|
||||
typedef class TCPHandler
|
||||
{
|
||||
public:
|
||||
TCPHandler();
|
||||
~TCPHandler();
|
||||
TCPHandler(USHORT tcpPort);
|
||||
|
||||
BOOL init();
|
||||
void free();
|
||||
|
||||
HANDLE tcpLock = NULL;
|
||||
map<USHORT, PTCPINFO> tcpContext;
|
||||
private:
|
||||
void IPv4();
|
||||
void IPv6();
|
||||
void Handle(SOCKET client, USHORT side);
|
||||
|
||||
USHORT tcpPort = 0;
|
||||
|
||||
BOOL Started = FALSE;
|
||||
SOCKET IPv4Socket = NULL;
|
||||
SOCKET IPv6Socket = NULL;
|
||||
} *PTCPHandler;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1 +1,11 @@
|
||||
#include "UDPHandler.h"
|
||||
|
||||
BOOL UDPHandler::init()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void UDPHandler::free()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
#pragma once
|
||||
#ifndef UDPHANDLER_H
|
||||
#define UDPHANDLER_H
|
||||
#include "Based.h"
|
||||
|
||||
typedef struct _UDPINFO {
|
||||
SOCKET Socket;
|
||||
} UDPINFO, * PUDPINFO;
|
||||
|
||||
typedef class UDPHandler
|
||||
{
|
||||
public:
|
||||
BOOL init();
|
||||
void free();
|
||||
} *PUDPHandler;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,43 +1 @@
|
||||
#include "Utils.h"
|
||||
|
||||
#include "Data.h"
|
||||
|
||||
USHORT IPv4Checksum(PBYTE buffer, ULONG64 size)
|
||||
{
|
||||
UINT32 sum = 0;
|
||||
for (int i = 0; i < size; i += 2)
|
||||
{
|
||||
sum += (buffer[i] << 8) + buffer[i + 1];
|
||||
}
|
||||
|
||||
if ((size % 2) == 1)
|
||||
{
|
||||
sum += buffer[size - 1] << 8;
|
||||
}
|
||||
|
||||
while (sum > 0xffff)
|
||||
{
|
||||
sum = (sum >> 16) + (sum & 0xffff);
|
||||
}
|
||||
|
||||
return ~sum & 0xffff;
|
||||
}
|
||||
|
||||
USHORT ICMPChecksum(PBYTE buffer, ULONG64 size)
|
||||
{
|
||||
UINT32 sum = 0;
|
||||
for (int i = 0; i < size; i += 2)
|
||||
{
|
||||
sum += buffer[i] + (buffer[i + 1] << 8);
|
||||
}
|
||||
|
||||
if ((size % 2) == 1)
|
||||
{
|
||||
sum += buffer[size - 1];
|
||||
}
|
||||
|
||||
sum = (sum >> 16) + (sum & 0xffff);
|
||||
sum += (sum >> 16);
|
||||
|
||||
return ~sum & 0xffff;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
#pragma once
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
#include <Windows.h>
|
||||
|
||||
USHORT IPv4Checksum(PBYTE buffer, ULONG64 size);
|
||||
USHORT ICMPChecksum(PBYTE buffer, ULONG64 size);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user