Support RSA encrypt.

This commit is contained in:
floyd 2015-07-15 17:23:49 +08:00
parent 6877f94195
commit be978505b0
20 changed files with 369 additions and 108 deletions

View File

@ -151,8 +151,12 @@ void CGeneratorDlg::InitDlgEdit()
m_DefaultComm.SetItemData(1,COMMNAME_HTTPS);
m_DefaultComm.InsertString(2,_T("TCP"));
m_DefaultComm.SetItemData(2,COMMNAME_TCP);
m_DefaultComm.InsertString(2,_T("UDP"));
m_DefaultComm.SetItemData(2,COMMNAME_UDP);
m_DefaultComm.InsertString(3,_T("TCPS"));
m_DefaultComm.SetItemData(3,COMMNAME_TCPS);
m_DefaultComm.InsertString(4,_T("UDP"));
m_DefaultComm.SetItemData(4,COMMNAME_UDP);
m_DefaultComm.InsertString(5,_T("UDPS"));
m_DefaultComm.SetItemData(5,COMMNAME_UDPS);
m_DefaultComm.SetCurSel(config.commType);

View File

@ -27,7 +27,9 @@ BOOL CommManager::Init()
m_commList[COMMNAME_HTTP] = new HttpComm;
m_commList[COMMNAME_HTTPS] = new HttpComm(TRUE);
m_commList[COMMNAME_TCP] = new TcpComm;
m_commList[COMMNAME_TCPS] = new TcpComm(TRUE);
m_commList[COMMNAME_UDP] = new UdpComm;
m_commList[COMMNAME_UDPS] = new UdpComm(TRUE);
m_hExitEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
if (! m_hExitEvent.IsValid())
@ -204,10 +206,18 @@ BOOL CommManager::Str2Commname( LPCTSTR str, COMM_NAME& commName ) const
{
commName = COMMNAME_TCP;
}
else if (_tcscmp(str, _T("tcps")) == 0)
{
commName = COMMNAME_TCP;
}
else if (_tcscmp(str, _T("udp")) == 0)
{
commName = COMMNAME_UDP;
}
else if (_tcscmp(str, _T("udps")) == 0)
{
commName = COMMNAME_UDP;
}
return (COMMNAME_MAX != commName);
@ -226,9 +236,15 @@ BOOL CommManager::Commname2Str( COMM_NAME commName, tstring& str ) const
case COMMNAME_TCP:
str = _T("tcp");
break;
case COMMNAME_TCPS:
str = _T("tcps");
break;
case COMMNAME_UDP:
str = _T("udp");
break;
case COMMNAME_UDPS:
str = _T("udps");
break;
default:
str = _T("invalid");
return FALSE;
@ -244,7 +260,7 @@ BOOL CommManager::StartMessageWorker( ULONG testIntervalMS, DWORD dwSendInterval
m_bWorking = TRUE;
m_dwMsgIntervalMS = dwSendIntervalMS;
if (! IsCommAvailable(COMMNAME_DEFAULT))/* SendCommTestMessages()*/;
// if (! IsCommAvailable(COMMNAME_DEFAULT))/* SendCommTestMessages()*/;
return m_messageSenderThread.Start(MessageSender, this);
}

View File

