forked from eybisi/theFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
126 lines (103 loc) · 3.55 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
import sys
import rpyc
import os
import getpass
from pprint import pprint
def usage():
print("./client.py IPADDRESS")
def parseCMD():
return raw_input('theFS> ').split()
def xor_crypt(data, key='awesomepassword', encode=False, decode=False):
from itertools import izip, cycle
import base64
if decode:
data = base64.decodestring(data)
xored = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
if encode:
return base64.encodestring(xored).strip()
return xored
def getFileList(ns):
file_table = ns.get_file_table()
for entry in file_table:
print("{}".format(entry))
print("")
def read_from_storage(block_uuid, storage):
addr, port = storage
con = rpyc.connect(addr, port=port)
ss = con.root.Storage()
return ss.pull(block_uuid)
def getFile(ns, fname):
file_table = ns.get_file_table_entry(fname)
if not file_table:
print("[-] File not found!")
return
passphrase = getpass.getpass("Enter a passphrase to decrypt the file: ")
with open(fname, "w") as f:
for block in file_table:
for storageServer in [ns.get_storage_list()[_] for _ in block[1]]:
data = read_from_storage(block[0], storageServer)
if data:
f.write(xor_crypt(data, passphrase, decode=True))
break
else:
print("[-] No blocks found! Possible corruption!")
return
def send_to_storage(block_uuid, data, storageList):
print("sending: {} -> {}".format(str(block_uuid), str(storageList)))
storage = storageList[0]
storages = storageList[1:]
addr, port = storage
con = rpyc.connect(addr, port=port)
storage = con.root.Storage()
storage.push(data, storages, block_uuid)
def putFile(ns, fname):
size = os.path.getsize(fname)
blocks = ns.put(fname, size)
passphrase = getpass.getpass("Enter a passphrase to encrypt the file: ")
with open(fname) as f:
for b in blocks:
data = xor_crypt(f.read(ns.get_block_size()), passphrase, encode=True)
block_uuid=b[0]
storageList = [ns.get_storage_list()[_] for _ in b[1]]
send_to_storage(block_uuid, data, storageList)
def delete_from_storage(block_uuid, storage):
addr, port = storage
con = rpyc.connect(addr, port=port)
ss = con.root.Storage()
return ss.delete(block_uuid)
def rmFile(ns, fname):
file_table = ns.get_file_table_entry(fname)
if not file_table:
print("[-] File not found!")
return
for block in file_table:
for storageServer in [ns.get_storage_list()[_] for _ in block[1]]:
delete_from_storage(block[0], storageServer)
ns.delete_file_entry(fname)
def processCommand(ns, parameters):
if len(parameters) == 0:
return
elif parameters[0] == "quit":
exit()
elif parameters[0] == "list" or parameters[0] == "ls":
getFileList(ns)
elif parameters[0] == "get":
getFile(ns, parameters[1])
elif parameters[0] == "put":
putFile(ns, parameters[1])
elif parameters[0] == "delete" or parameters[0] == "rm":
rmFile(ns, parameters[1])
else:
return
def main(args):
addr, port = ("localhost", 5353)
if len(args) > 0:
addr = args[0]
if len(args) > 1:
port = int(args[1])
con = rpyc.connect(addr, port)
nameServer = con.root.NameServer()
while True:
processCommand(nameServer, parseCMD())
if __name__ == "__main__":
main(sys.argv[1:])