forked from Mikatux/docker-ftps-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentrypoint.py
65 lines (54 loc) · 2.26 KB
/
entrypoint.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
import os, random, string
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler, TLS_FTPHandler
from pyftpdlib.servers import FTPServer
import os.path
length = 8
chars = string.ascii_letters + string.digits
random.seed = (os.urandom(1024))
FTP_ROOT = '/home'
USER = os.getenv('USER', 'user')
PASSWORD = os.getenv('PASSWORD', ''.join(random.choice(chars) for i in range(length)))
HOST = os.getenv('HOST','0.0.0.0')
PORT = 21
PASSIVE_PORTS = '3000-3010'
ANONYMOUS = os.getenv('ANONYMOUS', False)
PROTOCOL = os.getenv('PROTOCOL', 'FTPS')
CERTIFICATE_SUBJECT = "/C=UK/ST=Warwickshire/L=Leamington/O=OrgName/OU=IT Department/CN=example.com"
def main():
user_dir = os.path.join(FTP_ROOT, USER)
if not os.path.isdir(user_dir):
os.mkdir(user_dir)
authorizer = DummyAuthorizer()
authorizer.add_user(USER, PASSWORD, user_dir, perm="elradfmw")
if ANONYMOUS:
authorizer.add_anonymous("/ftp_root/nobody")
generatedCertificate = False
if PROTOCOL == 'FTP':
handler = FTPHandler
else:
if not os.path.exists('./keycert.pem'):
generatedCertificate = True
os.system('openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout keycert.pem -out keycert.pem -days 365 -subj "' + CERTIFICATE_SUBJECT + '"')
handler = TLS_FTPHandler
handler.certfile = 'keycert.pem'
handler.authorizer = authorizer
handler.permit_foreign_addresses = True
passive_ports = list(map(int, PASSIVE_PORTS.split('-')))
handler.passive_ports = range(passive_ports[0], passive_ports[1])
print('*************************************************')
print('* *')
print('* Docker image: mikatux *')
print('* https://github.com/mikatux/ftp-server *')
print('* *')
print('*************************************************')
print('SERVER SETTINGS')
print('---------------')
print("FTP User: ", USER)
print("FTP Password: ", PASSWORD)
if generatedCertificate:
print("Generated new SSL certificate !")
server = FTPServer((HOST, PORT), handler)
server.serve_forever()
if __name__ == '__main__':
main()