Skip to content

Commit e9f6468

Browse files
ui: improved remote nodes status management
- Close notifications channel in more cases. - misc: use the word "proto" only for protocol, instead of protobuffer. Related: #1453.
1 parent 8270be1 commit e9f6468

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

ui/opensnitch/nodes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,14 @@ def reply_notification(self, addr, reply):
364364
except Exception as e:
365365
print(self.LOG_TAG, "notification exception:", e)
366366

367-
def stop_notifications(self):
367+
def stop_notifications(self, addr=None):
368368
"""Send a dummy notification to force Notifications class to exit.
369369
"""
370-
exit_noti = ui_pb2.Notification(clientName="", serverName="", type=0, data="", rules=[])
371-
self.send_notifications(exit_noti)
370+
exit_ntf = ui_pb2.Notification(clientName="", serverName="", type=-1, data="", rules=[])
371+
if addr is not None:
372+
self.send_notification(addr, exit_ntf)
373+
return
374+
self.send_notifications(exit_ntf)
372375

373376
def update(self, peer, status=ONLINE):
374377
try:

ui/opensnitch/service.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
path = os.path.abspath(os.path.dirname(__file__))
1313
sys.path.append(path)
1414

15-
import opensnitch.proto as proto
16-
ui_pb2, ui_pb2_grpc = proto.import_()
15+
import opensnitch.proto as pb2
16+
ui_pb2, ui_pb2_grpc = pb2.import_()
1717

1818
from opensnitch.dialogs.prompt import PromptDialog
1919
from opensnitch.dialogs.stats import StatsDialog
@@ -820,8 +820,7 @@ def Ping(self, request, context):
820820
# self._remote_stats[addr]['dialog'].update(addr, request.stats)
821821

822822
except Exception as e:
823-
print("Ping exception: ", e)
824-
823+
print("Ping exception", context.peer(), ":", e)
825824
return ui_pb2.PingReply(id=request.id)
826825

827826
def AskRule(self, request, context):
@@ -882,6 +881,7 @@ def Subscribe(self, node_config, context):
882881
883882
@doc: https://grpc.github.io/grpc/python/grpc.html#service-side-context
884883
"""
884+
print(datetime.now(), "[Subscribe]", context.peer())
885885
# if the exit mark is set, don't accept new connections.
886886
# db vacuum operation may take a lot of time to complete.
887887
if self._exit:
@@ -921,17 +921,26 @@ def Notifications(self, node_iter, context):
921921
@doc: https://grpc.github.io/grpc/python/grpc.html#service-side-context
922922
@doc: https://grpc.io/docs/what-is-grpc/core-concepts/
923923
"""
924+
local_peer = context.peer()
925+
926+
print(datetime.now(), "[Notifications] channel started:", context.peer())
927+
print()
924928
proto, addr = self._get_peer(context.peer())
925-
_node = self._nodes.get_node("%s:%s" % (proto, addr))
929+
node_addr = f"{proto}:{addr}"
930+
_node = self._nodes.get_node(node_addr)
926931
if _node == None:
927932
return
928933

929934
stop_event = Event()
930935
def _on_client_closed():
931936
stop_event.set()
937+
print(datetime.now(), "[Notifications] client closed", context.peer())
938+
self._nodes.stop_notifications(node_addr)
939+
940+
932941
self._node_actions_trigger.emit(
933942
{'action': self.NODE_DELETE,
934-
'peer': context.peer(),
943+
'peer': local_peer,
935944
})
936945

937946
self._status_change_trigger.emit(False)
@@ -944,12 +953,14 @@ def _on_client_closed():
944953
if self._is_local_request(proto, addr) == False:
945954
self._show_message_trigger.emit(
946955
"node exited",
947-
"({0})".format(context.peer()),
956+
f"({local_peer})",
948957
QtWidgets.QSystemTrayIcon.MessageIcon.Information,
949958
DesktopNotifications.URGENCY_LOW
950959
)
951960

952-
context.add_callback(_on_client_closed)
961+
added = context.add_callback(_on_client_closed)
962+
if added is False:
963+
print("[Notifications] add_callback() not added")
953964

954965
# TODO: move to notifications.py
955966
def new_node_message():
@@ -969,9 +980,11 @@ def new_node_message():
969980
print("[Notifications] Node {0} exited".format(addr))
970981
break
971982
except grpc.RpcError as e:
972-
print("[Notifications] grpc exception new_node_message(): ", addr, in_message)
983+
print(datetime.now(), "[Notifications] grpc exception new_node_message(): ", addr, e)
984+
break
973985
except Exception as e:
974-
print("[Notifications] unexpected exception new_node_message(): ", addr, e, in_message)
986+
print("[Notifications] unexpected exception new_node_message(): ", addr, e)
987+
break
975988

976989
read_thread = Thread(target=new_node_message)
977990
read_thread.daemon = True
@@ -983,11 +996,18 @@ def new_node_message():
983996

984997
try:
985998
noti = _node['notifications'].get()
986-
if noti != None:
987-
_node['notifications'].task_done()
988-
yield noti
999+
if noti is not None:
1000+
if noti.type > 0:
1001+
_node['notifications'].task_done()
1002+
yield noti
1003+
elif noti.type == -1:
1004+
print("[Notifications] notify exit, break the loop")
1005+
break
9891006
except Exception as e:
9901007
print("[Notifications] exception getting notification from queue:", addr, e)
991-
context.cancel()
1008+
break
9921009

1010+
# force call to _on_client_closed
1011+
context.cancel()
1012+
print("[Notifications] channel closed:", local_peer)
9931013
return node_iter

0 commit comments

Comments
 (0)