import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

AddItem("开启/关闭", "开关状态");

public void 开关状态(String s) {
    String uin = getUin();
    if ("0".equals(uin)) {
        Toast("当前窗口异常，请重新进入该聊天");
        return;
    }

    boolean currentStatus = getBoolean("开关状态", uin, true);
    putBoolean("开关状态", uin, !currentStatus);
    Toast("群/私聊" + uin + (currentStatus ? "已关闭" : "已开启"));
}

public String getUin() {
    if (getChatType() == 1) {
        return getCurrentFriendUin();
    } else if (getChatType() == 2) {
        return getCurrentGroupUin();
    }
    return "0";
}

public void onMsg(Object msg) {
    if (!msg.IsGroup) return;

    String targetSubstring = "https://gateway.platoboost.com/a/8?id=";
    String uin = getUin();
    boolean 开关状态Status = getBoolean("开关状态", uin, true);

    if (!开关状态Status) {
        return;
    }

    if (msg.MessageContent.contains(targetSubstring)) {
        sendMsg(msg.GroupUin, "", "检测到delta卡密链接，正在帮您绕过!!!");
        Pattern pattern = Pattern.compile("https://gateway.platoboost.com/a/8\\?id=([a-zA-Z0-9]+)");
        Matcher matcher = pattern.matcher(msg.MessageContent);
        
        if (matcher.find()) {
            String hwid = matcher.group(1);
            try {
                String apiURL = "http://95.216.39.179:6780/api/bypass?link=https://gateway.platoboost.com/a/8?id=" + URLEncoder.encode(hwid, "UTF-8");
                String responseText = sendHttpGetRequest(apiURL);
                
                // 解析返回的 JSON
                if (responseText.contains("\"key\":\"")) {
                    String key = responseText.replaceAll(".*\"key\":\"([^\"]+)\".*", "$1");
                    // 检查是否有特定返回
                    if ("Delta bypass hcaptcha patched. We only support links with out hcaptcha".equals(key)) {
                        sendMsg(msg.GroupUin, "", "有人机验证，请手动操作。");
                    } else {
                        String atUser = "[AtQQ=" + msg.UserUin + "]";
                        sendMsg(msg.GroupUin, "", atUser + " " + key);
                    }
                } else {
                    sendMsg(msg.GroupUin, "", "请求结果格式不正确: " + responseText);
                }
            } catch (Exception e) {
                e.printStackTrace();
                sendMsg(msg.GroupUin, "", "请求失败: " + e.getMessage());
            }
        } else {
            sendMsg(msg.GroupUin, "", "无法提取 HWID");
        }
    }
}

private String sendHttpGetRequest(String apiURL) throws Exception {
    HttpURLConnection connection = (HttpURLConnection) new URL(apiURL).openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", "application/json");
    connection.setConnectTimeout(5000); // 设置连接超时
    connection.setReadTimeout(5000); // 设置读取超时

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
}

// 其余代码....