linux版本中间件

SoftAuth.cpp 7.5KB

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