<?php
/**
 * License 生成器 - PHP 适配版
 * 建议环境：PHP 7.4 或 8.x (需开启 openssl 扩展)
 */

// --- 配置区 ---
// 这是你 main.go 里的私钥 (Base64 格式)
$privateKeyBase64 = "02aUU4EpZZ3w5DgAZsf/aTbMS9JI7HosVYeNPtuYpWZICxQzwj4BooZJ8SDakqwPNjNqbLmac7H59bgF814yZA==";

// 处理生成请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['hwid'])) {
    $hwid = trim($_POST['hwid']);
    
    if (empty($hwid)) {
        die("错误：机器码不能为空");
    }

    // 解码 Base64 私钥
    $rawKey = base64_decode($privateKeyBase64);

    // 提示：标准 PHP 的 openssl_sign 可能不支持 Ed25519
    // 如果你的 PHP 环境安装了 sodium 扩展（PHP 7.2+ 自带），效果最好
    if (function_exists('sodium_crypto_sign_detached')) {
        // Ed25519 私钥在 PHP sodium 中通常需要种子(seed)，
        // 或者直接提取前 32 字节。这里我们处理原始私钥：
        $signature = sodium_crypto_sign_detached($hwid, $rawKey);
        $signatureHex = bin2hex($signature);
    } else {
        // 兜底方案：如果服务器没开启 sodium，提示报错
        die("服务器错误：未开启 PHP sodium 扩展，请在宝塔面板 PHP 设置中安装/开启 sodium。");
    }

    // 强制下载文件
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="license.key"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    echo $signatureHex;
    exit;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>授权码在线生成</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #f0f2f5; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .card { background: #fff; padding: 2rem; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); width: 100%; max-width: 400px; }
        h2 { text-align: center; color: #1a1a1a; margin-bottom: 1.5rem; }
        .input-group { margin-bottom: 1rem; }
        label { display: block; margin-bottom: 0.5rem; color: #666; font-size: 0.9rem; }
        input[type="text"] { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 6px; box-sizing: border-box; font-size: 1rem; }
        button { width: 100%; padding: 12px; background: #007bff; color: white; border: none; border-radius: 6px; font-size: 1rem; font-weight: bold; cursor: pointer; transition: background 0.2s; }
        button:hover { background: #0056b3; }
        .footer { text-align: center; margin-top: 1rem; font-size: 0.8rem; color: #999; }
    </style>
</head>
<body>

<div class="card">
    <h2>License 生成器</h2>
    <form method="POST">
        <div class="input-group">
            <label for="hwid">机器码 (HWID)</label>
            <input type="text" id="hwid" name="hwid" placeholder="请输入用户的机器码" required>
        </div>
        <button type="submit">生成授权文件 (license.key)</button>
    </form>
    <div class="footer">
        生成的密钥文件请放置于软件根目录
    </div>
</div>

</body>
</html>