Skip to content

Commit

Permalink
Move message parsing to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainL972 committed Feb 13, 2020
1 parent 01e9638 commit 7705d91
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
16 changes: 16 additions & 0 deletions backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import binascii
import gmpy2 as gmp
import urllib.request
import re


def createKey():
Expand Down Expand Up @@ -87,3 +88,18 @@ def getPublicIp():
response = urllib.request.urlopen(req)
data = response.read()
return data.decode("utf-8")


def parseMessage(message, key):
regex = re.search("^([a-z]) ([a-zA-Z0-9+/=]*)\n$", message)
if(regex):
command = regex.group(1)
arg = regex.group(2)
if command == "p":
return "pubkey", keyFromBase64(arg)
elif command == "m":
return "message", decryptText(key, arg)
else:
raise ValueError("Incorrect command : " + command)
else:
raise ValueError("Incorrect message : " + message)
32 changes: 9 additions & 23 deletions mainserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import socket
import select
import miniupnpc
import re

from threading import Thread

Expand Down Expand Up @@ -118,28 +117,15 @@ def run(self):
else:
self.message += read_data.decode()
if(self.message[-1] == "\n"):
regex = re.search(
"^([a-z]) ([a-zA-Z0-9+/=]*)\n$", self.message)
if(regex):
command = regex.group(1)
arg = regex.group(2)
if command == "p":
self.key = backend.keyFromBase64(arg)
print("[Thr {}] Received public key"
.format(self.number))
elif command == "m":
print("[Thr {}] Received message : {}"
.format(
self.number,
backend.decryptText(self.key, arg)
))
else:
raise ValueError("Incorrect command : "
+ repr(command))
self.message = ""
else:
raise ValueError("Incorrect message : "
+ self.message)
result = backend.parseMessage(
self.message, self.key)
if(result[0] == "pubkey"):
print("[Thr {}] Received public key."
.format(self.number))
elif(result[0] == "message"):
print("[Thr {}] Received message : {}"
.format(self.number, result[1]))
self.message = ""
else:
print("[Thr {}] No client is connected, SocketServer can't " +
"receive data".format(self.number))
Expand Down

0 comments on commit 7705d91

Please sign in to comment.