2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-12 20:27:55 +02:00
|
|
|
-- FTP support for the Lua language
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket toolkit.
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Author: Diego Nehab
|
|
|
|
-----------------------------------------------------------------------------
|
2004-06-04 17:15:45 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-09-27 06:01:18 +02:00
|
|
|
-- Declare module and import dependencies
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2005-06-14 06:29:23 +02:00
|
|
|
local base = _G
|
2004-11-27 08:58:04 +01:00
|
|
|
local table = require("table")
|
|
|
|
local string = require("string")
|
|
|
|
local math = require("math")
|
2004-06-04 17:15:45 +02:00
|
|
|
local socket = require("socket")
|
2004-09-27 06:01:18 +02:00
|
|
|
local url = require("socket.url")
|
|
|
|
local tp = require("socket.tp")
|
2004-06-04 17:15:45 +02:00
|
|
|
local ltn12 = require("ltn12")
|
2013-05-27 10:45:09 +02:00
|
|
|
socket.ftp = {}
|
|
|
|
local _M = socket.ftp
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- timeout in seconds before the program gives up on a connection
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.TIMEOUT = 60
|
2000-12-29 23:15:09 +01:00
|
|
|
-- default port for ftp service
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.PORT = 21
|
2000-12-29 23:15:09 +01:00
|
|
|
-- this is the default anonymous password. used when no password is
|
2001-09-12 20:27:55 +02:00
|
|
|
-- provided in url. should be changed to your e-mail.
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.USER = "ftp"
|
|
|
|
_M.PASSWORD = "anonymous@anonymous.org"
|
2000-12-29 23:15:09 +01:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-05-25 07:27:44 +02:00
|
|
|
-- Low level FTP API
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-05-25 07:27:44 +02:00
|
|
|
local metat = { __index = {} }
|
2000-12-29 23:15:09 +01:00
|
|
|
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.open(server, port, create)
|
|
|
|
local tp = socket.try(tp.connect(server, port or _M.PORT, _M.TIMEOUT, create))
|
2004-11-27 08:58:04 +01:00
|
|
|
local f = base.setmetatable({ tp = tp }, metat)
|
2004-06-18 10:02:09 +02:00
|
|
|
-- make sure everything gets closed in an exception
|
2004-06-18 23:41:44 +02:00
|
|
|
f.try = socket.newtry(function() f:close() end)
|
2005-11-22 09:33:29 +01:00
|
|
|
return f
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-06-18 10:02:09 +02:00
|
|
|
function metat.__index:portconnect()
|
2013-05-27 10:45:09 +02:00
|
|
|
self.try(self.server:settimeout(_M.TIMEOUT))
|
2004-06-18 10:02:09 +02:00
|
|
|
self.data = self.try(self.server:accept())
|
2013-05-27 10:45:09 +02:00
|
|
|
self.try(self.data:settimeout(_M.TIMEOUT))
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
2004-06-18 10:02:09 +02:00
|
|
|
function metat.__index:pasvconnect()
|
|
|
|
self.data = self.try(socket.tcp())
|
2013-05-27 10:45:09 +02:00
|
|
|
self.try(self.data:settimeout(_M.TIMEOUT))
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.data:connect(self.pasvt.ip, self.pasvt.port))
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
function metat.__index:login(user, password)
|
2013-05-27 10:45:09 +02:00
|
|
|
self.try(self.tp:command("user", user or _M.USER))
|
2004-06-18 10:02:09 +02:00
|
|
|
local code, reply = self.try(self.tp:check{"2..", 331})
|
2004-05-25 07:27:44 +02:00
|
|
|
if code == 331 then
|
2013-05-27 10:45:09 +02:00
|
|
|
self.try(self.tp:command("pass", password or _M.PASSWORD))
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:check("2.."))
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:pasv()
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("pasv"))
|
|
|
|
local code, reply = self.try(self.tp:check("2.."))
|
2004-05-28 08:16:43 +02:00
|
|
|
local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)"
|
|
|
|
local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern))
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(a and b and c and d and p1 and p2, reply)
|
2005-11-22 09:33:29 +01:00
|
|
|
self.pasvt = {
|
|
|
|
ip = string.format("%d.%d.%d.%d", a, b, c, d),
|
2004-05-25 07:27:44 +02:00
|
|
|
port = p1*256 + p2
|
|
|
|
}
|
2005-11-22 09:33:29 +01:00
|
|
|
if self.server then
|
2004-06-18 10:02:09 +02:00
|
|
|
self.server:close()
|
|
|
|
self.server = nil
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
2005-11-22 09:33:29 +01:00
|
|
|
return self.pasvt.ip, self.pasvt.port
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
function metat.__index:port(ip, port)
|
|
|
|
self.pasvt = nil
|
2005-11-22 09:33:29 +01:00
|
|
|
if not ip then
|
2004-06-18 10:02:09 +02:00
|
|
|
ip, port = self.try(self.tp:getcontrol():getsockname())
|
|
|
|
self.server = self.try(socket.bind(ip, 0))
|
|
|
|
ip, port = self.try(self.server:getsockname())
|
2013-05-27 10:45:09 +02:00
|
|
|
self.try(self.server:settimeout(_M.TIMEOUT))
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
local pl = math.mod(port, 256)
|
|
|
|
local ph = (port - pl)/256
|
|
|
|
local arg = string.gsub(string.format("%s,%d,%d", ip, ph, pl), "%.", ",")
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("port", arg))
|
|
|
|
self.try(self.tp:check("2.."))
|
2004-05-25 07:27:44 +02:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:send(sendt)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.pasvt or self.server, "need port or pasv first")
|
2005-11-22 09:33:29 +01:00
|
|
|
-- if there is a pasvt table, we already sent a PASV command
|
2004-06-18 10:02:09 +02:00
|
|
|
-- we just get the data connection into self.data
|
|
|
|
if self.pasvt then self:pasvconnect() end
|
2005-11-22 09:33:29 +01:00
|
|
|
-- get the transfer argument and command
|
|
|
|
local argument = sendt.argument or
|
2005-04-21 07:38:07 +02:00
|
|
|
url.unescape(string.gsub(sendt.path or "", "^[/\\]", ""))
|
2004-06-15 08:24:00 +02:00
|
|
|
if argument == "" then argument = nil end
|
2004-06-18 10:02:09 +02:00
|
|
|
local command = sendt.command or "stor"
|
|
|
|
-- send the transfer command and check the reply
|
|
|
|
self.try(self.tp:command(command, argument))
|
|
|
|
local code, reply = self.try(self.tp:check{"2..", "1.."})
|
|
|
|
-- if there is not a a pasvt table, then there is a server
|
|
|
|
-- and we already sent a PORT command
|
|
|
|
if not self.pasvt then self:portconnect() end
|
|
|
|
-- get the sink, source and step for the transfer
|
2004-05-25 07:27:44 +02:00
|
|
|
local step = sendt.step or ltn12.pump.step
|
2006-03-13 08:16:39 +01:00
|
|
|
local readt = {self.tp.c}
|
2004-05-25 07:27:44 +02:00
|
|
|
local checkstep = function(src, snk)
|
2004-06-18 10:02:09 +02:00
|
|
|
-- check status in control connection while downloading
|
2004-05-25 07:27:44 +02:00
|
|
|
local readyt = socket.select(readt, nil, 0)
|
2006-03-13 08:16:39 +01:00
|
|
|
if readyt[tp] then code = self.try(self.tp:check("2..")) end
|
2004-06-18 10:02:09 +02:00
|
|
|
return step(src, snk)
|
2004-03-16 07:42:53 +01:00
|
|
|
end
|
2004-06-18 10:02:09 +02:00
|
|
|
local sink = socket.sink("close-when-done", self.data)
|
|
|
|
-- transfer all data and check error
|
|
|
|
self.try(ltn12.pump.all(sendt.source, sink, checkstep))
|
|
|
|
if string.find(code, "1..") then self.try(self.tp:check("2..")) end
|
|
|
|
-- done with data connection
|
|
|
|
self.data:close()
|
2004-10-13 00:35:20 +02:00
|
|
|
-- find out how many bytes were sent
|
|
|
|
local sent = socket.skip(1, self.data:getstats())
|
2004-06-18 10:02:09 +02:00
|
|
|
self.data = nil
|
2004-10-13 00:35:20 +02:00
|
|
|
return sent
|
2004-05-25 07:27:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:receive(recvt)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.pasvt or self.server, "need port or pasv first")
|
|
|
|
if self.pasvt then self:pasvconnect() end
|
2005-11-22 09:33:29 +01:00
|
|
|
local argument = recvt.argument or
|
2005-04-21 07:38:07 +02:00
|
|
|
url.unescape(string.gsub(recvt.path or "", "^[/\\]", ""))
|
2004-06-15 08:24:00 +02:00
|
|
|
if argument == "" then argument = nil end
|
2004-06-18 10:02:09 +02:00
|
|
|
local command = recvt.command or "retr"
|
|
|
|
self.try(self.tp:command(command, argument))
|
2013-05-30 09:01:07 +02:00
|
|
|
local code,reply = self.try(self.tp:check{"1..", "2.."})
|
|
|
|
if (code >= 200) and (code <= 299) then
|
|
|
|
recvt.sink(reply)
|
|
|
|
return 1
|
|
|
|
end
|
2004-06-18 10:02:09 +02:00
|
|
|
if not self.pasvt then self:portconnect() end
|
|
|
|
local source = socket.source("until-closed", self.data)
|
2004-05-25 07:27:44 +02:00
|
|
|
local step = recvt.step or ltn12.pump.step
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(ltn12.pump.all(source, recvt.sink, step))
|
|
|
|
if string.find(code, "1..") then self.try(self.tp:check("2..")) end
|
|
|
|
self.data:close()
|
|
|
|
self.data = nil
|
2004-05-25 07:27:44 +02:00
|
|
|
return 1
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
function metat.__index:cwd(dir)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("cwd", dir))
|
|
|
|
self.try(self.tp:check(250))
|
2004-05-25 07:27:44 +02:00
|
|
|
return 1
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
function metat.__index:type(type)
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("type", type))
|
|
|
|
self.try(self.tp:check(200))
|
2004-05-25 07:27:44 +02:00
|
|
|
return 1
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
function metat.__index:greet()
|
2004-06-18 10:02:09 +02:00
|
|
|
local code = self.try(self.tp:check{"1..", "2.."})
|
|
|
|
if string.find(code, "1..") then self.try(self.tp:check("2..")) end
|
2004-05-25 07:27:44 +02:00
|
|
|
return 1
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
function metat.__index:quit()
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.tp:command("quit"))
|
|
|
|
self.try(self.tp:check("2.."))
|
2004-05-25 07:27:44 +02:00
|
|
|
return 1
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
2004-05-25 07:27:44 +02:00
|
|
|
function metat.__index:close()
|
2004-06-18 10:02:09 +02:00
|
|
|
if self.data then self.data:close() end
|
|
|
|
if self.server then self.server:close() end
|
2004-06-18 23:41:44 +02:00
|
|
|
return self.tp:close()
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-05-25 07:27:44 +02:00
|
|
|
-- High level FTP API
|
2001-09-12 20:27:55 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2007-07-11 21:25:47 +02:00
|
|
|
local function override(t)
|
2004-11-27 08:58:04 +01:00
|
|
|
if t.url then
|
2007-07-11 21:25:47 +02:00
|
|
|
local u = url.parse(t.url)
|
2004-11-27 08:58:04 +01:00
|
|
|
for i,v in base.pairs(t) do
|
|
|
|
u[i] = v
|
|
|
|
end
|
|
|
|
return u
|
|
|
|
else return t end
|
|
|
|
end
|
|
|
|
|
2004-05-28 08:16:43 +02:00
|
|
|
local function tput(putt)
|
2004-11-27 08:58:04 +01:00
|
|
|
putt = override(putt)
|
|
|
|
socket.try(putt.host, "missing hostname")
|
2013-05-27 10:45:09 +02:00
|
|
|
local f = _M.open(putt.host, putt.port, putt.create)
|
2004-06-18 10:02:09 +02:00
|
|
|
f:greet()
|
|
|
|
f:login(putt.user, putt.password)
|
|
|
|
if putt.type then f:type(putt.type) end
|
|
|
|
f:pasv()
|
2004-10-13 00:35:20 +02:00
|
|
|
local sent = f:send(putt)
|
2004-06-18 10:02:09 +02:00
|
|
|
f:quit()
|
2004-10-13 00:35:20 +02:00
|
|
|
f:close()
|
|
|
|
return sent
|
2004-05-28 08:16:43 +02:00
|
|
|
end
|
2001-09-12 20:27:55 +02:00
|
|
|
|
2004-05-28 08:16:43 +02:00
|
|
|
local default = {
|
2011-05-25 22:57:22 +02:00
|
|
|
path = "/",
|
|
|
|
scheme = "ftp"
|
2004-05-28 08:16:43 +02:00
|
|
|
}
|
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
local function parse(u)
|
2004-06-15 08:24:00 +02:00
|
|
|
local t = socket.try(url.parse(u, default))
|
2004-11-27 08:58:04 +01:00
|
|
|
socket.try(t.scheme == "ftp", "wrong scheme '" .. t.scheme .. "'")
|
|
|
|
socket.try(t.host, "missing hostname")
|
2004-05-28 08:16:43 +02:00
|
|
|
local pat = "^type=(.)$"
|
2005-11-22 09:33:29 +01:00
|
|
|
if t.params then
|
2004-06-15 08:24:00 +02:00
|
|
|
t.type = socket.skip(2, string.find(t.params, pat))
|
|
|
|
socket.try(t.type == "a" or t.type == "i",
|
|
|
|
"invalid type '" .. t.type .. "'")
|
2004-05-28 08:16:43 +02:00
|
|
|
end
|
2004-06-15 08:24:00 +02:00
|
|
|
return t
|
2001-09-12 20:27:55 +02:00
|
|
|
end
|
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
local function sput(u, body)
|
2005-11-22 09:33:29 +01:00
|
|
|
local putt = parse(u)
|
2004-05-28 08:16:43 +02:00
|
|
|
putt.source = ltn12.source.string(body)
|
|
|
|
return tput(putt)
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2004-02-04 15:29:11 +01:00
|
|
|
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.put = socket.protect(function(putt, body)
|
2004-11-27 08:58:04 +01:00
|
|
|
if base.type(putt) == "string" then return sput(putt, body)
|
2004-05-28 08:16:43 +02:00
|
|
|
else return tput(putt) end
|
|
|
|
end)
|
|
|
|
|
|
|
|
local function tget(gett)
|
2004-11-27 08:58:04 +01:00
|
|
|
gett = override(gett)
|
|
|
|
socket.try(gett.host, "missing hostname")
|
2013-05-27 10:45:09 +02:00
|
|
|
local f = _M.open(gett.host, gett.port, gett.create)
|
2004-06-18 10:02:09 +02:00
|
|
|
f:greet()
|
|
|
|
f:login(gett.user, gett.password)
|
|
|
|
if gett.type then f:type(gett.type) end
|
|
|
|
f:pasv()
|
|
|
|
f:receive(gett)
|
|
|
|
f:quit()
|
|
|
|
return f:close()
|
2004-05-28 08:16:43 +02:00
|
|
|
end
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
local function sget(u)
|
2005-11-22 09:33:29 +01:00
|
|
|
local gett = parse(u)
|
2004-05-28 08:16:43 +02:00
|
|
|
local t = {}
|
|
|
|
gett.sink = ltn12.sink.table(t)
|
|
|
|
tget(gett)
|
|
|
|
return table.concat(t)
|
|
|
|
end
|
|
|
|
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.command = socket.protect(function(cmdt)
|
2004-11-27 08:58:04 +01:00
|
|
|
cmdt = override(cmdt)
|
|
|
|
socket.try(cmdt.host, "missing hostname")
|
|
|
|
socket.try(cmdt.command, "missing command")
|
2015-03-01 20:34:53 +01:00
|
|
|
local f = _M.open(cmdt.host, cmdt.port, cmdt.create)
|
2004-11-27 08:58:04 +01:00
|
|
|
f:greet()
|
|
|
|
f:login(cmdt.user, cmdt.password)
|
|
|
|
f.try(f.tp:command(cmdt.command, cmdt.argument))
|
|
|
|
if cmdt.check then f.try(f.tp:check(cmdt.check)) end
|
|
|
|
f:quit()
|
|
|
|
return f:close()
|
|
|
|
end)
|
|
|
|
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.get = socket.protect(function(gett)
|
2004-11-27 08:58:04 +01:00
|
|
|
if base.type(gett) == "string" then return sget(gett)
|
2004-05-28 08:16:43 +02:00
|
|
|
else return tget(gett) end
|
|
|
|
end)
|
2004-11-27 08:58:04 +01:00
|
|
|
|
2013-05-27 10:45:09 +02:00
|
|
|
return _M
|