get_server_data.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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);
  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. <?php if (!empty($server['cert_index'])): ?>
  81. <button onclick="handleAction('<?= $server_name ?>', 'revoke', '<?= htmlspecialchars($client['name']) ?>')"
  82. class="btn ban-btn">Revoke</button>
  83. <?php endif; ?>
  84. <button class="btn" onclick="editCCD('<?= $server_name ?>','<?= $client['name'] ?>')">Edit CCD</button>
  85. </td>
  86. </tr>
  87. <?php endforeach; ?>
  88. </tbody>
  89. </table>
  90. <?php else: ?>
  91. <p>No active connections</p>
  92. <?php endif; ?>
  93. </div>
  94. <div class="section">
  95. <div class="spoiler">
  96. <div class="spoiler-title collapsed" onclick="toggleSpoiler(this)">
  97. Configured Account List (<?= count($accounts) ?>)
  98. </div>
  99. <div class="spoiler-content">
  100. <table>
  101. <thead>
  102. <tr>
  103. <th>Account</th>
  104. <th>Assigned IP</th>
  105. <th>Status</th>
  106. <th>Cert</th>
  107. <th>Actions</th>
  108. </tr>
  109. </thead>
  110. <tbody>
  111. <?php foreach ($accounts as $account):
  112. if (isClientActive($clients,$account["username"])) { continue; }
  113. ?>
  114. <tr>
  115. <td>
  116. <a href="#" onclick="return generateConfig('<?= $server_name ?>', '<?= htmlspecialchars($account['username']) ?>', event)">
  117. <?= htmlspecialchars($account['username']) ?>
  118. </a>
  119. </td>
  120. <td><?= htmlspecialchars($account['ip'] ?? 'N/A') ?></td>
  121. <?php
  122. $is_revoked = $account['revoked'];
  123. $is_banned = $account['banned'];
  124. $status_class = $is_revoked ? 'status-banned' : ($is_banned ? 'status-banned' : 'status-active');
  125. $status_text = $is_revoked ? 'REVOKED' : ($is_banned ? 'BANNED' : 'ENABLED');
  126. ?>
  127. <td>
  128. <span class="status-badge <?= $status_class ?>">
  129. <?= htmlspecialchars($status_text) ?>
  130. </span>
  131. </td>
  132. <td>
  133. <?php if (isset($account['cert_date']) && $account['cert_date'] !== '-'): ?>
  134. <div class="cert-info">
  135. <span class="cert-date
  136. <?= $account['expired'] ? 'expired' : ($account['days_left'] < 7 ? 'expiring-soon' : 'valid') ?>">
  137. <?= htmlspecialchars($account['cert_date']) ?>
  138. </span>
  139. <?php if ($account['days_left'] !== null): ?>
  140. <span class="cert-days
  141. <?= $account['expired'] ? 'expired' : ($account['days_left'] < 7 ? 'urgent' : ($account['days_left'] < 30 ? 'warning' : '')) ?>">
  142. <?php if ($account['expired']): ?>
  143. (expired <?= $account['days_left'] ?>d ago)
  144. <?php else: ?>
  145. (<?= $account['days_left'] ?>d left)
  146. <?php endif; ?>
  147. </span>
  148. <?php endif; ?>
  149. </div>
  150. <?php else: ?>
  151. <span class="cert-date error">No certificate</span>
  152. <?php endif; ?>
  153. </td>
  154. <td class="actions">
  155. <?php if ($is_revoked): ?>
  156. <span class="revoked-text">Certificate revoked</span>
  157. <?php else: ?>
  158. <?php if (!$cert_info['valid']): ?>
  159. <button onclick="return confirmAction('renew', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  160. class="btn unban-btn">Renew</button>
  161. <?php endif; ?>
  162. <?php if ($is_banned): ?>
  163. <button onclick="return confirmAction('unban', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  164. class="btn unban-btn">Unban</button>
  165. <?php else: ?>
  166. <button onclick="return confirmAction('ban', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  167. class="btn ban-btn">Ban</button>
  168. <?php endif; ?>
  169. <?php if (!empty($server['cert_index'])): ?>
  170. <button onclick="return confirmAction('revoke', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  171. class="btn revoke-btn">Revoke</button>
  172. <?php else: ?>
  173. <button onclick="return confirmAction('remove', '<?= htmlspecialchars($account['username']) ?>', '<?= $server_name ?>', event)"
  174. class="btn remove-btn">Remove CCD</button>
  175. <?php endif; ?>
  176. <button class="btn" onclick="editCCD('<?= $server_name ?>','<?= $account['username'] ?>')">Edit CCD</button>
  177. <?php endif; ?>
  178. </td>
  179. </tr>
  180. <?php endforeach; ?>
  181. </tbody>
  182. </table>
  183. </div>
  184. </div>
  185. </div>
  186. <div class="last-update">
  187. Last update: <?= date('Y-m-d H:i:s') ?>
  188. </div>
  189. <?php
  190. echo ob_get_clean();