| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #include "SoftAuth.h"
- #include <boost/archive/iterators/base64_from_binary.hpp>
- #include <boost/archive/iterators/binary_from_base64.hpp>
- #include <boost/archive/iterators/transform_width.hpp>
- #include <boost/algorithm/string.hpp>
- #include <iostream>
- #include <sstream>
- #include <regex>
- #include <fstream>
- #include "Util.h"
- using namespace boost::archive::iterators;
- bool SoftAuth::MakeAuth(std::string machCode)
- {
- std::string data; //
- std::cout << "输入授权日期如:20200715" << std::endl;
- std::cin >> data;
- std::string out;
- bool ret = Base64Encode(machCode + data, out);
- /*AuthInfo info;
- info.code = machCode;
- info.data = data;*/
- std::cout << "授权码如下:" << std::endl;
- std::cout << out << std::endl;
- return ret;
- }
- bool SoftAuth::MakeAuthV2(std::string machCode)
- {
- std::string data; //
- std::cout << "输入授权日期如:20200715" << std::endl;
- std::cin >> data;
- std::string trunkNum;
- std::cout << "输入授权中继数量(输入整数):" << std::endl;
- std::cin >> trunkNum;
- std::string acdNum;
- std::cout << "输入授权ACD坐席数量(输入整数):" << std::endl;
- std::cin >> acdNum;
- std::string out;
- bool ret = Base64Encode(machCode + data + "#" + trunkNum + "#" + acdNum, out);
- std::cout << "授权码如下:" << std::endl;
- std::cout << out << std::endl;
- return ret;
- }
- bool SoftAuth::Auth()
- {
- ifstream is(MSHY, ios_base::in | ios_base::binary);
- if (is) {
- //is.read(reinterpret_cast<char *>(&aInfo), sizeof(aInfo));
- char str[200];
- is.read(str, sizeof(str));
- is.close();
- std::string out; // 解密后
- Base64Decode(str, out);
- std::string machcode1; // 机器码-本机
- __getdiskid(machcode1);
- std::string data = std::regex_replace(out, std::regex(machcode1), "");
- std::string machcode2; // 授权文件中的机器码
- machcode2 = std::regex_replace(out, std::regex(data), "");
- if (machcode2 != machcode1)
- {
- return false;
- }
- aInfo.data = data;
- aInfo.code = machcode1;
- return true;
- }
- return false;
- }
- bool SoftAuth::AuthV2()
- {
- char buffer[512];
- //getcwd(buffer, 512);
- std::cout << getcwd(buffer, 512) << std::endl;
- ifstream is(MSHY, ios_base::in | ios_base::binary);
- if (is) {
- char str[1024];
- //is.read(str, sizeof(str));
- is.getline(str, sizeof(str));
- is.close();
- // std::cout << "解密前:" << str << std::endl;
- std::string out; // 解密后
- Base64Decode(str, out);
- // std::cout << "解密后:" << out << std::endl;
- std::string machcode1; // 机器码-本机
- __getdiskid(machcode1);
- if (machcode1.empty())
- {
- std::cout << "机器码获取失败,请用root权限运行!" << std::endl;
- return false;
- }
- // std::cout << machcode1 << std::endl;
- // std::string data = std::regex_replace(out, std::regex(machcode1), "");
- std::string data = out.substr(machcode1.length(), out.length()- machcode1.length());
- // std::cout << data << std::endl;
- std::string machcode2; // 授权文件中的机器码
- machcode2 = out.substr(0, out.find(data));
- // std::cout << machcode2 << std::endl;
- if (machcode2 != machcode1)
- {
- return false;
- }
- std::vector<std::string> vects;
- boost::split(vects, data, boost::is_any_of("#"));
- if (vects.size() < 3)
- return false;
- aInfo.data = vects[0];
- aInfo.trunkNum = atoi(vects[1].c_str());
- aInfo.agentNum = atoi(vects[2].c_str());
- aInfo.code = machcode1;
- return true;
- }
- return false;
- }
- bool SoftAuth::Auth(std::string& encode)
- {
- std::string out;
- if (!Base64Decode(encode, out))
- return false;
- ofstream os(MSHY, ios_base::out | ios_base::binary);
- //os.write(reinterpret_cast<char *>(&employee1), sizeof(employee1));
- os.write(encode.c_str(), encode.length());
- os.close();
- /*AuthInfo* aInfo = (AuthInfo*)out.c_str();*/
- // aInfo = (AuthInfo*)out.c_str();
- std::string hardc;
- __getdiskid(hardc);
- if (out.find(hardc) == string::npos)
- {
- std::cout << "机器码和授权码中不相符" << std::endl;
- return false;
- }
- aInfo.data = std::regex_replace(out, std::regex(hardc), "");
- aInfo.code = hardc;
- if (aInfo.code != hardc || hardc.empty())
- {
- return false;
- }
- return true;
- }
- bool SoftAuth::AuthV2(std::string & encode)
- {
- std::string out;
- if (!Base64Decode(encode, out))
- return false;
- ofstream os(MSHY, ios_base::out | ios_base::binary);
- os.write(encode.c_str(), encode.length());
- os.close();
- std::string hardc;
- __getdiskid(hardc);
- if (out.find(hardc) == string::npos)
- {
- std::cout << "机器码和授权码中不相符" << std::endl;
- return false;
- }
- std::string data = out.substr(hardc.length(), out.length() - hardc.length());
- // std::string data = std::regex_replace(out, std::regex(hardc), "");
- std::vector<std::string> vects;
- boost::split(vects, data, boost::is_any_of("#"));
- if (vects.size() < 3)
- return false;
- aInfo.data = vects[0];
- aInfo.trunkNum = atoi(vects[1].c_str());
- aInfo.agentNum = atoi(vects[2].c_str());
- aInfo.code = hardc;
- if (aInfo.code != hardc || hardc.empty())
- {
- return false;
- }
- return true;
- }
- bool SoftAuth::isExpire()
- {
- try
- {
- boost::gregorian::date curData = Util::CurData();
- boost::gregorian::date dData = boost::gregorian::from_undelimited_string(aInfo.data);
- /*boost::gregorian::date dData = boost::gregorian::from_undelimited_string("20200716");*/
- if (dData.is_special()) // 时间无效
- {
- return false;
- }
- return dData > curData;
- }
- catch (boost::bad_lexical_cast& e)
- {
- return false;
- }
- }
- bool SoftAuth::getMachineCode(std::string & code)
- {
- if (__getdiskid(code) == 0)
- {
- return true;
- }
- return false;
- }
- int SoftAuth::__getdiskid(std::string & hardc)
- {
- {
- char cmd[] = "blkid"; //sudo dmidecode -s system-serial-number \n neutron ALL=(ALL) NOPASSWD:ALL
- FILE *file = popen(cmd, "r");
- if (!file)
- {
- fprintf(stderr, "popen top fail");
- return -1;
- }
- char buff[1024];
- while (fgets(buff, sizeof(buff), file) != NULL)
- {
- if (strstr(buff, "/dev/sda1: UUID=") != NULL) // /dev/sda1: UUID="1a2dffc7-a5c4-49a0-8158-147b3fa07f78" TYPE="ext4" PARTUUID="b48e27f0-01"
- {
- hardc = buff;
- hardc = std::regex_replace(hardc, std::regex("(/)|( )|(\")|(\\n)"), ""); // devsda1:UUID=1a2dffc7-a5c4-49a0-8158-147b3fa07f78TYPE=ext4PARTUUID=b48e27f0-01
- hardc = std::regex_replace(hardc, std::regex("devsda1:UUID=(.*)TYPE=(.*)PARTUUID=(.*)"), "$1");
- pclose(file);
- return 0;
- }
- else if (strstr(buff, "/dev/vda2: UUID=") != NULL) // 云服务器 /dev/vda2
- {
- hardc = buff;
- hardc = std::regex_replace(hardc, std::regex("(/)|( )|(\")|(\\n)"), ""); // devvda2:UUID=98778513-e1d6-42f7-8dd7-9684c3b6d7f4TYPE=ext4PARTUUID=cbe3ac09-02
- hardc = std::regex_replace(hardc, std::regex("devvda2:UUID=(.*)TYPE=(.*)PARTUUID=(.*)"), "$1");
- pclose(file);
- return 0;
- }
- }
- pclose(file);
- return -1;
- }
- }
- bool SoftAuth::Base64Encode(const string & input, string & output)
- {
- typedef base64_from_binary<transform_width<string::const_iterator, 6, 8>> Base64EncodeIterator;
- stringstream result;
- try {
- copy(Base64EncodeIterator(input.begin()), Base64EncodeIterator(input.end()), ostream_iterator<char>(result));
- }
- catch (...) {
- return false;
- }
- size_t equal_count = (3 - input.length() % 3) % 3;
- for (size_t i = 0; i < equal_count; i++)
- {
- result.put('=');
- }
- output = result.str();
- return output.empty() == false;
- }
- bool SoftAuth::Base64Decode(const string & input, string & output)
- {
- typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6> Base64DecodeIterator;
- stringstream result;
- try {
- copy(Base64DecodeIterator(input.begin()), Base64DecodeIterator(input.end()), ostream_iterator<char>(result));
- }
- catch (...) {
- return false;
- }
- output = result.str();
- return output.empty() == false;
- }
- SoftAuth SoftAuth::instance;
|