Skip to content

Commit 8ac02ac

Browse files
ui: display nodes ip and hostname on more places
Added the hostname to the node comboboxes, to help identify them more easily.
1 parent a741326 commit 8ac02ac

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

ui/opensnitch/dialogs/preferences.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,7 @@ def showEvent(self, event):
190190
self.comboNodeAddress.addItem("unix://%s/osui.sock" % var_run_path)
191191
self.comboServerAddr.addItem("unix://%s/osui.sock" % var_run_path)
192192

193-
self._node_list = self._nodes.get()
194-
for addr in self._node_list:
195-
self.comboNodes.addItem(addr)
196-
197-
if len(self._node_list) == 0:
198-
self._reset_node_settings()
199-
self._set_status_message(QC.translate("preferences", "There're no nodes connected"))
200-
201-
showNodes = len(self._node_list) > 1
202-
self.comboNodes.setVisible(showNodes)
203-
self.checkApplyToNodes.setVisible(showNodes)
193+
self.load_nodes()
204194
except Exception as e:
205195
print(self.LOG_TAG + "exception loading nodes:", e)
206196

@@ -257,8 +247,29 @@ def showEvent(self, event):
257247

258248
def show_node_prefs(self, addr):
259249
self.show()
260-
self.comboNodes.setCurrentText(addr)
261-
self.tabWidget.setCurrentIndex(self.TAB_NODES)
250+
nIdx = self.comboNodes.findData(addr)
251+
if nIdx != -1:
252+
self.comboNodes.setCurrentIndex(nIdx)
253+
self.tabWidget.setCurrentIndex(self.TAB_NODES)
254+
255+
def load_nodes(self):
256+
self._node_list = self._nodes.get()
257+
for addr in self._node_list:
258+
hostname = self._nodes.get_node_hostname(addr)
259+
self.comboNodes.addItem(f"{addr} - {hostname}", addr)
260+
261+
if len(self._node_list) == 0:
262+
self._reset_node_settings()
263+
self._set_status_message(QC.translate("preferences", "There're no nodes connected"))
264+
265+
showNodes = len(self._node_list) > 1
266+
self.comboNodes.setVisible(showNodes)
267+
self.checkApplyToNodes.setVisible(showNodes)
268+
269+
def get_node_addr(self):
270+
nIdx = self.comboNodes.currentIndex()
271+
addr = self.comboNodes.itemData(nIdx)
272+
return addr
262273

263274
def _load_langs(self):
264275
try:
@@ -417,8 +428,8 @@ def _load_ui_settings(self):
417428
self._load_ui_columns_config()
418429

419430
def _load_node_settings(self):
420-
addr = self.comboNodes.currentText()
421-
if addr == "":
431+
addr = self.get_node_addr()
432+
if addr is None:
422433
return
423434

424435
try:
@@ -506,7 +517,7 @@ def _load_node_settings(self):
506517

507518
except Exception as e:
508519
print(self.LOG_TAG + "exception loading config: ", e)
509-
self._set_status_error(QC.translate("preferences", "Error loading config: {0}".format(e)))
520+
self._set_status_error(QC.translate("preferences", "Error loading config {0}: {1}".format(addr, e)))
510521

511522
def _load_node_config(self, addr):
512523
"""load the config of a node before sending it back to the node"""
@@ -904,8 +915,8 @@ def _save_ui_columns_config(self):
904915
self._cfg.setSettings(Config.STATS_SHOW_COLUMNS, cols)
905916