@ -7,7 +7,7 @@
class HttpComm : public IComm
{
public:
HttpComm(BOOL isSSL = FALSE);
HttpComm(BOOL isSecure = FALSE);
~HttpComm();
//实现IComm接口
virtual COMM_NAME GetName() { if (m_ssl) return COMMNAME_HTTPS; else COMMNAME_HTTP; };

View File

@ -1,6 +1,19 @@
#include "stdafx.h"
#include "socket/MySocket.h"
#include "TcpComm.h"
#include "../shell/Exports.h"
TcpComm::TcpComm(BOOL isSecure):
m_xorKey1(0),
m_xorKey2(0)
{
m_isSecure = isSecure;
}
TcpComm::~TcpComm()
{
}
BOOL TcpComm::Send( ULONG targetIP, const LPBYTE pData, DWORD dwSize )
{
@ -12,37 +25,54 @@ BOOL TcpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
TCP_HEADER sendHead;
sendHead.flag = TCP_FLAG;
sendHead.nSize = dwSendSize;
BOOL ret = FALSE;
if (! Send(m_sock, targetIP, (PBYTE)&sendHead, sizeof(TCP_HEADER))) return FALSE;
if (! Send(m_sock, targetIP, pSendData, dwSendSize)) return FALSE;
TCP_HEADER recvHead = {0};
int iRecv = m_sock.ReceiveAll((LPBYTE)&recvHead, sizeof(TCP_HEADER));
if (iRecv < 0)
do
{
errorLog(_T("recv http failed WE%d"), ::WSAGetLastError());
}
ret = Send(m_sock, targetIP, (PBYTE)&sendHead, sizeof(TCP_HEADER));
if (!ret)
break;;
if (m_isSecure)
XFC(pSendData,dwSendSize,pSendData,m_xorKey1,m_xorKey2);
ret = Send(m_sock, targetIP, pSendData, dwSendSize);
if (!ret)
break;
TCP_HEADER recvHead = {0};
ret = m_sock.ReceiveAll((LPBYTE)&recvHead, sizeof(TCP_HEADER));
if ( !ret )
break;
ByteBuffer buffer;
buffer.Alloc(recvHead.nSize);
ByteBuffer buffer;
buffer.Alloc(recvHead.nSize);
ret = m_sock.ReceiveAll(buffer,recvHead.nSize);
iRecv = m_sock.ReceiveAll(buffer,recvHead.nSize);
if (!ret)
{
buffer.Free();
break;
}
if (iRecv < 0)
{
errorLog(_T("recv tcp failed WE%d"), ::WSAGetLastError());
}
//¸´ÖÆÊý¾Ý
*pRecvData = Alloc(recvHead.nSize);
memcpy(*pRecvData, (LPBYTE)buffer, recvHead.nSize);
dwRecvSize = recvHead.nSize;
//¸´ÖÆÊý¾Ý
*pRecvData = Alloc(recvHead.nSize);
memcpy(*pRecvData, (LPBYTE)buffer, recvHead.nSize);
dwRecvSize = recvHead.nSize;
if(m_isSecure)
XFC(*pRecvData,recvHead.nSize,*pRecvData,m_xorKey1,m_xorKey2);
buffer.Free();
} while (FALSE);
return TRUE;
return ret;
}
BOOL TcpComm::Connect( ULONG targetIP, MySocket& sock )
@ -59,16 +89,22 @@ BOOL TcpComm::Connect( ULONG targetIP, MySocket& sock )
errorLog(_T("connect [%u] failed"), targetIP);
return FALSE;
}
if (m_isSecure)
{
int key1 = 0;
int key2 = 0;
int value = 65535;
if (0 != setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&value, sizeof(value)))
{
errorLog(_T("setsockopt rcvbuf failed.WE%d"), ::WSAGetLastError());
}
value = 65535;
if (0 != setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&value, sizeof(value)))
{
errorLog(_T("setsockopt sndbuf failed.WE%d"), ::WSAGetLastError());
int flag = TCP_FLAG;
m_sock.SendAll((LPVOID)&flag,sizeof(int));
m_sock.ReceiveAll(&m_rsaKey,sizeof(RSA::RSA_PUBLIC_KEY));
RSA::RSAEncrypt((char*)&m_xorKey1,(int*)&key1,m_rsaKey.d,m_rsaKey.n,1);
RSA::RSAEncrypt((char*)&m_xorKey2,(int*)&key2,m_rsaKey.d,m_rsaKey.n,1);
m_sock.SendAll(&key1,sizeof(int));
m_sock.SendAll(&key2,sizeof(int));
}
return TRUE;

View File

@ -1,4 +1,5 @@
#pragma once
#include "rsa/librsa.h"
#include "socket/MySocket.h"
#include "IComm.h"
#include "TcpDefines.h"
@ -7,6 +8,9 @@
class TcpComm : public IComm
{
public:
TcpComm(BOOL isSecure = FALSE);
~TcpComm();
//实现IComm接口
virtual COMM_NAME GetName() {return COMMNAME_TCP; };
virtual DWORD GetMaxDataSizePerPacket() {return TCP_COMM_REQUEST_MAXSIZE;};
@ -19,4 +23,10 @@ private:
private:
MySocket m_sock;
RSA::RSA_PUBLIC_KEY m_rsaKey;
BYTE m_xorKey1;
BYTE m_xorKey2;
BOOL m_isSecure;
};

View File

