123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- #include "stdafx.h"
- #include "LYFZReceiveMsgApp.h"
- #include "ApplicationDlg.h"
- #include "LYFZReceiveMsg.h"
- #include "MyLock.h"
- #include <wincrypt.h>
- #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
- #define KEYLENGTH 0x00800000
- #define ENCRYPT_ALGORITHM CALG_RC4
- #define ENCRYPT_BLOCK_SIZE 8
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CDatabase *g_curdb=NULL;
- CDatabase g_db;
- CString g_mainpath;
- SENDHEAD g_sendhead;
- CString g_localname;
- BEGIN_MESSAGE_MAP(CLYFZReceiveMsgApp, CWinApp)
-
- ON_COMMAND(ID_APP_SHOW, OnAppShow)
- ON_COMMAND(ID_APP_EXIT, OnAppExit)
- ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
- ON_COMMAND(ID_HELP_INDEX, OnHelpIndex)
-
- ON_COMMAND(ID_HELP, CWinApp::OnHelp)
- END_MESSAGE_MAP()
- CLYFZReceiveMsgApp::CLYFZReceiveMsgApp()
- {
-
-
- }
- CLYFZReceiveMsgApp theApp;
- CLYFZReceiveMsg theServer;
- int g_port=8380;
- extern void Test();
- BOOL CLYFZReceiveMsgApp::InitInstance()
- {
- SetRegistryKey(_T("LYFZ-MSGSend"));
- CString version = _T ("Version 1.0");
- if(GetProfileInt (version, "debug", 0))
- return 0;
- if(m_lpCmdLine[0] == _T('\0'))
- {
- return false;
- }
- else
- {
- g_port=8380+atoi(m_lpCmdLine);
- }
- #if 1
- HANDLE hObject = CreateMutex(NULL,FALSE,_T("lyfzReceiveMsg"));
- if(GetLastError() == ERROR_ALREADY_EXISTS)
- {
- return false;
- }
- g_port=8381;
- #endif
- char path[MAX_PATH];
- ::GetModuleFileName (NULL, path, MAX_PATH);
- g_mainpath=path;
- g_mainpath=g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
- BOOL bMasterDb=0;
- BOOL bUserDb=0;
- char server[50];
- DWORD leng=50;
- memset(server, 0, 50);
- ::GetComputerName (server, &leng);
- g_localname=server;
- Global::GetIniInfo();
- CDatabase g_masterdb;
- ll: try
- {
- CString strCon;
- if (Global::g_dwDBServerPort != 0)
- {
- if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 )
- strCon.Format(DB_SW_CONN_WITH_PORT, Global::g_szDBSource, Global::g_dwDBServerPort,_T("master"));
- else
- strCon.Format(DB_SS_CONN_WITH_PORT,Global::g_szDBSource, Global::g_dwDBServerPort, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord);
- }
- else
- {
- if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 )
- strCon.Format(DB_SW_CONN_WITHOUT_PORT, Global::g_szDBSource, _T("master"));
- else
- strCon.Format(DB_SS_CONN_WITHOUT_PORT,Global::g_szDBSource, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord);
- }
- g_masterdb.OpenEx(strCon,CDatabase::noOdbcDialog);
- bMasterDb=1;
- }
- catch(CDBException * e)
- {
- }
- if(bMasterDb==0)
- {
- WriteLogin("sql数据库未启动!");
- ::Sleep (1000);
- goto ll;
- }
- try
- {
- g_db.OpenEx(Global::g_szConnectString,CDatabase::noOdbcDialog);
- bUserDb=1;
- g_curdb=&g_db;
- g_masterdb.Close();
- }
- catch(CDBException * e)
- {
- WriteLogin("数据库连接失败-"+e->m_strError);
- e->Delete();
- ::Sleep (1000);
- goto ll;
- }
- SetRegistryKey(_T("LYFZ-LYFZReceiveMsg"));
-
-
-
-
- #ifdef _AFXDLL
- Enable3dControls();
- #else
- Enable3dControlsStatic();
- #endif
- InitCommonControls();
-
- if (!m_TrayIcon.Create(WM_ICON_NOTIFY,
- _T("利亚方舟-短信接收"),
- LoadIcon(IDI_LYFZReceiveMsg),
- IDR_POPUP_MENU))
- return -1;
- int nLogLevel = GetProfileInt("Settings", "LogLevel" , 1);
- CApplicationDlg dlg;
- m_pMainWnd = &dlg;
- int nResponse = dlg.DoModal();
- if (nResponse == IDOK)
- {
-
-
- }
- else if (nResponse == IDCANCEL)
- {
-
-
- }
-
-
- return FALSE;
- }
- void CLYFZReceiveMsgApp::OnAppShow()
- {
- if (m_pMainWnd)
- m_pMainWnd->ShowWindow(SW_SHOW);
- }
- void CLYFZReceiveMsgApp::OnAppExit()
- {
- m_pMainWnd->DestroyWindow();
- }
- void CLYFZReceiveMsgApp::OnAppAbout()
- {
-
-
-
-
-
- }
- void CLYFZReceiveMsgApp::OnHelpIndex()
- {
-
-
- }
- BOOL AddConn(CDatabase *m_conndb)
- {
- try
- {
- m_conndb->OpenEx(Global::g_szConnectString,CDatabase::noOdbcDialog);
- }
- catch(CDBException * e)
- {
- return 0;
- }
- return 1;
- }
- BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
- {
- try
- {
- HCRYPTPROV hCryptProv;
- HCRYPTKEY hKey;
- HCRYPTHASH hHash;
- PBYTE pbBuffer;
- DWORD dwBlockLen;
- DWORD dwBufferLen;
- DWORD dwCount;
-
- if(CryptAcquireContext(
- &hCryptProv,
- NULL,
- NULL,
- PROV_RSA_FULL,
- 0))
- {
- printf("A cryptographic provider has been acquired. \n");
- }
- else
- {
- if(CryptAcquireContext(
- &hCryptProv,
- NULL,
- NULL,
- PROV_RSA_FULL,
- CRYPT_NEWKEYSET))
- {
-
- printf("A new key container has been created.\n");
- }
- else
- {
- return 0;
- }
- }
-
-
-
-
-
-
-
- if(CryptCreateHash(
- hCryptProv,
- CALG_MD5,
- 0,
- 0,
- &hHash))
- {
- printf("A hash object has been created. \n");
- }
- else
- {
- return 0;
- }
-
-
- if(CryptHashData(
- hHash,
- (BYTE *)szPassword,
- strlen(szPassword),
- 0))
- {
- printf("The password has been added to the hash. \n");
- }
- else
- {
- return 0;
- }
-
-
- if(CryptDeriveKey(
- hCryptProv,
- ENCRYPT_ALGORITHM,
- hHash,
- KEYLENGTH,
- &hKey))
- {
- printf("An encryption key is derived from the password hash. \n");
- }
- else
- {
- return 0;
- }
-
-
- CryptDestroyHash(hHash);
- hHash = NULL;
-
-
-
-
-
-
- dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
-
-
-
- if(ENCRYPT_BLOCK_SIZE > 1)
- dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
- else
- dwBufferLen = dwBlockLen;
- dwCount=dwBufferLen;
-
-
- int count;
-
- count=leng/dwBufferLen;
-
-
- int i ;
- for( i=0; i<count; i++)
- {
- pbBuffer=buffer+i*dwBufferLen;
-
- if(!CryptEncrypt(
- hKey,
- 0,
- 0,
-
- 0,
- pbBuffer,
- &dwCount,
- dwBufferLen))
- {
- return 0;
- }
- }
- if(leng%dwBlockLen)
- {
- pbBuffer=buffer+i*dwBufferLen;
- dwCount=leng-i*dwBufferLen;
- if(!CryptEncrypt(
- hKey,
- 0,
- TRUE,
-
- 0,
- pbBuffer,
- &dwCount,
- dwBufferLen))
- {
- return 0;
- }
- }
-
-
- if(hKey)
- CryptDestroyKey(hKey);
-
-
- if(hHash)
- CryptDestroyHash(hHash);
-
-
- if(hCryptProv)
- CryptReleaseContext(hCryptProv, 0);
- return(TRUE);
- }
- catch(...)
- {
- }
- }
- void WriteLogin(CString str)
- {
- MyLock lock("LYFZReceiveMsgWriteLogin");
- CStdioFile fp;
- CString path=g_mainpath+"\\LYFZReceiveMsg.txt";
- if(::PathFileExists (path))
- {
- fp.Open (path, CFile::modeWrite);
- int length=fp.GetLength ();
- if(length>1024*1024)
- {
- fp.Close ();
- ::DeleteFile (path);
- return;
- }
- fp.SeekToEnd ();
- }
- else
- fp.Open (path, CFile::modeCreate|CFile::modeWrite);
- fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
- fp.Close ();
- }
- void WriteLogin2(CString str)
- {
- MyLock lock("LYFZReceiveMsgWriteLogin");
- CStdioFile fp;
- CString path=g_mainpath+"\\LYFZReceiveMsg.txt";
- if(::PathFileExists (path))
- {
- fp.Open (path, CFile::modeWrite);
- int length=fp.GetLength ();
- if(length>1024*1024)
- {
- fp.Close ();
- ::DeleteFile (path);
- return;
- }
- fp.SeekToEnd ();
- }
- else
- fp.Open (path, CFile::modeCreate|CFile::modeWrite);
- fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
- fp.Close ();
- }
- void WriteError(CString str)
- {
- MyLock lock("LYFZReceiveMsgWriteError");
- CStdioFile fp;
- CString path=g_mainpath+"\\MsgError.txt";
- if(::PathFileExists (path))
- {
- fp.Open (path, CFile::modeWrite);
- int length=fp.GetLength ();
- if(length>1024*1024)
- {
- fp.Close ();
- ::DeleteFile (path);
- return;
- }
- fp.SeekToEnd ();
- }
- else
- fp.Open (path, CFile::modeCreate|CFile::modeWrite);
- fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
- fp.Close ();
- }
- void WritePswWrong(CString str)
- {
- MyLock lock("LYFZReceiveMsgWritePswWrong");
- CStdioFile fp;
- CString path=g_mainpath+"\\PswWrong.txt";
- if(::PathFileExists (path))
- {
- fp.Open (path, CFile::modeWrite);
- int length=fp.GetLength ();
- if(length>1024*1024)
- {
- fp.Close ();
- ::DeleteFile (path);
- return;
- }
- fp.SeekToEnd ();
- }
- else
- fp.Open (path, CFile::modeCreate|CFile::modeWrite);
- fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
- fp.Close ();
- }
- void WriteNullBalance(CString str)
- {
- MyLock lock("LYFZReceiveMsgWriteNullBalance");
- CStdioFile fp;
- CString path=g_mainpath+"\\NullBalance.txt";
- if(::PathFileExists (path))
- {
- fp.Open (path, CFile::modeWrite);
- int length=fp.GetLength ();
- if(length>1024*1024)
- {
- fp.Close ();
- ::DeleteFile (path);
- return;
- }
- fp.SeekToEnd ();
- }
- else
- fp.Open (path, CFile::modeCreate|CFile::modeWrite);
- fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
- fp.Close ();
- }
- void WriteUnallowIP(CString str)
- {
- CStdioFile fp;
- CString path=g_mainpath+"\\UnallowIP.txt";
- if(::PathFileExists (path))
- {
- fp.Open (path, CFile::modeWrite);
- int length=fp.GetLength ();
- if(length>1024*1024)
- {
- fp.Close ();
- ::DeleteFile (path);
- return;
- }
- fp.SeekToEnd ();
- }
- else
- fp.Open (path, CFile::modeCreate|CFile::modeWrite);
- fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
- fp.Close ();
- }
|