blacklist.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. {% extends "admin/base.html" %}
  2. {% block content %}
  3. {% include 'components/confirm_modal.html' %}
  4. {% include 'components/loading_spinner.html' %}
  5. <div x-data="{ activeTab: 'pihole' }" class="bg-white rounded-lg shadow-lg p-4 sm:p-6">
  6. <h2 class="text-xl sm:text-2xl font-bold text-gray-800 mb-4 sm:mb-6">{{ _("Blacklist - Gestor de Listas") }}</h2>
  7. <!-- Tabs -->
  8. <div class="flex border-b border-gray-200 flex-wrap">
  9. <button @click="activeTab = 'pihole'" :class="activeTab === 'pihole' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500'" class="py-2 px-4 border-b-2 font-medium text-sm whitespace-nowrap">
  10. {{ _("Pi-hole") }}
  11. </button>
  12. <button @click="activeTab = 'adguard'" :class="activeTab === 'adguard' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500'" class="py-2 px-4 border-b-2 font-medium text-sm whitespace-nowrap">
  13. {{ _("AdGuard") }}
  14. </button>
  15. <button @click="activeTab = 'dnsmasq'" :class="activeTab === 'dnsmasq' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500'" class="py-2 px-4 border-b-2 font-medium text-sm whitespace-nowrap">
  16. {{ _("DNSmasq") }}
  17. </button>
  18. <button @click="activeTab = 'custom'" :class="activeTab === 'custom' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500'" class="py-2 px-4 border-b-2 font-medium text-sm whitespace-nowrap">
  19. {{ _("Personalizada") }}
  20. </button>
  21. <button @click="activeTab = 'import'" :class="activeTab === 'import' ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-500'" class="py-2 px-4 border-b-2 font-medium text-sm whitespace-nowrap">
  22. {{ _("Importar") }}
  23. </button>
  24. </div>
  25. <!-- Pi-hole Tab -->
  26. <div x-show="activeTab === 'pihole'" class="mt-6">
  27. <h3 class="text-lg font-semibold mb-3 flex items-center gap-2">
  28. {{ _("Integración Pi-hole") }}
  29. <span class="inline-block bg-yellow-200 text-yellow-800 text-xs font-bold px-2 py-0.5 rounded align-middle border border-yellow-300 shadow-sm animate-pulse" title="{{ _("Funcionalidad en Beta") }}">{{ _("BETA") }}</span>
  30. </h3>
  31. <p class="text-sm text-gray-600 mb-4">{{ _("Conecta y sincroniza listas bloqueadas desde un servidor Pi-hole. Aquí puedes configurar conexión y ver el estado.") }}</p>
  32. <form method="POST" action="/admin/blacklist/test-connection" class="grid grid-cols-1 md:grid-cols-2 gap-4">
  33. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  34. <div class="bg-gray-50 p-4 rounded border">
  35. <label class="text-sm font-medium text-gray-700">{{ _("Pi-hole Host") }}</label>
  36. <input name="host" type="text" class="w-full mt-2 p-2 border rounded" placeholder="{{ _("192.168.1.2 or example.com") }}" />
  37. </div>
  38. <div class="bg-gray-50 p-4 rounded border">
  39. <label class="text-sm font-medium text-gray-700">{{ _("API Token") }}</label>
  40. <input name="token" type="password" class="w-full mt-2 p-2 border rounded" placeholder="{{ _("Token (opcional)") }}" />
  41. </div>
  42. <div class="md:col-span-2 mt-4 flex gap-2">
  43. <button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">{{ _("Probar Conexión") }}</button>
  44. </form>
  45. <form method="POST" action="/admin/blacklist/sync" class="mt-0">
  46. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  47. <button type="submit" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded">{{ _("Sincronizar Listas") }}</button>
  48. </form>
  49. </div>
  50. </div>
  51. <!-- AdGuard Tab -->
  52. <div x-show="activeTab === 'adguard'" class="mt-6">
  53. <h3 class="text-lg font-semibold mb-3">{{ _("Integración AdGuard") }}</h3>
  54. <p class="text-sm text-gray-600 mb-4">{{ _("Soporte para fuentes y listas públicas compatibles con AdGuard.") }}</p>
  55. <form id="importUrlForm" method="POST" action="/admin/blacklist/import" class="bg-gray-50 p-4 rounded border">
  56. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  57. <label class="text-sm font-medium text-gray-700">{{ _("URL de la lista") }}</label>
  58. <input name="url" type="text" class="w-full mt-2 p-2 border rounded" placeholder="https://example.com/list.txt" />
  59. <div class="mt-3">
  60. <button type="submit" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded">{{ _("Agregar Lista") }}</button>
  61. </div>
  62. </form>
  63. {% if url_lists %}
  64. <div class="mt-6">
  65. <h4 class="text-md font-semibold mb-2 text-gray-700">{{ _("Listas importadas") }}</h4>
  66. <ul class="divide-y divide-gray-200 bg-white rounded shadow">
  67. {% for item in url_lists %}
  68. <li class="flex items-center justify-between px-4 py-3 hover:bg-gray-50 gap-3" x-data="{ enforced: {{ 'true' if item.enforced else 'false' }}, loading: false }">
  69. <div class="flex flex-col min-w-0 flex-1">
  70. <span class="font-mono text-blue-700 text-sm truncate max-w-xs md:max-w-md" title="{{ item.source_url }}">{{ item.source_url }}</span>
  71. <div class="flex items-center gap-2 mt-0.5">
  72. <span class="text-xs text-gray-500">{{ item.count }} dominios</span>
  73. <span x-show="enforced" class="inline-flex items-center gap-1 text-xs font-medium text-emerald-700 bg-emerald-50 border border-emerald-200 px-1.5 py-0.5 rounded-full">
  74. <i class="fas fa-shield-alt text-[10px]"></i> {{ _("Activa en Squid") }}
  75. </span>
  76. <span x-show="!enforced" class="inline-flex items-center gap-1 text-xs font-medium text-gray-500 bg-gray-100 border border-gray-200 px-1.5 py-0.5 rounded-full">
  77. <i class="fas fa-ban text-[10px]"></i> {{ _("Inactiva") }}
  78. </span>
  79. </div>
  80. </div>
  81. <div class="flex items-center gap-2 flex-shrink-0">
  82. <!-- Toggle enforcement -->
  83. <button
  84. @click="toggleBlocklist('{{ item.source_url }}', !enforced, $el, (ok) => { if(ok) enforced = !enforced })"
  85. :disabled="loading"
  86. :class="enforced
  87. ? 'bg-emerald-500 hover:bg-emerald-600 focus:ring-emerald-300'
  88. : 'bg-gray-400 hover:bg-gray-500 focus:ring-gray-300'"
  89. class="relative inline-flex h-7 w-12 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-offset-1 disabled:opacity-50 disabled:cursor-wait"
  90. role="switch"
  91. :aria-checked="enforced.toString()"
  92. :title="enforced ? 'Desactivar bloqueo en Squid' : 'Activar bloqueo en Squid'"
  93. >
  94. <span
  95. :class="enforced ? 'translate-x-5' : 'translate-x-0'"
  96. class="pointer-events-none inline-block h-6 w-6 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out flex items-center justify-center"
  97. >
  98. <i x-show="loading" class="fas fa-spinner fa-spin text-gray-400 text-xs"></i>
  99. </span>
  100. </button>
  101. <!-- Delete list -->
  102. <form method="POST" action="/admin/blacklist/delete-list" class="delete-list-form">
  103. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  104. <input type="hidden" name="source_url" value="{{ item.source_url }}"/>
  105. <button type="submit" class="bg-red-500 hover:bg-red-600 text-white px-3 py-1.5 rounded text-xs font-semibold shadow delete-list-btn" title="{{ _("Eliminar lista y dominios") }}">
  106. <i class="fas fa-trash-alt"></i>
  107. </button>
  108. </form>
  109. </div>
  110. </li>
  111. {% endfor %}
  112. </ul>
  113. </div>
  114. {% endif %}
  115. </div>
  116. <script>
  117. /**
  118. * Toggle Squid enforcement for a blocklist via the API.
  119. *
  120. * @param {string|null} sourceUrl The source_url (null for custom list)
  121. * @param {boolean} enable true = activate, false = deactivate
  122. * @param {HTMLElement} el The toggle button element (for loading state)
  123. * @param {function} callback Called with (true) on success, (false) on error
  124. */
  125. function toggleBlocklist(sourceUrl, enable, el, callback) {
  126. const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
  127. // Disable button while processing
  128. el.disabled = true;
  129. fetch('/admin/api/blocklist/toggle', {
  130. method: 'POST',
  131. headers: {
  132. 'Content-Type': 'application/json',
  133. 'X-CSRFToken': csrfToken
  134. },
  135. body: JSON.stringify({
  136. source_url: sourceUrl,
  137. enable: enable
  138. })
  139. })
  140. .then(function(resp) { return resp.json().then(function(data) { return { ok: resp.ok, data: data }; }); })
  141. .then(function(result) {
  142. el.disabled = false;
  143. if (result.ok && result.data.status === 'success') {
  144. showToast(result.data.message, 'success');
  145. callback(true);
  146. } else {
  147. showToast(result.data.message || 'Error al cambiar estado', 'error');
  148. callback(false);
  149. }
  150. })
  151. .catch(function(err) {
  152. el.disabled = false;
  153. showToast('Error de conexión al servidor', 'error');
  154. callback(false);
  155. });
  156. }
  157. /**
  158. * Lightweight toast notification.
  159. */
  160. function showToast(message, type) {
  161. var container = document.getElementById('toastContainer');
  162. if (!container) {
  163. container = document.createElement('div');
  164. container.id = 'toastContainer';
  165. container.className = 'fixed top-4 right-4 z-[60] flex flex-col gap-2 max-w-sm';
  166. document.body.appendChild(container);
  167. }
  168. var colorClasses = type === 'success'
  169. ? 'bg-emerald-600 text-white'
  170. : type === 'error'
  171. ? 'bg-red-600 text-white'
  172. : 'bg-gray-700 text-white';
  173. var icon = type === 'success' ? 'fa-check-circle' : type === 'error' ? 'fa-exclamation-circle' : 'fa-info-circle';
  174. var toast = document.createElement('div');
  175. toast.className = 'flex items-center gap-2 px-4 py-3 rounded-lg shadow-lg text-sm font-medium transform translate-x-full transition-transform duration-300 ' + colorClasses;
  176. toast.innerHTML = '<i class="fas ' + icon + '"></i><span>' + message + '</span>';
  177. container.appendChild(toast);
  178. // Animate in
  179. requestAnimationFrame(function() {
  180. toast.classList.remove('translate-x-full');
  181. toast.classList.add('translate-x-0');
  182. });
  183. // Auto-dismiss
  184. setTimeout(function() {
  185. toast.classList.remove('translate-x-0');
  186. toast.classList.add('translate-x-full');
  187. setTimeout(function() { toast.remove(); }, 300);
  188. }, 4000);
  189. }
  190. // Reutiliza el modal de confirmación global
  191. document.addEventListener('DOMContentLoaded', function() {
  192. function showImportLoading(title, subtitle) {
  193. const overlay = document.getElementById('importLoadingOverlay');
  194. const loadingTitle = document.getElementById('importLoadingTitle');
  195. const loadingSubtitle = document.getElementById('importLoadingSubtitle');
  196. if (loadingTitle && title) loadingTitle.textContent = title;
  197. if (loadingSubtitle && subtitle) loadingSubtitle.textContent = subtitle;
  198. if (overlay) {
  199. overlay.classList.remove('hidden');
  200. overlay.classList.add('flex');
  201. }
  202. }
  203. let pendingDeleteForm = null;
  204. document.querySelectorAll('.delete-list-form').forEach(function(form) {
  205. form.addEventListener('submit', function(e) {
  206. e.preventDefault();
  207. pendingDeleteForm = form;
  208. const url = form.querySelector('input[name="source_url"]').value;
  209. showCustomConfirm(
  210. `¿Estás seguro de que deseas eliminar la siguiente lista?\n\nURL: ${url}\n\nEsta acción es irreversible y eliminará todos los dominios asociados.`,
  211. function() {
  212. if (pendingDeleteForm) {
  213. pendingDeleteForm.submit();
  214. pendingDeleteForm = null;
  215. }
  216. }
  217. );
  218. });
  219. });
  220. // Validación para evitar agregar listas duplicadas
  221. const addListForm = document.getElementById('importUrlForm');
  222. if (addListForm) {
  223. addListForm.addEventListener('submit', function(e) {
  224. const input = addListForm.querySelector('input[name="url"]');
  225. if (!input) return;
  226. const newUrl = input.value.trim();
  227. if (!newUrl) return;
  228. // Recolectar URLs ya existentes
  229. const existingUrls = Array.from(document.querySelectorAll('input[name="source_url"]'))
  230. .map(el => el.value.trim());
  231. if (existingUrls.includes(newUrl)) {
  232. showCustomConfirm(
  233. 'La lista ya existe en el sistema. No puedes agregar la misma URL dos veces.',
  234. function() {}, // Solo cerrar
  235. { onlyClose: true }
  236. );
  237. e.preventDefault();
  238. return;
  239. }
  240. showImportLoading(
  241. 'Importando lista desde URL',
  242. 'Validando enlace y procesando dominios...'
  243. );
  244. });
  245. }
  246. const importFileForm = document.getElementById('importFileForm');
  247. if (importFileForm) {
  248. importFileForm.addEventListener('submit', function() {
  249. showImportLoading(
  250. 'Importando archivo de listas',
  251. 'Leyendo archivo y aplicando reglas de bloqueo...'
  252. );
  253. });
  254. }
  255. window.addEventListener('pageshow', function() {
  256. const overlay = document.getElementById('importLoadingOverlay');
  257. if (overlay) {
  258. overlay.classList.add('hidden');
  259. overlay.classList.remove('flex');
  260. }
  261. });
  262. });
  263. </script>
  264. <!-- DNSmasq Tab -->
  265. <div x-show="activeTab === 'dnsmasq'" class="mt-6">
  266. <h3 class="text-lg font-semibold mb-3 flex items-center gap-2">
  267. {{ _("DNSmasq") }}
  268. <span class="inline-block bg-yellow-200 text-yellow-800 text-xs font-bold px-2 py-0.5 rounded align-middle border border-yellow-300 shadow-sm animate-pulse" title="{{ _("Funcionalidad en Beta") }}">{{ _("BETA") }}</span>
  269. </h3>
  270. <p class="text-sm text-gray-600 mb-4">{{ _("Exporta o importa reglas en formato compatible con dnsmasq.") }}</p>
  271. <div class="bg-gray-50 p-4 rounded border">
  272. <button class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">{{ _("Exportar archivo hosts") }}</button>
  273. </div>
  274. </div>
  275. <!-- Custom Tab -->
  276. <div x-show="activeTab === 'custom'" x-data="{ customEnforced: {{ 'true' if custom_enforced else 'false' }}, customLoading: false }" class="mt-6">
  277. <div class="flex items-center justify-between mb-3">
  278. <div>
  279. <h3 class="text-lg font-semibold">{{ _("Lista Personalizada") }}</h3>
  280. <p class="text-sm text-gray-600">{{ _("Añade dominios manualmente o pega una lista separada por comas/lineas.") }}</p>
  281. </div>
  282. <div class="flex items-center gap-3 flex-shrink-0">
  283. <div class="flex items-center gap-2">
  284. <span x-show="customEnforced" class="inline-flex items-center gap-1 text-xs font-medium text-emerald-700 bg-emerald-50 border border-emerald-200 px-2 py-1 rounded-full">
  285. <i class="fas fa-shield-alt text-[10px]"></i> {{ _("Activa en Squid") }}
  286. </span>
  287. <span x-show="!customEnforced" class="inline-flex items-center gap-1 text-xs font-medium text-gray-500 bg-gray-100 border border-gray-200 px-2 py-1 rounded-full">
  288. <i class="fas fa-ban text-[10px]"></i> {{ _("Inactiva") }}
  289. </span>
  290. </div>
  291. <button
  292. @click="toggleBlocklist(null, !customEnforced, $el, (ok) => { if(ok) customEnforced = !customEnforced })"
  293. :disabled="customLoading"
  294. :class="customEnforced
  295. ? 'bg-emerald-500 hover:bg-emerald-600 focus:ring-emerald-300'
  296. : 'bg-gray-400 hover:bg-gray-500 focus:ring-gray-300'"
  297. class="relative inline-flex h-7 w-12 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-offset-1 disabled:opacity-50 disabled:cursor-wait"
  298. role="switch"
  299. :aria-checked="customEnforced.toString()"
  300. :title="customEnforced ? 'Desactivar bloqueo en Squid' : 'Activar bloqueo en Squid'"
  301. >
  302. <span
  303. :class="customEnforced ? 'translate-x-5' : 'translate-x-0'"
  304. class="pointer-events-none inline-block h-6 w-6 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out flex items-center justify-center"
  305. >
  306. <i x-show="customLoading" class="fas fa-spinner fa-spin text-gray-400 text-xs"></i>
  307. </span>
  308. </button>
  309. </div>
  310. </div>
  311. <form method="POST" action="/admin/blacklist/save-custom" class="grid grid-cols-1 gap-4">
  312. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  313. <textarea name="custom_list" rows="6" class="w-full p-3 border rounded" placeholder="{{ _("example.com
  314. example.org") }}">{{ blacklist }}</textarea>
  315. <div class="flex gap-2">
  316. <button type="submit" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded">{{ _("Guardar") }}</button>
  317. <button type="button" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded">{{ _("Cancelar") }}</button>
  318. </div>
  319. </form>
  320. </div>
  321. <!-- Import Tab -->
  322. <div x-show="activeTab === 'import'" class="mt-6">
  323. <h3 class="text-lg font-semibold mb-3">{{ _("Importar listas") }}</h3>
  324. <p class="text-sm text-gray-600 mb-4">{{ _("Importa desde archivos locales o pega URLs de listas públicas.") }}</p>
  325. <form id="importFileForm" method="POST" action="/admin/blacklist/import" enctype="multipart/form-data" class="bg-gray-50 p-4 rounded border">
  326. <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
  327. <label class="text-sm font-medium text-gray-700">{{ _("Subir archivo") }}</label>
  328. <input name="file" type="file" class="w-full mt-2" />
  329. <div class="mt-3">
  330. <button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">{{ _("Importar") }}</button>
  331. </div>
  332. </form>
  333. </div>
  334. </div>
  335. {% endblock %}