| 1 |
local base = _G |
| 2 |
local table = require("table") |
| 3 |
local string = require("string") |
| 4 |
local math = require("math") |
| 5 |
local socket = socket or require("socket") |
| 6 |
local url = socket.url or require("socket.url") |
| 7 |
local tp = socket.tp or require("socket.tp") |
| 8 |
local ltn12 = ltn12 or require("ltn12") |
| 9 |
socket.ftp = {} |
| 10 |
local _M = socket.ftp |
| 11 |
module("socket.ftp") |
| 12 |
_M.TIMEOUT = 60 |
| 13 |
local PORT = 21 |
| 14 |
_M.USER = "ftp" |
| 15 |
_M.PASSWORD = "[email protected]" |
| 16 |
local metat = { __index = {} } |
| 17 |
function _M.open(server, port, create) |
| 18 |
local tp = socket.try(tp.connect(server, port or PORT, _M.TIMEOUT, create)) |
| 19 |
local f = base.setmetatable({ tp = tp }, metat) |
| 20 |
|
| 21 |
f.try = socket.newtry(function() f:close() end) |
| 22 |
return f |
| 23 |
end |
| 24 |
function metat.__index:portconnect() |
| 25 |
self.try(self.server:settimeout(_M.TIMEOUT)) |
| 26 |
self.data = self.try(self.server:accept()) |
| 27 |
self.try(self.data:settimeout(_M.TIMEOUT)) |
| 28 |
end |
| 29 |
function metat.__index:pasvconnect() |
| 30 |
self.data = self.try(socket.tcp()) |
| 31 |
self.try(self.data:settimeout(_M.TIMEOUT)) |
| 32 |
self.try(self.data:connect(self.pasvt.address, self.pasvt.port)) |
| 33 |
end |
| 34 |
function metat.__index:login(user, password) |
| 35 |
self.try(self.tp:command("user", user or _M.USER)) |
| 36 |
local code, reply = self.try(self.tp:check{"2..", 331}) |
| 37 |
if code == 331 then |
| 38 |
self.try(self.tp:command("pass", password or _M.PASSWORD)) |
| 39 |
self.try(self.tp:check("2..")) |
| 40 |
end |
| 41 |
return 1 |
| 42 |
end |
| 43 |
function metat.__index:pasv() |
| 44 |
self.try(self.tp:command("pasv")) |
| 45 |
local code, reply = self.try(self.tp:check("2..")) |
| 46 |
local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)" |
| 47 |
local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern)) |
| 48 |
self.try(a and b and c and d and p1 and p2, reply) |
| 49 |
self.pasvt = { |
| 50 |
address = string.format("%d.%d.%d.%d", a, b, c, d), |
| 51 |
port = p1*256 + p2 |
| 52 |
} |
| 53 |
if self.server then |
| 54 |
self.server:close() |
| 55 |
self.server = nil |
| 56 |
end |
| 57 |
return self.pasvt.address, self.pasvt.port |
| 58 |
end |
| 59 |
function metat.__index:epsv() |
| 60 |
self.try(self.tp:command("epsv")) |
| 61 |
local code, reply = self.try(self.tp:check("229")) |
| 62 |
local pattern = "%((.)(.-)%1(.-)%1(.-)%1%)" |
| 63 |
local d, prt, address, port = string.match(reply, pattern) |
| 64 |
self.try(port, "invalid epsv response") |
| 65 |
self.pasvt = { |
| 66 |
address = self.tp:getpeername(), |
| 67 |
port = port |
| 68 |
} |
| 69 |
if self.server then |
| 70 |
self.server:close() |
| 71 |
self.server = nil |
| 72 |
end |
| 73 |
return self.pasvt.address, self.pasvt.port |
| 74 |
end |
| 75 |
function metat.__index:port(address, port) |
| 76 |
self.pasvt = nil |
| 77 |
if not address then |
| 78 |
address, port = self.try(self.tp:getsockname()) |
| 79 |
self.server = self.try(socket.bind(address, 0)) |
| 80 |
address, port = self.try(self.server:getsockname()) |
| 81 |
self.try(self.server:settimeout(_M.TIMEOUT)) |
| 82 |
end |
| 83 |
local pl = math.mod(port, 256) |
| 84 |
local ph = (port - pl)/256 |
| 85 |
local arg = string.gsub(string.format("%s,%d,%d", address, ph, pl), "%.", ",") |
| 86 |
self.try(self.tp:command("port", arg)) |
| 87 |
self.try(self.tp:check("2..")) |
| 88 |
return 1 |
| 89 |
end |
| 90 |
function metat.__index:eprt(family, address, port) |
| 91 |
self.pasvt = nil |
| 92 |
if not address then |
| 93 |
address, port = self.try(self.tp:getsockname()) |
| 94 |
self.server = self.try(socket.bind(address, 0)) |
| 95 |
address, port = self.try(self.server:getsockname()) |
| 96 |
self.try(self.server:settimeout(_M.TIMEOUT)) |
| 97 |
end |
| 98 |
local arg = string.format("|%s|%s|%d|", family, address, port) |
| 99 |
self.try(self.tp:command("eprt", arg)) |
| 100 |
self.try(self.tp:check("2..")) |
| 101 |
return 1 |
| 102 |
end |
| 103 |
function metat.__index:send(sendt) |
| 104 |
self.try(self.pasvt or self.server, "need port or pasv first") |
| 105 |
if self.pasvt then self:pasvconnect() end |
| 106 |
local argument = sendt.argument or |
| 107 |
url.unescape(string.gsub(sendt.path or "", "^[/\\]", "")) |
| 108 |
if argument == "" then argument = nil end |
| 109 |
local command = sendt.command or "stor" |
| 110 |
self.try(self.tp:command(command, argument)) |
| 111 |
local code, reply = self.try(self.tp:check{"2..", "1.."}) |
| 112 |
if not self.pasvt then self:portconnect() end |
| 113 |
local step = sendt.step or ltn12.pump.step |
| 114 |
local readt = { self.tp } |
| 115 |
local checkstep = function(src, snk) |
| 116 |
local readyt = socket.select(readt, nil, 0) |
| 117 |
if readyt[tp] then code = self.try(self.tp:check("2..")) end |
| 118 |
return step(src, snk) |
| 119 |
end |
| 120 |
local sink = socket.sink("close-when-done", self.data) |
| 121 |
self.try(ltn12.pump.all(sendt.source, sink, checkstep)) |
| 122 |
if string.find(code, "1..") then self.try(self.tp:check("2..")) end |
| 123 |
self.data:close() |
| 124 |
local sent = socket.skip(1, self.data:getstats()) |
| 125 |
self.data = nil |
| 126 |
return sent |
| 127 |
end |
| 128 |
function metat.__index:receive(recvt) |
| 129 |
self.try(self.pasvt or self.server, "need port or pasv first") |
| 130 |
if self.pasvt then self:pasvconnect() end |
| 131 |
local argument = recvt.argument or |
| 132 |
url.unescape(string.gsub(recvt.path or "", "^[/\\]", "")) |
| 133 |
if argument == "" then argument = nil end |
| 134 |
local command = recvt.command or "retr" |
| 135 |
self.try(self.tp:command(command, argument)) |
| 136 |
local code,reply = self.try(self.tp:check{"1..", "2.."}) |
| 137 |
if (code >= 200) and (code <= 299) then |
| 138 |
recvt.sink(reply) |
| 139 |
return 1 |
| 140 |
end |
| 141 |
if not self.pasvt then self:portconnect() end |
| 142 |
local source = socket.source("until-closed", self.data) |
| 143 |
local step = recvt.step or ltn12.pump.step |
| 144 |
self.try(ltn12.pump.all(source, recvt.sink, step)) |
| 145 |
if string.find(code, "1..") then self.try(self.tp:check("2..")) end |
| 146 |
self.data:close() |
| 147 |
self.data = nil |
| 148 |
return 1 |
| 149 |
end |
| 150 |
function metat.__index:cwd(dir) |
| 151 |
self.try(self.tp:command("cwd", dir)) |
| 152 |
self.try(self.tp:check(250)) |
| 153 |
return 1 |
| 154 |
end |
| 155 |
function metat.__index:type(type) |
| 156 |
self.try(self.tp:command("type", type)) |
| 157 |
self.try(self.tp:check(200)) |
| 158 |
return 1 |
| 159 |
end |
| 160 |
function metat.__index:greet() |
| 161 |
local code = self.try(self.tp:check{"1..", "2.."}) |
| 162 |
if string.find(code, "1..") then self.try(self.tp:check("2..")) end |
| 163 |
return 1 |
| 164 |
end |
| 165 |
function metat.__index:quit() |
| 166 |
self.try(self.tp:command("quit")) |
| 167 |
self.try(self.tp:check("2..")) |
| 168 |
return 1 |
| 169 |
end |
| 170 |
function metat.__index:close() |
| 171 |
if self.data then self.data:close() end |
| 172 |
if self.server then self.server:close() end |
| 173 |
return self.tp:close() |
| 174 |
end |
| 175 |
local function override(t) |
| 176 |
if t.url then |
| 177 |
local u = url.parse(t.url) |
| 178 |
for i,v in base.pairs(t) do |
| 179 |
u[i] = v |
| 180 |
end |
| 181 |
return u |
| 182 |
else return t end |
| 183 |
end |
| 184 |
local function tput(putt) |
| 185 |
putt = override(putt) |
| 186 |
socket.try(putt.host, "missing hostname") |
| 187 |
local f = _M.open(putt.host, putt.port, putt.create) |
| 188 |
f:greet() |
| 189 |
f:login(putt.user, putt.password) |
| 190 |
if putt.type then f:type(putt.type) end |
| 191 |
f:epsv() |
| 192 |
local sent = f:send(putt) |
| 193 |
f:quit() |
| 194 |
f:close() |
| 195 |
return sent |
| 196 |
end |
| 197 |
local default = { |
| 198 |
path = "/", |
| 199 |
scheme = "ftp" |
| 200 |
} |
| 201 |
local function genericform(u) |
| 202 |
local t = socket.try(url.parse(u, default)) |
| 203 |
socket.try(t.scheme == "ftp", "wrong scheme '" .. t.scheme .. "'") |
| 204 |
socket.try(t.host, "missing hostname") |
| 205 |
local pat = "^type=(.)$" |
| 206 |
if t.params then |
| 207 |
t.type = socket.skip(2, string.find(t.params, pat)) |
| 208 |
socket.try(t.type == "a" or t.type == "i", |
| 209 |
"invalid type '" .. t.type .. "'") |
| 210 |
end |
| 211 |
return t |
| 212 |
end |
| 213 |
_M.genericform = genericform |
| 214 |
local function sput(u, body) |
| 215 |
local putt = genericform(u) |
| 216 |
putt.source = ltn12.source.string(body) |
| 217 |
return tput(putt) |
| 218 |
end |
| 219 |
_M.put = socket.protect(function(putt, body) |
| 220 |
if base.type(putt) == "string" then return sput(putt, body) |
| 221 |
else return tput(putt) end |
| 222 |
end) |
| 223 |
local function tget(gett) |
| 224 |
gett = override(gett) |
| 225 |
socket.try(gett.host, "missing hostname") |
| 226 |
local f = _M.open(gett.host, gett.port, gett.create) |
| 227 |
f:greet() |
| 228 |
f:login(gett.user, gett.password) |
| 229 |
if gett.type then f:type(gett.type) end |
| 230 |
f:epsv() |
| 231 |
f:receive(gett) |
| 232 |
f:quit() |
| 233 |
return f:close() |
| 234 |
end |
| 235 |
local function sget(u) |
| 236 |
local gett = genericform(u) |
| 237 |
local t = {} |
| 238 |
gett.sink = ltn12.sink.table(t) |
| 239 |
tget(gett) |
| 240 |
return table.concat(t) |
| 241 |
end |
| 242 |
_M.command = socket.protect(function(cmdt) |
| 243 |
cmdt = override(cmdt) |
| 244 |
socket.try(cmdt.host, "missing hostname") |
| 245 |
socket.try(cmdt.command, "missing command") |
| 246 |
local f = _M.open(cmdt.host, cmdt.port, cmdt.create) |
| 247 |
f:greet() |
| 248 |
f:login(cmdt.user, cmdt.password) |
| 249 |
if type(cmdt.command) == "table" then |
| 250 |
local argument = cmdt.argument or {} |
| 251 |
local check = cmdt.check or {} |
| 252 |
for i,cmd in ipairs(cmdt.command) do |
| 253 |
f.try(f.tp:command(cmd, argument[i])) |
| 254 |
if check[i] then f.try(f.tp:check(check[i])) end |
| 255 |
end |
| 256 |
else |
| 257 |
f.try(f.tp:command(cmdt.command, cmdt.argument)) |
| 258 |
if cmdt.check then f.try(f.tp:check(cmdt.check)) end |
| 259 |
end |
| 260 |
f:quit() |
| 261 |
return f:close() |
| 262 |
end) |
| 263 |
_M.get = socket.protect(function(gett) |
| 264 |
if base.type(gett) == "string" then return sget(gett) |
| 265 |
else return tget(gett) end |
| 266 |
end) |
| 267 |
socket.ftp = _M |
| 268 |
return _M |