-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdfs.py
More file actions
155 lines (126 loc) · 4.86 KB
/
dfs.py
File metadata and controls
155 lines (126 loc) · 4.86 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
import client_side_connection
import peer_ip
import filetable
import random
import openSSL
import fileIO
import base64
# Commented out all the code that encrypts a file before sending or retrieving it
# This way the server can handle encryption
def hash_filename(filename):
# I wanted to hash the filenames but had difficulty
# with it so I just store the filename in plaintext
# hashed_filename = openSSL.hash(filename)
# filename_b64 = str(base64.urlsafe_b64encode(bytes(hashed_filename, 'utf-8')))[2:42]
return filename
def storeFile(peer_name, filename):
# Use this peer's private key as the keyfile for encrypting the file (Symmetric encryption)
keyfile = 'CA/' + peer_name + '/' + peer_name + '.key'
print('What host would you like to store the file on?')
host_input = input()
file_location = None
# Request a list of files we can access from every host
for hostname, host in peer_ip.peer_map.items():
name = str(eval(hostname)[4][0][1])
if(host_input == name):
file_location = host
if(file_location == None):
print("Host not found")
return
# Hash the filename and encrypt the contents before sending to the host
# hashed_filename = hash_filename(filename)
file_contents = fileIO.readFile(filename)
# encrypted_contents = openSSL.encrypt(keyfile, file_contents)
# Send the file to the host
net = client_side_connection.ClientSideConnection(
peer_name = peer_name, ip = file_location[0], portno = int(file_location[1]))
net.send('Store')
net.send(filename)
net.send(file_contents)
print(net.recv())
# Add the file to the filetable
filetable.addFile(peer_name, filename, str(net.getPeerInfo()))
net.done()
def retrieveFile(peer_name, filename):
# Use this peer's private key as the keyfile for decrypting the file (Symmetric encryption)
keyfile = 'CA/' + peer_name + '/' + peer_name + '.key'
# Look up which host has the file
if filename in filetable.getFiletable(peer_name):
file_host = filetable.getFiletable(peer_name)[filename]
# Use the peer_map to figure out the address of that host
file_location = peer_ip.peer_map[file_host]
else:
print('What host would you like to retrieve the file from?')
host_input = input()
file_location = None
# Request a list of files we can access from every host
for hostname, host in peer_ip.peer_map.items():
name = str(eval(hostname)[4][0][1])
if(host_input == name):
file_location = host
if(file_location == None):
print("Host not found")
return
# Hash the filename so it matches what we stored on the host
# hashed_filename = hash_filename(filename)
# Request the file from the host
net = client_side_connection.ClientSideConnection(
peer_name = peer_name, ip = file_location[0], portno = int(file_location[1]))
net.send('Retrieve')
net.send(filename)
# Wait for the file contents, then decrypt them and store them
file_contents = net.recv()
# file_contents = openSSL.decrypt(keyfile, encrypted_contents)
print(net.recv())
fileIO.writeFile(filename, file_contents)
net.done()
def deleteFile(peer_name, filename):
# Hash the filename so it matches what we stored on the host
# hashed_filename = hash_filename(filename)
# Look up which host has the file
if filename in filetable.getFiletable(peer_name):
file_host = filetable.getFiletable(peer_name)[filename]
# Use the peer_map to figure out the address of that host
file_location = peer_ip.peer_map[file_host]
else:
print('What host would you like to delete the file from?')
host_input = input()
file_location = None
# Request a list of files we can access from every host
for hostname, host in peer_ip.peer_map.items():
name = str(eval(hostname)[4][0][1])
if(host_input == name):
file_location = host
if(file_location == None):
print("Host not found")
return
# Request the file from the host
net = client_side_connection.ClientSideConnection(
peer_name = peer_name, ip = file_location[0], portno = int(file_location[1]))
net.send('Delete')
net.send(filename)
print(net.recv())
net.done()
if filename in filetable.getFiletable(peer_name):
filetable.removeFile(peer_name, filename)
def getFiletable(peer_name):
# Not used ever. This function sucks.
dictionary = {}
# Request a list of files we can access from every host
for hostname, host in peer_ip.peer_map.items():
print('Requesting file list from', str(eval(hostname)[4][0][1]))
host_ip = host[0]
host_portno = int(host[1])
net = client_side_connection.ClientSideConnection(
peer_name = peer_name, ip = host_ip, portno = host_portno)
net.send('My_Files')
files = net.recv()
# We know who the server is and we trust them so we know that they will send us a list
list_files = eval(files)
for filename in list_files:
dictionary[filename] = hostname
# Add the file to the filetable
# filetable.addFile(peer_name, filename, str(net.getPeerInfo()))
net.done()
filetable.storeFiletable(peer_name, dictionary)
return dictionary