get_server_data.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. session_start();
  3. // 1. Проверяем AJAX-запрос
  4. if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
  5. dieAjaxError('Direct access not allowed');
  6. }
  7. // 2. Проверяем CSRF-токен
  8. if (empty($_GET['csrf']) || $_GET['csrf'] !== $_SESSION['csrf_token']) {
  9. dieAjaxError('Invalid CSRF token');
  10. }
  11. // Если все проверки пройдены, выполняем основной код
  12. function dieAjaxError($message) {
  13. header('HTTP/1.0 403 Forbidden');
  14. header('Content-Type: application/json');
  15. die(json_encode(['error' => $message]));
  16. }
  17. define("CONFIG", 1);
  18. require_once 'functions.php';
  19. $config_file = __DIR__ . '/config.php';
  20. if (!file_exists($config_file)) {
  21. die("Configuration file not found: $config_file");
  22. }
  23. $servers = require_once $config_file;
  24. $server_name = $_GET['server'] ?? '';
  25. if (!isset($servers[$server_name])) {
  26. die("Invalid server name");
  27. }
  28. $server = $servers[$server_name];
  29. $clients = getOpenVPNStatus($server);
  30. $banned_clients = getBannedClients($server, $clients);
  31. $accounts = getAccountList($server);
  32. // Генерируем HTML для этого сервера
  33. ob_start();
  34. ?>
  35. <h2><?= htmlspecialchars($server['title']) ?></h2>
  36. <div class="section">
  37. <h3>Active Connections</h3>
  38. <?php if (!empty($clients)): ?>
  39. <table>
  40. <thead>
  41. <tr>
  42. <th>Client</th>
  43. <th>Real IP</th>
  44. <th>Virtual IP</th>
  45. <th>Traffic</th>
  46. <th>Connected</th>
  47. <th>Status</th>
  48. <th>Actions</th>
  49. </tr>
  50. </thead>
  51. <tbody>
  52. <?php foreach ($clients as $client): ?>
  53. <tr class="<?= $client['banned'] ? 'banned' : '' ?>">
  54. <td><?= htmlspecialchars($client['name']) ?></td>
  55. <td><?= htmlspecialchars($client['real_ip']) ?></td>
  56. <td><?= htmlspecialchars($client['virtual_ip']) ?></td>
  57. <td>↓<?= $client['bytes_received'] ?> ↑<?= $client['bytes_sent'] ?></td>
  58. <td><?= htmlspecialchars($client['connected_since']) ?></td>
  59. <td>
  60. <span class="status-badge <?= $client['banned'] ? 'status-banned' : 'status-active' ?>">
  61. <?= $client['banned'] ? 'BANNED' : 'Active' ?>
  62. </span>
  63. </td>
  64. <td class="actions">
  65. <?php if ($client['banned']): ?>
  66. <button onclick="handleAction('<?= $server_name ?>', 'unban', '<?= htmlspecialchars($client['name']) ?>')"
  67. class="btn unban-btn">Unban</button>
  68. <?php else: ?>
  69. <button onclick="handleAction('<?= $server_name ?>', 'ban', '<?= htmlspecialchars($client['name']) ?>')"
  70. class="btn ban-btn">Ban</button>
  71. <?php endif; ?>
  72. </td>
  73. </tr>
  74. <?php endforeach; ?>
  75. </tbody>
  76. </table>
  77. <?php else: ?>
  78. <p>No active connections</p>
  79. <?php endif; ?>
  80. </div>
  81. <div class="section">
  82. <div class="spoiler">
  83. <div class="spoiler-title collapsed" onclick="toggleSpoiler(this)">
  84. Configured Account List (<?= count($accounts) ?>)
  85. </div>
  86. <div class="spoiler-content">
  87. <table>
  88. <thead>
  89. <tr>
  90. <th>Account</th>
  91. <th>Assigned IP</th>
  92. <th>Status</th>
  93. <th>Actions</th>
  94. </tr>
  95. </thead>
  96. <tbody>
  97. <?php foreach ($accounts as $account):
  98. if (isClientActive($clients,$account["username"])) { continue; }
  99. ?>
  100. <tr>
  101. <td><?= htmlspecialchars($account["username"]) ?></td>
  102. <td><?= htmlspecialchars($account['ip'] ?? 'N/A') ?></td>
  103. <td>
  104. <span class="status-badge <?= $account['banned'] ? 'status-banned' : 'status-active' ?>">
  105. <?= $account['banned'] ? 'BANNED' : 'ENABLED' ?>
  106. </span>
  107. </td>
  108. <td class="actions">
  109. <?php if ($account['banned']): ?>
  110. <button onclick="handleAction('<?= $server_name ?>', 'unban', '<?= htmlspecialchars($account['username']) ?>')"
  111. class="btn unban-btn">Unban</button>
  112. <?php else: ?>
  113. <button onclick="handleAction('<?= $server_name ?>', 'ban', '<?= htmlspecialchars($account['username']) ?>')"
  114. class="btn ban-btn">Ban</button>
  115. <?php endif; ?>
  116. </td>
  117. </tr>
  118. <?php endforeach; ?>
  119. </tbody>
  120. </table>
  121. </div>
  122. </div>
  123. </div>
  124. <div class="last-update">
  125. Last update: <?= date('Y-m-d H:i:s') ?>
  126. </div>
  127. <?php
  128. echo ob_get_clean();