Skip to content

Commit

Permalink
ui: allow to configure (local) server address
Browse files Browse the repository at this point in the history
Up until now, the daemon communicated with the GUI via a unix socket,
stored in /tmp.

/tmp however can be erased at any time (tmpreaper, systemd-tmpfiles.d),
which may lead to remove our unix socket file, and hence losing
connectiong with the daemon.

Now the user has the option to store the socket file under
/run/user/$uid/opensnitch/
https://www.linuxbase.org/betaspecs/fhs/fhs.html#runRuntimeVariableData

In the future we may switch to this path by default.
  • Loading branch information
gustavo-iniguez-goya committed Oct 6, 2022
1 parent cfeba55 commit 915b325
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
14 changes: 14 additions & 0 deletions ui/bin/opensnitch-ui
@@ -1,6 +1,8 @@
#!/usr/bin/env python3

from PyQt5 import QtWidgets, QtGui, QtCore
from opensnitch.config import Config
from opensnitch.utils import Utils

import sys
import os
Expand Down Expand Up @@ -66,6 +68,18 @@ if __name__ == '__main__':
thm = Themes.instance()
thm.load_theme(app)

Utils.create_socket_dirs()
if args.socket == "unix:///tmp/osui.sock":
cfg = Config.get()
addr = cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
if addr != None and addr != "" and addr.startswith("unix://"):
if not os.path.exists(os.path.dirname(addr[7:])):
print("WARNING: unix socket path does not exist, using unix:///tmp/osui.sock, ", addr)
else:
args.socket = addr

print("Using server address:", args.socket)

service = UIService(app, on_exit)
# @doc: https://grpc.github.io/grpc/python/grpc.html#server-object
server = grpc.server(futures.ThreadPoolExecutor(),
Expand Down
29 changes: 21 additions & 8 deletions ui/opensnitch/dialogs/preferences.py
Expand Up @@ -107,6 +107,15 @@ def showEvent(self, event):
self._hide_status_label()
self.comboNodes.clear()

self.comboNodeAddress.clear()
run_path = "/run/user/{0}/opensnitch/".format(os.getuid())
var_run_path = "/var{0}".format(run_path)
self.comboNodeAddress.addItem("unix:///tmp/osui.sock")
if os.path.exists(run_path):
self.comboNodeAddress.addItem("unix://%s/osui.sock" % run_path)
if os.path.exists(var_run_path):
self.comboNodeAddress.addItem("unix://%s/osui.sock" % var_run_path)

self._node_list = self._nodes.get()
for addr in self._node_list:
self.comboNodes.addItem(addr)
Expand Down Expand Up @@ -265,8 +274,7 @@ def _load_node_config(self, addr):

if node_config.get('Server') != None:
# skip setting Server Address if we're applying the config to all nodes
if self.checkApplyToNodes.isChecked():
node_config['Server']['Address'] = self.comboNodeAddress.currentText()
node_config['Server']['Address'] = self.comboNodeAddress.currentText()
node_config['Server']['LogFile'] = self.comboNodeLogFile.currentText()
else:
print(addr, " doesn't have Server item")
Expand Down Expand Up @@ -442,13 +450,18 @@ def _save_node_config(self, notifObject, addr):
if error != None:
return error

if addr.startswith("unix://"):
self._cfg.setSettings(self._cfg.DEFAULT_DEFAULT_SERVER_ADDR, self.comboNodeAddress.currentText())
else:
self._nodes.save_node_config(addr, notifObject.data)
nid = self._nodes.send_notification(addr, notifObject, self._notification_callback)
if addr.startswith("unix:/"):
if self._cfg.getSettings(Config.DEFAULT_SERVER_ADDR) != self.comboNodeAddress.currentText():
Message.ok(
QC.translate("preferences", "Ok"),
QC.translate("preferences", "Restart the GUI in order changes to take effect"),
QtWidgets.QMessageBox.Information)

self._cfg.setSettings(Config.DEFAULT_SERVER_ADDR, self.comboNodeAddress.currentText())

self._notifications_sent[nid] = notifObject
self._nodes.save_node_config(addr, notifObject.data)
nid = self._nodes.send_notification(addr, notifObject, self._notification_callback)
self._notifications_sent[nid] = notifObject
except Exception as e:
print(self.LOG_TAG + "exception saving node config on %s: " % addr, e)
self._set_status_error(QC.translate("Exception saving node config {0}: {1}").format((addr, str(e))))
Expand Down
15 changes: 15 additions & 0 deletions ui/opensnitch/utils.py
Expand Up @@ -278,6 +278,21 @@ def get_interfaces():
))[0]
return names.tobytes(), outbytes

@staticmethod
def create_socket_dirs():
"""https://www.linuxbase.org/betaspecs/fhs/fhs.html#runRuntimeVariableData
"""
run_path = "/run/user/{0}".format(os.getuid())
var_run_path = "/var{0}".format(run_path)

try:
if os.path.exists(run_path):
os.makedirs(run_path + "/opensnitch/")
if os.path.exists(var_run_path):
os.makedirs(var_run_path + "/opensnitch/")
except:
pass

class Message():

@staticmethod
Expand Down

0 comments on commit 915b325

Please sign in to comment.