@ -7,7 +7,9 @@
#include "file/MyFile.h"
#include "VtcpBinary.h"
UdpComm::UdpComm(void):m_isConnected(FALSE)
UdpComm::UdpComm(BOOL isSecure):m_isConnected(FALSE),
m_xorKey1(0),
m_xorKey2(0)
{
m_vtcp.MemLoadLibrary(mem_vtcp,sizeof(mem_vtcp));
@ -19,6 +21,15 @@ UdpComm::UdpComm(void):m_isConnected(FALSE)
m_vclose = (_vtcp_close)m_vtcp.MemGetProcAddress("vtcp_close");
m_vstartup();
if (isSecure)
{
srand(GetTickCount());
m_xorKey1 = (BYTE)(rand() % 255);
m_xorKey2 = (BYTE)(rand() % 255);
m_isSecure = isSecure;
}
}
UdpComm::~UdpComm(void)
@ -101,6 +112,9 @@ BOOL UdpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
{
if (! Send( targetIP, (PBYTE)&sendHead, sizeof(UDP_HEADER))) break;
if (m_isSecure)
XFC(pSendData,dwSendSize,pSendData,m_xorKey1,m_xorKey2);
if (! Send( targetIP, pSendData, dwSendSize)) break;
UDP_HEADER recvHead = {0};
@ -118,7 +132,7 @@ BOOL UdpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
if (! ReceiveAll(m_sock,(LPBYTE)buffer,recvHead.nSize))
{
m_isConnected = FALSE;
errorLog(_T("recv udp failed WE%d"), ::WSAGetLastError());
buffer.Free();
break;
}
@ -127,6 +141,11 @@ BOOL UdpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSendS
memcpy(*pRecvData, (LPBYTE)buffer, recvHead.nSize);
dwRecvSize = recvHead.nSize;
if(m_isSecure)
XFC(*pRecvData,recvHead.nSize,*pRecvData,m_xorKey1,m_xorKey2);
buffer.Free();
ret = TRUE;
} while (FALSE);
@ -152,6 +171,24 @@ BOOL UdpComm::Connect( ULONG targetIP,int port )
return FALSE;
}
if (m_isSecure)
{
int key1 = 0;
int key2 = 0;
int flag = UDP_FLAG;
SendAll(m_sock,(LPVOID)&flag,sizeof(int));
ReceiveAll(m_sock,&m_rsaKey,sizeof(RSA::RSA_PUBLIC_KEY));
RSA::RSAEncrypt((char*)&m_xorKey1,(int*)&key1,m_rsaKey.d,m_rsaKey.n,1);
RSA::RSAEncrypt((char*)&m_xorKey2,(int*)&key2,m_rsaKey.d,m_rsaKey.n,1);
SendAll(m_sock,&key1,sizeof(int));
SendAll(m_sock,&key2,sizeof(int));
}
return TRUE;
}

View File

@ -3,13 +3,14 @@
#include <Winsock2.h>
#include "memdll/MemLoadDll.h"
#include "MessageDefines.h"
#include "rsa/librsa.h"
#include "vtcp/vtcp.h"
#include "../shell/Exports.h"
class UdpComm: public IComm
{
public:
UdpComm(void);
UdpComm(BOOL isSecure = FALSE);
~UdpComm(void);
public:
@ -46,5 +47,12 @@ private:
BOOL m_isConnected;
CMemLoadDll m_vtcp;
private:
RSA::RSA_PUBLIC_KEY m_rsaKey;
BYTE m_xorKey1;
BYTE m_xorKey2;
BOOL m_isSecure;
};

View File

@ -175,6 +175,7 @@
<ClInclude Include="..\..\..\base\include\md5\md5.h" />
<ClInclude Include="..\..\..\base\include\memdll\MemLoadDll.h" />
<ClInclude Include="..\..\..\base\include\ods.h" />
<ClInclude Include="..\..\..\base\include\rsa\librsa.h" />
<ClInclude Include="..\..\..\base\include\socket\MySocket.h" />
<ClInclude Include="..\..\..\base\include\tstring.h" />
<ClInclude Include="..\..\..\base\include\winhttp\http.h" />
@ -218,6 +219,7 @@
<ClCompile Include="..\..\..\base\include\md5\md5.cpp" />
<ClCompile Include="..\..\..\base\include\memdll\MemLoadDll.cpp" />
<ClCompile Include="..\..\..\base\include\ods.cpp" />
<ClCompile Include="..\..\..\base\include\rsa\librsa.cpp" />
<ClCompile Include="..\..\..\base\include\socket\MySocket.cpp" />
<ClCompile Include="..\..\..\base\include\tstring.cpp" />
<ClCompile Include="..\..\..\base\include\winhttp\http.cpp" />

