-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab_firewall.py
More file actions
1025 lines (820 loc) · 39.5 KB
/
tab_firewall.py
File metadata and controls
1025 lines (820 loc) · 39.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel,
QMessageBox, QFrame, QDialog, QDialogButtonBox,
QComboBox, QApplication, QProgressDialog,
QTabWidget, QRadioButton, QButtonGroup, QPushButton,
QTableWidget, QHeaderView, QAbstractItemView,
QTableWidgetItem, QLineEdit, QCompleter)
from PySide6.QtCore import Qt, QThread, Signal, QTimer
from PySide6.QtGui import QFont, QColor, QIntValidator
# Importaciones locales
import locales
from firewall_detector import FirewallDetector, FirewallType, FirewallInstaller
from ui_components import ToggleSwitch
from config_manager import ConfigManager
from network_utils import NetworkUtils
# --- DIÁLOGO PARA RED NUEVA ---
class NewNetworkDialog(QDialog):
def __init__(self, net_name, parent=None):
super().__init__(parent)
self.setWindowTitle(locales.get_text("net_detect_title"))
self.setFixedSize(400, 250)
self.net_name = net_name
self.selected_zone = "public" # Por defecto seguridad
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.setSpacing(15)
# Icono o título
lbl_info = QLabel(locales.get_text("net_detect_msg").format(self.net_name))
lbl_info.setWordWrap(True)
lbl_info.setTextFormat(Qt.RichText) # Para permitir negrita <b>
lbl_info.setFont(QFont("Arial", 11))
layout.addWidget(lbl_info)
layout.addSpacing(10)
# Botones grandes
btn_public = QPushButton("☕ " + locales.get_text("net_btn_public"))
btn_public.setMinimumHeight(40)
btn_public.clicked.connect(lambda: self.finish("public"))
layout.addWidget(btn_public)
btn_home = QPushButton("🏠 " + locales.get_text("net_btn_home"))
btn_home.setMinimumHeight(40)
btn_home.clicked.connect(lambda: self.finish("home"))
layout.addWidget(btn_home)
layout.addStretch()
self.setLayout(layout)
def finish(self, zone):
self.selected_zone = zone
self.accept()
# --- CLASES DE UTILIDAD (WORKER y DIALOG) ---
# (Las mantenemos igual que antes, necesarias para la instalación)
class InstallWorker(QThread):
finished_signal = Signal(bool)
def __init__(self, installer, fw_name):
super().__init__()
self.installer = installer
self.fw_name = fw_name
def run(self):
success = self.installer.install_firewall(self.fw_name)
self.finished_signal.emit(success)
class InstallDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle(locales.get_text("inst_title"))
self.setFixedSize(400, 250)
self.installer = FirewallInstaller()
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.setSpacing(15)
lbl_title = QLabel(locales.get_text("inst_header"))
lbl_title.setFont(QFont("Arial", 14, QFont.Bold))
lbl_title.setAlignment(Qt.AlignCenter)
layout.addWidget(lbl_title)
distro = self.installer.get_distro_type().capitalize()
msg = locales.get_text("inst_desc").format(distro)
lbl_msg = QLabel(msg)
lbl_msg.setWordWrap(True)
layout.addWidget(lbl_msg)
layout.addWidget(QLabel(locales.get_text("inst_select_label")))
self.combo = QComboBox()
distro_key = self.installer.get_distro_type()
tag_rec = locales.get_text("inst_rec_tag")
tag_simple = locales.get_text("inst_simple_tag")
tag_adv = locales.get_text("inst_adv_tag")
if distro_key in ["arch", "redhat", "suse"]:
self.combo.addItem(f"Firewalld{tag_rec}", "firewalld")
self.combo.addItem(f"UFW{tag_simple}", "ufw")
elif distro_key == "debian":
self.combo.addItem(f"UFW{tag_rec}", "ufw")
self.combo.addItem(f"Firewalld{tag_adv}", "firewalld")
else:
self.combo.addItem("Firewalld", "firewalld")
self.combo.addItem("UFW", "ufw")
layout.addWidget(self.combo)
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttons.button(QDialogButtonBox.Ok).setText(locales.get_text("inst_btn_install"))
buttons.button(QDialogButtonBox.Cancel).setText(locales.get_text("inst_btn_cancel"))
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
self.setLayout(layout)
def get_selected_firewall(self):
return self.combo.currentData()
class AddRuleDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle(locales.get_text("add_rule_title"))
# CAMBIO AQUÍ: Aumentamos el tamaño (Antes era 300, 200)
self.setFixedSize(400, 350)
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.setSpacing(15) # Más espacio entre elementos
layout.setContentsMargins(25, 25, 25, 25) # Más margen alrededor
# Input Puerto
layout.addWidget(QLabel(locales.get_text("add_rule_port_label")))
self.inp_port = QLineEdit()
self.inp_port.setPlaceholderText("8080")
self.inp_port.setValidator(QIntValidator(1, 65535))
# Aseguramos altura para que se vea bien el texto
self.inp_port.setMinimumHeight(35)
layout.addWidget(self.inp_port)
# Input Protocolo
layout.addWidget(QLabel(locales.get_text("add_rule_proto_label")))
self.combo_proto = QComboBox()
self.combo_proto.addItems(["tcp", "udp"])
self.combo_proto.setMinimumHeight(35)
layout.addWidget(self.combo_proto)
# --- CAMPO: NOMBRE ---
layout.addWidget(QLabel(locales.get_text("add_rule_desc_label")))
self.inp_desc = QLineEdit()
self.inp_desc.setPlaceholderText("Ej: Servidor Minecraft")
self.inp_desc.setMinimumHeight(35)
layout.addWidget(self.inp_desc)
layout.addStretch()
# Botones
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttons.button(QDialogButtonBox.Ok).setText(locales.get_text("add_rule_btn_save"))
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
# Darle altura a los botones también
buttons.button(QDialogButtonBox.Ok).setMinimumHeight(35)
buttons.button(QDialogButtonBox.Cancel).setMinimumHeight(35)
layout.addWidget(buttons)
self.setLayout(layout)
def get_data(self):
return self.inp_port.text(), self.combo_proto.currentText(), self.inp_desc.text()
class InboundRulesPanel(QWidget):
def __init__(self):
super().__init__()
self.detector = FirewallDetector()
self.cfg = ConfigManager()
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.setContentsMargins(20, 20, 20, 20)
# --- CABECERA ---
lbl_title = QLabel(locales.get_text("inbound_table_title"))
lbl_title.setFont(QFont("Arial", 14, QFont.Bold))
layout.addWidget(lbl_title)
# --- TABLA ---
self.table = QTableWidget()
self.table.setColumnCount(4)
headers = [
locales.get_text("inbound_header_port"),
locales.get_text("inbound_header_proto"),
locales.get_text("inbound_header_action"),
locales.get_text("inbound_header_source"),
]
self.table.setHorizontalHeaderLabels(headers)
# Configuración importante para selección
self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
self.table.horizontalHeader().setStretchLastSection(True)
self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows) # Seleccionar fila completa
self.table.setSelectionMode(QAbstractItemView.SingleSelection) # Solo una a la vez
layout.addWidget(self.table)
# --- BARRA DE HERRAMIENTAS (BOTONES) ---
toolbar_layout = QHBoxLayout()
# Botón Añadir (+)
self.btn_add = QPushButton("➕ " + locales.get_text("btn_add_rule"))
self.btn_add.clicked.connect(self.action_add_rule)
toolbar_layout.addWidget(self.btn_add)
# Botón Eliminar (-)
self.btn_del = QPushButton("➖ " + locales.get_text("btn_del_rule"))
self.btn_del.setStyleSheet("background-color: #d32f2f; color: white;") # Rojo para peligro
self.btn_del.clicked.connect(self.action_del_rule)
toolbar_layout.addWidget(self.btn_del)
toolbar_layout.addStretch() # Empujamos botones a la izquierda
# Botón Refrescar (a la derecha)
# 1. Añadimos el texto traducido junto al icono
btn_text = locales.get_text("btn_refresh_rules")
btn_refresh = QPushButton(f"🔄 {btn_text}")
# 2. IMPORTANTE: Eliminamos o comentamos la línea de ancho fijo
# Si dejas esto activado, el texto saldrá cortado.
# btn_refresh.setFixedWidth(40) <--- BORRAR O COMENTAR ESTO
# Opcional: Ajustar un poco el padding si lo ves necesario
btn_refresh.setCursor(Qt.PointingHandCursor)
# Tooltip (opcional, ya que el botón lo explica, pero no estorba)
btn_refresh.setToolTip(locales.get_text("btn_refresh_tooltip"))
btn_refresh.clicked.connect(self.load_rules)
toolbar_layout.addWidget(btn_refresh)
layout.addLayout(toolbar_layout)
self.setLayout(layout)
self.load_rules()
def load_rules(self):
self.table.setRowCount(0)
rules = self.detector.get_active_rules()
if not rules: return # (O manejo de vacío previo)
self.table.setRowCount(len(rules))
for row, rule in enumerate(rules):
port = rule.get('port', '')
proto = rule.get('protocol', '')
# --- LÓGICA DE NOMBRE INTELIGENTE ---
# 1. Miramos si tú le pusiste un nombre personalizado
custom_name = self.cfg.get_rule_description(port, proto)
# 2. Si no, miramos si el sistema tiene un nombre (ej: ssh)
system_name = rule.get('service_name', '')
# 3. Decidimos qué mostrar en la columna 4
if custom_name:
display_name = f"⭐ {custom_name}" # Estrellita para tus notas
elif system_name:
display_name = system_name
else:
display_name = "-"
# ------------------------------------
port_item = QTableWidgetItem(port)
proto_item = QTableWidgetItem(proto)
action_item = QTableWidgetItem(rule.get('action', 'ALLOW'))
name_item = QTableWidgetItem(display_name) # Usamos el nombre calculado
if rule.get('action') == 'ALLOW':
action_item.setForeground(QColor("#4CAF50"))
self.table.setItem(row, 0, port_item)
self.table.setItem(row, 1, proto_item)
self.table.setItem(row, 2, action_item)
self.table.setItem(row, 3, name_item)
def action_add_rule(self):
dialog = AddRuleDialog(self)
if dialog.exec():
# Recuperamos los 3 datos
port, proto, desc = dialog.get_data()
if not port: return
QApplication.setOverrideCursor(Qt.WaitCursor)
success = self.detector.manage_port("add", port, proto)
QApplication.restoreOverrideCursor()
if success:
# --- GUARDAR TU NOMBRE EN LA CONFIG ---
self.cfg.save_rule_description(port, proto, desc)
# --------------------------------------
QMessageBox.information(self, "Éxito", locales.get_text("msg_success_add"))
self.load_rules()
else:
QMessageBox.critical(self, "Error", locales.get_text("msg_error_cmd"))
def action_del_rule(self):
current_row = self.table.currentRow()
if current_row < 0:
QMessageBox.warning(self, "Aviso", locales.get_text("msg_error_select"))
return
port = self.table.item(current_row, 0).text()
proto = self.table.item(current_row, 1).text()
confirm_text = locales.get_text("msg_confirm_del_text").format(port, proto)
reply = QMessageBox.question(self, locales.get_text("msg_confirm_del_title"),
confirm_text, QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
QApplication.setOverrideCursor(Qt.WaitCursor)
success = self.detector.manage_port("remove", port, proto)
QApplication.restoreOverrideCursor()
if success:
# --- BORRAR TU NOMBRE DE LA CONFIG PARA LIMPIAR ---
self.cfg.delete_rule_description(port, proto)
# --------------------------------------------------
QMessageBox.information(self, "Éxito", locales.get_text("msg_success_del"))
self.load_rules()
else:
QMessageBox.critical(self, "Error", locales.get_text("msg_error_cmd"))
class OutboundRulesPanel(QWidget):
def __init__(self):
super().__init__()
self.detector = FirewallDetector()
self.cfg = ConfigManager()
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.setContentsMargins(20, 20, 20, 20)
lbl_title = QLabel(locales.get_text("outbound_table_title"))
lbl_title.setFont(QFont("Arial", 14, QFont.Bold))
layout.addWidget(lbl_title)
self.table = QTableWidget()
self.table.setColumnCount(4)
headers = [
locales.get_text("outbound_header_port"),
locales.get_text("outbound_header_proto"),
locales.get_text("outbound_header_action"),
locales.get_text("outbound_header_source"),
]
self.table.setHorizontalHeaderLabels(headers)
self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
self.table.horizontalHeader().setStretchLastSection(True)
self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table.setSelectionMode(QAbstractItemView.SingleSelection)
layout.addWidget(self.table)
# Botones
toolbar_layout = QHBoxLayout()
# Botón Bloquear Salida (En salida solemos querer bloquear)
# Reusamos el texto "Añadir Regla" o podríamos poner "Bloquear Puerto"
self.btn_add = QPushButton("➕ " + locales.get_text("btn_add_rule"))
self.btn_add.clicked.connect(self.action_add_rule)
toolbar_layout.addWidget(self.btn_add)
self.btn_del = QPushButton("➖ " + locales.get_text("btn_del_rule"))
self.btn_del.setStyleSheet("background-color: #d32f2f; color: white;")
self.btn_del.clicked.connect(self.action_del_rule)
toolbar_layout.addWidget(self.btn_del)
toolbar_layout.addStretch()
btn_text = locales.get_text("btn_refresh_rules")
btn_refresh = QPushButton(f"🔄 {btn_text}")
btn_refresh.setCursor(Qt.PointingHandCursor)
btn_refresh.clicked.connect(self.load_rules)
toolbar_layout.addWidget(btn_refresh)
layout.addLayout(toolbar_layout)
self.setLayout(layout)
self.load_rules()
def load_rules(self):
self.table.setRowCount(0)
# Llamamos al método OUTBOUND del detector
rules = self.detector.get_active_outbound_rules()
if not rules:
# Opcional: Mostrar mensaje de "No hay reglas"
return
self.table.setRowCount(len(rules))
for row, rule in enumerate(rules):
port = rule.get('port', '')
proto = rule.get('protocol', '')
action = rule.get('action', 'DROP')
# Llamamos al config con "OUT"
custom_name = self.cfg.get_rule_description(port, proto, "OUT")
display_name = f"⭐ {custom_name}" if custom_name else "-"
port_item = QTableWidgetItem(port)
proto_item = QTableWidgetItem(proto)
action_item = QTableWidgetItem(action)
name_item = QTableWidgetItem(display_name)
# Color Rojo para DROP (Bloqueo)
if action == "DROP" or action == "REJECT":
action_item.setForeground(QColor("#d32f2f"))
else:
action_item.setForeground(QColor("#4CAF50"))
self.table.setItem(row, 0, port_item)
self.table.setItem(row, 1, proto_item)
self.table.setItem(row, 2, action_item)
self.table.setItem(row, 3, name_item)
def action_add_rule(self):
# Reusamos el diálogo de añadir regla
dialog = AddRuleDialog(self)
dialog.setWindowTitle("Bloquear Salida (Egress)") # Personalización rápida
if dialog.exec():
port, proto, desc = dialog.get_data()
if not port: return
QApplication.setOverrideCursor(Qt.WaitCursor)
# Acción OUTBOUND: Por defecto añadimos regla DROP (Bloquear)
success = self.detector.manage_outbound_port("add", port, proto, target="DROP")
QApplication.restoreOverrideCursor()
if success:
self.cfg.save_rule_description(port, proto, desc, "OUT") # Guardar como OUT
QMessageBox.information(self, "Éxito", "Regla de bloqueo añadida.")
self.load_rules()
else:
QMessageBox.critical(self, "Error", locales.get_text("msg_error_cmd"))
def action_del_rule(self):
current_row = self.table.currentRow()
if current_row < 0: return
port = self.table.item(current_row, 0).text()
proto = self.table.item(current_row, 1).text()
# Asumimos DROP porque es lo que crea nuestro botón de añadir
# (Para hacerlo perfecto deberíamos leer la acción de la tabla)
action_target = self.table.item(current_row, 2).text()
reply = QMessageBox.question(self, "Confirmar", f"¿Eliminar regla {port}/{proto}?", QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
QApplication.setOverrideCursor(Qt.WaitCursor)
success = self.detector.manage_outbound_port("remove", port, proto, target=action_target)
QApplication.restoreOverrideCursor()
if success:
self.cfg.delete_rule_description(port, proto, "OUT")
self.load_rules()
else:
QMessageBox.critical(self, "Error", locales.get_text("msg_error_cmd"))
# ==========================================
# NUEVAS CLASES PARA LOS PANELES (SUB-TABS)
# ==========================================
class NetworkTypePanel(QWidget):
zone_changed = Signal()
def __init__(self):
super().__init__()
self.detector = FirewallDetector()
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.setSpacing(15)
layout.setContentsMargins(20, 20, 20, 20)
# Título de sección
lbl_title = QLabel(locales.get_text("zone_section_title"))
lbl_title.setFont(QFont("Arial", 12, QFont.Bold))
layout.addWidget(lbl_title)
# Grupo de botones
self.bg_zones = QButtonGroup(self)
# --- PREGUNTAMOS AL SISTEMA ---
current_zone = self.detector.get_default_zone()
print(f"DEBUG: Zona detectada al inicio: {current_zone}")
# Opción 1: Pública
self.rb_public = QRadioButton(locales.get_text("zone_public_title"))
self.rb_public.setFont(QFont("Arial", 11, QFont.Bold))
layout.addWidget(self.rb_public)
self.bg_zones.addButton(self.rb_public, 1)
lbl_public_desc = QLabel(locales.get_text("zone_public_desc"))
# (Recuerda poner el color gris claro si estás en tema oscuro, o déjalo dinámico)
lbl_public_desc.setStyleSheet("color: #A0A0A0; margin-left: 25px;")
layout.addWidget(lbl_public_desc)
layout.addSpacing(10)
# Opción 2: Casa
self.rb_home = QRadioButton(locales.get_text("zone_home_title"))
self.rb_home.setFont(QFont("Arial", 11, QFont.Bold))
layout.addWidget(self.rb_home)
self.bg_zones.addButton(self.rb_home, 2)
lbl_home_desc = QLabel(locales.get_text("zone_home_desc"))
lbl_home_desc.setStyleSheet("color: #A0A0A0; margin-left: 25px;")
layout.addWidget(lbl_home_desc)
# --- LÓGICA DE SELECCIÓN AUTOMÁTICA ---
# Si la zona es 'home', 'work' o 'trusted', marcamos Casa.
if current_zone in ["home", "work", "trusted", "internal"]:
self.rb_home.setChecked(True)
else:
# Para 'public', 'block', 'drop', 'external' o cualquier otra
self.rb_public.setChecked(True)
layout.addSpacing(20)
# Botón de aplicar
self.btn_apply = QPushButton(locales.get_text("zone_btn_apply"))
self.btn_apply.setFixedWidth(200)
self.btn_apply.clicked.connect(self.apply_zone)
layout.addWidget(self.btn_apply)
layout.addStretch()
self.setLayout(layout)
def apply_zone(self):
selected_id = self.bg_zones.checkedId()
# Mapeamos la selección a zonas reales de firewalld
if selected_id == 1:
target_zone = "public" # Desconocida
zone_friendly = "Pública"
else:
target_zone = "home" # Conocida
zone_friendly = "Casa/Trabajo"
# Ejecutamos el cambio (Pedirá contraseña)
QApplication.setOverrideCursor(Qt.WaitCursor)
success = self.detector.set_default_zone(target_zone)
QApplication.restoreOverrideCursor()
if success:
QMessageBox.information(self, "Éxito", locales.get_text("zone_apply_success").format(zone_friendly))
# ¡AQUÍ ESTÁ LA MAGIA!
# Emitimos la señal para avisar al resto de la app
self.zone_changed.emit()
else:
QMessageBox.critical(self, "Error", locales.get_text("msg_error_cmd"))
def update_selection_visuals(self, zone):
"""Método auxiliar para cambiar el radio button visualmente desde fuera"""
if zone == "public":
self.rb_public.setChecked(True)
else:
self.rb_home.setChecked(True)
# Emitimos la señal para que se recarguen las tablas de reglas
self.zone_changed.emit()
# Clases Placeholder para las otras pestañas
class PlaceholderPanel(QWidget):
def __init__(self, text):
super().__init__()
layout = QVBoxLayout()
lbl = QLabel(f"🚧 {text} \n(Próximamente)")
lbl.setAlignment(Qt.AlignCenter)
layout.addWidget(lbl)
self.setLayout(layout)
# En tab_firewall.py -> Agrega QCompleter si quieres autocompletado pro,
# pero QComboBox editable sirve bien.
class SelectAppDialog(QDialog):
def __init__(self, all_services, parent=None):
super().__init__(parent)
self.setWindowTitle(locales.get_text("add_app_title"))
self.setFixedSize(350, 150)
self.all_services = all_services
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.addWidget(QLabel(locales.get_text("add_app_label")))
self.combo = QComboBox()
self.combo.setEditable(True)
self.combo.addItems(self.all_services)
# --- AQUÍ ESTÁ LA MAGIA DE QCOMPLETER ---
# 1. Creamos el autocompletador pasándole la misma lista de servicios
completer = QCompleter(self.all_services, self)
# 2. Configuración PRO: "MatchContains"
# Esto permite que si escribes "mana", te encuentre "network-manager".
# El comportamiento por defecto es "StartsWith" (solo busca el inicio).
completer.setFilterMode(Qt.MatchContains)
# 3. Ignorar mayúsculas/minúsculas (SSH = ssh)
completer.setCaseSensitivity(Qt.CaseInsensitive)
# 4. Asignamos este cerebro mejorado a nuestro combo
self.combo.setCompleter(completer)
# ----------------------------------------
layout.addWidget(self.combo)
layout.addStretch()
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
buttons.button(QDialogButtonBox.Ok).setText(locales.get_text("add_app_btn_save"))
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
self.setLayout(layout)
def get_selected_app(self):
return self.combo.currentText()
class AppsPanel(QWidget):
def __init__(self, mode="allow"):
"""
mode: 'allow' (Lista blanca) o 'block' (Lista negra/Rich Rules)
"""
super().__init__()
self.mode = mode
self.detector = FirewallDetector()
self.init_ui()
def init_ui(self):
layout = QVBoxLayout()
layout.setContentsMargins(20, 20, 20, 20)
# Título dinámico
title_key = "apps_allow_title" if self.mode == "allow" else "apps_block_title"
lbl_title = QLabel(locales.get_text(title_key))
lbl_title.setFont(QFont("Arial", 14, QFont.Bold))
layout.addWidget(lbl_title)
# Tabla
self.table = QTableWidget()
self.table.setColumnCount(2)
self.table.setHorizontalHeaderLabels([
locales.get_text("apps_header_name"),
locales.get_text("apps_header_desc")
])
self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
self.table.horizontalHeader().setStretchLastSection(True)
self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table.setSelectionMode(QAbstractItemView.SingleSelection)
self.table.setFocusPolicy(Qt.NoFocus) # Quitamos el borde azul al seleccionar
layout.addWidget(self.table)
# Botones
toolbar = QHBoxLayout()
self.btn_add = QPushButton("➕ " + locales.get_text("apps_btn_add"))
self.btn_add.clicked.connect(self.action_add)
toolbar.addWidget(self.btn_add)
self.btn_del = QPushButton("➖ " + locales.get_text("apps_btn_remove"))
self.btn_del.setStyleSheet("background-color: #d32f2f; color: white;")
self.btn_del.clicked.connect(self.action_remove)
toolbar.addWidget(self.btn_del)
toolbar.addStretch()
btn_refresh = QPushButton("🔄 " + locales.get_text("btn_refresh_rules"))
btn_refresh.setCursor(Qt.PointingHandCursor)
btn_refresh.clicked.connect(self.load_data)
toolbar.addWidget(btn_refresh)
layout.addLayout(toolbar)
self.setLayout(layout)
self.load_data()
def load_data(self):
self.table.setRowCount(0)
# Obtenemos la lista según el modo
if self.mode == "allow":
services = self.detector.get_active_services()
empty_msg = locales.get_text("apps_no_allowed")
color = "#4CAF50" # Verde
else:
services = self.detector.get_blocked_services()
empty_msg = locales.get_text("apps_no_blocked")
color = "#d32f2f" # Rojo
if not services:
# Opcional: Poner item de aviso
return
self.table.setRowCount(len(services))
for row, svc in enumerate(services):
item_name = QTableWidgetItem(svc)
item_name.setForeground(QColor(color))
item_name.setFont(QFont("Arial", 10, QFont.Bold))
# Descripción (Dummy por ahora, firewalld tiene descripciones pero es complejo sacarlas)
item_desc = QTableWidgetItem(f"Servicio {svc}")
self.table.setItem(row, 0, item_name)
self.table.setItem(row, 1, item_desc)
def action_add(self):
# 1. Obtenemos TODOS los servicios posibles para llenar la lista
all_svcs = self.detector.get_all_available_services()
dialog = SelectAppDialog(all_svcs, self)
if dialog.exec():
selected = dialog.get_selected_app()
if not selected: return
QApplication.setOverrideCursor(Qt.WaitCursor)
# manage_service(action="add", name, mode="allow/block")
success = self.detector.manage_service("add", selected, self.mode)
QApplication.restoreOverrideCursor()
if success:
self.load_data()
else:
QMessageBox.critical(self, "Error", locales.get_text("msg_error_cmd"))
def action_remove(self):
current_row = self.table.currentRow()
if current_row < 0: return
svc_name = self.table.item(current_row, 0).text()
QApplication.setOverrideCursor(Qt.WaitCursor)
success = self.detector.manage_service("remove", svc_name, self.mode)
QApplication.restoreOverrideCursor()
if success:
self.load_data()
else:
QMessageBox.critical(self, "Error", locales.get_text("msg_error_cmd"))
# ==========================================
# CLASE PRINCIPAL (FirewallTab)
# ==========================================
class FirewallTab(QWidget):
def __init__(self):
super().__init__()
self.detector = FirewallDetector()
self.installer = FirewallInstaller()
self.net_utils = NetworkUtils() # <--- Instanciamos utils
self.cfg = ConfigManager() # <--- Instanciamos config
self.current_service = None
self.last_known_net = None
self.init_ui()
# --- MONITORIZACIÓN DE RED (AUTO-PILOT) ---
self.net_timer = QTimer(self)
self.net_timer.timeout.connect(self.check_network_change)
self.net_timer.start(5000) # Chequear cada 5000ms (5 segundos)
def init_ui(self):
main_layout = QVBoxLayout()
main_layout.setContentsMargins(20, 20, 20, 20)
main_layout.setSpacing(10)
# --- 1. CABECERA (Header) ---
header_layout = QHBoxLayout()
title = QLabel(locales.get_text("fw_title"))
title.setFont(QFont("Arial", 16, QFont.Bold))
header_layout.addWidget(title)
header_layout.addStretch()
self.lbl_status_text = QLabel("...")
self.lbl_status_text.setFont(QFont("Arial", 10, QFont.Bold))
self.lbl_status_text.setContentsMargins(0, 0, 10, 0)
header_layout.addWidget(self.lbl_status_text)
self.toggle = ToggleSwitch()
self.toggle.clicked.connect(self.on_toggle_click)
header_layout.addWidget(self.toggle)
main_layout.addLayout(header_layout)
# --- 2. INFO TÉCNICA Y LÍNEA ---
self.lbl_tech_info = QLabel("-")
self.lbl_tech_info.setStyleSheet("color: gray; font-size: 11px;")
self.lbl_tech_info.setAlignment(Qt.AlignRight)
main_layout.addWidget(self.lbl_tech_info)
line = QFrame()
line.setFrameShape(QFrame.HLine)
line.setFrameShadow(QFrame.Sunken)
line.setStyleSheet("background-color: #cccccc;")
main_layout.addWidget(line)
# --- 3. ZONA DE PESTAÑAS ---
self.tabs_firewall = QTabWidget()
self.tabs_firewall.setFocusPolicy(Qt.NoFocus) # Sin borde de foco
self.tabs_firewall.setStyleSheet("""
QTabWidget::pane {
border: 1px solid #444444;
}
QTabBar::tab { padding: 8px 15px; outline: 0px; }
QTabBar::tab:selected { font-weight: bold; }
""")
# A. Instanciamos TODOS los paneles
self.panel_net = NetworkTypePanel()
self.panel_in = InboundRulesPanel()
self.panel_out = OutboundRulesPanel()
self.panel_apps_allow = AppsPanel(mode="allow")
self.panel_apps_block = AppsPanel(mode="block")
# B. Conectamos la señal de cambio de zona a TODOS los paneles que lo necesitan
# (Así cuando cambies de zona, se actualizan las reglas y las apps)
self.panel_net.zone_changed.connect(self.panel_in.load_rules)
self.panel_net.zone_changed.connect(self.panel_out.load_rules)
self.panel_net.zone_changed.connect(self.panel_apps_allow.load_data)
self.panel_net.zone_changed.connect(self.panel_apps_block.load_data)
# C. Añadimos las pestañas AL WIDGET (Solo una vez cada una)
self.tabs_firewall.addTab(self.panel_net, locales.get_text("fw_tab_net_type"))
self.tabs_firewall.addTab(self.panel_in, locales.get_text("fw_tab_rules_in"))
self.tabs_firewall.addTab(self.panel_out, locales.get_text("fw_tab_rules_out"))
self.tabs_firewall.addTab(self.panel_apps_allow, locales.get_text("fw_tab_apps_allow"))
self.tabs_firewall.addTab(self.panel_apps_block, locales.get_text("fw_tab_apps_block"))
main_layout.addWidget(self.tabs_firewall)
self.setLayout(main_layout)
self.check_status()
def check_status(self):
self.toggle.blockSignals(True)
status = self.detector.detect()
self.current_service = None
if status.type == FirewallType.FIREWALLD:
self.current_service = "firewalld"
elif status.type == FirewallType.UFW:
self.current_service = "ufw"
# 1. Actualizar el Toggle
self.toggle.setChecked(status.active)
# 2. Lógica de Activación de UI
if self.current_service:
self.update_ui_state(status.active, status.details)
self.toggle.setEnabled(True)
# --- LÓGICA DE PESTAÑAS (Aquí está el cambio) ---
if status.active:
self.tabs_firewall.setEnabled(True)
# CASO UFW: Desactivar Pestaña 0 (Tipo de Red)
if self.current_service == "ufw":
# Deshabilitar la pestaña 0 (Zonas)
self.tabs_firewall.setTabEnabled(0, False)
self.tabs_firewall.setTabToolTip(0, locales.get_text("msg_ufw_no_zones"))
# Si estamos en la pestaña 0, nos movemos a la 1 (Reglas) forzosamente
if self.tabs_firewall.currentIndex() == 0:
self.tabs_firewall.setCurrentIndex(1)
# CASO FIREWALLD: Todo habilitado
else:
self.tabs_firewall.setTabEnabled(0, True)
self.tabs_firewall.setTabToolTip(0, "")
else:
# Si el firewall está apagado, deshabilitamos todo el panel de pestañas
self.tabs_firewall.setEnabled(False)
# ------------------------------------------------
else:
# Caso sin gestor instalado
self.lbl_status_text.setText(locales.get_text("fw_missing_title"))
self.lbl_status_text.setStyleSheet("color: orange;")
self.lbl_tech_info.setText(locales.get_text("fw_missing_desc"))
self.toggle.setEnabled(True)
self.tabs_firewall.setEnabled(False)
self.toggle.blockSignals(False)
def update_ui_state(self, is_active, details):
service_name = self.current_service if self.current_service else "Desconocido"
if is_active:
self.lbl_status_text.setText(locales.get_text("fw_status_active"))
self.lbl_status_text.setStyleSheet("color: #2ecc71;")
self.lbl_tech_info.setText(locales.get_text("fw_backend").format(service_name))
else:
self.lbl_status_text.setText(locales.get_text("fw_status_inactive"))
self.lbl_status_text.setStyleSheet("color: #e74c3c;")
self.lbl_tech_info.setText(locales.get_text("fw_warning").format(service_name))
def on_toggle_click(self):
target_state = self.toggle.isChecked()
if not self.current_service:
# Lógica de instalación (reutilizada)
self.toggle.blockSignals(True)
self.toggle.setChecked(False)
self.toggle.blockSignals(False)
dialog = InstallDialog(self)
if dialog.exec():
fw_to_install = dialog.get_selected_firewall()
self.progress = QProgressDialog(
locales.get_text("inst_status_working"), None, 0, 0, self)
self.progress.setWindowTitle("SentinelX")
self.progress.setWindowModality(Qt.WindowModal)
self.progress.setMinimumDuration(0)
self.worker = InstallWorker(self.installer, fw_to_install)
self.worker.finished_signal.connect(self.on_install_finished)
self.progress.show()
self.worker.start()
return
# Lógica de toggle
QApplication.processEvents()
success = self.detector.set_firewall_state(self.current_service, target_state)
if success:
self.update_ui_state(target_state, f"Gestionado por {self.current_service}")
self.tabs_firewall.setEnabled(target_state) # Activar/Desactivar pestañas
else:
self.toggle.blockSignals(True)
self.toggle.setChecked(not target_state)
self.toggle.blockSignals(False)
def on_install_finished(self, success):
self.progress.close()
fw_name = self.worker.fw_name
if success:
QMessageBox.information(self, locales.get_text("inst_success_title"),
locales.get_text("inst_success_msg").format(fw_name))
self.check_status()
else:
QMessageBox.critical(self, locales.get_text("inst_error_title"),
locales.get_text("inst_error_msg"))
# --- LÓGICA AUTOMÁTICA WIFI ---
def check_network_change(self):
# 1. Si no hay firewall activo (o es UFW que no soporta zonas), no hacemos nada
if not self.current_service or self.current_service == "ufw":
return
# 2. Detectar red actual
current_net_name = self.net_utils.get_active_connection_name()
# Si no hay red o es la misma que ya procesamos, salir
if not current_net_name or current_net_name == self.last_known_net:
return
# 3. ¡Cambio de red detectado!
print(f"Red cambiada: {self.last_known_net} -> {current_net_name}")
self.last_known_net = current_net_name # Actualizamos memoria inmediata
# 4. Consultar Base de Datos
saved_zone = self.cfg.get_network_zone(current_net_name)
if saved_zone:
# A) RED CONOCIDA -> Aplicar silencio
print(f"Red conocida. Aplicando zona: {saved_zone}")
# Solo aplicamos si la zona actual del sistema es diferente (para no spamear comandos)
current_system_zone = self.detector.get_default_zone()
if current_system_zone != saved_zone:
self.detector.set_default_zone(saved_zone)
# Actualizamos la UI visualmente