linux版本中间件

SoftAuth.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.getline(str, sizeof(str));
  75. is.close();
  76. // std::cout << "解密前:" << str << std::endl;
  77. std::string out; // 解密后
  78. Base64Decode(str, out);
  79. // std::cout << "解密后:" << out << std::endl;
  80. std::string machcode1; // 机器码-本机
  81. __getdiskid(machcode1);
  82. if (machcode1.empty())
  83. {
  84. std::cout << "机器码获取失败,请用root权限运行!" << std::endl;
  85. return false;
  86. }
  87. // std::cout << machcode1 << std::endl;
  88. // std::string data = std::regex_replace(out, std::regex(machcode1), "");
  89. std::string data = out.substr(machcode1.length(), out.length()- machcode1.length());
  90. // std::cout << data << std::endl;
  91. std::string machcode2; // 授权文件中的机器码
  92. machcode2 = out.substr(0, out.find(data));
  93. // std::cout << machcode2 << std::endl;
  94. if (machcode2 != machcode1)
  95. {
  96. return false;
  97. }
  98. std::vector<std::string> vects;
  99. boost::split(vects, data, boost::is_any_of("#"));
  100. if (vects.size() < 3)
  101. return false;
  102. aInfo.data = vects[0];
  103. aInfo.trunkNum = atoi(vects[1].c_str());
  104. aInfo.agentNum = atoi(vects[2].c_str());
  105. aInfo.code = machcode1;
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool SoftAuth::Auth(std::string& encode)
  111. {
  112. std::string out;
  113. if (!Base64Decode(encode, out))
  114. return false;
  115. ofstream os(MSHY, ios_base::out | ios_base::binary);
  116. //os.write(reinterpret_cast<char *>(&employee1), sizeof(employee1));
  117. os.write(encode.c_str(), encode.length());
  118. os.close();
  119. /*AuthInfo* aInfo = (AuthInfo*)out.c_str();*/
  120. // aInfo = (AuthInfo*)out.c_str();
  121. std::string hardc;
  122. __getdiskid(hardc);
  123. if (out.find(hardc) == string::npos)
  124. {
  125. std::cout << "机器码和授权码中不相符" << std::endl;
  126. return false;
  127. }
  128. aInfo.data = std::regex_replace(out, std::regex(hardc), "");
  129. aInfo.code = hardc;
  130. if (aInfo.code != hardc || hardc.empty())
  131. {
  132. return false;
  133. }
  134. return true;
  135. }
  136. bool SoftAuth::AuthV2(std::string & encode)
  137. {
  138. std::string out;
  139. if (!Base64Decode(encode, out))
  140. return false;
  141. ofstream os(MSHY, ios_base::out | ios_base::binary);
  142. os.write(encode.c_str(), encode.length());
  143. os.close();
  144. std::string hardc;
  145. __getdiskid(hardc);
  146. if (out.find(hardc) == string::npos)
  147. {
  148. std::cout << "机器码和授权码中不相符" << std::endl;
  149. return false;
  150. }
  151. std::string data = out.substr(hardc.length(), out.length() - hardc.length());
  152. // std::string data = std::regex_replace(out, std::regex(hardc), "");
  153. std::vector<std::string> vects;
  154. boost::split(vects, data, boost::is_any_of("#"));
  155. if (vects.size() < 3)
  156. return false;
  157. aInfo.data = vects[0];
  158. aInfo.trunkNum = atoi(vects[1].c_str());
  159. aInfo.agentNum = atoi(vects[2].c_str());
  160. aInfo.code = hardc;
  161. if (aInfo.code != hardc || hardc.empty())
  162. {
  163. return false;
  164. }
  165. return true;
  166. }
  167. bool SoftAuth::isExpire()
  168. {
  169. try
  170. {
  171. boost::gregorian::date curData = Util::CurData();
  172. boost::gregorian::date dData = boost::gregorian::from_undelimited_string(aInfo.data);
  173. /*boost::gregorian::date dData = boost::gregorian::from_undelimited_string("20200716");*/
  174. if (dData.is_special()) // 时间无效
  175. {
  176. return false;
  177. }
  178. return dData > curData;
  179. }
  180. catch (boost::bad_lexical_cast& e)
  181. {
  182. return false;
  183. }
  184. }
  185. bool SoftAuth::getMachineCode(std::string & code)
  186. {
  187. if (__getdiskid(code) == 0)
  188. {
  189. return true;
  190. }
  191. return false;
  192. }
  193. int SoftAuth::__getdiskid(std::string & hardc)
  194. {
  195. {
  196. char cmd[] = "blkid"; //sudo dmidecode -s system-serial-number \n neutron ALL=(ALL) NOPASSWD:ALL
  197. FILE *file = popen(cmd, "r");
  198. if (!file)
  199. {
  200. fprintf(stderr, "popen top fail");
  201. return -1;
  202. }
  203. char buff[1024];
  204. while (fgets(buff, sizeof(buff), file) != NULL)
  205. {
  206. if (strstr(buff, "/dev/sda1: UUID=") != NULL) // /dev/sda1: UUID="1a2dffc7-a5c4-49a0-8158-147b3fa07f78" TYPE="ext4" PARTUUID="b48e27f0-01"
  207. {
  208. hardc = buff;
  209. hardc = std::regex_replace(hardc, std::regex("(/)|( )|(\")|(\\n)"), ""); // devsda1:UUID=1a2dffc7-a5c4-49a0-8158-147b3fa07f78TYPE=ext4PARTUUID=b48e27f0-01
  210. hardc = std::regex_replace(hardc, std::regex("devsda1:UUID=(.*)TYPE=(.*)PARTUUID=(.*)"), "$1");
  211. pclose(file);
  212. return 0;
  213. }
  214. }
  215. pclose(file);
  216. return -1;
  217. }
  218. }
  219. bool SoftAuth::Base64Encode(const string & input, string & output)
  220. {
  221. typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
  222. stringstream result;
  223. try {
  224. copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
  225. }
  226. catch (...) {
  227. return false;
  228. }
  229. size_t equal_count = (3 - input.length() % 3) % 3;
  230. for (size_t i = 0; i < equal_count; i++)
  231. {
  232. result.put('=');
  233. }
  234. output = result.str();
  235. return output.empty() == false;
  236. }
  237. bool SoftAuth::Base64Decode(const string & input, string & output)
  238. {
  239. typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
  240. stringstream result;
  241. try {
  242. copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
  243. }
  244. catch (...) {
  245. return false;
  246. }
  247. output = result.str();
  248. return output.empty() == false;
  249. }
  250. SoftAuth SoftAuth::instance;