IL ME MANGE DEUX CARACT7RES
This commit is contained in:
parent
37145136bf
commit
8fe90ac427
@ -257,7 +257,8 @@ Initialisation des connections
|
||||
###### En-tête ######
|
||||
|
||||
EICP2P2 V1
|
||||
type: RSAget
|
||||
type: RSASend
|
||||
from: noeud/client
|
||||
|
||||
###### Contenu ######
|
||||
|
||||
@ -276,7 +277,6 @@ Cette requête est utilisé pour initialiser la communication crypté entre deux
|
||||
|
||||
EICP2P2 V1
|
||||
type: init
|
||||
from: noeud/client
|
||||
|
||||
###### Contenu ######
|
||||
|
||||
|
238
client/main.py
238
client/main.py
@ -6,16 +6,19 @@ import logging
|
||||
import logging.config
|
||||
import os
|
||||
import socket
|
||||
import threading
|
||||
|
||||
try:
|
||||
# noinspection PyUnresolvedReferences
|
||||
from Crypto.PublicKey import RSA as RSA
|
||||
# noinspection PyUnresolvedReferences
|
||||
from Crypto.Cipher import PKCS1_OAEP
|
||||
|
||||
pycryptodome = False
|
||||
except ModuleNotFoundError: # Pycryptodomex
|
||||
from Cryptodome.PublicKey import RSA as RSA
|
||||
from Cryptodome.Cipher import PKCS1_OAEP
|
||||
|
||||
pycryptodome = True
|
||||
|
||||
|
||||
@ -49,6 +52,15 @@ HOST = '127.0.0.1'
|
||||
PORT = 8888
|
||||
BUFFER_SIZE = 4096
|
||||
CHUNK_SIZE = int(BUFFER_SIZE / 8)
|
||||
BEGIN_MESSAGE = bytes("debut".ljust(BUFFER_SIZE, ";"), "ascii")
|
||||
END_MESSAGE = bytes("fin".ljust(BUFFER_SIZE, ";"), "ascii")
|
||||
NAME = b"client1"
|
||||
VERSION = b"EICP2P2 V1"
|
||||
|
||||
REQUEST_TYPE = [
|
||||
b'ping', b'pingACK', b'updateAsk', b'updateBack', b'transfer', b'register', b'registerACK', b'send', b'sendACK',
|
||||
b'exit', b'RSASend', b'init',
|
||||
]
|
||||
|
||||
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
clientSocket.connect((HOST, PORT))
|
||||
@ -56,67 +68,169 @@ clientSocket.connect((HOST, PORT))
|
||||
|
||||
# ET ICI ON MET LE CLIENT
|
||||
|
||||
def send(to_send):
|
||||
clientSocket.send(BEGIN_MESSAGE)
|
||||
i = 0
|
||||
for to_send_text in [to_send[i:i + BUFFER_SIZE - 2] for i in range(0, len(to_send), BUFFER_SIZE - 2)]:
|
||||
print((len(to_send_text)).to_bytes(2, byteorder='big'))
|
||||
print((len(to_send_text)).to_bytes(2, byteorder='big') + to_send_text.ljust(BUFFER_SIZE, b";"))
|
||||
clientSocket.send((len(to_send_text)).to_bytes(2, byteorder='big') + to_send_text.ljust(BUFFER_SIZE - 2, b";"))
|
||||
i += 1
|
||||
print(i)
|
||||
clientSocket.send(END_MESSAGE)
|
||||
class MainThread(threading.Thread):
|
||||
"""Main client class, for each identity."""
|
||||
|
||||
def __init__(self, name):
|
||||
threading.Thread.__init__(self)
|
||||
self.name = name
|
||||
self.rsa = None
|
||||
self.aes_key = None
|
||||
self.clientSocket = None
|
||||
|
||||
def run(self):
|
||||
rsa = self.RsaGenThread(self)
|
||||
rsa.start()
|
||||
rsa.join()
|
||||
print(type(self.rsa))
|
||||
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.clientSocket.connect((HOST, PORT))
|
||||
self.initialisation()
|
||||
|
||||
def initialisation(self):
|
||||
header = self.gen_header(b"RSASend", from_=b"client")
|
||||
content = self.rsa.publickey().exportKey()
|
||||
self.send(header + content)
|
||||
data = self.receive_rsa()
|
||||
header = self.extract_header(data[:BUFFER_SIZE])
|
||||
if header["type"] != b"init":
|
||||
debug("Incorrect request type, end of connection")
|
||||
return
|
||||
self.aes_key = data[BUFFER_SIZE:]
|
||||
debug("End of initialisation")
|
||||
|
||||
@staticmethod
|
||||
def extract_header(data):
|
||||
"""Extract header from data
|
||||
|
||||
:param data: Data to extract header
|
||||
:type data: bytes
|
||||
|
||||
:return: Dictionary with header datas
|
||||
:rtype: dict{bytes: bytes}"""
|
||||
if len(data) > BUFFER_SIZE:
|
||||
debug("Header too long")
|
||||
data = data[:BUFFER_SIZE]
|
||||
data_lines = data.split(b'\n')
|
||||
if data_lines[0] != VERSION:
|
||||
raise ValueError("Version is incorrect.")
|
||||
return {
|
||||
l.split(b": ")[0]: l.split(b": ")[1] for l in data_lines
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def gen_header(type_, to_=None, from_=None):
|
||||
"""Generate header
|
||||
|
||||
:param type_: Request type
|
||||
:param to_: `to` field in header, cf ../RFC8497.md
|
||||
:param from_: `from` field in header, cf ../RFC8497.md
|
||||
:type type_: bytes
|
||||
:type to_: bytes
|
||||
:type from_: bytes
|
||||
|
||||
:return: header
|
||||
:rtype: bytes"""
|
||||
if type_ not in REQUEST_TYPE:
|
||||
raise ValueError("Unknown request type")
|
||||
return
|
||||
header = VERSION + b"\ntype: " + type_
|
||||
if to_:
|
||||
header += b"\nto: " + to_
|
||||
if from_:
|
||||
header += b"\nfrom: " + from_
|
||||
return header.ljust(BUFFER_SIZE, b';')
|
||||
|
||||
class RsaGenThread(threading.Thread):
|
||||
|
||||
def __init__(self, client_, difficulty=4):
|
||||
threading.Thread.__init__(self)
|
||||
self.client = client_
|
||||
self.difficulty = difficulty
|
||||
|
||||
def run(self):
|
||||
if os.path.isfile("private.pem"):
|
||||
try:
|
||||
with open("private.pem", "rb") as keyfile:
|
||||
rsa = RSA.importKey(keyfile.read())
|
||||
if not rsa.has_private():
|
||||
warning("Le fichier clef ne contient pas de clef privée")
|
||||
raise ValueError
|
||||
self.client.rsa = rsa
|
||||
except (IndexError, ValueError):
|
||||
warning("Fichier clef corrompu")
|
||||
debug("Suppression du fichier clef corromu")
|
||||
os.remove("private.pem")
|
||||
if not os.path.isfile("private.pem"): # We're not using if/else because we may delete the file in the
|
||||
# previous if statement
|
||||
debug("Génération de la clef RSA pour %s" % self.client.name)
|
||||
self.client.rsa = RSA.generate(BUFFER_SIZE + 256 * self.difficulty)
|
||||
with open("private.pem", "wb") as keyfile:
|
||||
keyfile.write(self.client.rsa.exportKey())
|
||||
with open("public.pem", "wb") as keyfile:
|
||||
keyfile.write(self.client.rsa.publickey().exportKey())
|
||||
|
||||
################################################ COMMUNICATION WITH RSA ############################################
|
||||
|
||||
def receive_rsa(self, rsa_key=None):
|
||||
if rsa_key is None:
|
||||
rsa_key = self.rsa.exportKey()
|
||||
if rsa_key is None:
|
||||
info("RSA key not generated, connection failure.")
|
||||
return
|
||||
recipient_key = RSA.importKey(rsa_key)
|
||||
cipher_rsa = PKCS1_OAEP.new(recipient_key)
|
||||
raw = self.receive()
|
||||
content = b""
|
||||
for to_send_text in [raw[i:i + CHUNK_SIZE] for i in range(0, len(raw), CHUNK_SIZE)]:
|
||||
print(content)
|
||||
content += cipher_rsa.decrypt(to_send_text)
|
||||
return content
|
||||
|
||||
############################################ COMMUNICATION WITHOUT CRYPTING ########################################
|
||||
|
||||
def receive(self):
|
||||
"""Receive message from connection
|
||||
|
||||
:rtype: bytes
|
||||
:return: Message's content"""
|
||||
chunk = bytes("", "ascii") # Temp variable to store received datas
|
||||
while chunk != BEGIN_MESSAGE:
|
||||
chunk = self.clientSocket.recv(BUFFER_SIZE)
|
||||
content = b''
|
||||
while chunk != END_MESSAGE:
|
||||
chunk = self.clientSocket.recv(BUFFER_SIZE)
|
||||
# Get only interesting chucks
|
||||
if chunk != END_MESSAGE:
|
||||
# Get content part
|
||||
# int.from_bytes(chunk[:2], byteorder='big') == Get content size
|
||||
content += chunk[2:int.from_bytes(chunk[:2], byteorder='big')+2]
|
||||
debug(b"Received : " + content)
|
||||
return content
|
||||
|
||||
def send(self, to_send):
|
||||
"""Send message to connection
|
||||
|
||||
:param to_send: message to send
|
||||
:type to_send: bytes
|
||||
|
||||
:return: Nothing
|
||||
:rtype: NoneType"""
|
||||
debug(b"Send " + to_send)
|
||||
# Sending the message start
|
||||
self.clientSocket.send(BEGIN_MESSAGE)
|
||||
i = 0
|
||||
for to_send_text in [to_send[i:i + BUFFER_SIZE - 2] for i in range(0, len(to_send), BUFFER_SIZE - 2)]:
|
||||
self.clientSocket.send(
|
||||
(len(to_send_text)).to_bytes(2, byteorder='big') # Size of the message contained by the chunk
|
||||
+ to_send_text.ljust(BUFFER_SIZE - 2, bytes(1)) # Content of the chunk
|
||||
)
|
||||
i += 1
|
||||
# Sending the message stop
|
||||
self.clientSocket.send(END_MESSAGE)
|
||||
return None
|
||||
|
||||
|
||||
BEGIN_MESSAGE = bytes("debut", "ascii").ljust(BUFFER_SIZE, b";")
|
||||
END_MESSAGE = bytes("fin", "ascii").ljust(BUFFER_SIZE, b";")
|
||||
HEADER_TXT = """\
|
||||
EICP2P2 V1
|
||||
type: RSASend"""
|
||||
HEADER = bytes(HEADER_TXT, "ascii").ljust(BUFFER_SIZE, b";")
|
||||
|
||||
## création des clef RSA
|
||||
RSA_KEY_SIZE = CHUNK_SIZE * 2
|
||||
key = RSA.generate(BUFFER_SIZE * 2)
|
||||
private_key = key.export_key()
|
||||
file_out = open("private.pem", "wb")
|
||||
file_out.write(private_key)
|
||||
public_key = key.publickey().export_key()
|
||||
file_out = open("receiver.pem", "wb")
|
||||
file_out.write(public_key)
|
||||
to_send = public_key.ljust(BUFFER_SIZE, b';')
|
||||
|
||||
clientSocket.send(BEGIN_MESSAGE)
|
||||
clientSocket.send(HEADER)
|
||||
clientSocket.send(to_send)
|
||||
clientSocket.send(END_MESSAGE)
|
||||
|
||||
print('ok')
|
||||
chunk = bytes("", "ascii")
|
||||
while chunk != BEGIN_MESSAGE:
|
||||
chunk = clientSocket.recv(BUFFER_SIZE)
|
||||
last_chunk = chunk
|
||||
while last_chunk != END_MESSAGE:
|
||||
last_chunk = clientSocket.recv(BUFFER_SIZE)
|
||||
chunk += last_chunk
|
||||
|
||||
print(len(chunk[BUFFER_SIZE:-BUFFER_SIZE]))
|
||||
chunk = chunk[BUFFER_SIZE:-BUFFER_SIZE]
|
||||
|
||||
# HEADER+AES key are encypted in same time, so decrypt time at same time
|
||||
cipher_rsa = PKCS1_OAEP.new(key)
|
||||
texte_to_decrypt = b''
|
||||
|
||||
for ligne in [chunk[i:i + BUFFER_SIZE] for i in range(0, len(chunk), BUFFER_SIZE)]:
|
||||
to_decrypt = ligne[2:]
|
||||
to_decrypt = to_decrypt[:int.from_bytes(ligne[:2], byteorder='big')]
|
||||
texte_to_decrypt += to_decrypt
|
||||
print(len(texte_to_decrypt))
|
||||
texte = b''
|
||||
for to_decrypt in [texte_to_decrypt[i:i + RSA_KEY_SIZE] for i in range(0, len(texte_to_decrypt), RSA_KEY_SIZE)]:
|
||||
print(len(to_decrypt))
|
||||
print(to_decrypt)
|
||||
print(texte)
|
||||
print(len(texte))
|
||||
texte += cipher_rsa.decrypt(to_decrypt)
|
||||
print(texte)
|
||||
if __name__ == "__main__":
|
||||
client = MainThread(NAME)
|
||||
client.start()
|
||||
|
@ -1,99 +1,63 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIISJwIBAAKCBAEAj00qbP8CXp1dbqXG4vV+chzJx7HvkuTWqB3hQo712LjtseJ/
|
||||
i3KltwWHB+QAZ1GyswmJNW2KVDyyIq74d6K1bxy+EoSiXMctZdK3l5xL0JQwCnka
|
||||
poryAuxwMkqu9scpcZ7rMIsphdvk6V4OmJdLF/LLHLRsiMKGiaYmmzB5nyQDkiQ0
|
||||
+Pxmf+baMQ6YT713O9z2aunKdrMl8sQpbms+QUOmj6VLrJ9+ttcpRoTx0LxW0yl3
|
||||
bxKM7LrMnHazOEwLR2pnTlHB5NanmDyts1XU6CzxFuCA8o3MLjpOIEw7IrllcDvF
|
||||
nZrh9SrtzKaCJIXhIT5yS4q/hZ7w9BWZBKzmmiudGmNYeyJUeVfnqk7AnofE4Rb9
|
||||
5jnUWQkl3m8h1CkLRS7p0DVTvVqyeEPWXwgvzGhLZP9xBDlQJMuNoRS8xqSy/7KW
|
||||
dunUt5oWPB/UwAsDLerSWp18Yi1g4q1d8o1S2Q8t5kqOE6U985jyz9YPtCcFYQUD
|
||||
uyXWaFTTeGEfcY04pK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HVS
|
||||
zHKCTV572Z5KjEasUylPNVTudtS9s/V1O1wqaUFQUysKbH8SxnLH+rNgKL40pTFT
|
||||
CMZLbGwkNT9YRBZD8tv8LcyrYU5IsykaiSFbtsiwQw2v66A/23LwisiqQVmdKtJ7
|
||||
oVTKgVjTED0v/A4T4quYuBtAw3jxEbdVnn8ZswQZToCke396PB6MXCBhKB9OeJT3
|
||||
UbHawiSOz1DbU1oT4gliiazNbNgmqpXh6gn7wIs2Qc+Zc0xjGWJLS5cJRHpIWT+l
|
||||
aLynSiAOZ11hFvvINk49u1BqcjcH/0DmaqW/ekxtg/uD4QaypqSur3Ohe3Upy8Er
|
||||
lSCxmjKecSECggFBAJ1LONyTcVEwIJmklQur+V3yOz2csIYwl2s1BsG8eSSwfUcO
|
||||
YaHYNdDv/bkAqsoKVybBtIvdswHoPlx6VufaV7AvMpTGrxk+n11XTpOotOs0GDTv
|
||||
tEv46EC8aWxYxm+wheqUHZxEvjC/9qpVRj7aQFw/pZFK+xA1oEH8vlkxa3nzibqc
|
||||
8UrHIsCaRMEg9uebatCluDyiXXEfsSlcbVgGjwf/3+yrjIsuRtp2AawTjBWfaoxO
|
||||
HxqioFkMhyhExiLIuaLe7+CAYRGsC5Xqg1gkVUSaO5AqqnpvyZ9mC22XNeEgq8gR
|
||||
OA8kmmCurG2Yu98uqhgzLa+hmBQ+XWVmsxnMj3HYThMDos4ZP4LEi+8HeM3eLFDX
|
||||
gpSLdVqiuU3qIKCoKAgxeeaeQiuI0Gyh6E1MvHV5fwDMd/Mok2Bs60hCnktu
|
||||
-----END RSA PRIVATE KEY-----
|
17
client/public.pem
Normal file
17
client/public.pem
Normal file
@ -0,0 +1,17 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIICojANBgkqhkiG9w0BAQEFAAOCAo8AMIICigKCAoEAmoi9rbZ6kiCZMaLeX46S
|
||||
KQ+hN8kkt/d6VV6pH7n8Mw9Di4xc2dvosVGwBVvsDzYC4sS5+orU76ps0uvELZgQ
|
||||
Q8++2bfXXeRYWeucrZSA850vfokVKDTjkNVCJoOUGBeNgVxh2I8Mzn8logyFvbP6
|
||||
TQKRz+lY+ah7uKA2bp3PTr7uNrB4hhFvfwVBOMu9ismziBOy6Jjj4nHzhpWjPug7
|
||||
cWhiae2jPdacGCmqRog1cAspiyZ7Lkg+0pUrJWMeRHtCfhA8KHwYPQj423rO7Dz6
|
||||
lAEN/yqJeU34dva+rqWFPsrrXpO5FyJwa1VWqLZQvI3KjMmGd4d2Uxp4tIkJyEwE
|
||||
v7IiKKc7xcl4hoesQr4dQiBVD7434zhzvk4N1BPwq7dY0ryYYyv0UhAw3oJyk3GE
|
||||
agCejts0cej/MgugiR4qHkes59ZBd8KN96xI9J4iCSfFoOL8GqgsSYkD0rAn+JOY
|
||||
advvdhD+p2lok0PqaOZm702MbUO+d0NEZa18z0YrEM2ttig327g0X7o8P/zwrh0i
|
||||
5BCVtD6qeO2RFub0sDOYvqmDP2ueuX5lWq5oERi33Yxm7cEfZMD6i8eD03QIIWIU
|
||||
4R4drs9VmPoqsSXPLxn0o1W7qo27rIUrfgFylmQdWPef/rsEP0RVfaLoDNL1e/Fg
|
||||
RNi1eeueNlFoFgob/9GNekLhjQneTnvPntbFKD0fgYcmskW48J1eCego9CXKzfH+
|
||||
QMlDAknkq16ykmvyiNMisAGgxif9vnACkT4l3srHpv7PRDBR7YRS/4Yeu64FH9Oy
|
||||
QkX4eE9MCY8GdNfYE4Q0GQzDtmCpfAWO0oXOoFLA0CZZD4pqTKB7XnmUpjd+Wbfs
|
||||
mwIDAQAB
|
||||
-----END PUBLIC KEY-----
|
@ -1,25 +0,0 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIEIjANBgkqhkiG9w0BAQEFAAOCBA8AMIIECgKCBAEAj00qbP8CXp1dbqXG4vV+
|
||||
chzJx7HvkuTWqB3hQo712LjtseJ/i3KltwWHB+QAZ1GyswmJNW2KVDyyIq74d6K1
|
||||
bxy+EoSiXMctZdK3l5x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JMubfXHQ36dgNOEEFiekF9Do
|
||||
X0l8i8Ev6RdhrHJtJLDVE391yxx1hgW/JzB5hmcc8ryhtx7HsBt5vtrxE0JM1/9P
|
||||
xmOqqG9gXNY02ErkTK4TrJGkAx0z4ZGKh9BSypYtTxPWq5cDuT9q7gZTOnHZ+HtS
|
||||
oWnGRKwwLlLmyo8ACcVXzCbj/ZfqVcWZkVDM2rlCF9O7xkeAQNJUKvuLMVm4Zlqz
|
||||
M14TKzIOUtquanyx6zbajRNROE95Q8QAZw8I3P0pYY3+qX/7D0HJSGSN6cuNsngK
|
||||
AQIDAQAB
|
||||
-----END PUBLIC KEY-----
|
105
server/main.py
105
server/main.py
@ -58,11 +58,20 @@ CHUNK_SIZE = int(BUFFER_SIZE / 8)
|
||||
BEGIN_MESSAGE = bytes("debut".ljust(BUFFER_SIZE, ";"), "ascii")
|
||||
END_MESSAGE = bytes("fin".ljust(BUFFER_SIZE, ";"), "ascii")
|
||||
|
||||
VERSION = "EICP2P2 V1"
|
||||
VERSION = b"EICP2P2 V1"
|
||||
|
||||
REQUEST_TYPE = [
|
||||
b'ping', b'pingACK', b'updateAsk', b'updateBack', b'transfer', b'register', b'registerACK', b'send', b'sendACK',
|
||||
b'exit', b'RSASend', b'init',
|
||||
]
|
||||
|
||||
DONE = 0
|
||||
ERROR = 1
|
||||
|
||||
T_NONE = 0b0000000000
|
||||
T_NODE = 0b0000000001
|
||||
T_CLIENT = 0b00000010
|
||||
|
||||
#### Socket ####
|
||||
main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
main_socket.bind((HOST, PORT))
|
||||
@ -93,6 +102,7 @@ class ClientThread(threading.Thread):
|
||||
self.status = None
|
||||
self.rsa_client = None
|
||||
self.aes_key = get_random_bytes(64)
|
||||
self.type = T_NONE
|
||||
debug("Creation du thread pour %s reussie" % ip_client)
|
||||
|
||||
def initialize(self):
|
||||
@ -102,37 +112,73 @@ class ClientThread(threading.Thread):
|
||||
:return: Nothing"""
|
||||
# Receive message
|
||||
message = self.receive()
|
||||
# Get informations
|
||||
header = message[:BUFFER_SIZE]
|
||||
header = self.extract_header(message)
|
||||
content = message[BUFFER_SIZE:]
|
||||
# Extract header data
|
||||
header_txt = header.decode("ascii").rstrip(";")
|
||||
version = header_txt.split("\n")[0]
|
||||
# Extraction des champs du header
|
||||
if version != VERSION:
|
||||
debug("Incorrect protocol version.")
|
||||
self.send(b"Error")
|
||||
# Extract information from header
|
||||
champs = {l.split(": ")[0]: l.split(": ")[1] for l in header_txt.split('\n')[1:]}
|
||||
|
||||
if champs.get("type", None) is None:
|
||||
if header.get(b"type", None) is None:
|
||||
debug("The type field is not in the header")
|
||||
self.send(b"Error")
|
||||
return
|
||||
|
||||
if self.status is None and champs["type"] != "RSASend":
|
||||
debug("Requête différente de RASSend avec une connection non initialisée")
|
||||
self.send(b"Error".ljust(BUFFER_SIZE, b";"))
|
||||
if header.get(b"from", None) is None:
|
||||
debug("The from field is not in the header")
|
||||
self.send(b"error")
|
||||
return
|
||||
|
||||
if champs["type"] == "RSASend":
|
||||
if self.status is None and header[b"type"] != b"RSASend":
|
||||
debug("Requête différente de RSASend avec une connection non initialisée")
|
||||
self.send(b"Error")
|
||||
return
|
||||
if header[b"type"] == b"RSASend":
|
||||
self.type = T_CLIENT if header[b"from"] == b"client" else T_NODE
|
||||
debug("Réception de la clef RSA de %s", self.ip)
|
||||
self.rsa_client = content.rstrip(b";").rstrip(b' ')
|
||||
header = bytes((VERSION + "\ntype: init").ljust(BUFFER_SIZE, ";"), 'ascii')
|
||||
content = self.aes_key.ljust(BUFFER_SIZE, b";")
|
||||
print(content)
|
||||
self.rsa_client = content
|
||||
header = self.gen_header(b"init")
|
||||
content = self.aes_key
|
||||
self.send_rsa(header + content)
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
def extract_header(data):
|
||||
"""Extract header from data
|
||||
|
||||
:param data: Data to extract header
|
||||
:type data: bytes
|
||||
|
||||
:return: Dictionary with header datas
|
||||
:rtype: dict{bytes: bytes}"""
|
||||
if len(data) > BUFFER_SIZE:
|
||||
debug("Header too long")
|
||||
data = data[:BUFFER_SIZE]
|
||||
data_lines = data.split(b'\n')
|
||||
if data_lines[0] != VERSION:
|
||||
raise ValueError("Version is incorrect.")
|
||||
return {
|
||||
l.split(b": ")[0]: l.split(b": ")[1] for l in data_lines[1:]
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def gen_header(type_, to_=None, from_=None):
|
||||
"""Generate header
|
||||
|
||||
:param type_: Request type
|
||||
:param to_: `to` field in header, cf ../RFC8497.md
|
||||
:param from_: `from` field in header, cf ../RFC8497.md
|
||||
:type type_: bytes
|
||||
:type to_: bytes
|
||||
:type from_: bytes
|
||||
|
||||
:raise ValueError: `type_` is not a valid request type
|
||||
|
||||
:return: header
|
||||
:rtype: bytes"""
|
||||
if type_ not in REQUEST_TYPE:
|
||||
raise ValueError("Unknown request type")
|
||||
header = VERSION + b"\ntype: " + type_
|
||||
if to_:
|
||||
header += b"\nto: " + to_
|
||||
if from_:
|
||||
header += b"\nfrom: " + from_,
|
||||
return header.ljust(BUFFER_SIZE, b';')
|
||||
|
||||
################################################ COMMUNICATION WITH RSA ################################################
|
||||
|
||||
def send_rsa(self, to_send, key=None):
|
||||
@ -150,18 +196,15 @@ class ClientThread(threading.Thread):
|
||||
key = self.rsa_client
|
||||
if key is None:
|
||||
info("RSA key not received, connection failure.")
|
||||
self.client.send(b"Error".ljust(BUFFER_SIZE, bytes(1)))
|
||||
self.client.send(b"Error")
|
||||
return
|
||||
# Get RSA key
|
||||
print(len(to_send))
|
||||
recipient_key = RSA.importKey(key)
|
||||
# RSA encryption object
|
||||
cipher_rsa = PKCS1_OAEP.new(recipient_key)
|
||||
encrypted = b""
|
||||
for to_send_text in [to_send[i:i + CHUNK_SIZE] for i in range(0, len(to_send), CHUNK_SIZE)]:
|
||||
print(len(to_send_text))
|
||||
encrypted += cipher_rsa.encrypt(to_send_text)
|
||||
print(len(encrypted))
|
||||
self.send(encrypted)
|
||||
return None
|
||||
|
||||
@ -182,8 +225,10 @@ class ClientThread(threading.Thread):
|
||||
if chunk != END_MESSAGE:
|
||||
# Get content part
|
||||
# int.from_bytes(chunk[:2], byteorder='big') == Get content size
|
||||
content += chunk[2:int.from_bytes(chunk[:2], byteorder='big')]
|
||||
debug(b"Received from" + bytes(str(self.ip), ascii) + b" : " + content)
|
||||
content += chunk[2:int.from_bytes(chunk[:2], byteorder='big')] #+2] # Avec le +2 il me mange plus les caractères mais je sais pas pourquoi @suwako
|
||||
print(chunk[2:int.from_bytes(chunk[:2], byteorder='big')])
|
||||
print(content[:BUFFER_SIZE])
|
||||
debug(b"Received from" + bytes(str(self.ip), 'ascii') + b" : " + content)
|
||||
return content
|
||||
|
||||
def send(self, to_send):
|
||||
@ -198,7 +243,6 @@ class ClientThread(threading.Thread):
|
||||
# Sending the message start
|
||||
self.client.send(BEGIN_MESSAGE)
|
||||
i = 0
|
||||
print(len(to_send))
|
||||
for to_send_text in [to_send[i:i + BUFFER_SIZE - 2] for i in range(0, len(to_send), BUFFER_SIZE - 2)]:
|
||||
self.client.send(
|
||||
(len(to_send_text)).to_bytes(2, byteorder='big') # Size of the message contained by the chunk
|
||||
@ -217,7 +261,6 @@ class ClientThread(threading.Thread):
|
||||
info(self.ip + "connected, initialize connection...")
|
||||
self.initialize()
|
||||
info(self.ip + "connection initialized.")
|
||||
print("done")
|
||||
self.client.close()
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user