get_server_data.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $action = $_GET['action'] ?? '';
  26. $username = $_GET['username'] ?? '';
  27. if (!isset($servers[$server_name])) {
  28. die("Invalid server name");
  29. }
  30. $server = $servers[$server_name];
  31. $clients = getOpenVPNStatus($server);
  32. $banned_clients = getBannedClients($server, $clients);
  33. $accounts = getAccountList($server);
  34. // Генерируем HTML для этого сервера
  35. ob_start();
  36. ?>
  37. <h2><?= htmlspecialchars($server['title']) ?></h2>
  38. <div class="section">
  39. <h3>Active Connections</h3>
  40. <?php if (!empty($clients)): ?>
  41. <table>
  42. <thead>
  43. <tr>
  44. <th>Client</th>
  45. <th>Real IP</th>
  46. <th>Virtual IP</th>
  47. <th>Traffic</th>
  48. <th>Connected</th>
  49. <th>Status</th>
  50. <th>Actions</th>
  51. </tr>
  52. </thead>
  53. <tbody>
  54. <?php foreach ($clients as $client): ?>
  55. <tr class="<?= $client['banned'] ? 'banned' : '' ?>">
  56. <td>
  57. <a href="#" onclick="return generateConfig('<?= $server_name ?>', '<?= htmlspecialchars($client['name']) ?>', event)">
  58. <?= htmlspecialchars($client['name']) ?>
  59. </a>
  60. </td>
  61. <td><?= htmlspecialchars($client['real_ip']) ?></td>
  62. <td><?= htmlspecialchars($client['virtual_ip']) ?></td>
  63. <td>↓<?= $client['bytes_received'] ?> ↑<?= $client['bytes_sent'] ?></td>
  64. <td><?= htmlspecialchars($client['connected_since']) ?></td>
  65. <td>
  66. <span class="status-badge <?= $client['banned'] ? 'status-banned' : 'status-active' ?>">
  67. <?= $client['banned'] ? 'BANNED' : 'Active' ?>
  68. </span>
  69. </td>
  70. <td class="actions">
  71. <?php if ($client['banned']): ?>
  72. <button onclick="handleAction('<?= $server_name ?>', 'unban', '<?= htmlspecialchars($client['name']) ?>')"
  73. class="btn unban-btn">Unban</button>
  74. <?php else: ?>
  75. <button onclick="handleAction('<?= $server_name ?>', 'ban', '<?= htmlspecialchars($client['name']) ?>')"
  76. class="btn ban-btn">Ban</button>
  77. <?php endif; ?>
  78. </td>
  79. </tr>
  80. <?php endforeach; ?>
  81. </tbody>
  82. </table>
  83. <?php else: ?>
  84. <p>No active connections</p>
  85. <?php endif; ?>
  86. </div>
  87. <div class="section">
  88. <div class="spoiler">
  89. <div class="spoiler-title collapsed" onclick="toggleSpoiler(this)">
  90. Configured Account List (<?= count($accounts) ?>)
  91. </div>
  92. <div class="spoiler-content">
  93. <table>
  94. <thead>
  95. <tr>
  96. <th>Account</th>
  97. <th>Assigned IP</th>
  98. <th>Status</th>
  99. <th>Actions</th>
  100. </tr>
  101. </thead>
  102. <tbody>
  103. <?php foreach ($accounts as $account):
  104. if (isClientActive($clients,$account["username"])) { continue; }
  105. ?>
  106. <tr>
  107. <td>
  108. <a href="#" onclick="return generateConfig('<?= $server_name ?>', '<?= htmlspecialchars($account['username']) ?>', event)">
  109. <?= htmlspecialchars($account['username']) ?>
  110. </a>
  111. </td>
  112. <td><?= htmlspecialchars($account['ip'] ?? 'N/A') ?></td>
  113. <td>
  114. <span class="status-badge <?= $account['banned'] ? 'status-banned' : 'status-active' ?>">
  115. <?= $account['banned'] ? 'BANNED' : 'ENABLED' ?>
  116. </span>
  117. </td>
  118. <td class="actions">
  119. <?php if ($account['banned']): ?>
  120. <button onclick="handleAction('<?= $server_name ?>', 'unban', '<?= htmlspecialchars($account['username']) ?>')"
  121. class="btn unban-btn">Unban</button>
  122. <?php else: ?>
  123. <button onclick="handleAction('<?= $server_name ?>', 'ban', '<?= htmlspecialchars($account['username']) ?>')"
  124. class="btn ban-btn">Ban</button>
  125. <?php endif; ?>
  126. </td>
  127. </tr>
  128. <?php endforeach; ?>
  129. </tbody>
  130. </table>
  131. </div>
  132. </div>
  133. </div>
  134. <div class="last-update">
  135. Last update: <?= date('Y-m-d H:i:s') ?>
  136. </div>
  137. <?php
  138. echo ob_get_clean();