Reworked setup.py, now you can run cmd_chat directly. Reworked sanic http webserver to make it work. Update readme, etc...
This commit is contained in:
parent
c5fa982f65
commit
316a0e3e1e
21
README.MD
21
README.MD
|
|
@ -27,35 +27,20 @@ if __name__ == '__main__':
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Or (Windows)
|
### Or
|
||||||
|
|
||||||
Start server:
|
Start server:
|
||||||
|
|
||||||
```
|
```
|
||||||
.\cmd_chat.bat serve localhost 5000
|
cmd_chat serve localhost 5000
|
||||||
```
|
```
|
||||||
|
|
||||||
Connect to server:
|
Connect to server:
|
||||||
|
|
||||||
```
|
```
|
||||||
.\cmd_chat.bat connect localhost 5000 tyler
|
cmd_chat connect localhost 5000 tyler
|
||||||
```
|
```
|
||||||
|
|
||||||
### Or (Linux)
|
|
||||||
|
|
||||||
Start server:
|
|
||||||
|
|
||||||
```
|
|
||||||
python3 cmd_chat.py serve localhost 5000
|
|
||||||
```
|
|
||||||
|
|
||||||
Connect to server:
|
|
||||||
|
|
||||||
```
|
|
||||||
python3 cmd_chat.py connect localhost 5000 tyler
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
How does encryption work?
|
How does encryption work?
|
||||||
|
|
||||||
* The client generates a private key.
|
* The client generates a private key.
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
@echo off
|
|
||||||
python cmd_chat.py %*
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import cmd_chat
|
import cmd_chat
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
await cmd_chat.run()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(
|
asyncio.run(main())
|
||||||
cmd_chat.run()
|
|
||||||
)
|
|
||||||
|
|
@ -1,19 +1,15 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
from cmd_chat.server.server import app
|
from cmd_chat.server.server import run_server
|
||||||
from cmd_chat.client.client import Client
|
from cmd_chat.client.client import Client
|
||||||
|
|
||||||
|
|
||||||
async def run_server(
|
def run_http_server(
|
||||||
ip: str,
|
ip: str,
|
||||||
port: int
|
port: int
|
||||||
) -> None:
|
) -> None:
|
||||||
app.run(
|
run_server(ip, port, False)
|
||||||
host=ip,
|
|
||||||
port=port,
|
|
||||||
dev=False
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def run_client(
|
async def run_client(
|
||||||
|
|
@ -53,12 +49,16 @@ async def run() -> None:
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.command == 'serve':
|
if args.command == 'serve':
|
||||||
await run_server(args.ip_address, int(args.port))
|
run_http_server(args.ip_address, int(args.port))
|
||||||
elif args.command == 'connect':
|
elif args.command == 'connect':
|
||||||
if not args.username:
|
if not args.username:
|
||||||
parser.error("Username is required for 'connect' command")
|
parser.error("Username is required for 'connect' command")
|
||||||
await run_client(args.username, args.ip_address, int(args.port))
|
await run_client(args.username, args.ip_address, int(args.port))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def main():
|
||||||
asyncio.run(run())
|
asyncio.run(run())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -3,6 +3,10 @@ import asyncio
|
||||||
import rsa
|
import rsa
|
||||||
from cryptography.fernet import Fernet
|
from cryptography.fernet import Fernet
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
|
from sanic.worker.loader import AppLoader
|
||||||
|
|
||||||
from sanic.response import HTTPResponse
|
from sanic.response import HTTPResponse
|
||||||
from sanic import Sanic, Request, response, Websocket
|
from sanic import Sanic, Request, response, Websocket
|
||||||
|
|
||||||
|
|
@ -30,39 +34,54 @@ USERS: dict[str, str] = {}
|
||||||
PUBLIC_KEY = Fernet.generate_key()
|
PUBLIC_KEY = Fernet.generate_key()
|
||||||
|
|
||||||
|
|
||||||
@app.websocket("/talk")
|
def attach_endpoints(app: Sanic):
|
||||||
async def talk_ws_view(request: Request, ws: Websocket) -> HTTPResponse:
|
|
||||||
while True:
|
@app.websocket("/talk")
|
||||||
serialized_message: dict = await _get_bytes_and_serialize(ws)
|
async def talk_ws_view(request: Request, ws: Websocket) -> HTTPResponse:
|
||||||
await _check_ws_for_close_status(
|
while True:
|
||||||
serialized_message,
|
serialized_message: dict = await _get_bytes_and_serialize(ws)
|
||||||
ws
|
await _check_ws_for_close_status(
|
||||||
)
|
serialized_message,
|
||||||
new_message = await _generate_new_message(
|
ws
|
||||||
serialized_message.get("text")
|
)
|
||||||
)
|
new_message = await _generate_new_message(
|
||||||
MESSAGES_MEMORY_DB.append(new_message)
|
serialized_message.get("text")
|
||||||
await ws.send(
|
)
|
||||||
str({"status": "ok"})
|
MESSAGES_MEMORY_DB.append(new_message)
|
||||||
)
|
await ws.send(
|
||||||
await asyncio.sleep(0.2)
|
str({"status": "ok"})
|
||||||
|
)
|
||||||
|
await asyncio.sleep(0.2)
|
||||||
|
|
||||||
|
|
||||||
@app.websocket("/update")
|
@app.websocket("/update")
|
||||||
async def update_ws_view(request: Request, ws: Websocket) -> HTTPResponse:
|
async def update_ws_view(request: Request, ws: Websocket) -> HTTPResponse:
|
||||||
while True:
|
while True:
|
||||||
payload = await _generate_update_payload(
|
payload = await _generate_update_payload(
|
||||||
MESSAGES_MEMORY_DB,
|
MESSAGES_MEMORY_DB,
|
||||||
USERS
|
USERS
|
||||||
)
|
)
|
||||||
await ws.send(payload.encode())
|
await ws.send(payload.encode())
|
||||||
await asyncio.sleep(0.2)
|
await asyncio.sleep(0.2)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/get_key', methods=['GET', 'POST'])
|
@app.route('/get_key', methods=['GET', 'POST'])
|
||||||
async def get_key_view(request: Request) -> HTTPResponse:
|
async def get_key_view(request: Request) -> HTTPResponse:
|
||||||
public_key = rsa.PublicKey.load_pkcs1(request.form.get('pubkey'))
|
public_key = rsa.PublicKey.load_pkcs1(request.form.get('pubkey'))
|
||||||
encrypted_data = rsa.encrypt(PUBLIC_KEY, public_key)
|
encrypted_data = rsa.encrypt(PUBLIC_KEY, public_key)
|
||||||
if request.ip not in USERS:
|
if request.ip not in USERS:
|
||||||
USERS[f"{request.ip}, {request.form.get('username')}"] = PUBLIC_KEY
|
USERS[f"{request.ip}, {request.form.get('username')}"] = PUBLIC_KEY
|
||||||
return response.raw(encrypted_data)
|
return response.raw(encrypted_data)
|
||||||
|
|
||||||
|
|
||||||
|
def create_app(app_name: str) -> Sanic:
|
||||||
|
app = Sanic(app_name)
|
||||||
|
attach_endpoints(app)
|
||||||
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
def run_server(host: str, port: int, dev: bool=False) -> None:
|
||||||
|
loader = AppLoader(factory=partial(create_app, "CMD_SERVER"))
|
||||||
|
app = loader.load()
|
||||||
|
app.prepare(host=host, port=port, dev=dev)
|
||||||
|
Sanic.serve(primary=app, app_loader=loader)
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,4 @@ cryptography
|
||||||
colorama
|
colorama
|
||||||
pydantic
|
pydantic
|
||||||
websocket-client
|
websocket-client
|
||||||
|
flask
|
||||||
9
setup.py
9
setup.py
|
|
@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="secured_console_chat",
|
name="secured_console_chat",
|
||||||
version="1.1.1",
|
version="1.1.21",
|
||||||
author="dinosaurtirex",
|
author="dinosaurtirex",
|
||||||
author_email="sneakybeaky18@gmail.com",
|
author_email="sneakybeaky18@gmail.com",
|
||||||
packages=[
|
packages=[
|
||||||
|
|
@ -13,7 +13,7 @@ setuptools.setup(
|
||||||
"cmd_chat/client",
|
"cmd_chat/client",
|
||||||
"cmd_chat/client/core",
|
"cmd_chat/client/core",
|
||||||
"cmd_chat/client/core/abs",
|
"cmd_chat/client/core/abs",
|
||||||
"cmd_chat/server",
|
"cmd_chat/server"
|
||||||
],
|
],
|
||||||
description="Secured console chat with RSA & Fernet",
|
description="Secured console chat with RSA & Fernet",
|
||||||
long_description=description,
|
long_description=description,
|
||||||
|
|
@ -21,6 +21,11 @@ setuptools.setup(
|
||||||
url="https://github.com/dinosaurtirex/cmd-chat",
|
url="https://github.com/dinosaurtirex/cmd-chat",
|
||||||
license='MIT',
|
license='MIT',
|
||||||
python_requires='>=3.10',
|
python_requires='>=3.10',
|
||||||
|
entry_points={
|
||||||
|
'console_scripts': [
|
||||||
|
'cmd_chat = cmd_chat:main'
|
||||||
|
]
|
||||||
|
},
|
||||||
install_requires=[
|
install_requires=[
|
||||||
"sanic",
|
"sanic",
|
||||||
"requests",
|
"requests",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user