#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dirent.h>
#include <pthread.h>
#include <fstream>
#include <string.h>
#include <time.h>
#include <malloc.h>
#include <iostream>
#include <fstream>

#define MAX_LINE 1024
#include "res/网络操作.h"
#include "res/cJSON.h"
#include "res/cJSON.c"
#include "res/Encrypt.h"

#include<iostream>
#include<ctime>


using namespace std;

#define MAX_LINE 1024

int main(int argc, char *argv[])
{
	yjjm("���");
	printf("\033[35;1m");		// 粉红色
	printf("欢迎使用极简云验证\n\n");
	printf("极简云验证，创造无限可能！\n\n制作人：抹布\n");
	printf("\033[32;1m");		// 绿色
	printf("\n极简云官网:https://simpleauth.cn\n\n");
	printf("\033[33;1m");		// 黄色

    //内测地址暂不支持对接，所以注入后无用
	const static char *host = "simpleauth.cn";
	// 填入 http://后/?前的链接

	const static char *APPID = "APPID";
	// 填入 APPID

	const static char *APPKEY = "APPKEY";
	// 填入 APPKEY

	const static char *RC4KEY = "RC4KEY";
	// 用户管理后台-RC4密钥

	const static char *RC4_switch = "开";
	// RC4 加密开关

	const static char *km_luj = "/sdcard/km";
	// 卡密路径

	const static char *imei_luj = "/sdcard/imei";
	// 机器码路径

	char km[40];				// 卡密
	if (fopen(km_luj, "r") == NULL)
	{

		printf("\033[31;1m");
		printf("卡密读取失败\n");
		exit(1);
	}
	fscanf(fopen(km_luj, "r"), "%s", &km);


	char imei[40];				// 设备码
	if (fopen(imei_luj, "r") == NULL)
	{

		printf("\033[31;1m");
		printf("卡密读取失败\n");
		exit(1);
	}
	fscanf(fopen(imei_luj, "r"), "%s", &imei);


	printf("卡密： %s\n设备码： %s\n", km, imei);
	// ---------------------------------------------------------


	if (km == "" or imei == "")
	{

		printf("\033[31;1m");
		printf("没有设备码或者卡密");
		exit(1);
	}



	char *u;
	char url[256];
	char post[1024];
	char *key;
	time_t t;
	t = time(NULL);
	int ii = time(&t);
	// 时间搓 

	// ---------------------------------------------------------

	sprintf(url, "api.php?api=kmlogon&app=%s", APPID);
	sprintf(key, "kami=%s&markcode=%s&t=%d&%s", km, imei, ii, APPKEY);
	char *aaa = key;
	unsigned char *bbb = (unsigned char *)aaa;
	MD5_CTX md5c;
	MD5Init(&md5c);
	int i;
	unsigned char decrypt[16];
	MD5Update(&md5c, bbb, strlen((char *)bbb));
	MD5Final(&md5c, decrypt);
	char lkey[32] = { 0 };

	for (i = 0; i < 16; i++)
	{
		sprintf(&lkey[i * 2], "%02x", decrypt[i]);
	}
	// md5验证签名
	// ---------------------------------------------------------

	sprintf(post, "kami=%s&markcode=%s&t=%d&sign=%s", km, imei, ii, lkey);


	if (RC4_switch == "开")
	{
		sprintf(post, "data=%s", Encrypt(post, RC4KEY));
		// RC4加密发送 数据
	}

	httppost(host, url, post, &u);


	if (RC4_switch == "开")
	{
		sprintf(u, "%s", Decrypt(u, RC4KEY));
	}


	cJSON *cjson = cJSON_Parse(u);
	printf("\033[37;1m");		// 白色
	if (cjson == NULL)
	{
		printf("网络错误...");
	}
	else
	{
		printf("\n服务器正常  ✧*｡٩(ˊωˋ*)و✧*｡\n");
	}



	int code = cJSON_GetObjectItem(cjson, "code")->valueint;
	char *msg = cJSON_GetObjectItem(cjson, "msg")->valuestring;
	int time = cJSON_GetObjectItem(cjson, "time")->valueint;
	char *check = cJSON_GetObjectItem(cjson, "check")->valuestring;
	cJSON_Delete(cjson);



	printf("\033[31;1m");		// 红色
	if (code == 200)
	{
		printf("\n-----[开始验证****数据]----------\n\n");

		if (time - ii > 10 or time - ii < -10)
			// 验证时间小于10秒 大于10秒判定中间拦截数据
		{
			printf("\n[验证****数据失败]-----\n>>>开始格式化>>>>\n");
			// ↓↓↓↓验证识别你们想干啥随便---------------------------------------------------------




			// ↑↑↑↑↑可能误判😂😂
			// ---------------------------------------------------------
		}
		else
		{
			printf("\033[32;1m");	// 绿色
			printf
				("\n[验证成功，欢迎使用！]\n------------------\n(゜v゜)\n-----------------\n");

			cJSON *root = cJSON_Parse(u);
			cJSON *object = cJSON_GetObjectItem(root, "msg");
			cJSON *item;
			item = cJSON_GetObjectItem(object, "vip");
			char *kami = cJSON_GetObjectItem(object, "kami")->valuestring;
			// 卡密
			char *vip = cJSON_GetObjectItem(object, "vip")->valuestring;
			// 到期时间戳
			cJSON_Delete(root);
			// ---------------------------------------------------------
			time_t curtime;
			curtime = atoi(vip);
			char temp[100];
			curtime += 28800;
			struct tm *timeSet = gmtime(&curtime);
			strftime(temp, 50, "%Y-%m-%d %H:%M:%S", timeSet);
		
			printf("\n卡密： %s\n", kami);
			
			printf("\n到期时间： %s", temp);
			 
			// 功能可以写在下面---------------------------------------------------------
            //content

			// ↑↑↑↑↑↑↑↑--------------------------------------------------------- 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
			// 
		}
	}
	else
	{
		printf("\n%s", msg);	// 错误提示
	}


}