-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_client.py
More file actions
executable file
·101 lines (83 loc) · 3.47 KB
/
simple_client.py
File metadata and controls
executable file
·101 lines (83 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
"""
Simple CLI chat client (no web UI needed).
"""
import asyncio
import websockets
import json
import sys
async def receive_messages(ws):
"""Receive and display messages."""
try:
async for message in ws:
data = json.loads(message)
if data['type'] == 'welcome':
print(f"\n✅ {data['message']}")
if data.get('users'):
print(f"👥 Users online: {', '.join(data['users'])}\n")
elif data['type'] == 'user_joined':
print(f"\n[+] {data['username']} joined")
elif data['type'] == 'user_left':
print(f"\n[-] {data['username']} left")
elif data['type'] == 'message':
mode_icon = {"hybrid": "🛡️", "pq": "🔐", "classical": "🔑"}.get(data.get('encryption_mode'), "🔒")
print(f"\n{mode_icon} [{data['from']}]: {data['message']}")
print("> ", end="", flush=True)
except websockets.exceptions.ConnectionClosed:
print("\n\n❌ Connection closed")
async def send_messages(ws, username, mode):
"""Send messages from user input."""
loop = asyncio.get_event_loop()
while True:
try:
message = await loop.run_in_executor(None, input, "> ")
if message.strip():
if message.strip().lower() == '/quit':
break
await ws.send(json.dumps({
'type': 'message',
'message': message,
'encryption_mode': mode
}))
except EOFError:
break
async def main():
if len(sys.argv) < 3:
print("Usage: python3 simple_client.py <server-ip> <username> [mode]")
print("Example: python3 simple_client.py localhost Alice hybrid")
sys.exit(1)
server = sys.argv[1]
username = sys.argv[2]
mode = sys.argv[3] if len(sys.argv) > 3 else "hybrid"
uri = f"ws://{server}:8080"
print(f"╔════════════════════════════════════════════════════════════╗")
print(f"║ PCQ Messenger - {username:^30s} ║")
print(f"╚════════════════════════════════════════════════════════════╝")
print(f"\n🔐 Encryption: {mode}")
print(f"🌐 Server: {server}:8080")
print(f"\nConnecting...\n")
try:
async with websockets.connect(uri) as ws:
# Register
await ws.send(json.dumps({
'type': 'register',
'username': username
}))
# Send keys
await ws.send(json.dumps({
'type': 'key_exchange',
'classical_pub': 'demo_key',
'pq_pub': 'demo_key'
}))
# Run send and receive concurrently
await asyncio.gather(
receive_messages(ws),
send_messages(ws, username, mode)
)
except Exception as e:
print(f"\n❌ Error: {e}")
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")