LYFZReceiveMsgApp.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /****************************************************************/
  2. /* */
  3. /* LYFZReceiveMsgApp.cpp */
  4. /* */
  5. /* Defines the class behaviors for the application. */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Copyright LYFZ Software Solutions 2002 */
  9. /* http://www.LYFZvandermeer.nl */
  10. /* */
  11. /* Last updated: 10 july 2002 */
  12. /* */
  13. /****************************************************************/
  14. #include "stdafx.h"
  15. #include "LYFZReceiveMsgApp.h"
  16. #include "ApplicationDlg.h"
  17. #include "LYFZReceiveMsg.h"
  18. #include "MyLock.h"
  19. #include <wincrypt.h>
  20. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  21. #define KEYLENGTH 0x00800000
  22. #define ENCRYPT_ALGORITHM CALG_RC4
  23. #define ENCRYPT_BLOCK_SIZE 8
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. CDatabase *g_curdb=NULL;
  30. CDatabase g_db;
  31. CString g_mainpath;
  32. SENDHEAD g_sendhead;
  33. CString g_localname;
  34. BEGIN_MESSAGE_MAP(CLYFZReceiveMsgApp, CWinApp)
  35. //{{AFX_MSG_MAP(CLYFZReceiveMsgApp)
  36. ON_COMMAND(ID_APP_SHOW, OnAppShow)
  37. ON_COMMAND(ID_APP_EXIT, OnAppExit)
  38. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  39. ON_COMMAND(ID_HELP_INDEX, OnHelpIndex)
  40. //}}AFX_MSG_MAP
  41. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CLYFZReceiveMsgApp construction
  45. CLYFZReceiveMsgApp::CLYFZReceiveMsgApp()
  46. {
  47. // TODO: add construction code here,
  48. // Place all significant initialization in InitInstance
  49. }
  50. /////////////////////////////////////////////////////////////////////////////
  51. // The one and only CLYFZReceiveMsgApp object
  52. CLYFZReceiveMsgApp theApp;
  53. CLYFZReceiveMsg theServer;
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CLYFZReceiveMsgApp initialization
  56. int g_port=8380;
  57. extern void Test();
  58. BOOL CLYFZReceiveMsgApp::InitInstance()
  59. {
  60. SetRegistryKey(_T("LYFZ-MSGSend"));
  61. CString version = _T ("Version 1.0");
  62. if(GetProfileInt (version, "debug", 0))
  63. return 0;
  64. if(m_lpCmdLine[0] == _T('\0')) //没有命令行参数
  65. {
  66. return false;
  67. }
  68. else
  69. {
  70. g_port=8380+atoi(m_lpCmdLine);
  71. }
  72. #if 1 // 固定端口号,且单例运行;
  73. HANDLE hObject = CreateMutex(NULL,FALSE,_T("lyfzReceiveMsg"));
  74. if(GetLastError() == ERROR_ALREADY_EXISTS)
  75. {
  76. return false;
  77. }
  78. g_port=8381;
  79. #endif
  80. char path[MAX_PATH];
  81. ::GetModuleFileName (NULL, path, MAX_PATH);
  82. g_mainpath=path;
  83. g_mainpath=g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
  84. BOOL bMasterDb=0;
  85. BOOL bUserDb=0;
  86. char server[50];
  87. DWORD leng=50;
  88. memset(server, 0, 50);
  89. ::GetComputerName (server, &leng);
  90. g_localname=server;
  91. Global::GetIniInfo();
  92. CDatabase g_masterdb;
  93. ll: try
  94. {
  95. CString strCon;
  96. if (Global::g_dwDBServerPort != 0)
  97. {
  98. if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 )
  99. strCon.Format(DB_SW_CONN_WITH_PORT, Global::g_szDBSource, Global::g_dwDBServerPort,_T("master"));
  100. else
  101. strCon.Format(DB_SS_CONN_WITH_PORT,Global::g_szDBSource, Global::g_dwDBServerPort, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord);
  102. }
  103. else
  104. {
  105. if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 )
  106. strCon.Format(DB_SW_CONN_WITHOUT_PORT, Global::g_szDBSource, _T("master"));
  107. else
  108. strCon.Format(DB_SS_CONN_WITHOUT_PORT,Global::g_szDBSource, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord);
  109. }
  110. g_masterdb.OpenEx(strCon,CDatabase::noOdbcDialog);
  111. bMasterDb=1;
  112. }
  113. catch(CDBException * e)
  114. {
  115. }
  116. if(bMasterDb==0)
  117. {
  118. WriteLogin("sql数据库未启动!");
  119. ::Sleep (1000);
  120. goto ll;
  121. }
  122. try
  123. {
  124. g_db.OpenEx(Global::g_szConnectString,CDatabase::noOdbcDialog);
  125. bUserDb=1;
  126. g_curdb=&g_db;
  127. g_masterdb.Close();
  128. }
  129. catch(CDBException * e)
  130. {
  131. WriteLogin("数据库连接失败-"+e->m_strError);
  132. e->Delete();
  133. ::Sleep (1000);
  134. goto ll;
  135. }
  136. SetRegistryKey(_T("LYFZ-LYFZReceiveMsg"));
  137. // Standard initialization
  138. // If you are not using these features and wish to reduce the size
  139. // of your final executable, you should remove from the following
  140. // the specific initialization routines you do not need.
  141. #ifdef _AFXDLL
  142. Enable3dControls(); // Call this when using MFC in a shared DLL
  143. #else
  144. Enable3dControlsStatic(); // Call this when linking to MFC statically
  145. #endif
  146. InitCommonControls();
  147. // Create the tray icon
  148. if (!m_TrayIcon.Create(WM_ICON_NOTIFY, // Icon notify message to use
  149. _T("利亚方舟-短信接收"), // tooltip
  150. LoadIcon(IDI_LYFZReceiveMsg), // Icon
  151. IDR_POPUP_MENU)) // ID of tray icon
  152. return -1;
  153. int nLogLevel = GetProfileInt("Settings", "LogLevel" , 1);
  154. CApplicationDlg dlg;
  155. m_pMainWnd = &dlg;
  156. int nResponse = dlg.DoModal();
  157. if (nResponse == IDOK)
  158. {
  159. // TODO: Place code here to handle when the dialog is
  160. // dismissed with OK
  161. }
  162. else if (nResponse == IDCANCEL)
  163. {
  164. // TODO: Place code here to handle when the dialog is
  165. // dismissed with Cancel
  166. }
  167. // Since the dialog has been closed, return FALSE so that we exit the
  168. // application, rather than start the application's message pump.
  169. return FALSE;
  170. }
  171. void CLYFZReceiveMsgApp::OnAppShow()
  172. {
  173. if (m_pMainWnd)
  174. m_pMainWnd->ShowWindow(SW_SHOW);
  175. }
  176. void CLYFZReceiveMsgApp::OnAppExit()
  177. {
  178. m_pMainWnd->DestroyWindow();
  179. }
  180. void CLYFZReceiveMsgApp::OnAppAbout()
  181. {
  182. // CSettingsSheet propsheet("Settings", AfxGetApp()->m_pMainWnd, 2);
  183. // remove Help button
  184. // propsheet.m_psh.dwFlags &= ~(PSH_HASHELP);
  185. // display Settings dialogbox
  186. // propsheet.DoModal();
  187. }
  188. /********************************************************************/
  189. /* */
  190. /* Function name : OnHelpIndex */
  191. /* Description : Command to show help file. */
  192. /* */
  193. /********************************************************************/
  194. void CLYFZReceiveMsgApp::OnHelpIndex()
  195. {
  196. // launch help
  197. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  198. }
  199. BOOL AddConn(CDatabase *m_conndb)
  200. {
  201. try
  202. {
  203. m_conndb->OpenEx(Global::g_szConnectString,CDatabase::noOdbcDialog);
  204. }
  205. catch(CDBException * e)
  206. {
  207. return 0;
  208. }
  209. return 1;
  210. }
  211. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  212. {
  213. try
  214. {
  215. HCRYPTPROV hCryptProv;
  216. HCRYPTKEY hKey;
  217. HCRYPTHASH hHash;
  218. PBYTE pbBuffer;
  219. DWORD dwBlockLen;
  220. DWORD dwBufferLen;
  221. DWORD dwCount;
  222. //以下获得一个CSP句柄
  223. if(CryptAcquireContext(
  224. &hCryptProv,
  225. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  226. NULL,
  227. PROV_RSA_FULL,
  228. 0))
  229. {
  230. printf("A cryptographic provider has been acquired. \n");
  231. }
  232. else//密钥容器不存在
  233. {
  234. if(CryptAcquireContext(
  235. &hCryptProv,
  236. NULL,
  237. NULL,
  238. PROV_RSA_FULL,
  239. CRYPT_NEWKEYSET))//创建密钥容器
  240. {
  241. //创建密钥容器成功,并得到CSP句柄
  242. printf("A new key container has been created.\n");
  243. }
  244. else
  245. {
  246. return 0;
  247. }
  248. }
  249. //--------------------------------------------------------------------
  250. // 创建一个会话密钥(session key)
  251. // 会话密钥也叫对称密钥,用于对称加密算法。
  252. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  253. // CryptReleaseContext 期间的阶段。)
  254. //--------------------------------------------------------------------
  255. // Create a hash object.
  256. if(CryptCreateHash(
  257. hCryptProv,
  258. CALG_MD5,
  259. 0,
  260. 0,
  261. &hHash))
  262. {
  263. printf("A hash object has been created. \n");
  264. }
  265. else
  266. {
  267. return 0;
  268. }
  269. //--------------------------------------------------------------------
  270. // 用输入的密码产生一个散列
  271. if(CryptHashData(
  272. hHash,
  273. (BYTE *)szPassword,
  274. strlen(szPassword),
  275. 0))
  276. {
  277. printf("The password has been added to the hash. \n");
  278. }
  279. else
  280. {
  281. return 0;
  282. }
  283. //--------------------------------------------------------------------
  284. // 通过散列生成会话密钥(session key)
  285. if(CryptDeriveKey(
  286. hCryptProv,
  287. ENCRYPT_ALGORITHM,
  288. hHash,
  289. KEYLENGTH,
  290. &hKey))
  291. {
  292. printf("An encryption key is derived from the password hash. \n");
  293. }
  294. else
  295. {
  296. return 0;
  297. }
  298. //--------------------------------------------------------------------
  299. // Destroy the hash object.
  300. CryptDestroyHash(hHash);
  301. hHash = NULL;
  302. //--------------------------------------------------------------------
  303. // The session key is now ready.
  304. //--------------------------------------------------------------------
  305. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  306. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  307. // 数据长度。
  308. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  309. //--------------------------------------------------------------------
  310. // Determine the block size. If a block cipher is used,
  311. // it must have room for an extra block.
  312. if(ENCRYPT_BLOCK_SIZE > 1)
  313. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  314. else
  315. dwBufferLen = dwBlockLen;
  316. dwCount=dwBufferLen;
  317. //--------------------------------------------------------------------
  318. // In a do loop, encrypt the source file and write to the source file.
  319. int count;
  320. // if(leng%dwBlockLen==0)
  321. count=leng/dwBufferLen;
  322. // else
  323. // count=leng/dwBlockLen+1;
  324. int i ;
  325. for( i=0; i<count; i++)
  326. {
  327. pbBuffer=buffer+i*dwBufferLen;
  328. // 加密数据
  329. if(!CryptEncrypt(
  330. hKey, //密钥
  331. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  332. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  333. //这里通过判断是否到文件尾来决定是否为最后一块。
  334. 0, //保留
  335. pbBuffer, //输入被加密数据,输出加密后的数据
  336. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  337. dwBufferLen)) //pbBuffer的大小。
  338. {
  339. return 0;
  340. }
  341. }
  342. if(leng%dwBlockLen)
  343. {
  344. pbBuffer=buffer+i*dwBufferLen;
  345. dwCount=leng-i*dwBufferLen;
  346. if(!CryptEncrypt(
  347. hKey, //密钥
  348. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  349. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  350. //这里通过判断是否到文件尾来决定是否为最后一块。
  351. 0, //保留
  352. pbBuffer, //输入被加密数据,输出加密后的数据
  353. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  354. dwBufferLen)) //pbBuffer的大小。
  355. {
  356. return 0;
  357. }
  358. }
  359. //--------------------------------------------------------------------
  360. // Destroy session key.
  361. if(hKey)
  362. CryptDestroyKey(hKey);
  363. //--------------------------------------------------------------------
  364. // Destroy hash object.
  365. if(hHash)
  366. CryptDestroyHash(hHash);
  367. //--------------------------------------------------------------------
  368. // Release provider handle.
  369. if(hCryptProv)
  370. CryptReleaseContext(hCryptProv, 0);
  371. return(TRUE);
  372. }
  373. catch(...)
  374. {
  375. }
  376. }
  377. void WriteLogin(CString str)
  378. {
  379. MyLock lock("LYFZReceiveMsgWriteLogin");
  380. CStdioFile fp;
  381. CString path=g_mainpath+"\\LYFZReceiveMsg.txt";
  382. if(::PathFileExists (path))
  383. {
  384. fp.Open (path, CFile::modeWrite);
  385. int length=fp.GetLength ();
  386. if(length>1024*1024)
  387. {
  388. fp.Close ();
  389. ::DeleteFile (path);
  390. return;
  391. }
  392. fp.SeekToEnd ();
  393. }
  394. else
  395. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  396. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
  397. fp.Close ();
  398. }
  399. void WriteLogin2(CString str)
  400. {
  401. MyLock lock("LYFZReceiveMsgWriteLogin");
  402. CStdioFile fp;
  403. CString path=g_mainpath+"\\LYFZReceiveMsg.txt";
  404. if(::PathFileExists (path))
  405. {
  406. fp.Open (path, CFile::modeWrite);
  407. int length=fp.GetLength ();
  408. if(length>1024*1024)
  409. {
  410. fp.Close ();
  411. ::DeleteFile (path);
  412. return;
  413. }
  414. fp.SeekToEnd ();
  415. }
  416. else
  417. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  418. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
  419. fp.Close ();
  420. }
  421. void WriteError(CString str)
  422. {
  423. MyLock lock("LYFZReceiveMsgWriteError");
  424. CStdioFile fp;
  425. CString path=g_mainpath+"\\MsgError.txt";
  426. if(::PathFileExists (path))
  427. {
  428. fp.Open (path, CFile::modeWrite);
  429. int length=fp.GetLength ();
  430. if(length>1024*1024)
  431. {
  432. fp.Close ();
  433. ::DeleteFile (path);
  434. return;
  435. }
  436. fp.SeekToEnd ();
  437. }
  438. else
  439. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  440. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
  441. fp.Close ();
  442. }
  443. void WritePswWrong(CString str)
  444. {
  445. MyLock lock("LYFZReceiveMsgWritePswWrong");
  446. CStdioFile fp;
  447. CString path=g_mainpath+"\\PswWrong.txt";
  448. if(::PathFileExists (path))
  449. {
  450. fp.Open (path, CFile::modeWrite);
  451. int length=fp.GetLength ();
  452. if(length>1024*1024)
  453. {
  454. fp.Close ();
  455. ::DeleteFile (path);
  456. return;
  457. }
  458. fp.SeekToEnd ();
  459. }
  460. else
  461. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  462. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
  463. fp.Close ();
  464. }
  465. void WriteNullBalance(CString str)
  466. {
  467. MyLock lock("LYFZReceiveMsgWriteNullBalance");
  468. CStdioFile fp;
  469. CString path=g_mainpath+"\\NullBalance.txt";
  470. if(::PathFileExists (path))
  471. {
  472. fp.Open (path, CFile::modeWrite);
  473. int length=fp.GetLength ();
  474. if(length>1024*1024)
  475. {
  476. fp.Close ();
  477. ::DeleteFile (path);
  478. return;
  479. }
  480. fp.SeekToEnd ();
  481. }
  482. else
  483. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  484. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
  485. fp.Close ();
  486. }
  487. void WriteUnallowIP(CString str)
  488. {
  489. CStdioFile fp;
  490. CString path=g_mainpath+"\\UnallowIP.txt";
  491. if(::PathFileExists (path))
  492. {
  493. fp.Open (path, CFile::modeWrite);
  494. int length=fp.GetLength ();
  495. if(length>1024*1024)
  496. {
  497. fp.Close ();
  498. ::DeleteFile (path);
  499. return;
  500. }
  501. fp.SeekToEnd ();
  502. }
  503. else
  504. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  505. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" );
  506. fp.Close ();
  507. }