Skip to content

Commit

Permalink
Unified UI config files
Browse files Browse the repository at this point in the history
Originally UI config file was saved to ~/.opensnitch/ui-config.json

Now those values are saved to ~/.config/opensnitch/settings.conf, along
with new ones.

Closes #3
  • Loading branch information
gustavo-iniguez-goya committed Feb 9, 2020
1 parent a5f8d5b commit 1e6d2c0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 52 deletions.
3 changes: 1 addition & 2 deletions ui/bin/opensnitch-ui
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def supported_qt_version(major, medium, minor):
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='OpenSnitch UI service.')
parser.add_argument("--socket", dest="socket", default="unix:///tmp/osui.sock", help="Path of the unix socket for the gRPC service (https://github.com/grpc/grpc/blob/master/doc/naming.md).", metavar="FILE")
parser.add_argument("--config", dest="config", default="~/.opensnitch/ui-config.json", help="Path of the UI json configuration file.", metavar="FILE")

args = parser.parse_args()

Expand All @@ -42,7 +41,7 @@ if __name__ == '__main__':
if supported_qt_version(5,6,0):
app.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

service = UIService(app, on_exit, args.config)
service = UIService(app, on_exit)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=4))

add_UIServicer_to_server(service, server)
Expand Down
39 changes: 7 additions & 32 deletions ui/opensnitch/config.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,28 @@
import os
import json
from PyQt5 import QtCore

class Config:
__instance = None

@staticmethod
def init(filename):
Config.__instance = Config(filename)
def init():
Config.__instance = Config()
return Config.__instance

@staticmethod
def get():
return Config.__instance

def __init__(self, filename):
self.filename = os.path.abspath( os.path.expanduser(filename) )
self.exists = os.path.isfile(self.filename)

def __init__(self):
self.settings = QtCore.QSettings("opensnitch", "settings")

self.default_timeout = 15
self.default_action = "allow"
self.default_duration = "until restart"

if self.exists:
# print( "Loading configuration from %s ..." % self.filename )
data = json.load(open(self.filename))

self.default_timeout = data["default_timeout"]
self.default_action = data["default_action"]
self.default_duration = data["default_duration"]
if self.settings.value("global/default_timeout") == None:
self.setSettings("global/default_timeout", 15)
self.setSettings("global/default_action", "allow")
self.setSettings("global/default_duration", "until restart")

def setSettings(self, path, value):
self.settings.setValue(path, value)

def getSettings(self, path):
return self.settings.value(path)

def save(self):
dirname = os.path.dirname(self.filename)
if os.path.isdir(dirname) == False:
os.makedirs(dirname, exist_ok=True)

with open(self.filename, 'w') as fp:
data = {
'default_timeout': self.default_timeout,
'default_action': self.default_action,
'default_duration': self.default_duration
}
json.dump(data, fp)
self.exists = True
22 changes: 11 additions & 11 deletions ui/opensnitch/dialogs/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, parent=None):
self._prompt_trigger.connect(self.on_connection_prompt_triggered)
self._timeout_trigger.connect(self.on_timeout_triggered)
self._tick_trigger.connect(self.on_tick_triggered)
self._tick = self._cfg.default_timeout
self._tick = int(self._cfg.getSettings("global/default_timeout"))
self._tick_thread = None
self._done = threading.Event()
self._apply_text = "Apply"
Expand Down Expand Up @@ -102,7 +102,7 @@ def promptUser(self, connection, is_local, peer):
# reset state
if self._tick_thread != None and self._tick_thread.is_alive():
self._tick_thread.join()
self._tick = self._cfg.default_timeout
self._tick = int(self._cfg.getSettings("global/default_timeout"))
self._tick_thread = threading.Thread(target=self._timeout_worker)
self._tick_thread.stop = self._is_advanced_checked
self._timeout_triggered = False
Expand All @@ -124,7 +124,7 @@ def _timeout_worker(self):
while self._tick > 0 and self._done.is_set() is False:
t = threading.currentThread()
if getattr(t, "stop", True):
self._tick = self._cfg.default_timeout
self._tick = int(self._cfg.getSettings("global/default_timeout"))
continue

self._tick -= 1
Expand Down Expand Up @@ -223,24 +223,24 @@ def _render_connection(self, con):
self._what_combo.addItem("to %s.*" % '.'.join(parts[:i]), "regex_ip")
self._what_dstip_combo.addItem("to %s.*" % '.'.join(parts[:i]), "regex_ip")

if self._cfg.default_action == "allow":
if self._cfg.getSettings("global/default_action") == "allow":
self._action_combo.setCurrentIndex(0)
else:
self._action_combo.setCurrentIndex(1)

if self._cfg.default_duration == "once":
if self._cfg.getSettings("global/default_duration") == "once":
self._duration_combo.setCurrentIndex(0)
elif self._cfg.default_duration == "30s":
elif self._cfg.getSettings("global/default_duration") == "30s":
self._duration_combo.setCurrentIndex(1)
elif self._cfg.default_duration == "5m":
elif self._cfg.getSettings("global/default_duration") == "5m":
self._duration_combo.setCurrentIndex(2)
elif self._cfg.default_duration == "15m":
elif self._cfg.getSettings("global/default_duration") == "15m":
self._duration_combo.setCurrentIndex(3)
elif self._cfg.default_duration == "30m":
elif self._cfg.getSettings("global/default_duration") == "30m":
self._duration_combo.setCurrentIndex(4)
elif self._cfg.default_duration == "1h":
elif self._cfg.getSettings("global/default_duration") == "1h":
self._duration_combo.setCurrentIndex(5)
elif self._cfg.default_duration == "until restart":
elif self._cfg.getSettings("global/default_duration") == "until restart":
self._duration_combo.setCurrentIndex(6)
else:
self._duration_combo.setCurrentIndex(7)
Expand Down
9 changes: 2 additions & 7 deletions ui/opensnitch/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
_version_warning_trigger = QtCore.pyqtSignal(str, str)
_status_change_trigger = QtCore.pyqtSignal()

def __init__(self, app, on_exit, config):
def __init__(self, app, on_exit):
super(UIService, self).__init__()
self._db = Database.instance()

self._cfg = Config.init(config)
self._cfg = Config.init()
self._last_ping = None
self._version_warning_shown = False
self._asking = False
Expand All @@ -48,11 +48,6 @@ def __init__(self, app, on_exit, config):
self._remote_lock = Lock()
self._remote_stats = {}

# make sure we save the configuration if it
# does not exist as a file yet
if self._cfg.exists == False:
self._cfg.save()

self._setup_interfaces()
self._setup_slots()
self._setup_icons()
Expand Down

0 comments on commit 1e6d2c0

Please sign in to comment.