中间件标准版5.1git,去除基础模块

LicenseMgr.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include "StdAfx.h"
  2. #include "LicenseMgr.h"
  3. #include<Iphlpapi.h>
  4. #pragma comment(lib, "Iphlpapi.lib")
  5. SINGLETON_IMPLEMENT(CLicenseMgr)
  6. CLicenseMgr::CLicenseMgr(void)
  7. {
  8. }
  9. CLicenseMgr::~CLicenseMgr( void )
  10. {
  11. }
  12. /*****************************************************************
  13. **【函数名称】 __vaildTimer
  14. **【函数功能】 检测定时器
  15. **【参数】
  16. **【返回值】
  17. ****************************************************************/
  18. void CALLBACK CLicenseMgr::__vaildTimer( void )
  19. {
  20. if (!CLicenseMgr::GetInstance().__checkTime(CLicenseMgr::GetInstance().m_License))
  21. {
  22. PostQuitMessage(0);
  23. }
  24. }
  25. /*****************************************************************
  26. **【函数名称】 __readlicense
  27. **【函数功能】 读授权
  28. **【参数】
  29. **【返回值】
  30. ****************************************************************/
  31. bool CLicenseMgr::__readlicense( CString& License )
  32. {
  33. TCHAR path[MAX_PATH] = { 0 };
  34. //获得当前应用程序路径
  35. if(GetCurrentDirectory (MAX_PATH, path) == 0)
  36. return false;
  37. CString LicFileName;
  38. #ifdef _DEBUG
  39. LicFileName.Format(_T("D:\\DATA\\project\\HY\\Middleware\\Debug\\lic.txt"));
  40. #else
  41. LicFileName.Format(_T("%s\\lic.txt"), path);
  42. #endif
  43. CFile File;
  44. if(!File.Open(LicFileName, CFile::modeRead))
  45. return false;
  46. TCHAR LicCode[1024] = { 0 };
  47. File.Read(LicCode, 1024);
  48. License = LicCode;
  49. File.Close();
  50. return true;
  51. }
  52. /*****************************************************************
  53. **【函数名称】 __decode
  54. **【函数功能】 解密授权码
  55. **【参数】
  56. **【返回值】
  57. ****************************************************************/
  58. CString CLicenseMgr::__decode( CString LicCode )
  59. {
  60. CString strValue;
  61. char szAr1[1024];
  62. char szAr2[1024];
  63. char szAr3[1024];
  64. memset(szAr1, 0, 1024);
  65. memset(szAr2, 0, 1024);
  66. memset(szAr3, 0, 1024);
  67. // 获得字符串长度
  68. int nCodeLen = LicCode.GetLength();
  69. // 拷贝源字符串到szAr3
  70. lstrcpy(szAr3, LicCode);
  71. // 加密字符串恢复顺序
  72. for (int nIndex = 0; nIndex < (nCodeLen-(nCodeLen % 2))/2; nIndex++)
  73. {
  74. if ((nIndex % 2) == 0 && nIndex < (nCodeLen - nIndex -1))
  75. {
  76. char cTmp = szAr3[nIndex];
  77. szAr3[nIndex] = szAr3[nCodeLen - nIndex -1];
  78. szAr3[nCodeLen - nIndex -1] = cTmp;
  79. }
  80. }
  81. //int t1[] = { 6, 12, 5, 9, 10, 0, 13, 8, 15, 3, 14, 4, 2, 11, 1, 7 };
  82. int t2[] = { 5, 14, 12, 9, 11, 2, 0, 15, 7, 3, 4, 13, 1, 6, 10, 8 };
  83. char t3[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  84. int i=0, j=0;
  85. int p, q, temp;
  86. while ((i < nCodeLen) && (szAr3[i] != '\0 '))
  87. {
  88. szAr1[j] = szAr3[i];
  89. p=0;
  90. while((p <16) && (szAr1[j] != t3[p++]));
  91. szAr2[j] = szAr3[i+1];
  92. q=0;
  93. while ((q <16) && (szAr2[j] != t3[q++]));
  94. szAr1[j] = (char)(t2[p-1]*16 + t2[q-1]);
  95. temp = (int)szAr1[j];
  96. temp = temp ^ 0xFF;
  97. szAr1[j]=(char)temp;
  98. i=i+2;
  99. j++;
  100. }
  101. szAr1[j] = 0;
  102. strValue.Format("%s", szAr1);
  103. return strValue;
  104. }
  105. /*****************************************************************
  106. **【函数名称】 __getValue
  107. **【函数功能】 获取授权码指定值
  108. **【参数】
  109. **【返回值】
  110. ****************************************************************/
  111. CString CLicenseMgr::__getValue( CString License, License_Key_Type Key )
  112. {
  113. CString szString[12];
  114. int i = 0;
  115. while(1)
  116. {
  117. int pos = License.Find('|');
  118. if(pos>=0){
  119. szString[i] = License.Left(pos);
  120. License = License.Mid(pos+1);
  121. }
  122. else
  123. {
  124. szString[i] = License;
  125. break;
  126. }
  127. i++;
  128. }
  129. CString returnVlue;
  130. switch (Key)
  131. {
  132. case License_Key_CpuId:
  133. returnVlue = szString[License_Key_CpuId];
  134. break;
  135. case License_Key_PhysicalValue:
  136. returnVlue = szString[License_Key_PhysicalValue];
  137. break;
  138. case License_Key_HardDiskId:
  139. returnVlue = szString[License_Key_HardDiskId];
  140. break;
  141. case License_Key_TrunkNum:
  142. returnVlue = szString[License_Key_TrunkNum];
  143. break;
  144. case License_Key_AgentNum:
  145. returnVlue = szString[License_Key_AgentNum];
  146. break;
  147. case License_Key_FaxNum:
  148. returnVlue = szString[License_Key_FaxNum];
  149. break;
  150. case License_Key_IvrFlowNum:
  151. returnVlue = szString[License_Key_IvrFlowNum];
  152. break;
  153. case License_Key_VoipNum:
  154. returnVlue = szString[License_Key_VoipNum];
  155. break;
  156. case License_Key_EndTime:
  157. returnVlue = szString[License_Key_EndTime];
  158. break;
  159. case License_Key_BeginTime:
  160. returnVlue = szString[License_Key_BeginTime];
  161. break;
  162. }
  163. return returnVlue.Mid(returnVlue.Find('=') + 1);
  164. }
  165. /*****************************************************************
  166. **【函数名称】 __getCpuId
  167. **【函数功能】 读取CPU ID
  168. **【参数】
  169. **【返回值】
  170. ****************************************************************/
  171. bool CLicenseMgr::__getCpuId( CString& CpuId )
  172. {
  173. char szOEMString[13]; // CPU名称
  174. int iEAXValue, iEBXValue, iECXValue, iEDXValue;
  175. _asm
  176. {
  177. mov eax, 0
  178. cpuid
  179. mov DWORD PTR szOEMString, ebx
  180. mov DWORD PTR szOEMString+4, edx
  181. mov DWORD PTR szOEMString+8, ecx
  182. mov BYTE PTR szOEMString+12, 0
  183. }
  184. _asm
  185. {
  186. mov eax,1
  187. cpuid
  188. mov iEAXValue, eax
  189. mov iEBXValue, ebx
  190. mov iECXValue, ecx
  191. mov iEDXValue, edx
  192. }
  193. int iCPUFamily = (0xf00 & iEAXValue) >> 8; // CPU系列
  194. char szFamily[256] = { 0 };
  195. _itoa_s(iCPUFamily, szFamily, 256, 10);
  196. _asm
  197. {
  198. mov eax, 2
  199. CPUID
  200. }
  201. char szCPUID[256] = { NULL }; // CPUID号
  202. unsigned long s1 = 0, s2 = 0;
  203. _asm
  204. {
  205. mov eax, 01h
  206. xor edx, edx
  207. cpuid
  208. mov s1, edx
  209. mov s2, eax
  210. }
  211. sprintf_s(szCPUID, "%08X%08X", s1, s2);
  212. // CPU ID信息整合
  213. CpuId.Format("%s%s%s", szOEMString, szFamily, szCPUID);
  214. return true;
  215. }
  216. /*****************************************************************
  217. **【函数名称】 __getMac
  218. **【函数功能】 读取MAC
  219. **【参数】
  220. **【返回值】
  221. ****************************************************************/
  222. bool CLicenseMgr::__getMac( CString& Mac )
  223. {
  224. bool Res = false;
  225. // 获取MAC地址相关参数定义
  226. ULONG uFamily = AF_UNSPEC;
  227. ULONG uFlag = GAA_FLAG_INCLUDE_PREFIX;
  228. PIP_ADAPTER_ADDRESSES pIaa = NULL;
  229. DWORD uBufLen = 15000;
  230. // 获取网卡MAC地址
  231. pIaa = (IP_ADAPTER_ADDRESSES*)malloc(uBufLen);
  232. DWORD dwResult = GetAdaptersAddresses(uFamily, uFlag, NULL, pIaa, &uBufLen);
  233. if ( dwResult == NO_ERROR )
  234. {
  235. for (int i = 0; i < 6; i++)
  236. {
  237. CString strTmp;
  238. strTmp.Format("%02X", *(pIaa->PhysicalAddress + i));
  239. Mac += strTmp;
  240. } // end for
  241. Res = true;
  242. }
  243. free(pIaa);
  244. return Res;
  245. }
  246. /*****************************************************************
  247. **【函数名称】 __getTimeFromString
  248. **【函数功能】 由时间字符串生成时间对象
  249. **【参数】
  250. **【返回值】
  251. ****************************************************************/
  252. CTime CLicenseMgr::__getTimeFromString( LPCTSTR TimeString )
  253. {
  254. int nYear, nMonth, nDate, nHour, nMin, nSec;
  255. sscanf_s(TimeString, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
  256. CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);
  257. return t;
  258. }
  259. /*****************************************************************
  260. **【函数名称】 __checkTimeInReg
  261. **【函数功能】 检测注册表授权时间,防止出现修改系统时间来延长授权
  262. **【参数】
  263. **【返回值】
  264. ****************************************************************/
  265. bool CLicenseMgr::__checkTimeInReg( void )
  266. {
  267. CRegKey key;
  268. if(key.Open(HKEY_CURRENT_USER, "Software\\Midware\\CurTime", KEY_READ|KEY_WRITE) != ERROR_SUCCESS)
  269. {
  270. key.Create(HKEY_CURRENT_USER, "Software\\Midware\\CurTime");
  271. }
  272. ULONG nSize = OTL_REG_KEY_SIZE;
  273. CHAR szTmp[OTL_REG_KEY_SIZE] = { 0 };
  274. key.QueryStringValue("TIME", szTmp, &nSize);
  275. CString strTime; //注册表时间记录
  276. strTime.Format("%s",szTmp);
  277. CTime Curtime = CTime::GetCurrentTime(); // 系统当前时间
  278. time_t Systime = Curtime.GetTime();
  279. if (strTime.IsEmpty()) // 如果为空(第一次打开),则写入当前时间
  280. {
  281. CString strBuff;
  282. strBuff.Format("%d",Systime);
  283. key.SetStringValue("TIME",strBuff);
  284. return true;
  285. }
  286. time_t Regtime = atol(strTime.GetBuffer(0));
  287. if (Systime >= Regtime) // 如果大于注册表时间,则在授权范围内
  288. {
  289. CString strBuff;
  290. strBuff.Format("%d",Systime);
  291. key.SetStringValue("TIME",strBuff);
  292. return true;
  293. }
  294. else // 如果小于注册表时间,则被认为是超出授权范围
  295. {
  296. return false;
  297. }
  298. }
  299. /*****************************************************************
  300. **【函数名称】 __checkCpuId
  301. **【函数功能】 验证CPUID
  302. **【参数】
  303. **【返回值】
  304. ****************************************************************/
  305. bool CLicenseMgr::__checkCpuId( CString License )
  306. {
  307. CString CpuId;
  308. if(!__getCpuId(CpuId))
  309. return false;
  310. CString CpuIdInLic = __getValue(License, License_Key_CpuId);
  311. return CpuId == CpuIdInLic;
  312. }
  313. /*****************************************************************
  314. **【函数名称】 __checkMac
  315. **【函数功能】 验证MAC
  316. **【参数】
  317. **【返回值】
  318. ****************************************************************/
  319. bool CLicenseMgr::__checkMac( CString License )
  320. {
  321. CString Mac;
  322. if(!__getMac(Mac))
  323. return false;
  324. CString MacInLic = __getValue(License, License_Key_PhysicalValue);
  325. return Mac == MacInLic;
  326. }
  327. /*****************************************************************
  328. **【函数名称】 __checkTime
  329. **【函数功能】 验证时间
  330. **【参数】
  331. **【返回值】
  332. ****************************************************************/
  333. bool CLicenseMgr::__checkTime( CString License )
  334. {
  335. if(!__checkTimeInReg())
  336. {
  337. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 许可证注册时间到期"));
  338. return false;
  339. }
  340. CTime tCurrentTime = CTime::GetCurrentTime();
  341. CTime tEndTime;
  342. CString strEndTime = __getValue(License, License_Key_EndTime);
  343. if (!strEndTime.IsEmpty())
  344. {
  345. tEndTime = __getTimeFromString(strEndTime);
  346. }
  347. CTime tBeginTime;
  348. CString strBeginTime = __getValue(License, License_Key_BeginTime);
  349. if (!strBeginTime.IsEmpty())
  350. {
  351. tBeginTime = __getTimeFromString(strBeginTime);
  352. }
  353. if ((tCurrentTime - tEndTime) > 0 || (tBeginTime - tCurrentTime > 0) || strBeginTime.IsEmpty() || strEndTime.IsEmpty())
  354. {
  355. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 许可证时间到期: %s"), strEndTime);
  356. return false;
  357. }
  358. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_NORMAL, _T("{License}: 许可证结束时间: %s"), strEndTime);
  359. return true;
  360. }
  361. /*****************************************************************
  362. **【函数名称】 checkLicense
  363. **【函数功能】 检查授权
  364. **【参数】
  365. **【返回值】
  366. ****************************************************************/
  367. bool CLicenseMgr::checkLicense( void )
  368. {
  369. if(!__readlicense(m_License))
  370. {
  371. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 读取许可信息失败"));
  372. return false;
  373. }
  374. m_License = __decode(m_License);
  375. if(m_License == "")
  376. {
  377. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 解析许可信息失败"));
  378. return false;
  379. }
  380. if(!__checkCpuId(m_License))
  381. {
  382. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 核对CPU信息失败"));
  383. return false;
  384. }
  385. if(!__checkMac(m_License))
  386. {
  387. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 核对MAC信息失败"));
  388. return false;
  389. }
  390. if(!__checkTime(m_License))
  391. {
  392. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 核对时间信息失败"));
  393. return false;
  394. }
  395. // 定时器
  396. SetTimer(NULL, NULL, 1000*60*60, (TIMERPROC)__vaildTimer);
  397. return true;
  398. }