<?php
require_once 'config.php';
if (!extension_loaded('sodium')) die('   !');
if (!is_dir(KEYS_DIR)) mkdir(KEYS_DIR, 0755, true);
if (!is_dir(CONFIGS_DIR)) mkdir(CONFIGS_DIR, 0755, true);
$message = ''; $download_link = '';
$subscription_link = 'https://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/subscription.php?token=' . SUB_TOKEN;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['client_name'])) {
    $client_name = preg_replace('/[^a-zA-Z0-9_-]/', '', $_POST['client_name']);
    if (empty($client_name)) { $message = '   .'; }
    else {
        $keypair = sodium_crypto_box_keypair();
        $private_b64 = sodium_bin2base64(sodium_crypto_box_secretkey($keypair), SODIUM_BASE64_VARIANT_ORIGINAL);
        $public_b64 = sodium_bin2base64(sodium_crypto_box_publickey($keypair), SODIUM_BASE64_VARIANT_ORIGINAL);
        file_put_contents(KEYS_DIR . $client_name . '.priv', $private_b64);
        file_put_contents(KEYS_DIR . $client_name . '.pub', $public_b64);
        $client_ip = BASE_IP . (10 + count(glob(CONFIGS_DIR . '*.conf')));
        $config = "[Interface]\nPrivateKey = $private_b64\nAddress = $client_ip/32\nDNS = " . SERVER_DNS . "\n\n[Peer]\nPublicKey = " . SERVER_PUBLIC_KEY . "\nEndpoint = " . SERVER_ENDPOINT . "\nAllowedIPs = " . ALLOWED_IPS . "\nPersistentKeepalive = 25\n";
        $filename = $client_name . '.conf';
        file_put_contents(CONFIGS_DIR . $filename, $config);
        $download_link = DOWNLOAD_BASE_URL . urlencode($filename);
        $message = "  $client_name  !";
    }
}
?>
<!DOCTYPE html>
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title> WireGuard</title>
<style>body{font-family:Tahoma;direction:rtl;background:#f4f4f4;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;}.box{background:white;padding:30px;border-radius:12px;box-shadow:0 4px 12px rgba(0,0,0,0.1);width:90%;max-width:500px;}input,button{padding:10px;margin:5px 0;width:100%;box-sizing:border-box;border:1px solid #ddd;border-radius:6px;}button{background:#28a745;color:white;font-weight:bold;border:none;cursor:pointer;}.link-box{background:#e9f7ef;padding:15px;border-radius:8px;margin-top:20px;word-break:break-all;}a{color:#0066cc;text-decoration:none;}hr{margin:20px 0;}.sub-box{background:#fff3cd;border:1px solid #ffc107;padding:15px;border-radius:8px;}</style>
</head><body><div class="box"><h2>   </h2><form method="post"><input type="text" name="client_name" placeholder=" " required><button type="submit"> </button></form>
<?php if ($message) echo "<p style='color:green;'>$message</p>"; ?>
<?php if ($download_link): ?><div class="link-box"><p><strong> :</strong></p><a href="<?= $download_link ?>" target="_blank"><?= $download_link ?></a></div><?php endif; ?>
<hr><div class="sub-box"><p><strong>  :</strong></p><a href="<?= $subscription_link ?>" target="_blank"><?= $subscription_link ?></a><br><small> Hiddify / NekoBox </small></div></div></body></html>