diff options
author | Jesusaves <cpntb1@ymail.com> | 2022-01-02 14:34:03 -0300 |
---|---|---|
committer | Jesusaves <cpntb1@ymail.com> | 2022-01-02 14:34:03 -0300 |
commit | af829f9ac358e1d81df569725712e611b8819c50 (patch) | |
tree | ee0ee6bfe06059b9d1cc9bf245670e39c8c4b7ea | |
parent | de81e40c934b622457113418c526b2a736f6961e (diff) | |
download | server-af829f9ac358e1d81df569725712e611b8819c50.tar.gz server-af829f9ac358e1d81df569725712e611b8819c50.tar.bz2 server-af829f9ac358e1d81df569725712e611b8819c50.tar.xz server-af829f9ac358e1d81df569725712e611b8819c50.zip |
Implement a short-lived packet cache when client request is the same as previous.
This allows the client to re-send packets safely in case of network issues,
without the server duplicating the action.
Note that server-initiated requests are never cached.
...Which are only AP or Broadcast, so makes no difference, I guess.
-rwxr-xr-x | server.py | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -18,7 +18,7 @@ ################################################################################# ## Global Modules -import threading, time, json, syslog +import threading, time, json, syslog, base64 LOG_AUTH=(syslog.LOG_NOTICE | syslog.LOG_AUTH) # Tweak as needed/desired @@ -53,22 +53,45 @@ class WebSocketConn(WebSocket): If the frame is Binary then self.data is a bytearray object. """ print("Message received from %s - %s" % (self.address[0], self.data)) + print("Cache Recv: %s" % self.cacher) + print("Cache Send: %s" % self.caches) + + # Check cache first + try: + if (base64.b64encode(bytearray(self.data, 'utf-8')) == self.cacher): + print("Replying from cache... (%s)" % self.caches) + self.send_message(self.caches) + if (self.caches == "NACK\n"): + security.score(self, 1) + return + else: + print("Not cached, continue") + except: + traceback.print_exc() + stdout("Cache lookup skipped (ERROR)") + + # Handle packet try: r=protocol.parse(self.data, self) stdout("Status: %s" % str(r[0]), 2) stdout("Reply: %s" % r[1], 2) if r[0] < PACKET_ACK: + self.caches = "NACK\n" stdout("%s - %s" % (self.address[0], r[1])) syslog.syslog(LOG_AUTH, "%s - %s" % (self.address[0], r[1])) self.send_message("NACK\n") security.score(self, security.get_score(r[0])) else: + self.caches = str(r[1]) self.send_message(str(r[1])) except: traceback.print_exc() self.send_message("ERROR\n") + self.caches = "ERROR\n" stdout("Message sent", 2) + self.cacher = base64.b64encode(bytearray(self.data, 'utf-8')) + #stdout(self.address[0] + u' - %s' % (self.data)) #self.send_message('ACK') @@ -107,6 +130,8 @@ class WebSocketConn(WebSocket): self.MS_auth = False self.token = "0" self.userid = 0 + self.caches = "INIT" + self.cacher = "INIT" def handle_close(self): global clients |