linux版本中间件

SoftAuth.cpp 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "SoftAuth.h"
  2. #include <boost/archive/iterators/base64_from_binary.hpp>
  3. #include <boost/archive/iterators/binary_from_base64.hpp>
  4. #include <boost/archive/iterators/transform_width.hpp>
  5. #include <boost/algorithm/string.hpp>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <regex>
  9. #include <fstream>
  10. #include "Util.h"
  11. using namespace boost::archive::iterators;
  12. bool SoftAuth::MakeAuth(std::string machCode)
  13. {
  14. std::string data; //
  15. std::cout << "输入授权日期如:20200715" << std::endl;
  16. std::cin >> data;
  17. std::string out;
  18. bool ret = Base64Encode(machCode + data, out);
  19. /*AuthInfo info;
  20. info.code = machCode;
  21. info.data = data;*/
  22. std::cout << "授权码如下:" << std::endl;
  23. std::cout << out << std::endl;
  24. return ret;
  25. }
  26. bool SoftAuth::MakeAuthV2(std::string machCode)
  27. {
  28. std::string data; //
  29. std::cout << "输入授权日期如:20200715" << std::endl;
  30. std::cin >> data;
  31. std::string trunkNum;
  32. std::cout << "输入授权中继数量(输入整数):" << std::endl;
  33. std::cin >> trunkNum;
  34. std::string acdNum;
  35. std::cout << "输入授权ACD坐席数量(输入整数):" << std::endl;
  36. std::cin >> acdNum;
  37. std::string out;
  38. bool ret = Base64Encode(machCode + data + "#" + trunkNum + "#" + acdNum, out);
  39. std::cout << "授权码如下:" << std::endl;
  40. std::cout << out << std::endl;
  41. return ret;
  42. }
  43. bool SoftAuth::Auth()
  44. {
  45. ifstream is(MSHY, ios_base::in | ios_base::binary);
  46. if (is) {
  47. //is.read(reinterpret_cast<char *>(&aInfo), sizeof(aInfo));
  48. char str[200];
  49. is.read(str, sizeof(str));
  50. is.close();
  51. std::string out; // 解密后
  52. Base64Decode(str, out);
  53. std::string machcode1; // 机器码-本机
  54. __getdiskid(machcode1);
  55. std::string data = std::regex_replace(out, std::regex(machcode1), "");
  56. std::string machcode2; // 授权文件中的机器码
  57. machcode2 = std::regex_replace(out, std::regex(data), "");
  58. if (machcode2 != machcode1)
  59. {
  60. return false;
  61. }
  62. aInfo.data = data;
  63. aInfo.code = machcode1;
  64. return true;
  65. }
  66. return false;
  67. }
  68. bool SoftAuth::AuthV2()
  69. {
  70. ifstream is(MSHY, ios_base::in | ios_base::binary);
  71. if (is) {
  72. char str[1024];
  73. is.read(str, sizeof(str));
  74. is.close();
  75. // std::cout << "解密前:" << str << std::endl;
  76. std::string out; // 解密后
  77. Base64Decode(str, out);
  78. // std::cout << "解密后:" << out << std::endl;
  79. std::string machcode1; // 机器码-本机
  80. __getdiskid(machcode1);
  81. // std::cout << machcode1 << std::endl;
  82. std::string data = std::regex_replace(out, std::regex(machcode1), "");
  83. // std::cout << data << std::endl;
  84. std::string machcode2; // 授权文件中的机器码
  85. //machcode2 = std::regex_replace(out, std::regex(data), "");
  86. machcode2 = out.substr(0, out.find(data));
  87. // std::cout << machcode2 << std::endl;
  88. if (machcode2 != machcode1)
  89. {
  90. return false;
  91. }
  92. std::vector<std::string> vects;
  93. boost::split(vects, data, boost::is_any_of("#"));
  94. if (vects.size() < 3)
  95. return false;
  96. aInfo.data = vects[0];
  97. aInfo.trunkNum = atoi(vects[1].c_str());
  98. aInfo.agentNum = atoi(vects[2].c_str());
  99. aInfo.code = machcode1;
  100. return true;
  101. }
  102. return false;
  103. }
  104. bool SoftAuth::Auth(std::string& encode)
  105. {
  106. std::string out;
  107. if (!Base64Decode(encode, out))
  108. return false;
  109. ofstream os(MSHY, ios_base::out | ios_base::binary);
  110. //os.write(reinterpret_cast<char *>(&employee1), sizeof(employee1));
  111. os.write(encode.c_str(), encode.length());
  112. os.close();
  113. /*AuthInfo* aInfo = (AuthInfo*)out.c_str();*/
  114. // aInfo = (AuthInfo*)out.c_str();
  115. std::string hardc;
  116. __getdiskid(hardc);
  117. if (out.find(hardc) == string::npos)
  118. {
  119. std::cout << "机器码和授权码中不相符" << std::endl;
  120. return false;
  121. }
  122. aInfo.data = std::regex_replace(out, std::regex(hardc), "");
  123. aInfo.code = hardc;
  124. if (aInfo.code != hardc || hardc.empty())
  125. {
  126. return false;
  127. }
  128. return true;
  129. }
  130. bool SoftAuth::AuthV2(std::string & encode)
  131. {
  132. std::string out;
  133. if (!Base64Decode(encode, out))
  134. return false;
  135. ofstream os(MSHY, ios_base::out | ios_base::binary);
  136. os.write(encode.c_str(), encode.length());
  137. os.close();
  138. std::string hardc;
  139. __getdiskid(hardc);
  140. if (out.find(hardc) == string::npos)
  141. {
  142. std::cout << "机器码和授权码中不相符" << std::endl;
  143. return false;
  144. }
  145. std::string data = std::regex_replace(out, std::regex(hardc), "");
  146. std::vector<std::string> vects;
  147. boost::split(vects, data, boost::is_any_of("#"));
  148. if (vects.size() < 3)
  149. return false;
  150. aInfo.data = vects[0];
  151. aInfo.trunkNum = atoi(vects[1].c_str());
  152. aInfo.agentNum = atoi(vects[2].c_str());
  153. aInfo.code = hardc;
  154. if (aInfo.code != hardc || hardc.empty())
  155. {
  156. return false;
  157. }
  158. return true;
  159. }
  160. bool SoftAuth::isExpire()
  161. {
  162. boost::gregorian::date curData = Util::CurData();
  163. boost::gregorian::date dData = boost::gregorian::from_undelimited_string(aInfo.data);
  164. /*boost::gregorian::date dData = boost::gregorian::from_undelimited_string("20200716");*/
  165. if (dData.is_special()) // 时间无效
  166. {
  167. return false;
  168. }
  169. return dData > curData;
  170. }
  171. bool SoftAuth::getMachineCode(std::string & code)
  172. {
  173. if (__getdiskid(code) == 0)
  174. {
  175. return true;
  176. }
  177. return false;
  178. }
  179. int SoftAuth::__getdiskid(std::string & hardc)
  180. {
  181. {
  182. char cmd[] = "blkid"; //sudo dmidecode -s system-serial-number \n neutron ALL=(ALL) NOPASSWD:ALL
  183. FILE *file = popen(cmd, "r");
  184. if (!file)
  185. {
  186. fprintf(stderr, "popen top fail");
  187. return -1;
  188. }
  189. char buff[1024];
  190. while (fgets(buff, sizeof(buff), file) != NULL)
  191. {
  192. if (strstr(buff, "/dev/sda1: UUID=") != NULL) // /dev/sda1: UUID="1a2dffc7-a5c4-49a0-8158-147b3fa07f78" TYPE="ext4" PARTUUID="b48e27f0-01"
  193. {
  194. hardc = buff;
  195. hardc = std::regex_replace(hardc, std::regex("(/)|( )|(\")|(\\n)"), ""); // devsda1:UUID=1a2dffc7-a5c4-49a0-8158-147b3fa07f78TYPE=ext4PARTUUID=b48e27f0-01
  196. hardc = std::regex_replace(hardc, std::regex("devsda1:UUID=(.*)TYPE=(.*)PARTUUID=(.*)"), "$1");
  197. pclose(file);
  198. return 0;
  199. }
  200. }
  201. pclose(file);
  202. return -1;
  203. }
  204. }
  205. bool SoftAuth::Base64Encode(const string & input, string & output)
  206. {
  207. typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
  208. stringstream result;
  209. try {
  210. copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
  211. }
  212. catch (...) {
  213. return false;
  214. }
  215. size_t equal_count = (3 - input.length() % 3) % 3;
  216. for (size_t i = 0; i < equal_count; i++)
  217. {
  218. result.put('=');
  219. }
  220. output = result.str();
  221. return output.empty() == false;
  222. }
  223. bool SoftAuth::Base64Decode(const string & input, string & output)
  224. {
  225. typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
  226. stringstream result;
  227. try {
  228. copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
  229. }
  230. catch (...) {
  231. return false;
  232. }
  233. output = result.str();
  234. return output.empty() == false;
  235. }
  236. SoftAuth SoftAuth::instance;