1
0

get_server_data.php 11 KB

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