View File

@ -67,6 +67,9 @@
<Filter Include="comm\udp">
<UniqueIdentifier>{733f9141-9f46-4a37-9272-b68b1dfec55e}</UniqueIdentifier>
</Filter>
<Filter Include="include\rsa">
<UniqueIdentifier>{85040103-af49-463f-ad4b-d6be372c78f1}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="body.def">
@ -207,6 +210,9 @@
<ClInclude Include="VtcpBinary.h">
<Filter>comm\udp</Filter>
</ClInclude>
<ClInclude Include="..\..\..\base\include\rsa\librsa.h">
<Filter>include\rsa</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@ -293,6 +299,9 @@
<ClCompile Include="UdpComm.cpp">
<Filter>comm\udp</Filter>
</ClCompile>
<ClCompile Include="..\..\..\base\include\rsa\librsa.cpp">
<Filter>include\rsa</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="body.rc">

View File

@ -226,7 +226,7 @@ SHELL_API BOOL Init(BOOL bWait)
debugLog(_T("init servantshell. filepath is %s%s"), GetBinFilepath(), GetBinFilename());
#ifdef _DEBUG
g_ConfigInfo.nDefaultCommType = COMMNAME_UDP;
g_ConfigInfo.nDefaultCommType = COMMNAME_UDPS;
g_ConfigInfo.nPort = 8082;
g_ConfigInfo.nFirstConnectHour = -1;
g_ConfigInfo.nFirstConnectMinute = -1;

View File

@ -5,7 +5,9 @@ typedef enum
{
COMMNAME_DEFAULT = 0,
COMMNAME_TCP,
COMMNAME_TCPS,
COMMNAME_UDP,
COMMNAME_UDPS,
COMMNAME_HTTP,
COMMNAME_HTTPS,
COMMNAME_MAX,

View File

@ -51,8 +51,14 @@ void CListenDlg::InitView()
m_protoList.InsertString(2,_T("TCP"));
m_protoList.SetItemData(2,COMMNAME_TCP);
m_protoList.InsertString(3,_T("UDP"));
m_protoList.SetItemData(3,COMMNAME_UDP);
m_protoList.InsertString(3,_T("TCPS"));
m_protoList.SetItemData(3,COMMNAME_TCPS);
m_protoList.InsertString(4,_T("UDP"));
m_protoList.SetItemData(4,COMMNAME_UDP);
m_protoList.InsertString(5,_T("UDPS"));
m_protoList.SetItemData(5,COMMNAME_UDPS);
m_protoList.SetCurSel(0);
@ -131,10 +137,18 @@ void CListenDlg::OnBnClickedButtonStart()
{
strProtocol = _T("TCP");
}
else if (data == COMMNAME_TCPS)
{
strProtocol = _T("TCPS");
}
else if (data == COMMNAME_UDP)
{
strProtocol = _T("UDP");
}
else if (data == COMMNAME_UDPS)
{
strProtocol = _T("UDPS");
}
m_listenList.InsertItem(nCount,strProtocol,0);
m_listenList.SetItemText(nCount,1,strPort);
@ -217,22 +231,22 @@ void CListenDlg::OnCbnSelchangeComboProto()
if(data == COMMNAME_HTTP)
{
SetDlgItemText(IDC_EDIT_PORT,_T("80"));
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(TRUE);
}
else if(data == COMMNAME_HTTPS)
{
SetDlgItemText(IDC_EDIT_PORT,_T("443"));
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(TRUE);
}
else if(data == COMMNAME_TCP)
{
SetDlgItemText(IDC_EDIT_PORT,_T("8082"));
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(TRUE);
}
else if(data == COMMNAME_UDP)
{
SetDlgItemText(IDC_EDIT_PORT,_T("8082"));
GetDlgItem(IDC_EDIT_PORT)->EnableWindow(TRUE);
}
else
{
SetDlgItemText(IDC_EDIT_PORT,_T("8082"));
}
}

View File

