-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpserver.py
280 lines (237 loc) · 8.33 KB
/
ftpserver.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
# coding=utf-8
import os
import socket
import threading
import time
import traceback
import requests
import logging
def make_path(*parts):
path = os.path.join(*parts)
return path
class BaseFtpThread(threading.Thread):
GUEST = 'guest'
def __init__(self, (conn, addr), ftp_server):
#self.logger = logging.getLogger(__name__)
#self.logger.setLevel(logging.INFO)
self.allowed_commands = ''
self.conn = conn
self.addr = addr
self.server = ftp_server
self._base_dir = self.server.ftp_path
self.home_dir = self._base_dir
self.cwd = self.home_dir
self._authorized = False
self._user = self.GUEST
self.rest = False
self.pasv_mode = False
self.mode = 'I'
self.pos = self.rnfn = self.datasock = self.servsock = self.data_addr = \
self.data_port = None
threading.Thread.__init__(self)
def _send(self, data, channel=None):
if channel == 'data':
self._log('<= ' + data.rstrip())
self.datasock.send(data + '\r\n')
else:
self._log('< ' + data)
self.conn.send(data + '\r\n')
def _log(self, s, *args):
import datetime
t = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
print('[%s %s:%s] %s' % (t, self.addr[0], self.addr[1], s % args))
def _command_allowed(self, cmd):
return True
def run(self):
self._log('Thread started.')
self._send('220 Welcome!')
while True:
try:
cmd = self.conn.recv(256)
except Exception as e:
self._log('Connection aborted %r', e)
break
# TODO: clean characters
if not cmd:
break
else:
self._log(' > ' + cmd.strip())
try:
c = cmd[:4].strip().upper()
if not hasattr(self, c) or not self._command_allowed(c):
self._send('502 Command not implemented.')
elif self._authorized and c in ['USER', 'PASS']:
self._send('503 Bad sequence of commands.')
elif not self._user and c in ['PASS']:
self._send('503 Bad sequence of commands.')
elif not self._authorized and c not in ['USER', 'PASS', 'QUIT']:
self._send('530 Not logged in.')
else:
getattr(self, c)(cmd)
except Exception:
traceback.print_exc()
self._send('500 Error.')
self._log('Thread closed.')
def start_datasock(self):
if self.pasv_mode:
self.datasock, addr = self.servsock.accept()
self._log('Open passive socket %s:%s', *addr)
else:
self.datasock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.datasock.connect((self.data_addr, self.data_port))
self._log('Open given socket %s:%s', self.data_addr, self.data_port)
def stop_datasock(self):
if self.datasock:
self._log('Close socket %s:%s', *self.datasock.getsockname())
self.datasock.close()
if self.pasv_mode:
try:
self._log('Close passive socket %s:%s', *self.servsock.getsockname())
except:
pass
self.servsock.close()
class FtpThread(BaseFtpThread):
def OPTS(self, cmd):
if cmd[5:-2].upper() == 'UTF8 ON':
self._send('200 OK.')
else:
self._send('451 Sorry.')
def SYST(self, cmd):
self._send('215 UNIX Type: L8') # See http://cr.yp.to/ftp/syst.html
def USER(self, cmd):
self._user = cmd[4:].strip() or self.GUEST
self._send('331 OK.')
def PASS(self, cmd):
self._send('230 OK.')
self._authorized = True
self.cwd = self.home_dir = make_path(self._base_dir, self._user)
# self._send('530 Incorrect.')
def QUIT(self, cmd):
self._send('221 Goodbye.')
def NOOP(self, cmd):
self._send('200 OK.')
def TYPE(self, cmd):
if cmd[5] in ['I', 'A']:
self.mode = cmd[5]
if self.mode == 'I':
self._send('200 Type set to: Binary.')
else:
self._send('200 Type set to: ASCII.')
else:
self._send('500 Sorry, only binary and ASCII type supported.')
def CDUP(self, cmd):
self._send('200 OK.')
def PWD(self, cmd):
self._send('257 "%s"' % self.cwd)
def CWD(self, cmd):
chwd = cmd[4:-2]
if chwd == '/':
self.cwd = self.home_dir
elif chwd[0] == '/':
self.cwd = chwd
elif chwd == '..':
head, tail = os.path.split(self.cwd)
self.cwd = head
else:
self.cwd = os.path.join(self.cwd, chwd[0:])
self._send('250 "%s" is the current directory.' % (self.cwd))
def PORT(self, cmd):
if self.pasv_mode:
self.servsock.close()
self.pasv_mode = False
l = cmd[5:].split(',')
self.data_addr = '.'.join(l[:4])
self.data_port = (int(l[4]) << 8) + int(l[5])
self._log('Custom addr and port %s:%s', self.data_addr, self.data_port)
self._send('200 Get port.')
def PASV(self, cmd): # from http://goo.gl/3if2U
self.pasv_mode = True
self.servsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.servsock.bind((self.server.ftp_ip, 0))
self.servsock.listen(1)
ip, port = self.servsock.getsockname()
self._log('Opened socket %s:%s', ip, port)
self._send('227 Entering Passive Mode (%s,%u,%u).' % (
','.join(ip.split('.')),
port >> 8 & 0xFF,
port & 0xFF,
))
def LIST(self, cmd):
self._send('150 Here comes the directory listing.')
print('list:', self.cwd)
self.start_datasock()
self.stop_datasock()
self._send('226 Directory send OK.')
def MKD(self, cmd):
self._send('550 File exists.')
def RMD(self, cmd):
self._send('250 Directory deleted.')
def DELE(self, cmd):
self._send('250 File deleted.')
def RNFR(self, cmd):
self._send('350 Ready.')
def RNTO(self, cmd):
self._send('250 File renamed.')
def REST(self, cmd):
self.pos = int(cmd[5:-2])
self.rest = True
self._send('350 Restarting at position %s.' % (self.pos))
def RETR(self, cmd):
self._send('150 Opening data connection.')
self.start_datasock()
self.stop_datasock()
self._send('226 Transfer complete.')
def STOR(self, cmd):
fn = os.path.join(self.cwd, cmd[5:-2])
self._log('Upload: ' + fn)
#fo = open(fn, 'wb')
self._send('150 Opening data connection.')
self.start_datasock()
while True:
data = self.datasock.recv(1024)
if not data:
break
# fo.write(data)
#fo.close()
self.stop_datasock()
self._send('226 Transfer complete.')
def SIZE(self, cmd):
self._send('213 0')
class FTPserver(threading.Thread):
def __init__(self, ip='127.0.0.1', port=21, path='/home', can_delete=False):
self.ftp_ip = ip
self.ftp_port = port
self.ftp_path = make_path(path)
self.can_delete = can_delete
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((ip, port))
threading.Thread.__init__(self)
def run(self):
self.sock.listen(5)
while True:
th = FtpThread(self.sock.accept(), ftp_server=self)
th.daemon = True
th.start()
def stop(self):
self.sock.close()
if __name__ == '__main__':
import sys
server_ip = '10.10.0.129'
server_port = 2121
if len(sys.argv) > 1:
if '.' in sys.argv[1]:
server_ip = sys.argv[1]
else:
server_port = int(sys.argv[1])
if len(sys.argv) > 2:
server_port = int(sys.argv[2])
ftp = FTPserver(ip=server_ip, port=server_port)
ftp.daemon = True
try:
ftp.start()
raw_input('Enter to quit...\n')
except KeyboardInterrupt:
pass
finally:
ftp.stop()
print('Bye.')