-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
94 lines (75 loc) · 2.75 KB
/
server.py
File metadata and controls
94 lines (75 loc) · 2.75 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
import os
import csv
import socket
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.servers import FTPServer
from pyftpdlib.handlers import FTPHandler
from collections import namedtuple
User = namedtuple("User", "username password user_type")
"""
Explanation of permissions as described in the source code of pyftpdlib:
Read permissions:
- "e" = change directory (CWD command)
- "l" = list files (LIST, NLST, STAT, MLSD, MLST, SIZE, MDTM commands)
- "r" = retrieve file from the server (RETR command)
Write permissions:
- "a" = append data to an existing file (APPE command)
- "d" = delete file or directory (DELE, RMD commands)
- "f" = rename file or directory (RNFR, RNTO commands)
- "m" = create directory (MKD command)
- "w" = store a file to the server (STOR, STOU commands)
- "M" = change file mode (SITE CHMOD command)
- "T" = update file last modified time (MFMT command)
"""
def main():
users = get_users()
authorizer = DummyAuthorizer()
private_files = "/Repository/"
shared_files = "/Repository/Shared/"
# Sets home folder and permissions for each user
for user in users:
if user.user_type == "admin":
home = os.getcwd() + private_files
permissions = "elradfmwMT"
elif user.user_type == "friend":
home = os.getcwd() + shared_files
permissions = "elrafmwMT"
else:
print("Error: user type '" + user.user_type + "' is invalid.")
continue
authorizer.add_user(user.username, user.password, home, permissions)
handler = FTPHandler
handler.banner = "Hello World!"
handler.authorizer = authorizer
handler.masquerade_address = get_local_ip()
handler.passive_ports = range(60000, 65535)
server = FTPServer(("0.0.0.0", 21), handler)
server.max_cons = 200
server.max_cons_per_ip = 10
server.serve_forever()
# Reads user accounts from a .csv file
def get_users():
users = []
with open("users.csv", "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",", skipinitialspace=True)
next(csv_reader) # Skips the first row
for row in csv_reader:
users.append(User(row[0], row[1], row[2]))
print("Users:")
for user in users:
print(user.username + ", " + user.password + ", " + user.user_type)
return users
# Gets the local IP of the machine the server is running on
# The code for this was found here: https://stackoverflow.com/a/28950776/11562557
def get_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("10.255.255.255", 1))
ip = s.getsockname()[0]
except:
ip = "127.0.0.1"
finally:
s.close()
return ip
if __name__ == "__main__":
main()