-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·289 lines (225 loc) · 9.29 KB
/
server.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/python3
import sys
import socket
import os
import struct
import io
import logging
import hashlib
import zlib
from shutil import copyfile, move, rmtree
# SIZES
UNSIGNED_LONG_LONG_INT_SIZE = 8
UNSIGNED_LONG_INT_SIZE = 4
SHA256_SIZE = 32
COMMAND_SIZE = 1
# REQUESTS
FILE_UPLOAD_REQUEST = b'F'
FILE_DELETE_REQUEST = b'D'
FILE_COPY_REQUEST = b'C'
MOVE_REQUEST = b'M'
FOLDER_CREATE_REQUEST = b'X'
if os.getenv("DEBUG"):
l = logging.DEBUG
else:
l = logging.INFO
logging.basicConfig(level=l)
logger = logging.getLogger('tcpserver')
class Server:
def __init__(self, host, port, directory):
self.host = host
self.port = port
self.directory = directory
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.conn = None
def setup(self):
server_address = (self.host, self.port)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.settimeout(30)
self.sock.bind(server_address)
self.sock.listen(1)
self.conn, _ = self.sock.accept()
def readInBytes(self, n):
# BUG?: we should probably make sure we read n bytes
data_bytes = self.conn.recv(n)
return io.BytesIO(data_bytes).read(n)
# we get the length of the compressed payload, read it all, and then decompress it
def readCompressedData(self):
cdata = bytearray()
cdata_len = self.readInBytes(UNSIGNED_LONG_INT_SIZE)
cdata_len = struct.unpack("!L", cdata_len)[0]
cdata_left = cdata_len
while cdata_left > 0:
read = self.readInBytes(cdata_left)
cdata_left -= len(read)
cdata += read
return zlib.decompress(cdata), cdata_len
# make sure we don't get a milicious request that modifies a file outsides of
def isPathValid(self, fp):
if os.path.commonprefix((os.path.realpath(fp),
os.path.realpath(self.directory))) != os.path.realpath(self.directory):
return False
return True
def truncateFile(self, dst):
if os.path.exists(dst):
os.truncate(dst, 0)
def makeFolder(self, fp):
dst = os.path.join(self.directory, fp)
if self.isPathValid(dst):
if not os.path.exists(dst):
os.makedirs(dst)
def copyFileAndRename(self, src, dst):
src = os.path.join(self.directory, src)
dst = os.path.join(self.directory, dst)
if self.isPathValid(src) and self.isPathValid(dst):
# make dir if it doesn't exist
dst_folder = os.path.dirname(dst)
if not os.path.exists(dst):
try:
os.makedirs(dst_folder)
except FileExistsError:
pass
copyfile(src, dst)
def moveFileFolder(self, src, dst):
src = os.path.join(self.directory, src)
dst = os.path.join(self.directory, dst)
if self.isPathValid(src) and self.isPathValid(dst):
dst_folder = os.path.dirname(dst)
if not os.path.exists(dst_folder):
try:
os.makedirs(dst_folder)
except FileExistsError:
pass
move(src, dst)
def writeFile(self, data, dest):
if not os.path.exists(dest):
try:
os.makedirs(os.path.dirname(dest))
except FileExistsError:
pass
with open(dest, "ab") as f:
f.write(data)
def rmFile(self, fp):
fp = os.path.join(self.directory, fp)
if not self.isPathValid(fp):
logging.warn("Path traversal - ignore request.")
return
is_dir = os.path.isdir(fp)
try:
if is_dir:
rmtree(fp, True)
else:
os.remove(fp)
logging.info(msg="Deleted: " + fp)
except FileNotFoundError:
logging.warn("Tried to delete a non-existing file!")
except IsADirectoryError:
logging.warn("Tried to delete a folder!")
def parseFilePayload(self, fp, size, sha256expected):
fp = os.path.join(self.directory, fp)
sha256_calculated = hashlib.sha256()
bytes_left, total_size = size, size
def progress(percent):
print("downloading %s, %d%% complete.." % (fp, percent), end="\r")
if percent == 100:
print()
if not self.isPathValid(fp):
logging.warn("Path traversal - ignoring file download.")
# we need to read and discard the rest of the payload..
while bytes_left > 0:
data, _ = self.readCompressedData()
bytes_left -= len(data)
return
self.truncateFile(fp)
total_cdata_len = 0
# special case, write empty file.
if bytes_left == 0:
self.writeFile(bytearray(), fp)
progress(100)
else:
while bytes_left > 0:
data, cdata_len = self.readCompressedData()
total_cdata_len += cdata_len
self.writeFile(data, fp)
sha256_calculated.update(data)
bytes_left -= len(data)
progress((total_size-bytes_left)/total_size * 100)
if tuple(sha256_calculated.digest()) != sha256expected:
logging.warn(msg="Ouch! SHA256 verification failed for: " + fp)
logging.debug("File size: {}, Data recieved: {}".format(total_size, total_cdata_len))
# parse header as described in documentation
def parse(self):
data = self.readInBytes(UNSIGNED_LONG_LONG_INT_SIZE + COMMAND_SIZE)
if len(data) == 0:
print("Connection closed? Bye!")
sys.exit(0)
# make sure we have enough bytes!
total_payload_size, request_type = struct.unpack("!Qc", data)
if request_type == FILE_UPLOAD_REQUEST:
data = self.readInBytes(UNSIGNED_LONG_INT_SIZE)
filepath_size = struct.unpack("!L", data)[0]
data = self.readInBytes(filepath_size)
filepath = struct.unpack("!%ds" % filepath_size, data)[0]
data = self.readInBytes(SHA256_SIZE)
sha256 = struct.unpack("!32B", data)
# make sure this works
filepath = filepath.decode("utf-8")
# we need to do a little math to determine that actual filesize
file_size = (total_payload_size
- UNSIGNED_LONG_LONG_INT_SIZE
- COMMAND_SIZE
- UNSIGNED_LONG_INT_SIZE
- filepath_size
- SHA256_SIZE)
logging.debug(msg=("Total payload: %s" % repr(total_payload_size),
"Filepath size: %s" % repr(filepath_size),
"Filepath: %s" % repr(filepath),
"File size: %d" % file_size))
self.parseFilePayload(filepath, file_size, sha256)
# delete file request
elif request_type == FILE_DELETE_REQUEST:
data = self.readInBytes(UNSIGNED_LONG_INT_SIZE)
filepath_size = struct.unpack("!L", data)[0]
data = self.readInBytes(filepath_size)
filepath = struct.unpack("!%ds" % filepath_size, data)[0]
filepath = filepath.decode()
self.rmFile(filepath)
# copy or move file
elif request_type == FILE_COPY_REQUEST or request_type == MOVE_REQUEST:
data = self.readInBytes(UNSIGNED_LONG_INT_SIZE)
src_path_size = struct.unpack("!L", data)[0]
data = self.readInBytes(src_path_size)
src_path = struct.unpack("!%ds" % src_path_size, data)[0]
data = self.readInBytes(UNSIGNED_LONG_INT_SIZE)
dst_path_size = struct.unpack("!L", data)[0]
data = self.readInBytes(dst_path_size)
dst_path = struct.unpack("!%ds" % dst_path_size, data)[0]
src_path = src_path.decode()
dst_path = dst_path.decode()
if request_type == FILE_COPY_REQUEST:
logging.debug(msg=("Copying {} to {}".format(src_path, dst_path)))
self.copyFileAndRename(src_path, dst_path)
else:
logging.debug(msg=("Moving {} to {}".format(src_path, dst_path)))
self.moveFileFolder(src_path, dst_path)
elif request_type == FOLDER_CREATE_REQUEST:
data = self.readInBytes(UNSIGNED_LONG_INT_SIZE)
folder_path_size = struct.unpack("!L", data)[0]
data = self.readInBytes(folder_path_size)
folder_path = struct.unpack("!%ds" % folder_path_size, data)[0]
folder_path = folder_path.decode()
logging.debug("Creating folder: {}".format(folder_path))
self.makeFolder(folder_path)
if __name__ == "__main__":
if len(sys.argv) != 2:
raise SystemExit("please pass an empty directory as an argument!")
empty_dir = sys.argv[1]
if os.listdir(empty_dir):
raise SystemExit("%s is not empty!" % (empty_dir))
s = Server(os.getenv("HOST", "localhost"),
int(os.getenv("PORT", 10001)),
empty_dir)
logger.info("Waiting for client to send a payload to: {}:{}".format(s.host, s.port))
s.setup()
while True:
s.parse()