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)
|
thm.load_theme(app)
|
||||||
|
|
||||||
Utils.create_socket_dirs()
|
Utils.create_socket_dirs()
|
||||||
if args.socket == "unix:///tmp/osui.sock":
|
|
||||||
cfg = Config.get()
|
cfg = Config.get()
|
||||||
addr = cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
|
addr = cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
|
||||||
if addr != None and addr != "" and addr.startswith("unix://"):
|
if addr != None and addr != "":
|
||||||
|
if addr.startswith("unix://"):
|
||||||
if not os.path.exists(os.path.dirname(addr[7:])):
|
if not os.path.exists(os.path.dirname(addr[7:])):
|
||||||
print("WARNING: unix socket path does not exist, using unix:///tmp/osui.sock, ", addr)
|
print("WARNING: unix socket path does not exist, using unix:///tmp/osui.sock, ", addr)
|
||||||
else:
|
else:
|
||||||
args.socket = addr
|
args.socket = addr
|
||||||
|
else:
|
||||||
|
args.socket = addr
|
||||||
|
|
||||||
print("Using server address:", args.socket)
|
print("Using server address:", args.socket)
|
||||||
|
|
||||||
|
|
|
@ -373,8 +373,10 @@ class Database:
|
||||||
|
|
||||||
return self._insert(qstr, columns)
|
return self._insert(qstr, columns)
|
||||||
|
|
||||||
def update(self, table, fields, values, condition, action_on_conflict="OR IGNORE"):
|
def update(self, table, fields, values, condition=None, action_on_conflict="OR IGNORE"):
|
||||||
qstr = "UPDATE " + action_on_conflict + " " + table + " SET " + fields + " WHERE " + condition
|
qstr = "UPDATE " + action_on_conflict + " " + table + " SET " + fields
|
||||||
|
if condition != None:
|
||||||
|
qstr += " WHERE " + condition
|
||||||
try:
|
try:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
q = QSqlQuery(qstr, self.db)
|
q = QSqlQuery(qstr, self.db)
|
||||||
|
@ -382,7 +384,7 @@ class Database:
|
||||||
for idx, v in enumerate(values):
|
for idx, v in enumerate(values):
|
||||||
q.bindValue(idx, v)
|
q.bindValue(idx, v)
|
||||||
if not q.exec_():
|
if not q.exec_():
|
||||||
print("update ERROR", qstr)
|
print("update ERROR:", qstr, "values:", values)
|
||||||
print(q.lastError().driverText())
|
print(q.lastError().driverText())
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -146,9 +146,9 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
|
||||||
self._node_needs_update = False
|
self._node_needs_update = False
|
||||||
|
|
||||||
def show_node_prefs(self, addr):
|
def show_node_prefs(self, addr):
|
||||||
self.comboNodeAddress.setCurrentText(addr)
|
|
||||||
self.tabWidget.setCurrentIndex(self.TAB_NODES)
|
|
||||||
self.show()
|
self.show()
|
||||||
|
self.comboNodes.setCurrentText(addr)
|
||||||
|
self.tabWidget.setCurrentIndex(self.TAB_NODES)
|
||||||
|
|
||||||
def _load_themes(self):
|
def _load_themes(self):
|
||||||
theme_idx, self._saved_theme = self._themes.get_saved_theme()
|
theme_idx, self._saved_theme = self._themes.get_saved_theme()
|
||||||
|
@ -455,7 +455,6 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
|
||||||
if error != None:
|
if error != None:
|
||||||
return error
|
return error
|
||||||
|
|
||||||
if addr.startswith("unix:/"):
|
|
||||||
savedAddr = self._cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
|
savedAddr = self._cfg.getSettings(Config.DEFAULT_SERVER_ADDR)
|
||||||
if savedAddr != None and savedAddr != self.comboNodeAddress.currentText():
|
if savedAddr != None and savedAddr != self.comboNodeAddress.currentText():
|
||||||
Message.ok(
|
Message.ok(
|
||||||
|
@ -470,7 +469,7 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
|
||||||
self._notifications_sent[nid] = notifObject
|
self._notifications_sent[nid] = notifObject
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(self.LOG_TAG + "exception saving node config on %s: " % addr, 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 addr + ": " + str(e)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -36,26 +36,26 @@ class Nodes(QObject):
|
||||||
def count(self):
|
def count(self):
|
||||||
return len(self._nodes)
|
return len(self._nodes)
|
||||||
|
|
||||||
def add(self, peer, client_config=None):
|
def add(self, _peer, client_config=None):
|
||||||
try:
|
try:
|
||||||
proto, _addr = self.get_addr(peer)
|
proto, addr = self.get_addr(_peer)
|
||||||
addr = "%s:%s" % (proto, _addr)
|
peer = proto+":"+addr
|
||||||
if addr not in self._nodes:
|
if peer not in self._nodes:
|
||||||
self._nodes[addr] = {
|
self._nodes[peer] = {
|
||||||
'notifications': Queue(),
|
'notifications': Queue(),
|
||||||
'online': True,
|
'online': True,
|
||||||
'last_seen': datetime.now()
|
'last_seen': datetime.now()
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
self._nodes[addr]['last_seen'] = datetime.now()
|
self._nodes[peer]['last_seen'] = datetime.now()
|
||||||
|
|
||||||
self._nodes[addr]['online'] = True
|
self._nodes[peer]['online'] = True
|
||||||
self.add_data(addr, client_config)
|
self.add_data(peer, client_config)
|
||||||
self.update(proto, _addr)
|
self.update(peer)
|
||||||
|
|
||||||
self.nodesUpdated.emit(self.count())
|
self.nodesUpdated.emit(self.count())
|
||||||
|
|
||||||
return self._nodes[addr], addr
|
return self._nodes[peer], peer
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(self.LOG_TAG, "exception adding/updating node: ", e, "addr:", addr, "config:", client_config)
|
print(self.LOG_TAG, "exception adding/updating node: ", e, "addr:", addr, "config:", client_config)
|
||||||
|
@ -336,24 +336,8 @@ class Nodes(QObject):
|
||||||
exit_noti = ui_pb2.Notification(clientName="", serverName="", type=0, data="", rules=[])
|
exit_noti = ui_pb2.Notification(clientName="", serverName="", type=0, data="", rules=[])
|
||||||
self.send_notifications(exit_noti)
|
self.send_notifications(exit_noti)
|
||||||
|
|
||||||
def update(self, proto, addr, status=ONLINE):
|
def update(self, peer, status=ONLINE):
|
||||||
try:
|
try:
|
||||||
self._db.update("nodes",
|
|
||||||
"hostname=?,version=?,last_connection=?,status=?",
|
|
||||||
(
|
|
||||||
self._nodes[proto+":"+addr]['data'].name,
|
|
||||||
self._nodes[proto+":"+addr]['data'].version,
|
|
||||||
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
||||||
status,
|
|
||||||
addr),
|
|
||||||
"addr=?"
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
print(self.LOG_TAG + " exception updating DB: ", e, addr)
|
|
||||||
|
|
||||||
def update_all(self, status=OFFLINE):
|
|
||||||
try:
|
|
||||||
for peer in self._nodes:
|
|
||||||
proto, addr = self.get_addr(peer)
|
proto, addr = self.get_addr(peer)
|
||||||
self._db.update("nodes",
|
self._db.update("nodes",
|
||||||
"hostname=?,version=?,last_connection=?,status=?",
|
"hostname=?,version=?,last_connection=?,status=?",
|
||||||
|
@ -362,12 +346,34 @@ class Nodes(QObject):
|
||||||
self._nodes[proto+":"+addr]['data'].version,
|
self._nodes[proto+":"+addr]['data'].version,
|
||||||
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
status,
|
status,
|
||||||
addr),
|
"{0}:{1}".format(proto, addr)),
|
||||||
|
"addr=?"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(self.LOG_TAG + " exception updating DB: ", e, peer)
|
||||||
|
|
||||||
|
def update_all(self, status=OFFLINE):
|
||||||
|
try:
|
||||||
|
for peer in self._nodes:
|
||||||
|
self._db.update("nodes",
|
||||||
|
"hostname=?,version=?,last_connection=?,status=?",
|
||||||
|
(
|
||||||
|
self._nodes[peer]['data'].name,
|
||||||
|
self._nodes[peer]['data'].version,
|
||||||
|
datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
|
status,
|
||||||
|
peer),
|
||||||
"addr=?"
|
"addr=?"
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(self.LOG_TAG + " exception updating nodes: ", 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):
|
def reload_fw(self, addr, fw_config, callback):
|
||||||
notif = ui_pb2.Notification(
|
notif = ui_pb2.Notification(
|
||||||
id=int(str(time.time()).replace(".", "")),
|
id=int(str(time.time()).replace(".", "")),
|
||||||
|
|
|
@ -95,6 +95,7 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
|
||||||
self._setup_slots()
|
self._setup_slots()
|
||||||
|
|
||||||
self._nodes = Nodes.instance()
|
self._nodes = Nodes.instance()
|
||||||
|
self._nodes.reset_status()
|
||||||
|
|
||||||
self._last_stats = {}
|
self._last_stats = {}
|
||||||
self._last_items = {
|
self._last_items = {
|
||||||
|
@ -459,7 +460,7 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
|
||||||
if addr in self._last_items[table]:
|
if addr in self._last_items[table]:
|
||||||
del self._last_items[table][addr]
|
del self._last_items[table][addr]
|
||||||
|
|
||||||
self._nodes.update(proto, addr, Nodes.OFFLINE)
|
self._nodes.update(peer, Nodes.OFFLINE)
|
||||||
self._nodes.delete(peer)
|
self._nodes.delete(peer)
|
||||||
self._stats_dialog.update(True, None, True)
|
self._stats_dialog.update(True, None, True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Add table
Reference in a new issue