get_server_data.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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>Cipher</th>
  50. <th>Status</th>
  51. <th>Actions</th>
  52. </tr>
  53. </thead>
  54. <tbody>
  55. <?php foreach ($clients as $client): ?>
  56. <tr class="<?= $client['banned'] ? 'banned' : '' ?>">
  57. <td>
  58. <a href="#" onclick="return generateConfig('<?= $server_name ?>', '<?= htmlspecialchars($client['name']) ?>', event)">
  59. <?= htmlspecialchars($client['name']) ?>
  60. </a>
  61. </td>
  62. <td><?= htmlspecialchars($client['real_ip']) ?></td>
  63. <td><?= htmlspecialchars($client['virtual_ip']) ?></td>
  64. <td>↓<?= $client['bytes_received'] ?> ↑<?= $client['bytes_sent'] ?></td>
  65. <td><?= htmlspecialchars($client['connected_since']) ?></td>
  66. <td><?= htmlspecialchars($client['cipher']) ?></td>
  67. <td>
  68. <span class="status-badge <?= $client['banned'] ? 'status-banned' : 'status-active' ?>">
  69. <?= $client['banned'] ? 'BANNED' : 'Active' ?>
  70. </span>
  71. </td>
  72. <td class="actions">
  73. <?php if ($client['banned']): ?>
  74. <button onclick="handleAction('<?= $server_name ?>', 'unban', '<?= htmlspecialchars($client['name']) ?>')"
  75. class="btn unban-btn">Unban</button>
  76. <?php else: ?>
  77. <button onclick="handleAction('<?= $server_name ?>', 'ban', '<?= htmlspecialchars($client['name']) ?>')"
  78. class="btn ban-btn">Ban</button>
  79. <?php endif; ?>
  80. <button onclick="handleAction('<?= $server_name ?>', 'revoke', '<?= htmlspecialchars($client['name']) ?>')"
  81. class="btn ban-btn">Revoke</button>
  82. </td>
  83. </tr>
  84. <?php endforeach; ?>
  85. </tbody>
  86. </table>
  87. <?php else: ?>
  88. <p>No active connections</p>
  89. <?php endif; ?>
  90. </div>
  91. <div class="section">
  92. <div class="spoiler">
  93. <div class="spoiler-title collapsed" onclick="toggleSpoiler(this)">
  94. Configured Account List (<?= count($accounts) ?>)
  95. </div>
  96. <div class="spoiler-content">
  97. <table>
  98. <thead>
  99. <tr>
  100. <th>Account</th>
  101. <th>Assigned IP</th>
  102. <th>Status</th>
  103. <th>Actions</th>
  104. </tr>
  105. </thead>
  106. <tbody>
  107. <?php foreach ($accounts as $account):
  108. if (isClientActive($clients,$account["username"])) { continue; }
  109. ?>
  110. <tr>
  111. <td>
  112. <a href="#" onclick="return generateConfig('<?= $server_name ?>', '<?= htmlspecialchars($account['username']) ?>', event)">
  113. <?= htmlspecialchars($account['username']) ?>
  114. </a>
  115. </td>
  116. <td><?= htmlspecialchars($account['ip'] ?? 'N/A') ?></td>
  117. <?php
  118. $is_revoked = $account['revoked'];
  119. $is_banned = $account['banned'];
  120. $status_class = $is_revoked ? 'status-banned' : ($is_banned ? 'status-banned' : 'status-active');
  121. $status_text = $is_revoked ? 'REVOKED' : ($is_banned ? 'BANNED' : 'ENABLED');
  122. ?>
  123. <td>
  124. <span class="status-badge <?= $status_class ?>">
  125. <?= $status_text ?>
  126. </span>
  127. </td>
  128. <td class="actions">
  129. <?php if ($is_revoked): ?>
  130. <span class="revoked-text">Certificate revoked</span>
  131. <?php else: ?>
  132. <?php if ($is_banned): ?>
  133. <button onclick="return confirmAction('unban', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  134. // <button onclick="handleAction('<?= $server_name ?>', 'unban', '<?= htmlspecialchars($account['username']) ?>')"
  135. class="btn unban-btn">Unban</button>
  136. <?php else: ?>
  137. <button onclick="return confirmAction('ban', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  138. // <button onclick="handleAction('<?= $server_name ?>', 'ban', '<?= htmlspecialchars($account['username']) ?>')"
  139. class="btn ban-btn">Ban</button>
  140. <?php endif; ?>
  141. <?php if (!empty($server['cert_index'])): ?>
  142. <button onclick="return confirmAction('revoke', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  143. // <button onclick="handleAction('<?= $server_name ?>', 'revoke', '<?= htmlspecialchars($account['username']) ?>')"
  144. class="btn revoke-btn">Revoke</button>
  145. <?php endif; ?>
  146. <?php endif; ?>
  147. </td>
  148. </tr>
  149. <?php endforeach; ?>
  150. </tbody>
  151. </table>
  152. </div>
  153. </div>
  154. </div>
  155. <div class="last-update">
  156. Last update: <?= date('Y-m-d H:i:s') ?>
  157. </div>
  158. <?php
  159. echo ob_get_clean();