1
0

get_server_data.php 11 KB

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