-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.py
210 lines (153 loc) · 5.87 KB
/
fs.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
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
from xmlrpc.client import ServerProxy
from xmlrpc.server import SimpleXMLRPCServer
import os
import shutil
import sys
from cryptography.fernet import Fernet
from utils.reply import Reply
from utils.config import COORDINATOR_LOCATION, LOCALHOST
from utils.fernet_helper import encode_data, decode_data, serialize_list
COORDINATOR = ServerProxy(COORDINATOR_LOCATION)
KEY_BS = 0
KEY_BS_SUITE = None
def nonce_mod(nonce):
return nonce
def present_working_directory():
return Reply(success=True, data=os.getcwd())
def list_directory(nonce, client_id, payload_ses):
(ses_key, ses_suite) = extract_ses_key(payload_ses)
asking_client_id = extract_asking_client_id(payload_ses)
assert(asking_client_id == client_id)
dec_nonce = ses_suite.decrypt(encode_data(nonce)).decode()
dec_nonce = nonce_mod(dec_nonce)
print(f'[ls] Executing ls for {client_id}')
nonce = ses_suite.encrypt(encode_data(dec_nonce))
files = os.listdir(os.getcwd())
files = serialize_list(files)
files = ses_suite.encrypt(encode_data(files))
return Reply(success=True, nonce=nonce, data=files)
def copy_file(f1, f2, nonce, client_id, payload_ses):
(ses_key, ses_suite) = extract_ses_key(payload_ses)
asking_client_id = extract_asking_client_id(payload_ses)
assert(asking_client_id == client_id)
src = ses_suite.decrypt(encode_data(f1)).decode()
dest = ses_suite.decrypt(encode_data(f2)).decode()
dec_nonce = ses_suite.decrypt(encode_data(nonce)).decode()
dec_nonce = nonce_mod(dec_nonce)
print(f'[cp] Executing cp with src: {src}, dest: {dest} for {client_id}')
success = False
try:
shutil.copyfile(src, dest)
message = 'Successfully copied'
success = True
# return Reply(success=True, message='Successfully copied', nonce=dec_nonce)
except shutil.SameFileError:
message = 'Error: Can not copy to the same file'
except IsADirectoryError:
message = 'Error: Copying folders not allowed'
except PermissionError:
message = 'Error: Permission denied'
except IOError:
message = f'Error: src file ({src}) does not exist'
except Exception:
message = 'Error while copying file'
message = ses_suite.encrypt(encode_data(message))
nonce = ses_suite.encrypt(encode_data(dec_nonce))
return Reply(success=success, message=message, nonce=nonce)
def cat(file_arg, nonce, client_id, payload_ses):
(ses_key, ses_suite) = extract_ses_key(payload_ses)
asking_client_id = extract_asking_client_id(payload_ses)
assert(asking_client_id == client_id)
file_name = ses_suite.decrypt(encode_data(file_arg)).decode()
dec_nonce = ses_suite.decrypt(encode_data(nonce)).decode()
dec_nonce = nonce_mod(dec_nonce)
print(f'[cat] Executing cat with file: {file_name} for {client_id}')
nonce = ses_suite.encrypt(encode_data(dec_nonce))
success = False
try:
f = open(file_name, 'r')
text = f.read()
f.close()
text_enc = ses_suite.encrypt(encode_data(text))
return Reply(success=True, data=text_enc, nonce=nonce)
except FileNotFoundError:
message = 'Error: File does not exist'
except Exception:
message = 'Error while reading file'
message = ses_suite.encrypt(encode_data(message))
return Reply(success=False, message=message, nonce=nonce)
def extract_ses_key(payload_ses):
ses_key_enc = payload_ses['key_ab']
ses_key = KEY_BS_SUITE.decrypt(encode_data(ses_key_enc)).decode()
print('Kab: ', ses_key)
return (ses_key, Fernet(ses_key))
def extract_asking_client_id(payload_ses):
client_id_enc = payload_ses['client_id']
client_id = KEY_BS_SUITE.decrypt(encode_data(client_id_enc)).decode()
try:
client_id = int(client_id)
except ValueError:
client_id = None
return client_id
def test(payload_arg, payload_ses):
print(payload_ses)
# nonce_recv = payload_arg['nonce']
(ses_key, ses_suite) = extract_ses_key(payload_ses)
pay_arg_dec = ses_suite.decrypt(encode_data(payload_arg)).decode()
print(pay_arg_dec)
test_resp = {
'pay_arg': pay_arg_dec
}
return test_resp
def register_fs_functions(server):
server.register_function(list_directory)
server.register_function(present_working_directory)
server.register_function(copy_file)
server.register_function(cat)
server.register_function(test)
def set_server_key(fs_port_offset):
global KEY_BS
global KEY_BS_SUITE
try:
with open('keys/fs_keys.txt', 'r') as fs_keys:
for i, line in enumerate(fs_keys):
if i == fs_port_offset:
KEY_BS = line.rstrip().encode()
except OSError:
print('Unable to read fs_keys')
print(f'Key for server: {KEY_BS}')
KEY_BS_SUITE = Fernet(KEY_BS)
if __name__ == '__main__':
fs_port = 7000
if len(sys.argv) == 2:
try:
fs_port = int(sys.argv[1])
except ValueError:
print('Supply a valid base port number as an integer')
exit()
global server
max_tries = 10
fs_port_found = False
while fs_port_found is False and max_tries:
try:
server = SimpleXMLRPCServer((LOCALHOST, fs_port), logRequests=True)
fs_port_found = True
except OSError:
fs_port += 1
finally:
max_tries -= 1
if fs_port_found is False:
print('Ports already in use. Unable to create an RPC server.')
exit()
set_server_key(fs_port % 10)
if os.path.isdir(f'fs_{fs_port}') is False:
os.mkdir(f'fs_{fs_port}')
os.chdir(f'fs_{fs_port}')
register_fs_functions(server)
print(COORDINATOR.add_fs(fs_port))
try:
print(f'File server started at port {fs_port}')
server.serve_forever()
except KeyboardInterrupt:
print(COORDINATOR.remove_fs(fs_port))
print('File server terminated')