Skip to content
Permalink
Browse files
Merge pull request #2 from aa9863/buggs
Buggs
  • Loading branch information
aa9863 committed Oct 19, 2020
2 parents b959755 + 264a026 commit 92942602456ae55461e8a7dcf23e8bc62240497f
Show file tree
Hide file tree
Showing 10 changed files with 721 additions and 0 deletions.

Large diffs are not rendered by default.

Empty file.
@@ -0,0 +1,54 @@
from Crypto.Cipher import AES

#Our Static Key
KEY = b"ThisIsAStaticKey"
IV = b"ThisIsAnStaticIV"

def encryptMessage(message, IV=None):
"""Encrypt a message using a static key"""

#Create a AES object We use CFB as we dont need to Pad the object input
cipher = AES.new(KEY, AES.MODE_CFB, iv=IV)

#Encode the Data
enciphered = cipher.encrypt(message)

#The IV is automagically Generated for us
IV = cipher.iv

#REturn them
return enciphered, IV



def decryptMessage(message, IV):
"""
Given a message and a IV
Decrypt the data using a static key
"""

cipher = AES.new(KEY, AES.MODE_CFB, iv=iv)

#Deode the Data
deciphered = cipher.decrypt(message)

return deciphered



if __name__ == "__main__":



message = b"Hello World"

#Use a Static IV
code, iv = encryptMessage(message, IV)
print("Cipher Text is {0}".format(code))
print("IV is {0}".format(iv))

#Then Decipher

decode = decryptMessage(code, iv)
print("Decoded Message {0}".format(decode))

@@ -0,0 +1,50 @@
"""
Driver Code for the Buggs Chat Client
This is the Code I expect you to modify
"""

import socket
import logging

class ChatClient:

def __init__(self, host, port=4242):
"""
Initialise the server to connect to a given host and port
"""
self.log = logging.getLogger("CLIENT")
self.log.debug("Initialise Client")
self.host = host
self.port = port


def sendMessage(self, message):
"""Helper Function to send a message
This is what we need to modify to send a message
"""
self.sock.sendall(message.encode())



"""
-------------------------------------------------
YOU DO NOT NEED TO MODIFY ANYTHING BELOW HERE
--------------------------------------------------
"""


def run(self):
#Main Loop
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as self.sock:
self.sock.connect((self.host, self.port))
while True:
data = input(">")
if data == "-1":
break
self.sendMessage(data)

self.sock.close()

@@ -0,0 +1,48 @@
"""
Main Driver Program for the Chat Client
"""

import threading

import logging
logging.basicConfig(level=logging.DEBUG) # CHANGE THIS FOR LOG LEVELS

logging.warning("YOU ARE NOT EXPECTED TO IMPORT THIS CODE")
logging.warning("Run as $python main.py")

#And the REst of our imports
import network
import serverDriver
import clientDriver

if __name__ == "__main__":
log = logging.getLogger("MAIN")

print("You need to specify an address to connect to")
print("for example:")
print(" - 127.0.0.1 (for your machine)")
print(" - 10.0.2.15 (for someone elses machine)")

ipAddr = input(">")


log.debug("Creating Server")

#Listen on EVERY port
server = network.ChatServer(("0.0.0.0", 4242), serverDriver.RequestHandler)

#And Register the server so we can send data back

with server:
#Start the Server Thread
serverThread = threading.Thread(target = server.serve_forever)
serverThread.deamon = True #Kill with the main process
serverThread.start() #And Get the Thread running

#And Now the Client
client = clientDriver.ChatClient(ipAddr)
client.run()

server.shutdown()


@@ -0,0 +1,45 @@
"""
This is the Core network functionality for the server.
- Creates a Request Handler to deal with data
- Creates a TCP Handler, that will push each request into a new thread
- A Simple Funciton that will send data back across the network
I dont really expect you to modify this.
"""

import socket
import socketserver
import logging

class ChatServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
"""
And this is the Server Itself.
Having it Threaded means (in theory) we could hanbdle connections from
multiple clients. However, we need to modify the client, to deal with addressing.
That is left as an Excersise for the reader.
"""



# And To Test
if __name__ == "__main__":

logging.basicConfig(level=logging.DEBUG)
#Start a Server
server = ChatServer(("127.0.0.1", 4242), RequestHandler)
#server = True
with server:
#Start the Server Thread
serverThread = threading.Thread(target = server.serve_forever)
serverThread.deamon = True #Kill with the main process
serverThread.start() #And Get the Thread running

#And Now the Client
client = ChatClient("127.0.0.1")
client.run()

server.shutdown()


@@ -0,0 +1,77 @@
"""
Driver Code for the Buggs Chat Client
This is the Code I expect you to modify
"""
import logging


import socketserver

import logging

class RequestHandler(socketserver.BaseRequestHandler):
"""
This is a class to handle requests. Made to the Chat Client
This class holds extra functionalty that is called
when we receive a message.
You can add your encryption logic here
"""

def __init__(self, server_address, RequestHandler, bind_and_activate=True):
self.log = logging.getLogger("SERVER")
socketserver.BaseRequestHandler.__init__(self,
server_address,
RequestHandler,
bind_and_activate)


def processData(self, data):
"""
And process the data
Add our Logic Here
"""
self.log.debug("Message {0}".format(data))











"""
-------------------------------------------------
YOU DO NOT NEED TO MODIFY ANYTHING BELOW HERE
--------------------------------------------------
"""

def handle(self):
"""
The Handle Function is called each time a socket connection in made
It remains until the connection is closed.
As we are threaded, It means that we can deal with multiple clients
(in theory, there is some "fun" handling different addresses)
This function will simply run untill the socket is closed.
"""
self.log.debug("Connection from {0}".format(self.client_address))
while True:
data = self.request.recv(1024)
#And Do whatever we want to do with it
self.processData(data)
#And Do Whatver Processing we want

#Check if our data is Empty (EOF) If so the connetion
#Is Closed and we end.
if data == b'':
print("EOF")
break

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9294260

Please sign in to comment.