Merge pull request #2 from hornetfighter515/feature/py-cli

Python end user client base code.
This commit is contained in:
hornet 2021-11-11 17:41:31 +00:00 committed by GitHub
commit 16c2c6f8a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 148 additions and 1 deletions

1
.gitignore vendored Normal file → Executable file
View File

@ -2,6 +2,7 @@
__pycache__/ __pycache__/
*.py[cod] *.py[cod]
*$py.class *$py.class
config.yaml
# C extensions # C extensions
*.so *.so

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

42
src/client/cli.py Normal file
View File

@ -0,0 +1,42 @@
import websocket
import _thread
import time
import ssl
import yaml
def on_message(ws, message):
print(f"<< {message}")
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print(f"### closed. reason: {close_msg} ###")
def on_open(ws):
def run(*args):
running = True
while running:
outbound = input(">")
if outbound == 'q':
running = False
else:
ws.send(outbound)
ws.close()
_thread.start_new_thread(run, ())
def open_socket(url, port):
websocket.enableTrace(True)
ws = websocket.WebSocketApp(f"ws://{url}:{port}",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
if __name__ == "__main__":
with open('config.yaml', 'r') as f:
conf = yaml.safe_load(f)
open_socket(conf['ws']['url'], conf['ws']['port'])

43
src/client/test-cli.py Normal file
View File

@ -0,0 +1,43 @@
import websocket
import _thread
import ssl
import json
def on_message(ws, message):
print(f"<< {message}")
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print(f"### closed. reason: {close_msg} ###")
def on_open(ws):
def run(*args):
running = True
uname = input('input username:')
msg = {
"user":uname,
"content":None
}
ws.send(json.dumps(msg))
while running:
outbound = input(">")
if outbound == 'q':
running = False
else:
msg = {
"user":uname,
"content":outbound
}
ws.send(json.dumps(msg))
ws.close()
_thread.start_new_thread(run, ())
if __name__ == "__main__":
ws = websocket.WebSocketApp("wss://hornetfighter.com:9443",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

View File

@ -0,0 +1,3 @@
ws:
url: localhost
port: 6873

38
src/py-cli/encrypt_decrypt.py Executable file
View File

@ -0,0 +1,38 @@
"""
Encrypting and decrypting a message
https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-strings-in-python/
@author Todorov-Lachezar
"""
import rsa
class encrypt_decrypt():
# the message we want to encrypt
message = "Hello World"
def make_keys(key_length):
publicKey, privateKey = ""
# Generates public and private keys
# Method takes in key length as a parameter
# Note: the key length should be >16
if(key_length < 16):
publicKey, privateKey = rsa.newkeys(key_length)
return publicKey, privateKey
else:
return "Enter a key_length of >16"
def encryption(plaintext, publicKey):
# Encrypts the message with the public key
# Note: make sure to encode the message before encrypting
encMessage = rsa.encrypt(plaintext.encode(), publicKey)
return encMessage
def decryption(ciphertext, privateKey):
# Decrypts the encrypted message with the private key
# Note: make sure to decode the message after decryption
decMessage = rsa.decrypt(ciphertext, privateKey).decode()
return decMessage

22
src/py-cli/main.py Normal file → Executable file
View File

@ -1,5 +1,25 @@
""" """
A client to send an encrypted message to another client A client to send an encrypted message to another client
@author hornetfighter515 @author hornetfighter515, Lachezar Todorov
""" """
import encrypt_decrypt
import websock.alice
import websock.bob
import asyncio
def main():
message = "Hello World"
key_length = 512
#Bob's keys
publicKey, privateKey = encrypt_decrypt.encrypt_decrypt.make_keys(key_length)
asyncio.run(websock.alice.Alice.messageEncrypt("ws://localhost:8765", publicKey, message))
asyncio.run(websock.bob.Bob.messageDecrypt(privateKey))