#include "common__.h"
#include "飞鸟快验__.h"
extern CXTextA 全_AppWeb;
extern CXTextA 全_CryptoKeyAes;
extern CXTextA 全_CryptoKeyPublic;
extern CXTextA 全_Token;
extern int 全_CryptoType;
extern int 全_RSA指针;
CXTextA 全_AppWeb = L"";
CXTextA 全_CryptoKeyAes = L"";
CXTextA 全_CryptoKeyPublic = L"";
CXTextA 全_Token = L"";
int 全_CryptoType = 0;
int 全_RSA指针 = 0;
CXTextA sendPacketGetToken(CXTextA& 数据, BOOL 带token)
{
	CXWinHttp http;
	CXTextA 返回文本;
	CXTextA 随机密钥;
	CXTextA 临时密文;
	CJsonObject json;
	CXBytes encrypt_data;
	char iv[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0};
	http.Open(全_AppWeb, CXText(L"POST"));
	if(带token)
	{
		http.SetRequestHeader(CXText(L"Token"), 全_Token);
	}
	http.SendTextA(数据);
	http.GetResponseBodyText_Utf8ToA(返回文本);
	if(全_CryptoType == 1)
	{
		;
	}
	if(全_CryptoType == 2)
	{
		json.parse(返回文本.get());
		encrypt_data = X_Check_BASE64_DeCodeA(json["a"].as_text());
		encrypt_data = openssl_cipher(encrypt_data.getTextPtrA(), encrypt_data.getSize(), 全_CryptoKeyAes.get(), iv, 0, EVP_aes_192_cbc());
		返回文本 = XC_utf8toa(xcl_toTextA(encrypt_data).get());
	}
	if(全_CryptoType == 3)
	{
		json.parse(返回文本.get());
		临时密文 = json["b"].as_text();
		xcl_log(临时密文);
		if(临时密文.getSize() > 32)
		{
			encrypt_data = X_Check_BASE64_DeCodeA(临时密文);
			xcl_log(encrypt_data.toHexA());
			EVP_PKEY* key = openssl_rsa_pem_getkey(全_CryptoKeyPublic.get(), 全_CryptoKeyPublic.getSize(), TRUE, "");
			encrypt_data = openssl_rsa_decrypt(key, encrypt_data.getTextPtrA(), encrypt_data.getSize());
			if(encrypt_data.getSize() != 24)
			{
				xcl_log(L"RSA解密失败");
				return L"";
			}
			随机密钥 = xcl_toTextA(encrypt_data);
			临时密文 = json["a"].as_text();
			encrypt_data = X_Check_BASE64_DeCodeA(临时密文);
			encrypt_data = openssl_cipher(encrypt_data.getTextPtrA(), encrypt_data.getSize(), 随机密钥.get(), iv, 0, EVP_aes_192_cbc());
			返回文本 = (xcl_toTextA(encrypt_data));
		}
		else
		{
			encrypt_data = X_Check_BASE64_DeCodeA(json["a"].as_text());
			encrypt_data = openssl_cipher(encrypt_data.getTextPtrA(), encrypt_data.getSize(), 全_CryptoKeyAes.get(), iv, 0, EVP_aes_192_cbc());
			返回文本 = XC_utf8toa(xcl_toTextA(encrypt_data).get());
		}
	}
	return 返回文本;
}
CXTextA 取MD5(CXTextA& 数据)
{
	BYTE md_value[EVP_MAX_MD_SIZE];
	UINT md_len;
	CXTextA MD5结果;
	CXTextA 临时;
	EVP_MD_CTX* mdctx = EVP_MD_CTX_new() ;
	EVP_DigestInit_ex(mdctx, EVP_md5(), NULL) ;
	EVP_DigestUpdate(mdctx, 数据.get(), strlen(数据.get())) ;
	EVP_DigestFinal_ex(mdctx, md_value, &md_len) ;
	EVP_MD_CTX_free(mdctx) ;
	临时 = xcl_dataToHexTextA(md_value, EVP_MAX_MD_SIZE, FALSE, FALSE);
	MD5结果 = text_getLeftA(临时.get(), 32);
	return MD5结果;
}
CXTextA encryptAndSign(CXTextA& 数据, BOOL RSA_encrypt)
{
	CXTextA 返回结果;
	CXTextA JOSN_A;
	CXTextA JOSN_B;
	CXBytes encrypt_data;
	char iv[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0};
	if((全_CryptoType == 1) )
	{
		返回结果 = 数据;
	}
	if((全_CryptoType == 2) )
	{
		encrypt_data = openssl_cipher(数据.getPtr(), 数据.getSize(), 全_CryptoKeyAes.get(), iv, 1, EVP_aes_192_cbc());
		JOSN_A = X_Check_BASE64_EnCode_BytesA(encrypt_data);
		xcl_log(encrypt_data.toHexA(), JOSN_A);
		JOSN_B = 取MD5(JOSN_A+全_CryptoKeyAes);
		返回结果 = R"({"a": "11111","b": "22222"})";
		返回结果 = text_replaceA(返回结果.get(), "11111", JOSN_A.get());
		返回结果 = text_replaceA(返回结果.get(), "22222", JOSN_B.get());
		xcl_log(返回结果);
	}
	if((全_CryptoType == 3 && RSA_encrypt) )
	{
		CXTextA 随机AES_KEY = text_getRandCharA(24);
		encrypt_data = openssl_cipher(数据.getPtr(), 数据.getSize(), 随机AES_KEY.get(), iv, 1, EVP_aes_192_cbc());
		JOSN_A = X_Check_BASE64_EnCode_BytesA(encrypt_data);
		EVP_PKEY* key = openssl_rsa_pem_getkey(全_CryptoKeyPublic.get(), 全_CryptoKeyPublic.getSize(), TRUE, "");
		encrypt_data = openssl_rsa_encrypt(key, 随机AES_KEY.get(), 随机AES_KEY.getSize());
		JOSN_B = X_Check_BASE64_EnCode_BytesA(encrypt_data);
		返回结果 = R"({"a": "11111","b": "22222"})";
		返回结果 = text_replaceA(返回结果.get(), "11111", JOSN_A.get());
		返回结果 = text_replaceA(返回结果.get(), "22222", JOSN_B.get());
		xcl_log(返回结果);
	}
	else
	{
		xcl_log(数据);
		encrypt_data = openssl_cipher(数据.getPtr(), 数据.getSize(), 全_CryptoKeyAes.get(), iv, 1, EVP_aes_192_cbc());
		xcl_log(encrypt_data.toHexA());
		JOSN_A = X_Check_BASE64_EnCode_BytesA(encrypt_data);
		xcl_log(encrypt_data.toHexA(), JOSN_A);
		JOSN_B = 取MD5(JOSN_A+全_CryptoKeyAes);
		返回结果 = R"({"a": "11111","b": "22222"})";
		返回结果 = text_replaceA(返回结果.get(), "11111", JOSN_A.get());
		返回结果 = text_replaceA(返回结果.get(), "22222", JOSN_B.get());
		xcl_log(返回结果);
	}
	return 返回结果;
}
BOOL 快验_取Token()
{
	CJsonObject json数组;
	CJsonObject JSON;
	json数组["Api"] = "GetToken";
	int 电脑时间戳 = time(NULL);
	json数组["Time"] = 电脑时间戳;
	json数组["Status"] = xcl_getRand(10001, 2146666666);
	xcl_log(json数组.writetext());
	CXTextA 密文 = encryptAndSign(json数组.writetext(), TRUE);
	CXTextA tempText = sendPacketGetToken(密文, FALSE);
	xcl_log(tempText);
	JSON.parse(tempText.get());
	if(json数组.get_obj("Time").as_int() - JSON.get_obj("Time").as_int() > 1800)
	{
		xcl_log(L"封包时间异常");
		return FALSE;
	}
	if(json数组.get_obj("Status").as_int() != JSON.get_obj("Status").as_int())
	{
		xcl_log(L"Status 合法性错误");
		return FALSE;
	}
	全_CryptoKeyAes = JSON["Data"]["CryptoKeyAes"].as_text();
	全_Token = JSON["Data"]["Token"].as_text();
	if(全_Token == CXText(L""))
	{
		xcl_log(L"Token 返回空白");
		return FALSE;
	}
	return TRUE;
}
CXTextA 快验_取应用公告()
{
	CJsonObject json数组;
	CJsonObject JSON;
	json数组["Api"] = "GetAppGongGao";
	int 电脑时间戳 = time(NULL);
	json数组["Time"] = 电脑时间戳;
	json数组["Status"] = xcl_getRand(10001, 2146666666);
	CXTextA 密文 = encryptAndSign(json数组.writetext());
	CXTextA tempText = sendPacketGetToken(密文, TRUE);
	xcl_log(tempText);
	JSON.parse(tempText.get());
	if(json数组.get_obj("Time").as_int() - JSON.get_obj("Time").as_int() > 1800)
	{
		xcl_log(L"封包时间异常");
		return (L"封包时间异常");
	}
	if(json数组.get_obj("Status").as_int() != JSON.get_obj("Status").as_int())
	{
		xcl_log(L"Status 合法性错误");
		return (L"Status 合法性错误");
	}
	return JSON["Data"]["AppGongGao"].as_text();
}
BOOL 快验_初始化(CXText& AppWeb, CXText& CryptoKeyAes, CXText& CryptoKeyPublic, int CryptoType)
{
	全_AppWeb = AppWeb;
	全_CryptoKeyAes = CryptoKeyAes;
	全_CryptoKeyPublic = CryptoKeyPublic;
	全_CryptoType = CryptoType;
	if(快验_取Token())
	{
		return TRUE;
	}
	return FALSE;
}
CXTextA 快验_注册用户(CXTextA& 账号, CXTextA& 密码, CXTextA& 绑定信息, CXTextA& 超级密码, CXTextA& QQ, CXTextA& 邮箱, CXTextA& 手机号)
{
	CJsonObject json数组;
	CJsonObject JSON;
	CXTextA JOSN文本 = R"({"Api":"NewUserInfo","User":"User1","PassWord":"PassWord1","Key":"Key1","SuperPassWord":"SuperPassWord1","Qq":"Qq1","Email":"Email1","Phone":"Phone1"})";
	JOSN文本 = text_replaceA(JOSN文本.get(), "User1", 账号.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "PassWord1", 密码.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "Key1", 绑定信息.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "SuperPassWord", 超级密码.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "Qq1", QQ.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "Email1", 邮箱.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "Phone1", 手机号.get());
	JOSN文本 = XC_atoutf8(JOSN文本.get());
	json数组.parse(JOSN文本.get());
	xcl_log(JOSN文本);
	int 电脑时间戳 = time(NULL);
	json数组["Time"] = 电脑时间戳;
	json数组["Status"] = xcl_getRand(10001, 2146666666);
	xcl_log(json数组.writetext());
	CXTextA 密文 = encryptAndSign(json数组.writetext());
	CXTextA tempText = sendPacketGetToken(密文, TRUE);
	xcl_log(tempText);
	JSON.parse(tempText.get());
	if(json数组.get_obj("Time").as_int() - JSON.get_obj("Time").as_int() > 1800)
	{
		xcl_log(L"封包时间异常");
		return (L"封包时间异常");
	}
	if(json数组.get_obj("Status").as_int() != JSON.get_obj("Status").as_int())
	{
		xcl_log(L"Status 合法性错误");
		return JSON["Msg"].as_text();
	}
	return tempText;
}
CXTextA 快验_用户登录_通用(CXTextA& 账号, CXTextA& 密码, CXTextA& 绑定信息, CXTextA& 动态标记, CXTextA& 当前版本)
{
	CJsonObject json数组;
	CJsonObject JSON;
	CXTextA JOSN文本 = R"({"Api":"UserLogin","UserOrKa":"UserOrKa1","PassWord":"PassWord1","Key":"Key1","Tab":"Tab1","AppVer":"AppVer1"})";
	JOSN文本 = text_replaceA(JOSN文本.get(), "UserOrKa1", 账号.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "PassWord1", 密码.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "Key1", 绑定信息.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "Tab1", 动态标记.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "AppVer1", 当前版本.get());
	JOSN文本 = XC_atoutf8(JOSN文本.get());
	json数组.parse(JOSN文本.get());
	xcl_log(JOSN文本);
	int 电脑时间戳 = time(NULL);
	json数组["Time"] = 电脑时间戳;
	json数组["Status"] = xcl_getRand(10001, 2146666666);
	xcl_log(json数组.writetext());
	CXTextA 密文 = encryptAndSign(json数组.writetext(), TRUE);
	CXTextA tempText = sendPacketGetToken(密文, TRUE);
	xcl_log(tempText);
	JSON.parse(tempText.get());
	if(json数组.get_obj("Time").as_int() - JSON.get_obj("Time").as_int() > 1800)
	{
		xcl_log(L"封包时间异常");
		return (L"封包时间异常");
	}
	if(json数组.get_obj("Status").as_int() != JSON.get_obj("Status").as_int())
	{
		xcl_log(L"Status 合法性错误");
		return JSON["Msg"].as_text();
	}
	return tempText;
}
CXTextA 快验_卡号充值(CXTextA& 充值账号, CXTextA& 充值卡号, CXTextA& 推荐人)
{
	CJsonObject json数组;
	CJsonObject JSON;
	CXTextA JOSN文本 = R"({"Api":"UseKa","User":"User1","Ka":"Ka1","InviteUser":"InviteUser1"})";
	JOSN文本 = text_replaceA(JOSN文本.get(), "User1", 充值账号.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "Ka1", 充值卡号.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "InviteUser1", 推荐人.get());
	JOSN文本 = XC_atoutf8(JOSN文本.get());
	json数组.parse(JOSN文本.get());
	xcl_log(JOSN文本);
	int 电脑时间戳 = time(NULL);
	json数组["Time"] = 电脑时间戳;
	json数组["Status"] = xcl_getRand(10001, 2146666666);
	xcl_log(json数组.writetext());
	CXTextA 密文 = encryptAndSign(json数组.writetext(), TRUE);
	CXTextA tempText = sendPacketGetToken(密文, TRUE);
	xcl_log(tempText);
	JSON.parse(tempText.get());
	if(json数组.get_obj("Time").as_int() - JSON.get_obj("Time").as_int() > 1800)
	{
		xcl_log(L"封包时间异常");
		return (L"封包时间异常");
	}
	if(json数组.get_obj("Status").as_int() != JSON.get_obj("Status").as_int())
	{
		xcl_log(L"Status 合法性错误");
		return JSON["Msg"].as_text();
	}
	return tempText;
}
CXTextA 快验_取公共变量(CXTextA& 变量名)
{
	CJsonObject json数组;
	CJsonObject JSON;
	CXTextA JOSN文本 = R"({"Api":"GetPublicData","Name":"Name1"})";
	JOSN文本 = text_replaceA(JOSN文本.get(), "Name1", 变量名.get());
	JOSN文本 = XC_atoutf8(JOSN文本.get());
	json数组.parse(JOSN文本.get());
	xcl_log(JOSN文本);
	int 电脑时间戳 = time(NULL);
	json数组["Time"] = 电脑时间戳;
	json数组["Status"] = xcl_getRand(10001, 2146666666);
	xcl_log(json数组.writetext());
	CXTextA 密文 = encryptAndSign(json数组.writetext(), TRUE);
	CXTextA tempText = sendPacketGetToken(密文, TRUE);
	xcl_log(tempText);
	JSON.parse(tempText.get());
	if(json数组.get_obj("Time").as_int() - JSON.get_obj("Time").as_int() > 1800)
	{
		xcl_log(L"封包时间异常");
		return (L"封包时间异常");
	}
	if(json数组.get_obj("Status").as_int() != JSON.get_obj("Status").as_int())
	{
		xcl_log(L"Status 合法性错误");
		return JSON["Msg"].as_text();
	}
	return tempText;
}
CXTextA 快验_调用公共函数(CXTextA& 函数名, CXTextA& JSON格式参数, BOOL 是否为全局函数)
{
	CJsonObject json数组;
	CJsonObject JSON;
	CXTextA JOSN文本 = R"({"Api":"RunJS","Parameter":"Parameter1","JsName":"JsName1","IsGlobal":IsGlobal1})";
	JOSN文本 = text_replaceA(JOSN文本.get(), "Parameter1", JSON格式参数.get());
	JOSN文本 = text_replaceA(JOSN文本.get(), "JsName1", 函数名.get());
	JOSN文本 = XC_atoutf8(JOSN文本.get());
	if(是否为全局函数)
	{
		JOSN文本 = text_replaceA(JOSN文本.get(), "IsGlobal1", "true");
	}
	else
	{
		JOSN文本 = text_replaceA(JOSN文本.get(), "IsGlobal1", "false");
	}
	json数组.parse(JOSN文本.get());
	xcl_log(JOSN文本);
	int 电脑时间戳 = time(NULL);
	json数组["Time"] = 电脑时间戳;
	json数组["Status"] = xcl_getRand(10001, 2146666666);
	xcl_log(json数组.writetext());
	CXTextA 密文 = encryptAndSign(json数组.writetext(), TRUE);
	CXTextA tempText = sendPacketGetToken(密文, TRUE);
	xcl_log(tempText);
	JSON.parse(tempText.get());
	if(json数组.get_obj("Time").as_int() - JSON.get_obj("Time").as_int() > 1800)
	{
		xcl_log(L"封包时间异常");
		return (L"封包时间异常");
	}
	if(json数组.get_obj("Status").as_int() != JSON.get_obj("Status").as_int())
	{
		xcl_log(L"Status 合法性错误");
		return JSON["Msg"].as_text();
	}
	return tempText;
}
