ui: allow to configure (local) server address

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.
This commit is contained in:
Gustavo Iñiguez Goia 2022-10-06 13:57:52 +02:00
parent cfeba55515
commit 915b325a00
Failed to generate hash of commit
3 changed files with 50 additions and 8 deletions

View file

@ -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
@ -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(),

View file

@ -107,6 +107,15 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
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)
@ -265,8 +274,7 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
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")
@ -442,13 +450,18 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
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._notifications_sent[nid] = notifObject
self._cfg.setSettings(Config.DEFAULT_SERVER_ADDR, self.comboNodeAddress.currentText())
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))))

View file

@ -278,6 +278,21 @@ class Utils():
))[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