From 87917101a4818b34dd19114ddd1a5f88d2022b46 Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Thu, 5 Apr 2018 15:51:34 +0400 Subject: [PATCH 01/12] Create server.lua --- lua_modules/ftp/server.lua | 157 +++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 lua_modules/ftp/server.lua diff --git a/lua_modules/ftp/server.lua b/lua_modules/ftp/server.lua new file mode 100644 index 0000000000..8f1982165b --- /dev/null +++ b/lua_modules/ftp/server.lua @@ -0,0 +1,157 @@ +-- a simple ftp server +USER = "test" +PASS = "12345" +local file,net,pairs,print,string,table = file,net,pairs,print,string,table +data_fnc = nil +ftp_data = net.createServer(net.TCP, 180) +ftp_data:listen(20, function (s) + if data_fnc then data_fnc(s) end +end) +ftp_srv = net.createServer(net.TCP, 180) +ftp_srv:listen(21, function(socket) + local s = 0 + local cwd = "/" + local buf = "" + local t = 0 + socket:on("receive", function(c, d) + a = {} + for i in string.gmatch(d, "([^ \r\n]+)") do + table.insert(a,i) + end + if(a[1] == nil or a[1] == "")then return end + if(s == 0 and a[1] == "USER")then + if(a[2] ~= USER)then + return c:send("530 user not found\r\n") + end + s = 1 + return c:send("331 OK. Password required\r\n") + end + if(s == 1 and a[1] == "PASS")then + if(a[2] ~= PASS)then + return c:send("530 \r\n") + end + s = 2 + return c:send("230 OK.\r\n") + end + if(a[1] == "CDUP")then + return c:send("250 OK. Current directory is "..cwd.."\r\n") + end + if(a[1] == "CWD")then + if(a[2] == ".")then + return c:send("257 \""..cwd.."\" is your current directory\r\n") + end + cwd = a[2] + return c:send("250 OK. Current directory is "..cwd.."\r\n") + end + if(a[1] == "PWD")then + return c:send("257 \""..cwd.."\" is your current directory\r\n") + end + if(a[1] == "TYPE")then + if(a[2] == "A")then + t = 0 + return c:send("200 TYPE is now ASII\r\n") + end + if(a[2] == "I")then + t = 1 + return c:send("200 TYPE is now 8-bit binary\r\n") + end + return c:send("504 Unknown TYPE") + end + if(a[1] == "MODE")then + if(a[2] ~= "S")then + return c:send("504 Only S(tream) is suported\r\n") + end + return c:send("200 S OK\r\n") + end + if(a[1] == "PASV")then + local _,ip = socket:getaddr() + local _,_,i1,i2,i3,i4 = string.find(ip,"(%d+).(%d+).(%d+).(%d+)") + return c:send("227 Entering Passive Mode ("..i1..","..i2..","..i3..","..i4..",0,20).\r\n") + end + if(a[1] == "LIST" or a[1] == "NLST")then + c:send("150 Accepted data connection\r\n") + data_fnc = function(sd) + local l = file.list(); + for k,v in pairs(l) do + if(a[1] == "NLST")then + sd:send(k.."\r\n") + else + sd:send("+r,s"..v..",\t"..k.."\r\n") + end + end + sd:close() + data_fnc = nil + c:send("226 Transfer complete.\r\n") + end + return + end + if(a[1] == "RETR")then + f = file.open(a[2]:gsub("%/",""),"r") + if(f == nil)then + return c:send("550 File "..a[2].." not found\r\n") + end + c:send("150 Accepted data connection\r\n") + data_fnc = function(sd) + local b=f:read(1024) + sd:on("sent", function(cd) + if b then + sd:send(b) + b=f:read(1024) + else + sd:close() + f:close() + data_fnc = nil + c:send("226 Transfer complete.\r\n") + end + end) + if b then + sd:send(b) + b=f:read(1024) + end + end + return + end + if(a[1] == "STOR")then + f = file.open(a[2]:gsub("%/",""),"w") + if(f == nil)then + return c:send("451 Can't open/create "..a[2].."\r\n") + end + c:send("150 Accepted data connection\r\n") + data_fnc = function(sd) + sd:on("receive", function(cd, dd) + f:write(dd) + end) + socket:on("disconnection", function(c) + f:close() + data_fnc = nil + end) + c:send("226 Transfer complete.\r\n") + end + return + end + if(a[1] == "RNFR")then + buf = a[2] + return c:send("350 RNFR accepted\r\n") + end + if(a[1] == "RNTO" and buf ~= "")then + file.rename(buf, a[2]) + buf = "" + return c:send("250 File renamed\r\n") + end + if(a[1] == "DELE")then + if(a[2] == nil or a[2] == "")then + return c:send("501 No file name\r\n") + end + file.remove(a[2]:gsub("%/","")) + return c:send("250 Deleted "..a[2].."\r\n") + end + if(a[1] == "NOOP")then + return c:send("200 OK\r\n") + end + if(a[1] == "QUIT")then + return c:send("221 Goodbye\r\n", function (s) s:close() end) + end + c:send("500 Unknown error\r\n") + end) + socket:send("220--- Welcome to FTP for ESP8266/ESP32 ---\r\n220--- By NeiroN ---\r\n220 -- Version 1.5 --\r\n"); +end) From edaf946f274e828ece6440284a0a2d4bbb89acae Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Thu, 5 Apr 2018 18:54:31 +0400 Subject: [PATCH 02/12] Update and rename server.lua to ftpserver.lua turn it in module like "http" --- lua_modules/ftp/{server.lua => ftpserver.lua} | 107 ++++++++++-------- 1 file changed, 57 insertions(+), 50 deletions(-) rename lua_modules/ftp/{server.lua => ftpserver.lua} (62%) diff --git a/lua_modules/ftp/server.lua b/lua_modules/ftp/ftpserver.lua similarity index 62% rename from lua_modules/ftp/server.lua rename to lua_modules/ftp/ftpserver.lua index 8f1982165b..dd5d042cbd 100644 --- a/lua_modules/ftp/server.lua +++ b/lua_modules/ftp/ftpserver.lua @@ -1,12 +1,11 @@ -- a simple ftp server -USER = "test" -PASS = "12345" local file,net,pairs,print,string,table = file,net,pairs,print,string,table -data_fnc = nil +local ftp, ftp_data, ftp_srv +do +local createServer = function (user, pass) +local data_fnc = nil ftp_data = net.createServer(net.TCP, 180) -ftp_data:listen(20, function (s) - if data_fnc then data_fnc(s) end -end) +ftp_data:listen(20, function (s) if data_fnc then data_fnc(s) end end) ftp_srv = net.createServer(net.TCP, 180) ftp_srv:listen(21, function(socket) local s = 0 @@ -14,70 +13,70 @@ ftp_srv:listen(21, function(socket) local buf = "" local t = 0 socket:on("receive", function(c, d) - a = {} + local a = {} for i in string.gmatch(d, "([^ \r\n]+)") do table.insert(a,i) end - if(a[1] == nil or a[1] == "")then return end - if(s == 0 and a[1] == "USER")then - if(a[2] ~= USER)then + local a1,a2 = unpack(a) + if a1 == nil or a1 == "" then return end + if s == 0 and a1 == "USER" then + if a2 ~= user then return c:send("530 user not found\r\n") end s = 1 return c:send("331 OK. Password required\r\n") end - if(s == 1 and a[1] == "PASS")then - if(a[2] ~= PASS)then - return c:send("530 \r\n") + if s == 1 and a1 == "PASS" then + if a2 ~= pass then + return c:send("530 Try again\r\n") end s = 2 return c:send("230 OK.\r\n") end - if(a[1] == "CDUP")then + if s ~= 2 then + return c:send("530 Not logged in, authorization required\r\n") + end + if a1 == "CDUP" then return c:send("250 OK. Current directory is "..cwd.."\r\n") end - if(a[1] == "CWD")then - if(a[2] == ".")then + if a1 == "CWD" then + if a2 == "." then return c:send("257 \""..cwd.."\" is your current directory\r\n") end - cwd = a[2] + cwd = a2 return c:send("250 OK. Current directory is "..cwd.."\r\n") end - if(a[1] == "PWD")then + if a1 == "PWD" then return c:send("257 \""..cwd.."\" is your current directory\r\n") end - if(a[1] == "TYPE")then - if(a[2] == "A")then + if a1 == "TYPE" then + if a2 == "A" then t = 0 return c:send("200 TYPE is now ASII\r\n") end - if(a[2] == "I")then + if a2 == "I" then t = 1 return c:send("200 TYPE is now 8-bit binary\r\n") end return c:send("504 Unknown TYPE") end - if(a[1] == "MODE")then - if(a[2] ~= "S")then + if a1 == "MODE" then + if a2 ~= "S" then return c:send("504 Only S(tream) is suported\r\n") end return c:send("200 S OK\r\n") end - if(a[1] == "PASV")then + if a1 == "PASV" then local _,ip = socket:getaddr() local _,_,i1,i2,i3,i4 = string.find(ip,"(%d+).(%d+).(%d+).(%d+)") return c:send("227 Entering Passive Mode ("..i1..","..i2..","..i3..","..i4..",0,20).\r\n") end - if(a[1] == "LIST" or a[1] == "NLST")then + if a1 == "LIST" or a1 == "NLST" then c:send("150 Accepted data connection\r\n") data_fnc = function(sd) local l = file.list(); for k,v in pairs(l) do - if(a[1] == "NLST")then - sd:send(k.."\r\n") - else - sd:send("+r,s"..v..",\t"..k.."\r\n") - end + sd:send( a1 == "NLST" and k.."\r\n" or "+r,s"..v..",\t"..k.."\r\n") end sd:close() data_fnc = nil @@ -85,10 +84,10 @@ ftp_srv:listen(21, function(socket) end return end - if(a[1] == "RETR")then - f = file.open(a[2]:gsub("%/",""),"r") - if(f == nil)then - return c:send("550 File "..a[2].." not found\r\n") + if a1 == "RETR" then + f = file.open(a2:gsub("%/",""),"r") + if f == nil then + return c:send("550 File "..a2.." not found\r\n") end c:send("150 Accepted data connection\r\n") data_fnc = function(sd) @@ -111,17 +110,17 @@ ftp_srv:listen(21, function(socket) end return end - if(a[1] == "STOR")then - f = file.open(a[2]:gsub("%/",""),"w") - if(f == nil)then - return c:send("451 Can't open/create "..a[2].."\r\n") + if a1 == "STOR" then + f = file.open(a2:gsub("%/",""),"w") + if f == nil then + return c:send("451 Can't open/create "..a2.."\r\n") end c:send("150 Accepted data connection\r\n") data_fnc = function(sd) sd:on("receive", function(cd, dd) f:write(dd) end) - socket:on("disconnection", function(c) + sd:on("disconnection", function(c) f:close() data_fnc = nil end) @@ -129,29 +128,37 @@ ftp_srv:listen(21, function(socket) end return end - if(a[1] == "RNFR")then - buf = a[2] + if a1 == "RNFR" then + buf = a2 return c:send("350 RNFR accepted\r\n") end - if(a[1] == "RNTO" and buf ~= "")then - file.rename(buf, a[2]) + if a1 == "RNTO" and buf ~= "" then + file.rename(buf, a2) buf = "" return c:send("250 File renamed\r\n") end - if(a[1] == "DELE")then - if(a[2] == nil or a[2] == "")then + if a1 == "DELE" then + if a2 == nil or a2 == "" then return c:send("501 No file name\r\n") end - file.remove(a[2]:gsub("%/","")) - return c:send("250 Deleted "..a[2].."\r\n") + file.remove(a2:gsub("%/","")) + return c:send("250 Deleted "..a2.."\r\n") end - if(a[1] == "NOOP")then + if a1 == "NOOP" then return c:send("200 OK\r\n") end - if(a[1] == "QUIT")then + if a1 == "QUIT" then return c:send("221 Goodbye\r\n", function (s) s:close() end) end c:send("500 Unknown error\r\n") end) - socket:send("220--- Welcome to FTP for ESP8266/ESP32 ---\r\n220--- By NeiroN ---\r\n220 -- Version 1.5 --\r\n"); + socket:send("220--- Welcome to FTP for ESP8266/ESP32 ---\r\n220--- By NeiroN ---\r\n220 -- Version 1.8 --\r\n"); end) +end +local closeServer = function() + ftp_data:close() + ftp_srv:close() +end +ftp = { createServer = createServer, closeServer = closeServer } +end +return ftp From 2a6f4138c6067230519e8e77b77fa231031a93d2 Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Thu, 5 Apr 2018 18:55:10 +0400 Subject: [PATCH 03/12] Create ftpserver-example.lua --- lua_modules/ftp/ftpserver-example.lua | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 lua_modules/ftp/ftpserver-example.lua diff --git a/lua_modules/ftp/ftpserver-example.lua b/lua_modules/ftp/ftpserver-example.lua new file mode 100644 index 0000000000..ea1884d516 --- /dev/null +++ b/lua_modules/ftp/ftpserver-example.lua @@ -0,0 +1,2 @@ +-- Start a simple ftp server +require("ftpserver").createServer("test","12345") From 64fbf9885014c0c19b160417bf750d0f2e7a4f6d Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Thu, 5 Apr 2018 18:57:46 +0400 Subject: [PATCH 04/12] Update ftpserver-example.lua --- lua_modules/ftp/ftpserver-example.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua_modules/ftp/ftpserver-example.lua b/lua_modules/ftp/ftpserver-example.lua index ea1884d516..39cc5575d4 100644 --- a/lua_modules/ftp/ftpserver-example.lua +++ b/lua_modules/ftp/ftpserver-example.lua @@ -1,2 +1,3 @@ --- Start a simple ftp server +-- Start a simple ftp server +-- createServer("user", "password") require("ftpserver").createServer("test","12345") From c011da598c7c3de4502cefdcf0128d85b7449577 Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Thu, 5 Apr 2018 20:03:41 +0400 Subject: [PATCH 05/12] Update ftpserver.lua Update file reading algoritm - reading chunk before sending and nil it after send called. --- lua_modules/ftp/ftpserver.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lua_modules/ftp/ftpserver.lua b/lua_modules/ftp/ftpserver.lua index dd5d042cbd..f2d13b34f6 100644 --- a/lua_modules/ftp/ftpserver.lua +++ b/lua_modules/ftp/ftpserver.lua @@ -91,11 +91,11 @@ ftp_srv:listen(21, function(socket) end c:send("150 Accepted data connection\r\n") data_fnc = function(sd) - local b=f:read(1024) sd:on("sent", function(cd) + b=f:read(1024) if b then sd:send(b) - b=f:read(1024) + b=nil else sd:close() f:close() @@ -103,10 +103,9 @@ ftp_srv:listen(21, function(socket) c:send("226 Transfer complete.\r\n") end end) - if b then - sd:send(b) - b=f:read(1024) - end + local b=f:read(1024) + sd:send(b) + b=nil end return end From 212ee8186ee0a255cb72b26e1ab4dce0f0bb9168 Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Fri, 6 Apr 2018 01:04:04 +0400 Subject: [PATCH 06/12] Create README.md --- lua_modules/ftp/README.md | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lua_modules/ftp/README.md diff --git a/lua_modules/ftp/README.md b/lua_modules/ftp/README.md new file mode 100644 index 0000000000..d3e6130d3f --- /dev/null +++ b/lua_modules/ftp/README.md @@ -0,0 +1,64 @@ +# FTPServer Module + +This is a Lua module for the access to SPIFFS via FTP protocol. +FTP server uses only one specified user and password to auth clients. +All clients need authorization - anonumous do not supported yet. +All files have RW access. +Directory creation do not supported because SPIFFS do not have that. + +## Require +```lua +ftpserver = require("ftpserver") +``` +## Release +```lua +ftpserver:closeServer() +ftpserver = nil +package.loaded["ftpserver"]=nil +``` + +# Methods + +## createServer() +Starts listen on 20 and 21 ports for serve FTP clients. + +The module requires active network connection. + +#### Syntax +`createServer(username, password)` + +#### Parameters +- `username` string username for 'USER username' ftp command. +- `password` string password. + +#### Returns +nil + +#### Example +```lua +require("ftpserver").createServer("test","12345") +``` + +## closeServer() +CLoses all server sockets. + +#### Syntax +`closeServer()` + +#### Returns +nil + +#### Example +```lua +ftpserver = require("ftpserver") +ftpserver:createServer("test","12345") +------------------------- +-- Some program code +------------------------- +if needStopFtp = true then + ftpserver:closeServer() + ftpserver = nil + package.loaded["ftpserver"] = nil +end +``` + From b707ab1cc79e703f9df2a3ec95469dc34456f645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Thu, 5 Apr 2018 23:26:22 +0200 Subject: [PATCH 07/12] Fix small spelling & grammar issues --- lua_modules/ftp/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua_modules/ftp/README.md b/lua_modules/ftp/README.md index d3e6130d3f..e183d56d55 100644 --- a/lua_modules/ftp/README.md +++ b/lua_modules/ftp/README.md @@ -1,8 +1,8 @@ # FTPServer Module -This is a Lua module for the access to SPIFFS via FTP protocol. -FTP server uses only one specified user and password to auth clients. -All clients need authorization - anonumous do not supported yet. +This is a Lua module to access the SPIFFS file system via FTP protocol. +FTP server uses only one specified user and password to authenticate clients. +All clients need authentication - anonymous access is not supported yet. All files have RW access. Directory creation do not supported because SPIFFS do not have that. @@ -40,7 +40,7 @@ require("ftpserver").createServer("test","12345") ``` ## closeServer() -CLoses all server sockets. +Closes all server sockets. #### Syntax `closeServer()` From 3281e2826776252e0a1612669ebc7cd1a9c1ebf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Thu, 5 Apr 2018 23:32:49 +0200 Subject: [PATCH 08/12] Add link to SPIFFS documentation --- lua_modules/ftp/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua_modules/ftp/README.md b/lua_modules/ftp/README.md index e183d56d55..8375a7e394 100644 --- a/lua_modules/ftp/README.md +++ b/lua_modules/ftp/README.md @@ -1,6 +1,6 @@ # FTPServer Module -This is a Lua module to access the SPIFFS file system via FTP protocol. +This is a Lua module to access the [SPIFFS file system](../../docs/en/spiffs.md) via FTP protocol. FTP server uses only one specified user and password to authenticate clients. All clients need authentication - anonymous access is not supported yet. All files have RW access. From 12fbd5f9a6ca0e89f66119dfb768a1a6051c1c86 Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Fri, 6 Apr 2018 09:43:26 +0400 Subject: [PATCH 09/12] Update ftpserver.lua --- lua_modules/ftp/ftpserver.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua_modules/ftp/ftpserver.lua b/lua_modules/ftp/ftpserver.lua index f2d13b34f6..e964144db4 100644 --- a/lua_modules/ftp/ftpserver.lua +++ b/lua_modules/ftp/ftpserver.lua @@ -67,7 +67,7 @@ ftp_srv:listen(21, function(socket) return c:send("200 S OK\r\n") end if a1 == "PASV" then - local _,ip = socket:getaddr() + local _,ip = c:getaddr() local _,_,i1,i2,i3,i4 = string.find(ip,"(%d+).(%d+).(%d+).(%d+)") return c:send("227 Entering Passive Mode ("..i1..","..i2..","..i3..","..i4..",0,20).\r\n") end @@ -85,7 +85,7 @@ ftp_srv:listen(21, function(socket) return end if a1 == "RETR" then - f = file.open(a2:gsub("%/",""),"r") + local f = file.open(a2:gsub("%/",""),"r") if f == nil then return c:send("550 File "..a2.." not found\r\n") end @@ -94,23 +94,23 @@ ftp_srv:listen(21, function(socket) sd:on("sent", function(cd) b=f:read(1024) if b then - sd:send(b) + cd:send(b) b=nil else - sd:close() + cd:close() f:close() data_fnc = nil c:send("226 Transfer complete.\r\n") end end) local b=f:read(1024) - sd:send(b) + cd:send(b) b=nil end return end if a1 == "STOR" then - f = file.open(a2:gsub("%/",""),"w") + local f = file.open(a2:gsub("%/",""),"w") if f == nil then return c:send("451 Can't open/create "..a2.."\r\n") end From 7628537b251e12145d0acf2d77d5f7a5f5297823 Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Fri, 6 Apr 2018 10:14:56 +0400 Subject: [PATCH 10/12] Update ftpserver.lua Fix "early connect" logic --- lua_modules/ftp/ftpserver.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lua_modules/ftp/ftpserver.lua b/lua_modules/ftp/ftpserver.lua index e964144db4..dc1bf51c04 100644 --- a/lua_modules/ftp/ftpserver.lua +++ b/lua_modules/ftp/ftpserver.lua @@ -3,9 +3,9 @@ local file,net,pairs,print,string,table = file,net,pairs,print,string,table local ftp, ftp_data, ftp_srv do local createServer = function (user, pass) -local data_fnc = nil +local data_fnc, data_sock = nil, nil ftp_data = net.createServer(net.TCP, 180) -ftp_data:listen(20, function (s) if data_fnc then data_fnc(s) end end) +ftp_data:listen(20, function (s) if data_fnc then data_fnc(s) else data_sock = s end end) ftp_srv = net.createServer(net.TCP, 180) ftp_srv:listen(21, function(socket) local s = 0 @@ -82,6 +82,9 @@ ftp_srv:listen(21, function(socket) data_fnc = nil c:send("226 Transfer complete.\r\n") end + if data_sock then + node.task.post(function() data_fnc(data_sock);data_sock=nil end) + end return end if a1 == "RETR" then @@ -104,9 +107,12 @@ ftp_srv:listen(21, function(socket) end end) local b=f:read(1024) - cd:send(b) + sd:send(b) b=nil end + if data_sock then + node.task.post(function() data_fnc(data_sock);data_sock=nil end) + end return end if a1 == "STOR" then @@ -125,6 +131,9 @@ ftp_srv:listen(21, function(socket) end) c:send("226 Transfer complete.\r\n") end + if data_sock then + node.task.post(function() data_fnc(data_sock);data_sock=nil end) + end return end if a1 == "RNFR" then From 22b17360ed48260b04436c5884473703b1f5c576 Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Sun, 8 Apr 2018 18:29:07 +0300 Subject: [PATCH 11/12] Update ftpserver.lua Add SIZE and SYST commands, Fix "cwd" into file. Next stage will be check with file.stat() Test with "Chrome" and "ES Explorer" --- lua_modules/ftp/ftpserver.lua | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lua_modules/ftp/ftpserver.lua b/lua_modules/ftp/ftpserver.lua index dc1bf51c04..86d5e1b681 100644 --- a/lua_modules/ftp/ftpserver.lua +++ b/lua_modules/ftp/ftpserver.lua @@ -5,7 +5,7 @@ do local createServer = function (user, pass) local data_fnc, data_sock = nil, nil ftp_data = net.createServer(net.TCP, 180) -ftp_data:listen(20, function (s) if data_fnc then data_fnc(s) else data_sock = s end end) +ftp_data:listen(1024, function (s) if data_fnc then data_fnc(s) else data_sock = s end end) ftp_srv = net.createServer(net.TCP, 180) ftp_srv:listen(21, function(socket) local s = 0 @@ -18,6 +18,7 @@ ftp_srv:listen(21, function(socket) table.insert(a,i) end local a1,a2 = unpack(a) + print(d) if a1 == nil or a1 == "" then return end if s == 0 and a1 == "USER" then if a2 ~= user then @@ -36,6 +37,9 @@ ftp_srv:listen(21, function(socket) if s ~= 2 then return c:send("530 Not logged in, authorization required\r\n") end + if a1 == "SYST" then + return c:send("250 UNIX Type: L8\r\n") + end if a1 == "CDUP" then return c:send("250 OK. Current directory is "..cwd.."\r\n") end @@ -43,8 +47,11 @@ ftp_srv:listen(21, function(socket) if a2 == "." then return c:send("257 \""..cwd.."\" is your current directory\r\n") end - cwd = a2 - return c:send("250 OK. Current directory is "..cwd.."\r\n") + if a2 == "/" then + cwd = a2 + return c:send("250 OK. Current directory is "..cwd.."\r\n") + end + return c:send("550 Failed to change directory.\r\n") end if a1 == "PWD" then return c:send("257 \""..cwd.."\" is your current directory\r\n") @@ -69,14 +76,14 @@ ftp_srv:listen(21, function(socket) if a1 == "PASV" then local _,ip = c:getaddr() local _,_,i1,i2,i3,i4 = string.find(ip,"(%d+).(%d+).(%d+).(%d+)") - return c:send("227 Entering Passive Mode ("..i1..","..i2..","..i3..","..i4..",0,20).\r\n") + return c:send("227 Entering Passive Mode ("..i1..","..i2..","..i3..","..i4..",4,0).\r\n") end if a1 == "LIST" or a1 == "NLST" then c:send("150 Accepted data connection\r\n") data_fnc = function(sd) local l = file.list(); for k,v in pairs(l) do - sd:send( a1 == "NLST" and k.."\r\n" or "+r,s"..v..",\t"..k.."\r\n") + sd:send( a1 == "NLST" and k.."\r\n" or "-rwxrwxrwx 1 esp esp "..v.." Jan 1 2018 "..k.."\r\n") end sd:close() data_fnc = nil @@ -152,6 +159,10 @@ ftp_srv:listen(21, function(socket) file.remove(a2:gsub("%/","")) return c:send("250 Deleted "..a2.."\r\n") end + if a1 == "SIZE" then + local f = file.stat(a2) + return c:send("213 "..(f and f.size or 1).."\r\n") + end if a1 == "NOOP" then return c:send("200 OK\r\n") end From 3ef850d76dbdc6269cf3ff51a2968441c623bf1b Mon Sep 17 00:00:00 2001 From: NeiroNx Date: Sun, 8 Apr 2018 18:33:36 +0300 Subject: [PATCH 12/12] Update ftpserver.lua remove debug print --- lua_modules/ftp/ftpserver.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua_modules/ftp/ftpserver.lua b/lua_modules/ftp/ftpserver.lua index 86d5e1b681..db22a1d385 100644 --- a/lua_modules/ftp/ftpserver.lua +++ b/lua_modules/ftp/ftpserver.lua @@ -18,7 +18,6 @@ ftp_srv:listen(21, function(socket) table.insert(a,i) end local a1,a2 = unpack(a) - print(d) if a1 == nil or a1 == "" then return end if s == 0 and a1 == "USER" then if a2 ~= user then