// dllmain.cpp : 定义 DLL 应用程序的入口点。


#include "pch.h"








void LenheartThread()
{
    while (true)
    {
        Sleep(10);
        static int 可开始执行判断;
        if (DNFTOOL::GetHook(0x1A5FB4C, "0x14+0x28+") == 6)
        {
            //imguiC* ImguiObj = new imguiC();
            //ImguiObj->Run();
            可开始执行判断 = 1;
        }
        if (可开始执行判断 == 1)
        {
            //获取验证服务器ip
            std::string Rqip = DNFTOOL::GetIP();
            //std::cout << "验证服务器IP:" << Rqip << std::endl;

            //获取本机IP
            std::string MyIp;
            httplib::Client* IPCliObj = NULL;// http连接主体
            IPCliObj = new httplib::Client("ip.json-json.com");//初始化 http 对象
            auto MyIpres = IPCliObj->Get("/");
            if (MyIpres->status == 200)//如果返回包正常
            {
                MyIp = MyIpres->body;//取得date      
            }
            //std::cout << "本机IP:" << MyIp << std::endl;
            //已经得到了本机IP

            //获取验证IP
            std::string ippack;
            //获取ExeIP
            int gameip = *(int*)0x1AE9CEC;
            //如果ExeIP 等于 192.168.200.131
            if (gameip == 3735601)
            {
                ippack = MyIp;
            }
            else
            {
                wchar_t* wgameip = (wchar_t*)0x1AE9CEC;
                DNFTOOL::Wchar_tToString(ippack, wgameip);
            }
            //std::cout << "验证IP:" << ippack << std::endl;
            //已经获取了要发送的验证IP

            LenheartBase::CBASE64 bb;
            ippack += "nima";
            std::string rsastring = bb.RsaPriEncrypt(ippack, Pri_key);
            std::string enstring = bb.encode(rsastring);


            httplib::Client* CliObj = NULL;// http连接主体
            CliObj = new httplib::Client(Rqip + ":9007");//初始化 http 对象

            httplib::Params ParamsObj;//新建 Params 对象
            ParamsObj.emplace("ip", enstring.c_str());//加入账号数据进数据包
            auto res = CliObj->Post("/yz", ParamsObj);

            if (res->status == 200)//如果返回包正常
            {
                std::string date = res->body;//取得date
                std::string base64code = bb.decode(date);
                std::string decrypt_text = bb.RsaPriDecrypt(base64code, Pri_key);
                long long redate = atoll(decrypt_text.c_str()) / 1000;
                time_t myt = time(0);
                long long nowdate = (long long)myt;


                __int64 absnum = abs(nowdate - redate);
                if (absnum < 18000)
                {
#ifdef SELL
                    auto res = CliObj->Post("/transfer/getscript", ParamsObj);
                    if (res->status == 200)//如果返回包正常
                    {
                        std::string date = res->body;//取得date
                        //std::cout << date << std::endl;
                        if (!date.empty())
                        {
                            rapidjson::Document Dom;
                            Dom.Parse(date.c_str());//加载 字符串
                            int Size = Dom["size"].GetInt();//判断类型
                            for (int i = 0; i < Size; i++)
                            {
                                std::string filename = Dom["list"].GetArray()[i].GetArray()[0].GetString();
                                std::string str = Dom["list"].GetArray()[i].GetArray()[1].GetString();
                                //std::cout << "当前文件个数: " << i << std::endl << "当前文件名 :" << filename << std::endl << "当前文件内容  :"<< str << std::endl;

                                str = str.substr(str.find("[") + 1, str.length() - 2);

                                std::vector<std::string> Data;
                                DNFTOOL::Split(str, Data, ", ");
                                size_t Ds = Data.size();

                                char* nutstr = new char[Ds + 1];


                                for (size_t s = 0; s < Ds; s++)
                                {
                                    nutstr[s] = char(atoi(Data[s].c_str()));

                                }
                                nutstr[Ds] = '\0';

                                int skey[] = Skey;//定义解密数组
                                Cutecode(nutstr, skey);//解密

                                wchar_t* sfile = DNFTOOL::charTowchar_t((char*)filename.c_str());
                                wchar_t* ss = DNFTOOL::charTowchar_t((char*)nutstr);

                                //wprintf(L"Function:%s \n", sfile);
                                uint32_t v = GetSqVm();
                                squirrel::SQdofileBuffer(v, sfile, ss);

                            }
                        }
                    }

#endif // SELL
                    return;
                }
                else
                {
                    ExitProcess(0);
                }
            }
            else
            {
                ExitProcess(0);
            }
        }
    }
}


__declspec(dllexport) void Lenheart()
{

    DWORD threadID;
    HANDLE Thand = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LenheartThread, NULL, 0, &threadID);
}


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{

    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        

#if defined COUTWINDOWS_SWITCH

        AllocConsole();
        freopen(("CONOUT$"), ("w"), stdout);
        freopen(("CONOUT$"), ("w"), stderr);
        freopen(("CONIN$"), ("r"), stdin);

#endif
        //getchar();
        hook::RegisterHook();
        Lenheart();
        //::CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)InitD3d, NULL, NULL, NULL);
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

