diff options
Diffstat (limited to 'server.py')
-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 |