From 2465ef25b741dfc39df343e550e88fd0b61a6460 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado" Date: Thu, 25 Nov 2021 15:27:44 +0100 Subject: #1065 Add event datagram socket ogClient can receive events via a datagram socket opened at 55885. This socket is only opened when in windows or linux mode, for event reporting from within the system. Events reported this way are sent back to ogServer via a 103 Early Hints HTTP message. Information regarding the event is sent in the response's payload. --- src/ogClient.py | 30 +++++++++++++++++++++++++++++- src/ogRest.py | 3 +++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ogClient.py b/src/ogClient.py index 5fff77c..45f7007 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -35,6 +35,10 @@ class ogClient: self.mode = self.CONFIG['opengnsys']['mode'] if self.mode not in {'virtual', 'live', 'linux', 'windows'}: raise ValueError('Mode not supported.') + if self.mode in {'linux', 'windows'}: + self.event_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.event_sock.setblocking(0) + self.event_sock.bind(('127.0.0.1', 55885)) if self.CONFIG['samba']['activate']: assert('user' in self.CONFIG['samba']) @@ -47,9 +51,29 @@ class ogClient: def get_socket(self): return self.sock + def get_event_socket(self): + return self.event_sock + def get_state(self): return self.state + def send_event_hint(self, message): + try: + event, action, user = message.split(" ") + logging.warning("%s, %s, %s", event, action, user) + except: + logging.warning("Error parsing session datagram") + return + + if (event != "session" or + action not in ['start', 'stop'] or + not user): + logging.warning("Invalid value in session datagram: %s", message) + + payload = jsonBody({'event': event, 'action': action, 'user': user}) + response = restResponse(ogResponses.EARLY_HINTS, payload) + self.send(response.get()) + def cleanup(self): self.data = "" self.content_len = 0 @@ -138,6 +162,7 @@ class ogClient: def run(self): while 1: sock = self.get_socket() + event_sock = self.get_event_socket() state = self.get_state() if state == State.CONNECTING: @@ -147,7 +172,7 @@ class ogClient: elif state == State.FORCE_DISCONNECTED: return 0 else: - readset = [ sock ] + readset = [ sock, event_sock ] if event_sock else [ sock ] writeset = [ ] exceptset = [ ] @@ -158,5 +183,8 @@ class ogClient: self.receive() elif state == State.CONNECTING and sock in exception: self.connect2() + elif state == State.RECEIVING and event_sock in readable: + message = event_sock.recv(4096).decode('utf-8').rstrip() + self.send_event_hint(message) else: print('wrong state, not ever happen!' + str(state)) diff --git a/src/ogRest.py b/src/ogRest.py index db6a60e..0207af0 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -54,6 +54,8 @@ class restResponse(): self.msg = 'HTTP/1.0 401 Unauthorized' elif response == ogResponses.SERVICE_UNAVAILABLE: self.msg = 'HTTP/1.0 503 Service Unavailable' + elif response == ogResponses.EARLY_HINTS: + self.msg = 'HTTP/1.0 103 Early Hints' else: return self.msg @@ -244,6 +246,7 @@ class ogResponses(Enum): INTERNAL_ERR=3 UNAUTHORIZED=4 SERVICE_UNAVAILABLE=5 + EARLY_HINTS=6 class ogRest(): LOG_LENGTH = 32 -- cgit v1.2.3-18-g5258