-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathftp.be
578 lines (535 loc) · 17.4 KB
/
ftp.be
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#- Simple FTP server in Berry by Christian Baars
# supports active and passive mode - but passive is preferred!
# only light error handling
-#
#@ solidify:ftp
var ftp = module('ftp')
class PATH # helper class to hold the current directory
var p # path components in a list
def init()
import string
self.p = []
end
def set(p)
import string
import path
if path.isdir(p) != true
return false
end
var new = string.split(p,"/")
self.p = []
for c:new
if c != ""
self.p.push(c)
end
end
return true
end
def dir_up()
if size(self.p) > 0
self.p.pop()
end
end
def get_url()
var url = "/"
for c:self.p
if c != ""
url += f"{c}/"
end
end
return url
end
end
class FTP
var connection, server, client, data_server, data_client, data_ip
var dir, dir_list, dir_pos
var file, file_size, file_rename, retries, chunk_size
var binary_mode, active_ip, active_port, user_input
var data_buf, data_ptr, fast_loop, data_op
static port = 21
static data_port = 20 # data connection in passive mode
static allow_anonymous = true # allow everything ..
static user = "user"
static password = "pass"
def init()
self.server = tcpserver(self.port) # connection for control data
self.connection = false
self.data_ip = tasmota.wifi()['ip']
self.dir = PATH()
self.readDir()
self.data_ptr = 0
self.active_port = nil
# tasmota.add_driver(self)
log(f"FTP: init server on port {self.port}",1)
end
def deinit()
self.server.deinit()
self.data_server.deinit()
tasmota.remove_driver(self)
end
def every_50ms()
if self.connection == true
self.loop()
elif self.server.hasclient()
self.client = self.server.acceptasync()
self.sendResponse("220 Welcome")
self.connection = true
self.pubClientInfo()
else
self.connection = false
end
end
def every_second()
if self.client && self.connection != false
if self.client.connected() == false
self.pubClientInfo()
self.connection = false
self.abortDataOp()
end
end
end
def pubClientInfo()
import mqtt
var payload = self.client.info().tostring()
mqtt.publish("FTP",format("{'server':%s}", payload))
end
def loop()
if self.connection == true
self.handleConnection()
end
end
def abortDataOp()
if self.data_op == "d"
self.finishDownload(true)
elif self.data_op == "u"
self.finishUpload(true)
elif self.data_op == "dir"
self.finishUpload(false)
end
end
def download() # ESP -> client
self.data_buf..self.file.readbytes(self.chunk_size)
if size(self.data_buf) == 0
self.retries -= 1
if self.retries > 0
return
end
else
var written = self.data_client.write(self.data_buf)
self.data_buf.clear()
self.data_ptr += written
if self.data_ptr < self.file_size
self.file.seek(self.data_ptr)
if self.retries > 0
return
end
end
end
self.finishDownload()
end
def finishDownload(error)
self.data_client.close()
tasmota.remove_fast_loop(self.fast_loop)
self.file.close()
if error
self.sendResponse(f"426 Connection closed; transfer aborted after {self.data_ptr} bytes.")
else
self.sendResponse(f"250 download done with {self.data_ptr} bytes.")
end
self.data_op = nil
tasmota.gc()
end
def upload() # client -> ESP
self.data_buf..self.data_client.readbytes()
if size(self.data_buf) > 0
self.file.write(self.data_buf)
self.data_ptr += size(self.data_buf)
self.data_buf.clear()
else
log(f"FTP: {self.retries} retries",4)
self.retries -= 1
if self.retries > 0
return
end
self.finishUpload()
end
end
def finishUpload(error)
self.data_client.close()
tasmota.remove_fast_loop(self.fast_loop)
self.file.close()
if error
self.sendResponse(f"426 Connection closed; transfer after {self.data_ptr} bytes")
else
self.sendResponse(f"250 upload done with {self.data_ptr} bytes")
end
self.data_op = nil
tasmota.gc()
end
def transferDir(mode)
import path
var sz, date, isdir
var i = self.dir_list[self.dir_pos]
var url = f"{self.dir.get_url()}{i}"
isdir = path.isdir(url)
if isdir == false
var f = open(url,"r")
sz = f.size()
f.close()
date = path.last_modified(url)
end
if self.data_client.connected()
var dir = ""
if mode == "MLSD"
if isdir
dir = "Type=dir;Perm=edlmp; "
else
date = tasmota.time_dump(date)
var y = str(date['year'])
var m = f"{date['month']:02s}"
var d = f"{date['day']:02s}"
var h = f"{date['hour']:02s}"
var min = f"{date['min']:02s}"
var sec = f"{date['sec']:02s}"
var modif =f"{y}{m}{d}{h}{min}{sec}"
dir = f"Type=file;Perm=rwd;Modify={modif};Size={sz}; "
end
elif mode == "LIST"
var d = "-"
if isdir
d = "d"
date = ""
sz = ""
else
date = tasmota.strftime("%b %d %H:%M", date)
end
dir = f"{d}rw------- 1 all all{sz:14s} {date} "
elif mode == "NLST"
dir=self.dir.get_url()
end
var entry = f"{dir}{i}"
log(entry,4)
self.data_client.write(entry + "\r\n")
self.dir_pos += 1
else
self.finishTransferDir(false)
end
if self.dir_pos < size(self.dir_list)
return
end
self.finishTransferDir(true)
end
def finishTransferDir(success)
self.data_client.close()
if success
var n = size(self.dir_list)
self.sendResponse(f"226 {n} files in {self.dir.get_url()}")
else
self.sendResponse("426 Transfer aborted")
end
self.data_op = nil
tasmota.remove_fast_loop(self.fast_loop)
tasmota.gc()
end
def readDir()
import path
self.dir_list = path.listdir(self.dir.get_url())
end
def openFile(name,mode)
import path
var url = f"{self.dir.get_url()}{name}"
if path.isdir(url) == true
log(f"FTP: {url} is a folder",2)
return false
end
if mode == "r"
if path.exists(url) != true
log(f"FTP: {url} not found",2)
return false
end
end
log(f"FTP: Open file {url} in {mode} mode",3)
self.file = open(f"{url}",mode)
if mode == "a"
if self.data_ptr != 0
log(f"FTP: Appending file {url} at position {self.data_ptr}",3)
if self.data_ptr != self.file.size()
log(f"FTP: !!! resume position of {self.data_ptr} != file size of {self.file.size()} !!!",2)
end
end
end
return true
end
def close()
self.sendResponse("221 Closing connection")
self.connection = false
end
def deinitConnectServer()
if self.data_server != nil
self.data_server.close()
self.data_server.deinit()
self.data_server = nil
log("FTP: Delete server for passive data connection",2)
end
end
def initConnectServer()
if self.data_server == nil
self.data_server = tcpserver(self.data_port)
log("FTP: Start server for passive data connection",2)
end
end
def connectActive()
self.data_client = tcpclientasync()
if self.data_client.connect(self.active_ip,self.active_port) != false
log(f"FTP: Try to connect to {self.active_ip}:{self.active_port}",3)
end
end
def connectPassive()
if self.data_server.hasclient()
self.data_client = self.data_server.acceptasync()
end
end
def dataconnect()
if self.data_client != nil
self.data_client.close()
self.data_client.deinit()
end
if self.active_port != nil
self.connectActive()
else
self.connectPassive()
end
if self.data_client == nil
self.sendResponse("425 Data connection failed")
return false
end
self.data_buf = bytes()
self.retries = 10
self.chunk_size = 5760
self.sendResponse("150 Ready for data transfer")
return true
end
def sendResponse(resp)
self.client.write(f"{resp}\r\n")
log(f"FTP: Response: {resp}",3)
end
def handleConnection() # main loop for incoming commands
import string
import mqtt
import path
var d = self.client.read()
if size(d) == 0 return end
var items = string.split(d," ")
var cmd = items[0]
var arg = ""
var response = ""
if size(items) > 1
arg = string.split(items[1],'\r\n')[0]
else
cmd = string.split(cmd,'\r\n')[0]
end
log(f"FTP: Received: {cmd} {arg}",3)
# connect
if cmd == "USER"
if self.allow_anonymous
response = "230 accept any/anonymous user"
else
self.user_input = arg
response = "331 Password required"
end
elif cmd == "PASS"
if self.user_input == self.user && arg == self.password
response = "230 User accepted"
else
response = "530 Wrong login credentials"
mqtt.publish("FTP","{'login':'wrong credentials'}")
end
elif cmd == "AUTH"
response = f"500 Server does not support {arg}"
elif cmd == "ABOR"
self.abortDataOp()
response = f"200 Aborting"
elif cmd == "QUIT"
self.close()
#options
elif cmd == "FEAT"
self.sendResponse("211-Extensions supported:")
self.sendResponse(" MLSD")
self.sendResponse(" EPSV")
self.sendResponse(" SIZE")
# self.sendResponse(" MDTM")
self.sendResponse(" REST STREAM")
response = "211 End"
elif cmd == "OPTS"
if arg == "UTF8"
response = "200 UTF Ok"
else
response = f"500 Server does not support {arg}"
end
elif cmd == "STRU"
if arg == "F"
response = "200 F Ok"
else
response = "504 Only F (ile) is supported"
end
elif cmd == "SYST"
response = "215 UNIX"
elif cmd == "LPRT"
response = f"501 active connection with long address not supported"
elif cmd == "PORT"
var el = string.split(arg,",")
self.active_ip = f"{el[0]}.{el[1]}.{el[2]}.{el[3]}"
self.active_port = int(el[4])*256 + int(el[5])
response = f"200 port received {self.active_ip}:{self.active_port}"
self.deinitConnectServer()
# response = f"501 active connection not supported"
elif cmd == "EPRT"
var el = string.split(arg,"|") # |1|192.168.1.54|65519| -> 1 IPV4, 2 IPV6
self.active_ip = el[2]
self.active_port = int(el[3])
self.deinitConnectServer()
response = f"200 extended port received {self.active_ip}:{self.active_port}"
elif cmd == "TYPE"
if arg == "I"
response = "200 binary mode"
self.binary_mode = true
elif arg == "A"
response = "200 ascii mode"
self.binary_mode = false
end
elif cmd == "EPSV"
self.active_port = nil
self.initConnectServer()
response = f"229 Entering Extended Passive Mode (|||{self.data_port}|)"
elif cmd == "PASV"
self.active_port = nil
var el = string.split(self.data_ip,".")
var hi = self.data_port >> 8
var lo = self.data_port & 0xff
self.initConnectServer()
response = f"227 Entering passive mode ({el[0]},{el[1]},{el[2]},{el[3]},{hi},{lo})"
elif cmd == "DELE"
if path.remove(f"{self.dir.get_url()}{arg}")
response = f"250 {self.dir.get_url()}{arg} deleted"
else
response = f"550 Could not delete file {self.dir.get_url()}{arg}"
end
elif cmd == "RMD"
var url = arg
if arg[0] != "/"
url = f"{self.dir.get_url()}{arg}"
end
if path.rmdir(url)
response = f"250 {url} deleted"
else
response = f"550 Could not delete folder {url}"
end
elif cmd == "STOR"
self.dataconnect()
if self.data_client != nil
response = ""
var mode = "w"
if self.data_ptr > 0
mode = "a"
end
if self.openFile(arg,mode)
self.data_op = "u"
self.fast_loop = /->self.upload()
tasmota.add_fast_loop(self.fast_loop)
else
response = f"550 Could not open file"
end
else
response = f"501 Could not init data connection"
end
elif cmd == "REST"
self.data_ptr = int(arg)
response = f"350 {self.data_ptr}"
elif cmd == "RNFR"
if self.openFile(arg,"r")
self.file_rename = f"{self.dir.get_url()}{arg}"
response = f"350 {arg}"
self.file.close()
else
self.file_rename = nil
response = f"550 Could not open file"
end
elif cmd == "RNTO"
if self.file_rename != nil
tasmota.cmd(f"UfsRename {self.file_rename},{self.dir.get_url()}{arg}")
response = f"250 Renamed {self.file_rename} -> {arg}"
else
response = f"550 Could not rename file"
end
self.file_rename = nil
elif cmd == "SIZE"
if self.openFile(arg,"r")
response = f"213 {self.file.size()}"
self.file.close()
else
response = f"550 Could not open file"
end
elif cmd == "RETR"
self.dataconnect()
if self.data_client != nil
if self.openFile(arg,"r")
self.file_size = self.file.size()
self.data_op = "d"
self.fast_loop = /->self.download()
tasmota.add_fast_loop(self.fast_loop)
else
response = f"550 Could not open file"
end
else
response = f"501 Could not init data connection"
end
# folder
elif cmd == "CDUP"
self.dir.dir_up()
response = "250 okay"
elif cmd == "CWD"
if self.dir.set(arg)
response = "250 okay"
else
response = "550 Failed to change directory."
end
elif cmd == "PWD"
self.readDir()
response = f"250 {self.dir.get_url()}"
elif cmd == "MKD"
path.mkdir(f"{self.dir.get_url()}{arg}")
response = f"250 {self.dir.get_url()}{arg} created"
elif cmd == "LIST" || cmd == "MLSD" || cmd == "NLST"
if arg != ""
self.dir.set(arg)
end
self.readDir()
if self.dataconnect()
if size(self.dir_list) > 0
self.data_op = "dir"
self.dir_pos = 0
self.fast_loop = /->self.transferDir(cmd)
tasmota.add_fast_loop(self.fast_loop)
else
self.finishTransferDir(true)
end
else
response = f"501 Could not init data connection"
end
else # any unknown command
response = "202 Command not implemented in Berry FTP"
end
if response != ""
self.sendResponse(response)
end
end
end
def init(m)
import global
global.ftp = m
var ftp_driver = FTP()
tasmota.add_driver(ftp_driver)
return m
end
ftp.init = init
return ftp