-
Notifications
You must be signed in to change notification settings - Fork 0
/
psm_client.py
executable file
·140 lines (127 loc) · 4.9 KB
/
psm_client.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python3
from nacl.public import PrivateKey, Box, PublicKey
from nacl.signing import SigningKey, VerifyKey
from nacl.encoding import Base64Encoder
import requests
from random import randint
from base64 import b64encode, b64decode
from threading import Thread
import time
import curses
from curses import wrapper
import urllib3
import struct
import json
urllib3.disable_warnings(urllib3.exceptions.SubjectAltNameWarning)
server_endpoint="https://localhost:4443"
signKey = SigningKey.generate()
vk = signKey.verify_key
sk = signKey.to_curve25519_private_key()
pk = vk.to_curve25519_public_key()
user_id = -1
isLineBreak=False
offsetY=2
offsetX=3
chatUiWidth=60
chatUiHeight=40
maxMsgLen=chatUiWidth-10
def random_with_N_digits(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
def init_id():
global user_id
user_id = str(random_with_N_digits(5))
user_info = {"id":user_id, "pk":b64encode(pk.__bytes__()).decode("ascii"), "vk": b64encode(vk.__bytes__()).decode("ascii")}
response=requests.post(server_endpoint+"/start", json=user_info, verify='cert.pem')
return response.text
def get_getRecipientPk(id):
user_info = {"id":id}
response=requests.post(server_endpoint+"/getRecipientPk", json=user_info, verify='cert.pem')
if response.text == "Fail":
return
recipent_info = response.json()
return recipent_info["pk"]
def get_recipientId():
recipent_pk = ""
while not recipent_pk:
recipient_id = input("Enter your recipient ID:")
recipent_pk = get_getRecipientPk(recipient_id)
if not recipent_pk:
print("Recipient ID not found")
return {"id":recipient_id, "pk":recipent_pk}
def init_conversation():
init_result = init_id()
while init_result != "ok":
init_result = init_id()
print(f"Your ID: {user_id}")
return get_recipientId()
def send_msg(sender_id, recipient_id, msg):
msg_payload={"sender_id":sender_id, "recipient_id": recipient_id, "msg": msg, "sign": signKey.sign(struct.pack(">i", int(time.time())), encoder=Base64Encoder).decode("ascii")}
response=requests.post(server_endpoint+"/sendMsg", json=msg_payload, verify='cert.pem')
if response.text == "Fail":
print("[MESSAGE CANNOT BE SENT]")
return False
return True
def get_msg(my_id, from_id):
getMsg_payload={"id": my_id, "from":from_id, "sign": signKey.sign(struct.pack(">i", int(time.time())), encoder=Base64Encoder).decode("ascii")}
response=requests.post(server_endpoint+"/getMsg", json=getMsg_payload, verify='cert.pem')
if response.text == "Fail":
return
return response.json()
def print_msg(my_id, from_id, box):
while True:
msgs = get_msg(my_id,from_id)
if msgs:
for msg in msgs["msgs"]:
decoded_msg = b64decode(msg)
decrypted_msg = box.decrypt(decoded_msg).decode('utf-8')
print("<"+decrypted_msg)
time.sleep(5)
def chat_render(chatbox, my_id, from_id, box):
global offsetX, offsetY
while True:
msgs = get_msg(my_id,from_id)
if msgs:
for msg in msgs["msgs"]:
decoded_msg = b64decode(msg)
decrypted_msg = box.decrypt(decoded_msg).decode('utf-8')
chatbox.addstr(offsetY,offsetX,"<"+decrypted_msg,curses.color_pair(2))
offsetY+=1
chatbox.refresh(max(0,offsetY-chatUiHeight),0, 0,0, chatUiHeight, chatUiWidth)
time.sleep(5)
def chat_main(stdscr,mybox,recipent_id):
global offsetX, offsetY
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
chatbox = curses.newpad(1000, chatUiWidth)
chatbox.box()
chatbox.refresh(0,0,0,0,chatUiHeight, chatUiWidth)
t = Thread(target=chat_render, args=(chatbox, user_id, recipent_id, mybox,))
t.start()
curses.echo()
while True:
textbox = curses.newwin(5, chatUiWidth, 42, 0)
textbox.box()
textbox.addstr(2,5,"msg>")
textbox.addstr(3,5,"Your ID: "+user_id +" Recipent ID:"+recipent_id)
textbox.refresh()
message = textbox.getstr(2,10,maxMsgLen)
if message:
encrypted_message = mybox.encrypt(message)
encoded_message = b64encode(encrypted_message).decode("ascii")
if send_msg(user_id, recipent_id, encoded_message):
chatbox.addstr(offsetY,offsetX,b">"+message,curses.color_pair(1))
offsetY+=1
chatbox.refresh(max(0,offsetY-chatUiHeight),0, 0,0, chatUiHeight, chatUiWidth)
def main():
global user_id
recipent=init_conversation()
recipent_id = recipent["id"]
recipent_pk = PublicKey(b64decode(recipent["pk"]))
mybox = Box(sk, recipent_pk)
wrapper(chat_main,mybox,recipent_id)
#t = Thread(target=print_msg, args=(user_id, recipent_id, mybox,))
#t.start()
if __name__ == '__main__':
main()