Skip to content

Commit

Permalink
ui: improved DefaultAction behaviour
Browse files Browse the repository at this point in the history
- On the very first install, or if the GUI's settings.conf file was not
  created, GUI's DefaultAction item was not configured properly.
- Now when the daemon is not connected to the GUI, it'll use the
  DefaultAction configured in /etc/opensnitchd/default-config.json
- When the daemon is connected to the GUI, the GUI will reconfigure
  daemon's DefaultAction value when the one defined by the GUI.
  In this case the value defined in default-config.json is not modified,
  it'll only be valid while it's connected to the GUI.

Now when opening Preferences->Nodes, it'll display daemon's
DefaultAction defined in the file default-config.json file, which is the
default action applied when the daemon is not connected to the GUI.

Related: #884 , #896
  • Loading branch information
gustavo-iniguez-goya committed Apr 15, 2023
1 parent fb27e4c commit 74b6bc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ui/opensnitch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(self):
if self.settings.value(self.DEFAULT_TIMEOUT_KEY) == None:
self.setSettings(self.DEFAULT_TIMEOUT_KEY, self.DEFAULT_TIMEOUT)
if self.settings.value(self.DEFAULT_ACTION_KEY) == None:
self.setSettings(self.DEFAULT_ACTION_KEY, self.ACTION_ALLOW)
self.setSettings(self.DEFAULT_ACTION_KEY, self.ACTION_DENY_IDX)
if self.settings.value(self.DEFAULT_DURATION_KEY) == None:
self.setSettings(self.DEFAULT_DURATION_KEY, self.DEFAULT_DURATION_IDX)
if self.settings.value(self.DEFAULT_TARGET_KEY) == None:
Expand Down
16 changes: 11 additions & 5 deletions ui/opensnitch/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import sys
import json
import copy

path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(path)
Expand Down Expand Up @@ -592,19 +593,24 @@ def _populate_stats_events(self, db, addr, stats, table, colnames, cols, items):
return need_refresh

def _overwrite_nodes_config(self, node_config):
"""Overwrite daemon's DefaultAction value, with the one defined by the GUI.
It'll only be valid while the daemon is connected to the GUI (it's not saved to disk).
"""
newconf = copy.deepcopy(node_config)
_default_action = self._cfg.getInt(self._cfg.DEFAULT_ACTION_KEY)
temp_cfg = json.loads(node_config)
try:
temp_cfg = json.loads(newconf.config)
if _default_action == Config.ACTION_DENY_IDX:
temp_cfg['DefaultAction'] = Config.ACTION_DENY
else:
temp_cfg['DefaultAction'] = Config.ACTION_ALLOW

node_config = json.dumps(temp_cfg)
newconf.config = json.dumps(temp_cfg)
except Exception as e:
print("error parsing node's configuration:", e)
return node_config

return node_config
return newconf

@QtCore.pyqtSlot(dict)
def _on_node_actions(self, kwargs):
Expand Down Expand Up @@ -750,9 +756,9 @@ def Subscribe(self, node_config, context):
print("[Notifications] exception adding new node:", e)
context.cancel()

node_config.config = self._overwrite_nodes_config(node_config.config)
newconf = self._overwrite_nodes_config(node_config)

return node_config
return newconf

def Notifications(self, node_iter, context):
"""
Expand Down

0 comments on commit 74b6bc2

Please sign in to comment.