<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/functions.php';

$page_title   = 'DNS Leak Test – Check if your DNS is leaking';
$page_desc    = 'Free DNS Leak Test: check if your DNS requests are leaking outside your VPN tunnel – on ipcheck.tools.';
$page_current = 'dns-leak';

$token   = isset($_GET['token']) ? preg_replace('/[^a-f0-9]/', '', $_GET['token']) : '';
$results = [];

if ($token && strlen($token) === 16) {
    // DNS leak log prüfen
    $log_file = __DIR__ . '/../../dns-leak-log.php';
    if (file_exists($log_file)) {
        $log = include $log_file;
        $results = $log[$token] ?? [];
    }
}

require_once __DIR__ . '/header.php';
?>

<div class="hero">
  <div class="hero-label">Tool</div>
  <div class="hero-title">DNS Leak Test</div>
  <div class="hero-sub">Check if your DNS requests are leaking outside your VPN tunnel</div>
</div>

<div class="wrap">

  <div class="ad-slot"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8287576653347400" data-ad-slot="2715725452" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>

  <div class="card card-blue">
    <div class="card-title"><i class="ti ti-shield-off"></i> DNS Leak Test</div>

    <div id="dns-status" style="text-align:center; padding:20px;">
      <div class="spinner" style="margin:0 auto 12px;"></div>
      <p style="font-size:13px; color:var(--text4);">Starting DNS leak test...</p>
    </div>

    <div id="dns-result" style="display:none;">
      <div id="dns-servers"></div>
    </div>
  </div>

  <div class="grid2">
    <div class="card">
      <div class="card-title"><i class="ti ti-info-circle"></i> What is a DNS leak?</div>
      <p style="font-size:13px; color:var(--text3); line-height:1.7;">A DNS leak occurs when your DNS requests are sent outside your VPN tunnel – to your ISP's DNS servers instead of your VPN provider's servers. This reveals which websites you visit to your ISP, even though you are using a VPN.</p>
    </div>
    <div class="card">
      <div class="card-title"><i class="ti ti-shield"></i> How to fix DNS leaks</div>
      <div class="data-row"><span class="dk">VPN client</span><span class="dv" style="font-size:12px;">Use DNS leak protection feature</span></div>
      <div class="data-row"><span class="dk">DNS settings</span><span class="dv" style="font-size:12px;">Set DNS to 1.1.1.1 or 8.8.8.8</span></div>
      <div class="data-row"><span class="dk">Router</span><span class="dv" style="font-size:12px;">Configure DNS on router level</span></div>
      <div class="data-row"><span class="dk">Browser</span><span class="dv" style="font-size:12px;">Enable DNS-over-HTTPS (DoH)</span></div>
    </div>
  </div>

  <div class="card">
    <div class="card-title"><i class="ti ti-arrow-right"></i> Continue with</div>
    <div style="display:flex; gap:8px; flex-wrap:wrap;">
      <a href="/en/webrtc.php" class="btn btn-primary" style="font-size:12px; padding:6px 14px;">
        <i class="ti ti-broadcast"></i> WebRTC Test
      </a>
      <a href="/en/ip-lookup.php" class="btn btn-ghost" style="font-size:12px; padding:6px 14px;">
        <i class="ti ti-world-search"></i> IP Lookup
      </a>
    </div>
  </div>

  <div class="ad-slot"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8287576653347400" data-ad-slot="2715725452" data-ad-format="auto" data-full-width-responsive="true"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>

  <div class="tools-row">
    <a href="/en/"             class="tool-btn"><i class="ti ti-home"></i><span>My IP</span></a>
    <a href="/en/dns-leak.php" class="tool-btn current"><i class="ti ti-shield-off"></i><span>DNS Leak</span></a>
    <a href="/en/webrtc.php"   class="tool-btn"><i class="ti ti-broadcast"></i><span>WebRTC</span></a>
    <a href="/en/dns-lookup.php" class="tool-btn"><i class="ti ti-search"></i><span>DNS Lookup</span></a>
    <a href="/en/ip-lookup.php" class="tool-btn"><i class="ti ti-world-search"></i><span>IP Lookup</span></a>
  </div>

  <div class="seo-box">
    <h2>DNS Leak Test – protect your privacy</h2>
    <p>A DNS leak test checks whether your DNS requests go through your VPN tunnel or bypass it. When using a VPN, all DNS requests should go through the VPN's DNS servers – not your ISP's servers. If DNS requests leak, your ISP can see which websites you visit despite using a VPN.</p>
  </div>



<script>
async function runDNSLeakTest() {
  const token = Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10);
  const promises = [];

  for (let i = 1; i <= 6; i++) {
    promises.push(
      fetch(`https://${token}-${i}.leak.ipcheck.tools/`, {mode: 'no-cors', cache: 'no-store'})
        .catch(() => {})
    );
  }

  await Promise.allSettled(promises);
  await new Promise(r => setTimeout(r, 1500));

  try {
    const res = await fetch(`/en/dns-leak.php?token=${token}`);
    const html = await res.text();
    const parser = new DOMParser();
    const doc = parser.parseFromString(html, 'text/html');
    const servers = doc.getElementById('dns-servers');

    document.getElementById('dns-status').style.display = 'none';
    document.getElementById('dns-result').style.display = 'block';

    if (servers && servers.innerHTML.trim()) {
      document.getElementById('dns-servers').innerHTML = servers.innerHTML;
    } else {
      document.getElementById('dns-servers').innerHTML = `
        <div class="alert alert-success">
          <i class="ti ti-shield-check"></i>
          <span>DNS Leak Test complete. No DNS servers detected – your DNS requests appear to be secure.</span>
        </div>`;
    }
  } catch(e) {
    document.getElementById('dns-status').innerHTML = '<div class="alert alert-warn"><i class="ti ti-alert-triangle"></i><span>Test could not be completed. Please try again.</span></div>';
  }
}

runDNSLeakTest();
</script>

<?php require_once __DIR__ . '/footer.php'; ?>