@ -345,9 +345,15 @@ void ClientInfoManager::TransferInfo( LPCTSTR clientid, const CLIENT_BASE_INFO*
case COMMNAME_TCP:
_tcscpy_s(info.proto,_T("TCP"));
break;
case COMMNAME_TCPS:
_tcscpy_s(info.proto,_T("TCPS"));
break;
case COMMNAME_UDP:
_tcscpy_s(info.proto,_T("UDP"));
break;
case COMMNAME_UDPS:
_tcscpy_s(info.proto,_T("UDPS"));
break;
default:
break;
}

View File

@ -152,9 +152,10 @@ int CommManager::AddCommService(int port,int name)
break;
}
case COMMNAME_UDPS:
case COMMNAME_UDP:
{
CUdp *udp = new CUdp;
CUdp *udp = new CUdp(name == COMMNAME_UDP ? FALSE : TRUE);
udp->Init();
if (!udp->Start(port,UdpMsgHandler))
{
@ -171,9 +172,10 @@ int CommManager::AddCommService(int port,int name)
break;
}
case COMMNAME_TCPS:
case COMMNAME_TCP:
{
CTcp *tcp = new CTcp;
CTcp *tcp = new CTcp(name == COMMNAME_TCP ? FALSE : TRUE);
tcp->Init();
if (!tcp->Start(port,TcpMsgHandler))
{
@ -225,15 +227,18 @@ BOOL CommManager::DeleteCommService(int serialid)
break;
}
case COMMNAME_UDPS:
case COMMNAME_UDP:
{
CUdp *udp = (CUdp *)info.lpParameter1;
udp->Stop();
delete udp;
m_commMap.erase(it);
break;
}
case COMMNAME_TCPS:
case COMMNAME_TCP:
{
CTcp *tcp = (CTcp *)info.lpParameter1;

View File

@ -1,9 +1,18 @@
#include "StdAfx.h"
#include "Tcp.h"
#include "common.h"
CTcp::CTcp(void)
CTcp::CTcp(BOOL isSecure):
m_isSecure(FALSE)
{
if (isSecure)
{
RSA::GenRSAKey(&m_myPriKey,&m_myPubKey);
m_isSecure = isSecure;
srand(GetTickCount());
}
}
@ -61,8 +70,15 @@ bool CTcp::Start(int port , tcpHandler handler)
}
void CTcp::Worker(LPVOID lpParameter)
{
ARGV_LIST *argv = (ARGV_LIST*)lpParameter;
CTcp* tcp = (CTcp*)argv->lpParameter;
tcp->WorkerProc(lpParameter);
}
void CTcp::WorkerProc(LPVOID lpParameter)
{
ARGV_LIST *argv = (ARGV_LIST*)lpParameter;
@ -74,32 +90,63 @@ void CTcp::Worker(LPVOID lpParameter)
ByteBuffer toSender;
while(ret)
do
{
ret = socket.ReceiveAll(&header,sizeof(TCP_HEADER));
if (ret && header.flag == TCP_FLAG)
if (m_isSecure)
{
LPBYTE lpData = (LPBYTE)malloc(header.nSize);
int key1 = 0;
int key2 = 0;
ret = socket.ReceiveAll(lpData,header.nSize);
int flag = TCP_FLAG;
if ( ret )
socket.ReceiveAll(&flag,sizeof(int));
if(flag != TCP_FLAG )
break;
socket.SendAll(&m_myPubKey,sizeof(RSA::RSA_PUBLIC_KEY));
socket.ReceiveAll(&key1,sizeof(int));
socket.ReceiveAll(&key2,sizeof(int));
RSA::RSADecrypt((char*)&m_xorKey1,&key1,m_myPriKey.e,m_myPriKey.n,1);
RSA::RSADecrypt((char*)&m_xorKey2,&key2,m_myPriKey.e,m_myPriKey.n,1);
}
while(ret)
{
ret = socket.ReceiveAll(&header,sizeof(TCP_HEADER));
if (ret && header.flag == TCP_FLAG)
{
if (argv->handler(lpData,header.nSize,argv->sin,toSender))
LPBYTE lpData = (LPBYTE)malloc(header.nSize);
ret = socket.ReceiveAll(lpData,header.nSize);
if (m_isSecure)
XorFibonacciCrypt(lpData,header.nSize,lpData,m_xorKey1,m_xorKey2);
if ( ret )
{
header.nSize = toSender.Size();
socket.SendAll(&header,sizeof(TCP_HEADER));
socket.SendAll(toSender,toSender.Size());
if (argv->handler(lpData,header.nSize,argv->sin,toSender))
{
header.nSize = toSender.Size();
socket.SendAll(&header,sizeof(TCP_HEADER));
if (m_isSecure)
XorFibonacciCrypt(toSender,toSender.Size(),toSender,m_xorKey1,m_xorKey2);
socket.SendAll(toSender,toSender.Size());
}
}
free(lpData);
}
else
{
break;
}
free(lpData);
}
else
{
break;
}
}
} while (FALSE);
socket.Close();

View File

@ -1,4 +1,5 @@
#pragma once
#include "rsa/librsa.h"
#include "TcpDefines.h"
#include "socket/MySocket.h"
#include <WinSock2.h>
@ -18,7 +19,7 @@ typedef struct
class CTcp
{
public:
CTcp(void);
CTcp(BOOL isSecure = FALSE);
~CTcp(void);
typedef std::vector<SOCKET> VecSocket;
@ -37,8 +38,17 @@ private:
void ListenProc(ARGV_LIST *argv);
static void Worker(LPVOID lpParameter);
void WorkerProc(LPVOID lpParameter);
MySocket m_sock;
private:
RSA::RSA_PRIVATE_KEY m_myPriKey;
RSA::RSA_PUBLIC_KEY m_myPubKey;
BYTE m_xorKey1;
BYTE m_xorKey2;
BOOL m_isSecure;
};

View File

@ -1,14 +1,25 @@
#include "StdAfx.h"
#include "Udp.h"
#include <string>
#include "common.h"
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib,"vtcp.lib")
CUdp::CUdp(void)
CUdp::CUdp(BOOL isSecure):
m_isSecure(FALSE)
{
vtcp_startup();
if (isSecure)
{
srand(GetTickCount());
RSA::GenRSAKey(&m_myPriKey,&m_myPubKey);
m_isSecure = isSecure;
}
}
@ -108,21 +119,17 @@ BOOL CUdp::Start(int port, udpHandler handler)
void CUdp::Stop()
{
vtcp_close(m_sock);
m_cs.Enter();
VecSocket::iterator it = m_vecSock.begin();
for (; it != m_vecSock.end(); it++)
{
vtcp_close(*it);
}
m_cs.Leave();
}
void CUdp::Worker(LPVOID lpParameter)
{
UDP_ARGV *argv = (UDP_ARGV*)lpParameter;
CUdp* udp = (CUdp*)argv->lpParameter;
udp->WorkerProc(lpParameter);
}
void CUdp::WorkerProc(LPVOID lpParameter)
{
UDP_ARGV *argv = (UDP_ARGV*)lpParameter;
@ -134,32 +141,63 @@ void CUdp::Worker(LPVOID lpParameter)
ByteBuffer toSender;
while(ret)
do
{
int ret = ReceiveAll(socket,(char*)&header,sizeof(UDP_HEADER));
if (ret && header.flag == UDP_FLAG)
if (m_isSecure)
{
LPBYTE lpData = (LPBYTE)malloc(header.nSize);
int key1 = 0;
int key2 = 0;
ret = ReceiveAll(socket,(char*)lpData,header.nSize);
int flag = 0;
if ( ret )
ReceiveAll(socket,&flag,sizeof(int));
if (flag != UDP_FLAG)
break;
SendAll(socket,&m_myPubKey,sizeof(RSA::RSA_PUBLIC_KEY));
ReceiveAll(socket,&key1,sizeof(int));
ReceiveAll(socket,&key2,sizeof(int));
RSA::RSADecrypt((char*)&m_xorKey1,&key1,m_myPriKey.e,m_myPriKey.n,1);
RSA::RSADecrypt((char*)&m_xorKey2,&key2,m_myPriKey.e,m_myPriKey.n,1);
}
while(ret)
{
int ret = ReceiveAll(socket,(char*)&header,sizeof(UDP_HEADER));
if (ret && header.flag == UDP_FLAG)
{
if (argv->handler(lpData,header.nSize,argv->sin,toSender))
{
header.nSize = toSender.Size();
SendAll(socket,(char*)&header,sizeof(UDP_HEADER));
SendAll(socket,(char*)((LPBYTE)toSender),toSender.Size());
}
LPBYTE lpData = (LPBYTE)malloc(header.nSize);
ret = ReceiveAll(socket,(char*)lpData,header.nSize);
if (m_isSecure)
XorFibonacciCrypt(lpData,header.nSize,lpData,m_xorKey1,m_xorKey2);
if ( ret )
{
if (argv->handler(lpData,header.nSize,argv->sin,toSender))
{
header.nSize = toSender.Size();
SendAll(socket,(char*)&header,sizeof(UDP_HEADER));
if (m_isSecure)
XorFibonacciCrypt(toSender,toSender.Size(),toSender,m_xorKey1,m_xorKey2);
SendAll(socket,(char*)((LPBYTE)toSender),toSender.Size());
}
}
free(lpData);
}
else
{
break;
}
free(lpData);
}
else
{
break;
}
}
} while (FALSE);
vtcp_close(socket);
@ -178,10 +216,6 @@ void CUdp::ListenProc( UDP_ARGV *argv )
if (VTCP_INVALID_SOCKET == (fhandle = vtcp_accept(m_sock, (sockaddr *)&sin, &addrlen)))
break;
// m_cs.Enter();
// m_vecSock.push_back(fhandle);
// m_cs.Leave();
UDP_ARGV * client_argv = new UDP_ARGV;
client_argv->handler = argv->handler;

View File

@ -1,6 +1,7 @@
#pragma once
#include "vtcp/vtcp.h"
#include "UdpDefines.h"
#include "rsa/librsa.h"
typedef BOOL (*udpHandler)(LPBYTE data,DWORD size,SOCKADDR_IN sin,ByteBuffer& toSender);
@ -16,7 +17,7 @@ typedef struct
class CUdp
{
public:
CUdp(void);
CUdp(BOOL isSecure = FALSE);
~CUdp(void);
typedef std::vector<VTCP_SOCKET> VecSocket;
@ -35,9 +36,18 @@ private:
void ListenProc(UDP_ARGV *argv);
static void Worker(LPVOID lpParameter);
void WorkerProc(LPVOID lpParameter);
CriticalSection m_cs;
VecSocket m_vecSock;
private:
RSA::RSA_PRIVATE_KEY m_myPriKey;
RSA::RSA_PUBLIC_KEY m_myPubKey;
BYTE m_xorKey1;
BYTE m_xorKey2;
BOOL m_isSecure;
};

View File

@ -128,6 +128,7 @@
<ClInclude Include="..\..\..\base\include\md5\md5.h" />
<ClInclude Include="..\..\..\base\include\mongoose\mongoose.h" />
<ClInclude Include="..\..\..\base\include\ods.h" />
<ClInclude Include="..\..\..\base\include\rsa\librsa.h" />
<ClInclude Include="..\..\..\base\include\socket\MySocket.h" />
<ClInclude Include="..\..\..\base\include\thread\RepeatTask.h" />
<ClInclude Include="..\..\..\base\include\thread\ThreadArray.h" />
@ -179,6 +180,7 @@
<ClCompile Include="..\..\..\base\include\md5\md5.cpp" />
<ClCompile Include="..\..\..\base\include\mongoose\mongoose.cpp" />
<ClCompile Include="..\..\..\base\include\ods.cpp" />
<ClCompile Include="..\..\..\base\include\rsa\librsa.cpp" />
<ClCompile Include="..\..\..\base\include\socket\MySocket.cpp" />
<ClCompile Include="..\..\..\base\include\thread\RepeatTask.cpp" />
<ClCompile Include="..\..\..\base\include\thread\ThreadArray.cpp" />

View File

@ -67,6 +67,9 @@
<Filter Include="include\mongoose">
<UniqueIdentifier>{a937e001-c3a5-446e-bd28-6c42d7d263f3}</UniqueIdentifier>
</Filter>
<Filter Include="include\rsa">
<UniqueIdentifier>{fa7e5943-1059-41c7-a0b9-41c99b75dd49}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="ReadMe.txt" />
@ -303,6 +306,9 @@
<ClInclude Include="Udp.h">
<Filter>comm</Filter>
</ClInclude>
<ClInclude Include="..\..\..\base\include\rsa\librsa.h">
<Filter>include\rsa</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@ -404,6 +410,9 @@
<ClCompile Include="Udp.cpp">
<Filter>comm</Filter>
</ClCompile>
<ClCompile Include="..\..\..\base\include\rsa\librsa.cpp">
<Filter>include\rsa</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="master.rc">