#pragma once
#include "DNFTOOL.hpp"


// 自定义结构体，用于封装参数
struct VerifyThreadParams {
    std::string data;
};

static DWORD VerifyLogic(LPVOID lpParam) {
    std::string Ip = "1.2.3.4";
    // 将传入的参数转换为自定义结构体指针
    VerifyThreadParams* params = static_cast<VerifyThreadParams*>(lpParam);
    if (params) {
        Ip = params->data;
        // 释放动态分配的内存
        delete params;
    }

    //获取验证服务器ip
    std::string Rqip = DNFTOOL::GetIP();
    //std::cout << Rqip << std::endl;

    //如果没有DFC180文件
    if (Rqip.length() == 0)return 0;


    std::vector<std::string> IpArr;
    DNFTOOL::Split(Rqip, IpArr, "->");

    for (auto it = IpArr.begin(); it != IpArr.end(); ++it) {
        //验证并请求加载脚本
        std::string Domain = *it;

        bool In = DNFTOOL::ReqIpLicense(Ip, Domain);
        if (In)return 1;
    }
    return 0;
}

static DWORD WINAPI VerifyThread(LPVOID lpParam)
{
    return VerifyLogic(lpParam);
}


// 函数指针定义
typedef int (WINAPI* SENDTO)(SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int tolen);
SENDTO originalSendTo = nullptr;

// Hook函数
int WINAPI HookedSendTo(SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int tolen) {
    static bool vflag = false;
    if (to != nullptr && tolen >= sizeof(sockaddr_in)) {
        if (to->sa_family == AF_INET) {
            const sockaddr_in* addr = reinterpret_cast<const sockaddr_in*>(to);
            u_short port = ntohs(addr->sin_port);
            //std::cout << "Port： " << port << std::endl;

            //if (port == 2313 && !vflag) {
                //vflag = true;
                char ipStr[INET_ADDRSTRLEN];
                inet_ntop(AF_INET, (void*)(&(addr->sin_addr)), ipStr, INET_ADDRSTRLEN);
                std::string Ip(ipStr);
                //std::cout << "IP：" << Ip << std::endl;
//#ifdef SELL
//                VerifyThreadParams* params = new VerifyThreadParams;
//                params->data = Ip;
//                //多线程验证
//                DWORD threadID;
//                HANDLE Thand = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)VerifyThread, params, 0, &threadID);
//#endif
//                MH_DisableHook(&sendto);
            //}
        }
    }

    return originalSendTo(s, buf, len, flags, to, tolen);
}


// 声明原始 connect 函数的类型
typedef int (WINAPI* CONNECT)(SOCKET s, const struct sockaddr* name, int namelen);

// 存储原始 connect 函数的指针
CONNECT pOriginalConnect = NULL;
// 自定义的 connect 函数，用于替代原始函数
int WINAPI HookedConnect(SOCKET s, const struct sockaddr* name, int namelen) {
    // 打印连接信息
    if (name->sa_family == AF_INET) {
        const sockaddr_in* addr = reinterpret_cast<const sockaddr_in*>(name);
        u_short port = ntohs(addr->sin_port);
        //std::cout << "port:" << port << std::endl;
        if (port == 7001) {
            // IPv4 地址
            struct sockaddr_in* addr = (struct sockaddr_in*)name;
            char ip[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN);

            std::string Ip(ip);
            //std::cout << "Ip:" << Ip << std::endl;
#ifdef SELL
                VerifyThreadParams* params = new VerifyThreadParams;
                params->data = Ip;
                //多线程验证
                DWORD threadID;
                HANDLE Thand = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)VerifyThread, params, 0, &threadID);
#endif
                MH_DisableHook(&connect);
        }
    }

    // 调用原始的 connect 函数
    return pOriginalConnect(s, name, namelen);
}
