-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
274 lines (199 loc) · 8.08 KB
/
client.py
File metadata and controls
274 lines (199 loc) · 8.08 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import socket
import random
import click
import grpc
import pickle
import os
import subprocess
import sys
import keyDistServer_pb2
import keyDistServer_pb2_grpc
import fileserver_pb2
import fileserver_pb2_grpc
from alive_progress import alive_bar
from rich.console import Console
from rich.table import Table
from simplecrypt import encrypt, decrypt
from configparser import SafeConfigParser,ConfigParser
from utils import JsonToDict, dictToJSON
config = ConfigParser()
console = Console()
class Client(object):
def __init__(self):
self.host = None
self.port = None
self.kdcStub = None
self.myKey = None
self.myId = None
self.fs_stub = None
self.fs = None
self.availableServers = {}
pass_client = click.make_pass_decorator(Client, ensure=True)
@click.group()
@click.option('--fs', type=int, help='select file server')
# @click.option('--kport', type=int, help='KDS port')
@pass_client
def cli(client, fs):
# if kport:
# connection_url = 'localhost:' + kport
# channel = grpc.insecure_channel(connection_url)
# client.kdcStub = keyDistServer_pb2_grpc.ConnectStub(channel)
# if not config.has_section('main'):
# config.add_section('main')
# config.set('main', 'host', 'localhost')
# config.set('main', 'port', kport)
if 'main' not in config.sections():
config.add_section('main')
if os.path.isfile('./config.ini') == False:
pass
else:
config.read('config.ini')
client.host = config['main']['host']
client.port = config['main']['port']
client.myKey = config['main']['myKey']
client.myId = int(config['main']['myId'])
fileservers = config.sections()
for i in range(1, len(fileservers)):
client.availableServers[i] = config[str(fileservers[i])]['url']
connection_url = config['main']['host'] + ':' + config['main']['port']
channel = grpc.insecure_channel(connection_url)
client.kdcStub = keyDistServer_pb2_grpc.ConnectStub(channel)
if fs:
# print(client.availableServers)
if fs in client.availableServers:
channel = grpc.insecure_channel(client.availableServers[fs])
client.fs_stub = fileserver_pb2_grpc.FileServerStub(channel)
else:
sys.exit('Fileserver not connected')
@cli.command()
@click.option('--port', help='Port of key distribution server')
@pass_client
def getKey(client, port):
''' Connects you with file server or Key distribution server '''
connection_url = 'localhost:'+ port
channel = grpc.insecure_channel(connection_url)
client.kdcStub = keyDistServer_pb2_grpc.ConnectStub(channel)
k = keyDistServer_pb2.Info(type='ds')
skey = client.kdcStub.ConnectNew(k)
client.myKey = skey.key
client.myId = skey.id
config.set('main', 'host', 'localhost')
config.set('main', 'port', port)
config.set('main', 'myKey', client.myKey)
config.set('main', 'myId', str(client.myId))
with open('config.ini', 'w') as configfile:
config.write(configfile)
print('Got key from KDS successfully...')
@cli.command()
@click.option('--id', help='ID of fileserver')
@pass_client
def connect(client,id):
''' Connects and authenticates with file server '''
if int(id) not in client.availableServers:
sys.exit('Fileserver not found')
with alive_bar(3, spinner='dots_waves2', bar='blocks') as bar:
console.print('Authentication in progress...', style='green')
bar('PHASE I in progress ...')
nonce = random.randrange(1000)
idB = id
message = dictToJSON([client.myId, idB, nonce])
message = encrypt(client.myKey, message)
res = client.kdcStub.Authenticate(keyDistServer_pb2.AuthRequestEncrypted(id=client.myId, message=message))
bar('Phase II in progress...')
ks, idA, idB, nonce, messageToB = JsonToDict(decrypt(client.myKey, res.message))
channel = grpc.insecure_channel(client.availableServers[(idB)])
stub = fileserver_pb2_grpc.FileServerStub(channel)
encryptedII = stub.Authenticate(fileserver_pb2.AuthRequest(message=messageToB.encode('latin-1'))).message
bar('Final phase in progress...')
nonceII = int(decrypt(ks, encryptedII)) + 1
finalMessage = encrypt(ks, str(nonceII))
res = stub.AutheticationComplete(fileserver_pb2.AuthRequest(id=client.myId, message=finalMessage))
# if config.has_section():
# print()
if res.status == 200:
print('Authentication with file server {} completed successfully'.format(idB))
else:
print('Authentication with file server {} failed...'.format(idB))
@cli.command()
@click.option('--file', help='Path of file to upload')
@pass_client
def upload(client,file):
''' Upload a file to selected file server '''
with open(file, "r") as f:
content = f.read()
print(content)
command = "upload "+file
command = fileserver_pb2.CommandRequest(command=command, data=content, id=client.myId)
output = client.fs_stub.TakeCommand(command)
@cli.command()
@pass_client
def pwd(client):
''' Show current directory path '''
command = fileserver_pb2.CommandRequest(command="pwd", id=client.myId)
output = client.fs_stub.TakeCommand(command)
if output.status == 200:
click.echo(output.output)
elif output.status == 400:
click.echo('Terminal not authorized')
else:
click.echo('File server error')
@cli.command()
@pass_client
def ls(client):
''' Show contents of directory '''
command = fileserver_pb2.CommandRequest(command="ls", id=client.myId)
output = client.fs_stub.TakeCommand(command)
if output.status == 200:
click.echo(output.output)
elif output.status == 400:
click.echo('Terminal not authorized')
else:
click.echo('File server error')
@cli.command()
@click.option('--file', type=str, help='Path of file to print')
@pass_client
def cat(client, file):
''' Show contents of file '''
command = 'cat '+file
command = fileserver_pb2.CommandRequest(command=command, id=client.myId)
output = client.fs_stub.TakeCommand(command)
click.echo(output.output)
# if output.status == 200:
# click.echo(output.output)
# elif output.status == 400:
# click.echo('Terminal not authorized')
# else:
# click.echo('File server error')
@cli.command()
@click.option('--file1', type=str, help='Path of file1 to copy')
@click.option('--file2', type=str, help='Path of file2')
@pass_client
def cp(client, file1, file2):
''' Copy one file to another '''
command = 'cp {} {}'.format(file1, file2)
command = fileserver_pb2.CommandRequest(command=command, id=client.myId)
output = client.fs_stub.TakeCommand(command)
if output.status == 200:
click.echo(output.output)
elif output.status == 400:
click.echo('Terminal not authorized')
else:
click.echo('File server error')
@cli.command()
@pass_client
def fileservers(client):
''' Get information about all th=e connected file servers '''
infoReq = keyDistServer_pb2.InfoRequest()
client.availableServers = JsonToDict(client.kdcStub.GetServerInfo(infoReq).file_servers)
table = Table(show_header=True, header_style="bold magenta")
table.add_column("ID", style="dim", width=12)
table.add_column("URL")
for key, value in client.availableServers.items():
table.add_row(key, value)
if not config.has_section(key):
config.add_section(key)
config.set(key, 'url', value)
with open('config.ini', 'w') as configfile:
config.write(configfile)
console.print(table)
# click.echo(client.availableServers)