-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
346 lines (291 loc) · 12.5 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import ntpath
import os
import re
import socket
import threading
import zipfile
import io
import sys
import logging
BUFF = 4096
logging.basicConfig(filename='Logs/server_logs.log', filemode='a', format='%(asctime)s ; %(levelname)s - %(message)s',
datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)
def ipVersion(ip_addr):
try:
ip_addr = socket.gethostbyname(ip_addr)
except socket.error:
pass
try:
ip_addr = socket.getaddrinfo(ip_addr, None, socket.AF_INET6)[0][4][0]
except socket.error:
pass
try:
socket.inet_aton(ip_addr)
return 4
except socket.error:
pass
try:
socket.inet_pton(socket.AF_INET6, ip_addr)
return 6
except socket.error:
pass
return 1
def disconnectClient(conn, addr):
conn.close()
print("Disconnected with: " + addr[0] + ":" + str(addr[1]))
logging.info("Disconnected with: " + addr[0] + ":" + str(addr[1]))
def checkFileConditions(file_name, file_size):
illegal_characters = re.compile('[/\:*?"<>|]')
if illegal_characters.search(file_name) is not None:
return 101
elif file_size > 524288000:
return 102
elif not file_name.endswith(".zip"):
return 103
return 100
def saveFile(file_name, file_content):
counter = 1
file_name_temp = file_name
while os.path.exists("uploaded_files/" + file_name_temp):
if counter == 1:
file_name_temp = file_name_temp[:-4]
else:
if counter % 10 != 0:
del_chars = 7 + int(len(str(counter)))
else:
del_chars = (7 + int(len(str(counter)))) - 1
file_name_temp = file_name_temp[:-del_chars]
file_name_temp = file_name_temp + " (" + str(counter) + ")" + ".zip"
counter += 1
try:
file = open("uploaded_files/" + file_name_temp, "wb+")
file.write(file_content)
file.close()
except IOError as io_exc:
print("I/O error: {0}".format(io_exc))
logging.error("I/O error: {0}".format(io_exc))
return False
return file_name_temp
def zipFileContent(file_name):
zip_file = zipfile.ZipFile("uploaded_files/" + file_name, "r")
old_stdout = sys.stdout
content = io.StringIO()
sys.stdout = content
zip_file.printdir()
sys.stdout = old_stdout
zip_file.close()
return content.getvalue()
def checkFileExistence(file_path, zip_file):
file = zipfile.ZipFile("uploaded_files/" + zip_file, "r")
if file_path in file.namelist():
file.close()
return True
file.close()
return False
def getFileFromZip(file_path, zip_file, password):
if checkFileExistence(file_path, zip_file):
file = zipfile.ZipFile("uploaded_files/" + zip_file, "r")
if len(password) > 0:
opened_file = file.open(file_path, "r", password.encode("utf-8"))
else:
opened_file = file.open(file_path, "r")
file.close()
file_content = opened_file.read()
file_size = len(file_content)
file_data = {"filesize": file_size,
"filecontent": file_content}
return file_data
return False
def oneClient(conn, addr):
password = ""
recived_data = b''
while b'\r\n\r\n' not in recived_data:
recived_data += conn.recv(BUFF)
# Check if client requested upload
if "upload" in recived_data.decode('utf8').splitlines()[0].lower():
file_data = dict(
(k.lower(), v) for k, v in [i.split(': ') for i in recived_data[:-4].decode('utf8').splitlines()[1:]])
file_name = ntpath.basename(file_data.get('filename', "new_file"))
file_size = int(file_data.get('filesize', 0))
print("Upload request form: " + addr[0] + ":" + str(addr[1]) + "\n" +
"File name: " + file_name + "\n" +
"File size: " + str(file_size) + " B")
logging.info("Upload request form: " + addr[0] + ":" + str(addr[1]) + "\n" +
"File name: " + file_name + "\n" +
"File size: " + str(file_size) + " B")
response_code = checkFileConditions(file_name, file_size)
if response_code is not 100:
response = "RESPONSE: " + str(response_code) + "\r\n\r\n"
print("Uploading failed!")
logging.error("Uploading failed!")
conn.send(response.encode('utf8'))
disconnectClient(conn, addr)
return
response = "RESPONSE: 100\r\n\r\n"
conn.send(response.encode('utf8'))
if file_size <= 0:
disconnectClient(conn, addr)
return
# Getting file content
file_content = b''
while len(file_content) < file_size:
file_content += conn.recv(file_size - len(file_content))
if not file_content:
response = "RESPONSE: 201\r\n\r\n"
conn.send(response.encode('utf8'))
disconnectClient(conn, addr)
return
save_file_result = saveFile(file_name, file_content)
if not save_file_result:
response = "RESPONSE: 201\r\n\r\n"
conn.send(response.encode('utf8'))
disconnectClient(conn, addr)
return
temp_file_name = save_file_result
print("File " + file_name + " has been successfully saved on the server as " + temp_file_name)
logging.info("File " + file_name + " has been successfully saved on the server as " + temp_file_name)
is_encrypted = False
try:
test_zip = zipfile.ZipFile("uploaded_files/" + temp_file_name, "r")
test_zip.testzip()
except RuntimeError:
print("File " + temp_file_name + " is encrypted. A password is required to extraction.")
logging.info("File " + temp_file_name + " is encrypted. A password is required to extraction.")
is_encrypted = True
response = "RESPONSE: 204\r\n\r\n"
conn.send(response.encode('utf8'))
except Exception:
print("File " + temp_file_name + " is damaged")
logging.error("File " + temp_file_name + " is damaged")
response = "RESPONSE: 203\r\n\r\n"
conn.send(response.encode('utf8'))
disconnectClient(conn, addr)
os.remove("uploaded_files/" + temp_file_name)
return
if is_encrypted:
while True:
recived_data = b''
try:
while b'\r\n\r\n' not in recived_data:
recived_data += conn.recv(BUFF)
except socket.error as socket_exc:
print("Socket error: {0}".format(socket_exc))
logging.error("Socket error: {0}".format(socket_exc))
disconnectClient(conn, addr)
os.remove("uploaded_files/" + temp_file_name)
return
password_header = recived_data.decode('utf8').split(": ")
password = password_header[1][:-4]
try:
test = test_zip.infolist()
test_open = test_zip.open(test[0].filename, "r", password.encode("utf-8"))
test_open.close()
except IndexError as index_exc:
print("Zip file " + temp_file_name + " is empty")
logging.error("Zip file " + temp_file_name + " is empty")
disconnectClient(conn, addr)
os.remove("uploaded_files/" + temp_file_name)
return
except RuntimeError:
print("Bad password for file: " + temp_file_name)
logging.error("Bad password for file: " + temp_file_name)
response = "RESPONSE: 205\r\n\r\n"
conn.send(response.encode('utf8'))
continue
print("Correct password for file: " + temp_file_name)
logging.error("Correct password for file: " + temp_file_name)
break
test_zip.close()
response = "RESPONSE: 200\r\n\r\n"
conn.send(response.encode('utf8'))
recived_data = b''
try:
while b'\r\n\r\n' not in recived_data:
recived_data += conn.recv(BUFF)
except socket.error as socket_exc:
print("Socket error: {0}".format(socket_exc))
logging.error("Socket error: {0}".format(socket_exc))
disconnectClient(conn, addr)
os.remove("uploaded_files/" + temp_file_name)
return
if recived_data is not None and "fileslist_request" in recived_data.decode('utf8').lower():
response = zipFileContent(temp_file_name) + "\r\n\r\n"
conn.send(response.encode('utf8'))
else:
disconnectClient(conn, addr)
return
# Waiting for a client requests
while True:
recived_data = b''
try:
while b'\r\n\r\n' not in recived_data:
recived_data += conn.recv(BUFF)
except socket.error:
os.remove("uploaded_files/" + temp_file_name)
disconnectClient(conn, addr)
return
if "quit" in recived_data.decode('utf8').lower():
os.remove("uploaded_files/" + temp_file_name)
disconnectClient(conn, addr)
break
if "download" in recived_data.decode('utf8').splitlines()[0].lower():
download_request = dict((k.lower(), v) for k, v in
[i.split(': ') for i in recived_data[:-4].decode('utf8').splitlines()[1:]])
file_path = download_request.get('filepath')
print("Download request form: " + addr[0] + ":" + str(addr[1]) + "\n" +
"File: " + temp_file_name[:-4] + "/" + file_path + "\n")
logging.info("Download request form: " + addr[0] + ":" + str(addr[1]) + "\n" +
"File: " + temp_file_name[:-4] + "/" + file_path)
get_file_result = getFileFromZip(file_path, temp_file_name, password)
if not get_file_result:
response = "RESPONSE: 301\r\n" \
"FILESIZE: 0\r\n\r\n"
conn.send(response.encode('utf8'))
print("File " + temp_file_name[:-4] + "/" + file_path + " doesn't exist\n")
logging.warning("File " + temp_file_name[:-4] + "/" + file_path + " doesn't exist")
continue
response_header = "RESPONSE: 300\r\n" \
"FILESIZE: " + str(get_file_result["filesize"]) + "\r\n\r\n"
response = response_header.encode('utf8') + get_file_result["filecontent"]
try:
conn.send(response)
except socket.error as socket_exc:
print("Socket error: {0}".format(socket_exc))
logging.error("Socket error: {0}".format(socket_exc))
else:
print("File " + temp_file_name[:-4] + "/" + file_path + " successfully sent to client\n")
logging.info("File " + temp_file_name[:-4] + "/" + file_path + " successfully sent to client")
else:
os.remove("uploaded_files/" + temp_file_name)
disconnectClient(conn, addr)
break
if __name__ == "__main__":
server_address = ('127.0.0.1', 5000)
ip_ver = ipVersion(server_address[0])
if ip_ver == 4:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
elif ip_ver == 6:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
print("Invalid ip address! The server can't be started")
exit(1)
if not os.path.exists("uploaded_files"):
os.mkdir("uploaded_files")
try:
print("Running the server...")
logging.info("Running the server...")
sock.bind(server_address)
except socket.error as socket_exc:
print("Socket error: {0}".format(socket_exc))
logging.error("Socket error: {0}".format(socket_exc))
exit(1)
else:
sock.listen(5)
print("Server is running on port 5000")
logging.info("Server is running on port 5000")
while True:
client, address = sock.accept()
print("Connected with: " + address[0] + ":" + str(address[1]))
logging.info("Connected with: " + address[0] + ":" + str(address[1]))
t = threading.Thread(target=oneClient, args=[client, address])
t.start()