-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
136 lines (119 loc) · 4.04 KB
/
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
import socket
import MR
from random import randrange
from struct import *
from DH import *
from message import *
from caesar_cipher import *
from constants import *
def get_ip():
hostname = socket.gethostname()
return socket.gethostbyname(hostname)
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
s_addr = get_ip()
d_addr = HOST
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
#comuputing DH public (Ya, prime, alpha) for client A
prime, alpha = MR.get_Prime_PR() # public
Xa = randrange(2, prime - 2) #private to client A
Ya = pow(alpha, Xa, prime) # public
# DH public (Ya, prime, alpha) being shared with server B
DH_share_pack = create_DH_share_pack(Ya, prime, alpha)
s.sendall(DH_share_pack)
print("Sending to server", unpack_DH_share_pack(DH_share_pack))
# DH Yb from server B recevied for with client 'A'
DH_reshare_pack = s.recv(calcsize('i'))
DH_data = unpack_DH_reshare_pack(DH_reshare_pack)
print("Received from server", DH_data)
##computing the shared key
key = pow(DH_data['Yb'], Xa , prime)
print("key is " + str(key))
print("prime is " + str(prime))
print("Done Diffie-Hellmann!!")
logged_user = ""
while True:
if logged_user == "":
action = input('> ')
else:
action = input(logged_user + '> ')
if action == "newlogin":
user = input('Enter ID of user: ')
password = input('Enter password of user: ')
user = encrypt(user, key)
password = encrypt(password, key)
msg = create_message(s_addr=s_addr,d_addr=d_addr, opcode = LOGINCREAT, ID=user, password = password)
if(msg == "Err"):
continue
print("Sent LOGINCREAT Message", unpack_message(msg))
s.sendall(msg)
msg = s.recv(calcsize('iqq10s10si10si80si'))
msg = unpack_message(msg)
print("Received LOGINREPLY from Server")
if msg['status'] == 0:
print('Already in use login - ' + str(decrypt(user, key)))
print('Choose a different username.')
else:
print('Created new user login - ' + str(decrypt(user, key)))
print('You can now login with this username.')
elif action == "login":
if logged_user != "":
print("Log out first before new login!!")
continue
user = input('Enter ID of user: ')
password = input('Enter password of user: ')
user = encrypt(user, key)
password = encrypt(password, key)
msg = create_message(s_addr=s_addr,d_addr=d_addr, opcode = AUTHREQUEST, ID=user, password = password)
if(msg == "Err"):
continue
print("Sent AUTHREQUEST Message", unpack_message(msg))
s.sendall(msg)
msg = s.recv(calcsize('iqq10s10si10si80si'))
msg = unpack_message(msg)
print("Received AUTHREPLY from Server")
if msg['status'] == 0:
print('No such user found - ' + str(decrypt(user, key)))
print('Create a new login.')
elif msg['status'] == -1:
print('Wrong password entered for user - ' + str(decrypt(user, key)))
else:
print('Successfully Logged in as - ' + str(decrypt(user, key)))
logged_user = decrypt(user, key)
elif action == "getfile":
if logged_user == "":
print("Log in first before getfile!!")
continue
filename = input('Enter file path on server: ')
user = encrypt(logged_user, key)
msg = create_message(s_addr=s_addr,d_addr=d_addr, opcode = SERVICEREQUEST, ID=user, file = filename)
if(msg == "Err"):
continue
print("Sent SERVICEREQUEST Message", unpack_message(msg))
s.sendall(msg)
msg = s.recv(calcsize('iqq10s10si10si80si'))
msg = unpack_message(msg)
print(msg)
if(msg['status'] == -1):
print("File doesn't exist on server!!")
continue
else:
f = open(msg['file'], 'w')
f.write(msg['buf'])
while msg['status'] == 0:
msg = s.recv(calcsize('iqq10s10si10si80si'))
msg = unpack_message(msg)
f.write(msg['buf'])
print(msg)
f.close()
print("Done Transferring File from server!!!")
elif action == "quit" or action == "exit":
exit()
elif action == "logout":
logged_user = ""
print('User is now logged out.')
else:
print("No such action!!")
print("Choose one among the below actions:")
print("""1. login\n2. newlogin\n3. getfile\n4. logout\n5. quit/exit""")