906917
def _save_nodes_config(self):
907-
addr = self.comboNodes.currentText()
908-
if (self._node_needs_update or self.checkApplyToNodes.isChecked()) and addr != "":
918+
addr = self.get_node_addr()
919+
if (self._node_needs_update or self.checkApplyToNodes.isChecked()) and addr is not None:
909920
self._set_status_message(QC.translate("preferences", "Saving configuration..."))
910921
try:
911922
notif = ui_pb2.Notification(
@@ -929,7 +940,7 @@ def _save_nodes_config(self):
929940
self._set_status_error(QC.translate("preferences", "Exception saving config: {0}").format(str(e)))
930941
return False
931942

932-
elif addr == "":
943+
elif addr is None:
933944
self._set_status_message(QC.translate("preferences", "There're no nodes connected"))
934945
return False
935946

@@ -949,7 +960,8 @@ def _save_node_config(self, notifObject, addr):
949960
# exclude this message if there're more than one node connected
950961
# XXX: unix:/local is a special name for the node, when the gRPC
951962
# does not return the correct address of the node.
952-
if (self.comboNodes.currentText() != "unix:/local" and self.comboNodes.currentText() != self.comboNodeAddress.currentText()) or \
963+
naddr = self.get_node_addr()
964+
if (naddr != "unix:/local" and naddr != self.comboNodeAddress.currentText()) or \
953965
self.comboServerAddr.currentText() != self.comboNodeAddress.currentText():
954966
self._changes_needs_restart = QC.translate("preferences", "Node address changed (update GUI address if needed)")
955967

ui/opensnitch/dialogs/ruleseditor.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def showEvent(self, event):
136136
oldUid = self.uidCombo.currentText()
137137
self.ifaceCombo.clear()
138138
self.uidCombo.clear()
139-
if self._nodes.is_local(self.nodesCombo.currentText()):
139+
addr = self.get_node_addr()
140+
if addr is not None and self._nodes.is_local(addr):
140141
self.ifaceCombo.addItems(NetworkInterfaces.list().keys())
141142
try:
142143
for ip in NetworkInterfaces.list().values():
@@ -277,7 +278,8 @@ def _cb_save_clicked(self):
277278
if rule_name == "":
278279
return
279280

280-
node = self.nodesCombo.currentText()
281+
#node = self.nodesCombo.currentText()
282+
node = self.get_node_addr()
281283
# avoid to overwrite rules when:
282284
# - adding a new rule.
283285
# - when a rule is renamed, i.e., the rule is edited or added and the
@@ -431,15 +433,19 @@ def _is_valid_regex(self, regex):
431433
def _is_valid_list_path(self, listWidget):
432434
if listWidget.text() == "":
433435
return QC.translate("rules", "Lists field cannot be empty")
434-
if self._nodes.is_local(self.nodesCombo.currentText()) and \
436+
if self._nodes.is_local(self.get_node_addr()) and \
435437
self.nodeApplyAllCheck.isChecked() == False and \
436438
os.path.isdir(listWidget.text()) == False:
437439
return QC.translate("rules", "Lists field must be a directory")
438440

439441
return None
440442

441443
def set_fields_from_connection(self, records):
442-
self.nodesCombo.setCurrentText(records.value(ConnFields.Node))
444+
nIdx = self.nodesCombo.findData(records.value(ConnFields.Node))
445+
if nIdx == -1:
446+
self.set_status_error("Unable to load connection, unknown node? ({0})".format(nIdx))
447+
return
448+
self.nodesCombo.setCurrentIndex(nIdx)
443449
self.protoCombo.setCurrentText(records.value(ConnFields.Protocol).upper())
444450
self.srcIPCombo.setCurrentText(records.value(ConnFields.SrcIP))
445451
self.dstIPCombo.setCurrentText(records.value(ConnFields.DstIP))
@@ -691,7 +697,7 @@ def _load_nodes(self, addr=None):
691697
self.nodesCombo.clear()
692698
self._node_list = self._nodes.get()
693699

694-
if addr != None and addr not in self._node_list:
700+
if addr is not None and addr not in self._node_list:
695701
Message.ok(QC.translate("rules", "<b>Error loading rule</b>"),
696702
QC.translate("rules", "node {0} not connected".format(addr)),
697703
QtWidgets.QMessageBox.Icon.Warning)
@@ -701,10 +707,12 @@ def _load_nodes(self, addr=None):
701707
self.nodeApplyAllCheck.setVisible(False)
702708

703709
for node in self._node_list:
704-
self.nodesCombo.addItem(node)
710+
hostname = self._nodes.get_node_hostname(node)
711+
self.nodesCombo.addItem(f"{node} - {hostname}", node)
705712

706-
if addr != None:
707-
self.nodesCombo.setCurrentText(addr)
713+
nIdx = self.nodesCombo.findData(addr)
714+
if nIdx != -1:
715+
self.nodesCombo.setCurrentIndex(nIdx)
708716

709717
showNodes = len(self._node_list) > 1
710718
self.nodesCombo.setVisible(showNodes)
@@ -716,18 +724,24 @@ def _load_nodes(self, addr=None):
716724

717725
return True
718726

727+
def get_node_addr(self):
728+
nIdx = self.nodesCombo.currentIndex()
729+
addr = self.nodesCombo.itemData(nIdx)
730+
return addr
731+
719732
def _insert_rule_to_db(self, node_addr):
720733
# the order of the fields doesn't matter here, as long as we use the
721734
# name of the field.
722735
self._rules.add_rules(node_addr, [self.rule])
723736

724737
def _add_rule(self):
725738
try:
739+
addr = self.get_node_addr()
726740
if self.nodeApplyAllCheck.isChecked():
727-
for pos in range(self.nodesCombo.count()):
728-
self._insert_rule_to_db(self.nodesCombo.itemText(pos))
741+
for idx in range(self.nodesCombo.count()):
742+
self._insert_rule_to_db(self.nodesCombo.itemData(idx))
729743
else:
730-
self._insert_rule_to_db(self.nodesCombo.currentText())
744+
self._insert_rule_to_db(addr)
731745

732746
notif = ui_pb2.Notification(
733747
id=int(str(time.time()).replace(".", "")),
@@ -737,7 +751,7 @@ def _add_rule(self):
737751
if self.nodeApplyAllCheck.isChecked():
738752
nid = self._nodes.send_notifications(notif, self._notification_callback)
739753
else:
740-
nid = self._nodes.send_notification(self.nodesCombo.currentText(), notif, self._notification_callback)
754+
nid = self._nodes.send_notification(addr, notif, self._notification_callback)
741755

742756
self._notifications_sent[nid] = notif
743757
except Exception as e:
@@ -747,7 +761,7 @@ def _delete_rule(self):
747761
try:
748762
# if the rule name has changed, we need to remove the old one
749763
if self._old_rule_name != self.rule.name:
750-
node = self.nodesCombo.currentText()
764+
node = self.get_node_addr()
751765
old_rule = self.rule
752766
old_rule.name = self._old_rule_name
753767
if self.nodeApplyAllCheck.isChecked():

0 commit comments

Comments
 (0)