mirror of
https://github.com/evilsocket/opensnitch.git
synced 2025-03-04 08:34:40 +01:00
ui: improved nodes management
- Fixed reconfiguring nodes address from/to unix sockets <-> net sockets. - Fixed updating nodes status when connecting/disconnecting.
This commit is contained in:
parent
c364ee1d7a
commit
9d5823c48b
5 changed files with 46 additions and 36 deletions
|
@ -68,14 +68,16 @@ if __name__ == '__main__':
|
|||
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://"):
|
||||
cfg = Config.get()
|
||||
addr = cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
|
||||
if addr != None and addr != "":
|
||||
if 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
|
||||
else:
|
||||
args.socket = addr
|
||||
|
||||
print("Using server address:", args.socket)
|
||||
|
||||
|
|
|
@ -373,8 +373,10 @@ class Database:
|
|||
|
||||
return self._insert(qstr, columns)
|
||||
|
||||
def update(self, table, fields, values, condition, action_on_conflict="OR IGNORE"):
|
||||
qstr = "UPDATE " + action_on_conflict + " " + table + " SET " + fields + " WHERE " + condition
|
||||
def update(self, table, fields, values, condition=None, action_on_conflict="OR IGNORE"):
|
||||
qstr = "UPDATE " + action_on_conflict + " " + table + " SET " + fields
|
||||
if condition != None:
|
||||
qstr += " WHERE " + condition
|
||||
try:
|
||||
with self._lock:
|
||||
q = QSqlQuery(qstr, self.db)
|
||||
|
@ -382,7 +384,7 @@ class Database:
|
|||
for idx, v in enumerate(values):
|
||||
q.bindValue(idx, v)
|
||||
if not q.exec_():
|
||||
print("update ERROR", qstr)
|
||||
print("update ERROR:", qstr, "values:", values)
|
||||
print(q.lastError().driverText())
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
@ -146,9 +146,9 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
|
|||
self._node_needs_update = False
|
||||
|
||||
def show_node_prefs(self, addr):
|
||||
self.comboNodeAddress.setCurrentText(addr)
|
||||
self.tabWidget.setCurrentIndex(self.TAB_NODES)
|
||||
self.show()
|
||||
self.comboNodes.setCurrentText(addr)
|
||||
self.tabWidget.setCurrentIndex(self.TAB_NODES)
|
||||
|
||||
def _load_themes(self):
|
||||
theme_idx, self._saved_theme = self._themes.get_saved_theme()
|
||||
|
@ -455,22 +455,21 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
|
|||
if error != None:
|
||||
return error
|
||||
|
||||
if addr.startswith("unix:/"):
|
||||
savedAddr = self._cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
|
||||
if savedAddr != None and savedAddr != self.comboNodeAddress.currentText():
|
||||
Message.ok(
|
||||
QC.translate("preferences", "Ok"),
|
||||
QC.translate("preferences", "Restart the GUI in order changes to take effect"),
|
||||
QtWidgets.QMessageBox.Information)
|
||||
savedAddr = self._cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
|
||||
if savedAddr != None and savedAddr != 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._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))))
|
||||
self._set_status_error(QC.translate("preferences", "Exception saving node config {0}: {1}").format((addr, str(e))))
|
||||
return addr + ": " + str(e)
|
||||
|
||||
return None
|
||||
|
|
|
@ -36,26 +36,26 @@ class Nodes(QObject):
|
|||
def count(self):
|
||||
return len(self._nodes)
|
||||
|
||||
def add(self, peer, client_config=None):
|
||||
def add(self, _peer, client_config=None):
|
||||
try:
|
||||
proto, _addr = self.get_addr(peer)
|
||||
addr = "%s:%s" % (proto, _addr)
|
||||
if addr not in self._nodes:
|
||||
self._nodes[addr] = {
|
||||
proto, addr = self.get_addr(_peer)
|
||||
peer = proto+":"+addr
|
||||
if peer not in self._nodes:
|
||||
self._nodes[peer] = {
|
||||
'notifications': Queue(),
|
||||
'online': True,
|
||||
'last_seen': datetime.now()
|
||||
}
|
||||
else:
|
||||
self._nodes[addr]['last_seen'] = datetime.now()
|
||||
self._nodes[peer]['last_seen'] = datetime.now()
|
||||
|
||||
self._nodes[addr]['online'] = True
|
||||
self.add_data(addr, client_config)
|
||||
self.update(proto, _addr)
|
||||
self._nodes[peer]['online'] = True
|
||||
self.add_data(peer, client_config)
|
||||
self.update(peer)
|
||||
|
||||
self.nodesUpdated.emit(self.count())
|
||||
|
||||
return self._nodes[addr], addr
|
||||
return self._nodes[peer], peer
|
||||
|
||||
except Exception as e:
|
||||
print(self.LOG_TAG, "exception adding/updating node: ", e, "addr:", addr, "config:", client_config)
|
||||
|
@ -336,8 +336,9 @@ class Nodes(QObject):
|
|||
exit_noti = ui_pb2.Notification(clientName="", serverName="", type=0, data="", rules=[])
|
||||
self.send_notifications(exit_noti)
|
||||
|
||||
def update(self, proto, addr, status=ONLINE):
|
||||
def update(self, peer, status=ONLINE):
|
||||
try:
|
||||
proto, addr = self.get_addr(peer)
|
||||
self._db.update("nodes",
|
||||
"hostname=?,version=?,last_connection=?,status=?",
|
||||
(
|
||||
|
@ -345,29 +346,34 @@ class Nodes(QObject):
|
|||
self._nodes[proto+":"+addr]['data'].version,
|
||||
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
status,
|
||||
addr),
|
||||
"{0}:{1}".format(proto, addr)),
|
||||
"addr=?"
|
||||
)
|
||||
except Exception as e:
|
||||
print(self.LOG_TAG + " exception updating DB: ", e, addr)
|
||||
print(self.LOG_TAG + " exception updating DB: ", e, peer)
|
||||
|
||||
def update_all(self, status=OFFLINE):
|
||||
try:
|
||||
for peer in self._nodes:
|
||||
proto, addr = self.get_addr(peer)
|
||||
self._db.update("nodes",
|
||||
"hostname=?,version=?,last_connection=?,status=?",
|
||||
(
|
||||
self._nodes[proto+":"+addr]['data'].name,
|
||||
self._nodes[proto+":"+addr]['data'].version,
|
||||
self._nodes[peer]['data'].name,
|
||||
self._nodes[peer]['data'].version,
|
||||
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
status,
|
||||
addr),
|
||||
peer),
|
||||
"addr=?"
|
||||
)
|
||||
except Exception as e:
|
||||
print(self.LOG_TAG + " exception updating nodes: ", e)
|
||||
|
||||
def reset_status(self):
|
||||
try:
|
||||
self._db.update("nodes", "status=?", (self.OFFLINE,))
|
||||
except Exception as e:
|
||||
print(self.LOG_TAG + " exception resetting nodes status: ", e)
|
||||
|
||||
def reload_fw(self, addr, fw_config, callback):
|
||||
notif = ui_pb2.Notification(
|
||||
id=int(str(time.time()).replace(".", "")),
|
||||
|
|
|
@ -95,6 +95,7 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
|
|||
self._setup_slots()
|
||||
|
||||
self._nodes = Nodes.instance()
|
||||
self._nodes.reset_status()
|
||||
|
||||
self._last_stats = {}
|
||||
self._last_items = {
|
||||
|
@ -459,7 +460,7 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
|
|||
if addr in self._last_items[table]:
|
||||
del self._last_items[table][addr]
|
||||
|
||||
self._nodes.update(proto, addr, Nodes.OFFLINE)
|
||||
self._nodes.update(peer, Nodes.OFFLINE)
|
||||
self._nodes.delete(peer)
|
||||
self._stats_dialog.update(True, None, True)
|
||||
except Exception as e:
|
||||
|
|
Loading…
Add table
Reference in a new issue