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:
@@ -1,231 +0,0 @@
|
||||
#include "DNS.h"
|
||||
|
||||
#include "Data.h"
|
||||
#include "Utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
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<PDNSPKT> 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);
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef DNS_H
|
||||
#define DNS_H
|
||||
#include <Windows.h>
|
||||
|
||||
#include <nfapi.h>
|
||||
|
||||
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
|
||||
90
Redirector/DNSHandler.cpp
Normal file
90
Redirector/DNSHandler.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
43
Redirector/DNSHandler.h
Normal file
43
Redirector/DNSHandler.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#ifndef DNSHANDLER_H
|
||||
#define DNSHANDLER_H
|
||||
#include <Windows.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <nfapi.h>
|
||||
|
||||
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<PDNSPKT> DNSList;
|
||||
} *PDNSHandler;
|
||||
|
||||
#endif
|
||||
@@ -1,18 +1,23 @@
|
||||
#include "EventHandler.h"
|
||||
|
||||
#include "DNS.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;
|
||||
|
||||
@@ -28,11 +33,14 @@ typedef struct _UDPINFO {
|
||||
vector<wstring> handleList;
|
||||
vector<wstring> bypassList;
|
||||
|
||||
HANDLE TCPLock = NULL;
|
||||
HANDLE UDPLock = NULL;
|
||||
mutex TCPLock;
|
||||
mutex UDPLock;
|
||||
map<ENDPOINT_ID, PTCPINFO> TCPContext;
|
||||
map<ENDPOINT_ID, PUDPINFO> 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<mutex> tlg(TCPLock);
|
||||
lock_guard<mutex> 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()
|
||||
|
||||
@@ -97,17 +97,21 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Data.cpp" />
|
||||
<ClCompile Include="DNS.cpp" />
|
||||
<ClCompile Include="DNSHandler.cpp" />
|
||||
<ClCompile Include="EventHandler.cpp" />
|
||||
<ClCompile Include="IPEventHandler.cpp" />
|
||||
<ClCompile Include="Redirector.cpp" />
|
||||
<ClCompile Include="TCPHandler.cpp" />
|
||||
<ClCompile Include="UDPHandler.cpp" />
|
||||
<ClCompile Include="Utils.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Data.h" />
|
||||
<ClInclude Include="DNS.h" />
|
||||
<ClInclude Include="DNSHandler.h" />
|
||||
<ClInclude Include="EventHandler.h" />
|
||||
<ClInclude Include="IPEventHandler.h" />
|
||||
<ClInclude Include="TCPHandler.h" />
|
||||
<ClInclude Include="UDPHandler.h" />
|
||||
<ClInclude Include="Utils.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -26,7 +26,13 @@
|
||||
<ClCompile Include="IPEventHandler.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="DNS.cpp">
|
||||
<ClCompile Include="DNSHandler.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="TCPHandler.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="UDPHandler.cpp">
|
||||
<Filter>Source</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
@@ -43,7 +49,13 @@
|
||||
<ClInclude Include="IPEventHandler.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="DNS.h">
|
||||
<ClInclude Include="DNSHandler.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TCPHandler.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="UDPHandler.h">
|
||||
<Filter>Header</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
||||
21
Redirector/TCPHandler.cpp
Normal file
21
Redirector/TCPHandler.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "TCPHandler.h"
|
||||
|
||||
TCPHandler::TCPHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TCPHandler::~TCPHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
BOOL TCPHandler::init()
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void TCPHandler::free()
|
||||
{
|
||||
|
||||
}
|
||||
16
Redirector/TCPHandler.h
Normal file
16
Redirector/TCPHandler.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#ifndef TCPHANDLER_H
|
||||
#define TCPHANDLER_H
|
||||
#include <Windows.h>
|
||||
|
||||
typedef class TCPHandler
|
||||
{
|
||||
public:
|
||||
TCPHandler();
|
||||
~TCPHandler();
|
||||
|
||||
BOOL init();
|
||||
void free();
|
||||
} *PTCPHandler;
|
||||
|
||||
#endif
|
||||
1
Redirector/UDPHandler.cpp
Normal file
1
Redirector/UDPHandler.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "UDPHandler.h"
|
||||
5
Redirector/UDPHandler.h
Normal file
5
Redirector/UDPHandler.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#ifndef UDPHANDLER_H
|
||||
#define UDPHANDLER_H
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#define UTILS_H
|
||||
#include <Windows.h>
|
||||
|
||||
DWORD GetCPUCount();
|
||||
USHORT IPv4Checksum(PBYTE buffer, ULONG64 size);
|
||||
USHORT ICMPChecksum(PBYTE buffer, ULONG64 size);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user