-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
174 lines (149 loc) · 5.62 KB
/
server.py
File metadata and controls
174 lines (149 loc) · 5.62 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import socket
import threading
import pickle
import mysql.connector
import os
from os import walk
import base64
from Crypto.Cipher import AES
from Crypto import Random
# header size can be varied manually according to the maximum size of the file to be transferred or a while loop can be used in case there is no restriction
HEADER = 64*4096
PORT = 9001
SERVER = "0.0.0.0"
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
key = b'B?E(H+MbPeShVmYq3t6w9z$C&F)J@NcR'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
#Enabling authentication of users
def auth(uname,passw):
mydb = mysql.connector.connect(
host="db", port="3306", user="root", passwd="password", database="AlphaQMoM")
mycursor = mydb.cursor()
mycursor.execute(
f"Select * from allUsers where user='{uname}' AND password='{passw}'")
myresult = mycursor.fetchall()
x = len(myresult)
return x
def upload(conn,addr,domain,filename):
if os.path.exists(f"./{domain}/{filename}"):
conn.send("File already exists.".encode(FORMAT))
print(addr,": File already exists.")
else :
conn.send("proceed".encode(FORMAT))
info = conn.recv(HEADER)
info_list = pickle.loads(info)
enc_iv = info_list[0]
iv = base64.b64decode(enc_iv)
aes = AES.new(key, AES.MODE_CFB, iv)
conn.send('vector recvd'.encode(FORMAT))
data = conn.recv(HEADER)
file_data = aes.decrypt(data)
file = open(f"./{domain}/{filename}", 'wb')
file.write(file_data)
file.close()
conn.send("File uploaded successfully.".encode(FORMAT))
print(addr,": File uploaded successfully.")
def download(conn,addr,domain,filename):
if os.path.exists(f"./{domain}/{filename}"):
iv = Random.new().read(AES.block_size)
aes = AES.new(key, AES.MODE_CFB, iv)
enc_iv = base64.b64encode(iv)
file = open(f"./{domain}/{filename}", 'rb')
forEncData = file.read(HEADER)
info = [enc_iv,len(forEncData)]
info_list = pickle.dumps(info)
conn.send(info_list)
conn.recv(16)
encData = aes.encrypt(forEncData)
conn.sendall(encData)
print(addr, conn.recv(HEADER).decode(FORMAT))
else :
conn.send("File not found.".encode(FORMAT))
print(addr,": File not found.")
def remove(conn,addr,domain,filename):
if os.path.exists(f"./{domain}/{filename}"):
os.remove(f"./{domain}/{filename}")
conn.send("File removed successfully.".encode(FORMAT))
print(addr,": File removed successfully!")
else :
conn.send("File not found.".encode(FORMAT))
print(addr,": File not found.")
def view(conn,addr,domain):
f = []
for (dirpath, dirnames, filenames) in walk(f"./{domain}/"):
f.extend(filenames)
break
file_list = ''
for i in f:
file_list += f"{i}\n"
conn.send(file_list.encode(FORMAT))
print(addr,": Files sent successfully")
def search(conn,addr,domain,filename):
if os.path.exists(f"./{domain}/{filename}"):
conn.send("File exists.".encode(FORMAT))
print(addr,"File exists.")
else:
conn.send("File does not exist.".encode(FORMAT))
print(addr,"File does not exist.")
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
msgs = []
connected = True
while connected:
msg = conn.recv(HEADER)
msgs.append(msg)
if len(msgs) == 1:
cred = pickle.loads(msg)
uname1 = base64.b64decode(cred[0]).decode()
passw1 = base64.b64decode(cred[1]).decode()
cred = [uname1,passw1]
result = auth(cred[0],cred[1])
if result == 1:
uname = cred[0]
if uname[0:6] == "sysAd_":
domain = "sysAd"
conn.send("welcome".encode(FORMAT))
elif uname[0:7] == "webDev_":
domain = "webDev"
conn.send("welcome".encode(FORMAT))
elif uname[0:7] == "appDev_":
domain = "appDev"
conn.send("welcome".encode(FORMAT))
else :
conn.send("Invalid Credentials".encode(FORMAT))
print(addr,"Invalid User.")
connected = False
print(addr,"[Connection Closed]")
break
if len(msgs) != 1:
option = pickle.loads(msg)
if option[0] == "download":
download(conn,addr,domain,option[1])
elif option[0] == "upload":
upload(conn,addr,domain,option[1])
elif option[0] == "remove":
remove(conn,addr,domain,option[1])
elif option[0] == "view":
view(conn,addr,domain)
elif option[0] == "search":
search(conn,addr,domain,option[1])
elif option[0] == "disconnect":
conn.send("Successfully disconnected from the server.".encode(FORMAT))
conn.close()
connected = False
print(addr, "[Connection Closed]")
break
else :
print(addr,"Wrong input.")
def start():
server.listen()
print(f"[LISTENING] Server is listening on {SERVER}")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
print("[STARTING] Server is starting...")
start()