2018-08-11 13:59:44 +10:00
|
|
|
local net = require "minitel"
|
2018-02-19 21:04:22 +11:00
|
|
|
local event = require "event"
|
2018-07-28 05:19:58 +10:00
|
|
|
local syslog = require "syslog"
|
|
|
|
local fs = require "filesystem"
|
|
|
|
local computer = require "computer"
|
|
|
|
local serial = require "serialization"
|
|
|
|
|
|
|
|
local sockets = {}
|
|
|
|
local cfg = {}
|
|
|
|
cfg.path = "/srv"
|
|
|
|
cfg.looptime = 0.5
|
|
|
|
local timer = false
|
2018-02-19 21:04:22 +11:00
|
|
|
|
2018-07-28 05:19:58 +10:00
|
|
|
local function log(msg,level)
|
|
|
|
syslog(msg,level,"frequestd")
|
|
|
|
end
|
2018-02-19 21:04:22 +11:00
|
|
|
|
2018-07-28 05:19:58 +10:00
|
|
|
local function loadConfig()
|
|
|
|
local fobj = io.open("/etc/fserv.cfg","rb")
|
|
|
|
if fobj then
|
2018-07-28 05:26:19 +10:00
|
|
|
cfg = serial.unserialize(fobj:read("*a")) or cfg
|
2018-07-28 05:19:58 +10:00
|
|
|
fobj:close()
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-28 05:19:58 +10:00
|
|
|
local function writeConfig()
|
|
|
|
local fobj = io.open("/etc/fserv.cfg","wb")
|
|
|
|
if fobj then
|
2018-07-28 05:26:19 +10:00
|
|
|
fobj:write(serial.serialize(cfg))
|
2018-07-28 05:19:58 +10:00
|
|
|
fobj:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
overview of how this works:
|
|
|
|
socketHandler listens for connections on port 70, places them into the sockets table
|
|
|
|
socketLoop runs quite regularly - every cfg.looptime or so.
|
|
|
|
It iterates over each socket in the sockets table, and if any of them contain a valid request, it attempts to handle it:
|
|
|
|
- calls getFile to get a status code and file contents if applicable
|
|
|
|
- writes either a failure code and message, the file contents, or the file size
|
|
|
|
Previously this was based on a listener but that caused a race condition
|
|
|
|
|
|
|
|
note to self: consider using the FS library for handling stat requests, and returning an iterator instead of an entire file for transfer requests
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local function getFile(path)
|
|
|
|
path = cfg.path .. path
|
|
|
|
if not fs.exists(path) then
|
|
|
|
log(path.." not found",syslog.notice)
|
|
|
|
return "n",path.." not found"
|
|
|
|
end
|
|
|
|
if fs.isDirectory(path) then
|
|
|
|
local dlist = ""
|
|
|
|
for file in fs.list(path) do
|
|
|
|
dlist = dlist .. file .. "\n"
|
|
|
|
end
|
|
|
|
return "d", dlist
|
|
|
|
end
|
|
|
|
local f = io.open(path,"rb")
|
|
|
|
if not f then
|
|
|
|
log("unable to open "..path,syslog.notice)
|
|
|
|
return "f","unable to open "..path
|
|
|
|
end
|
|
|
|
local content = f:read("*a")
|
|
|
|
f:close()
|
|
|
|
return "y", content
|
|
|
|
end
|
|
|
|
|
|
|
|
local function socketLoop()
|
|
|
|
for sn,socket in pairs(sockets) do
|
|
|
|
local op, path = socket.rbuffer:match("([ts])(.+)\n")
|
|
|
|
if op and path then
|
|
|
|
path = fs.canonical(path)
|
|
|
|
if path:sub(1,1) ~= "/" then
|
|
|
|
path = "/" .. path
|
2018-02-19 22:49:23 +11:00
|
|
|
end
|
2018-07-28 05:19:58 +10:00
|
|
|
log("client "..tostring(socket.addr)..":"..tostring(socket.port).." requested "..path, syslog.debug)
|
|
|
|
if op == "t" and path then
|
|
|
|
local wf, file = getFile(path)
|
|
|
|
socket:write(wf .. file)
|
|
|
|
socket:close()
|
|
|
|
sockets[sn] = nil
|
|
|
|
elseif op == "s" and path then
|
|
|
|
local wf, file = getFile(path)
|
|
|
|
if wf == "y" then
|
|
|
|
socket:write(wf..file:len())
|
2018-02-19 21:04:22 +11:00
|
|
|
else
|
2018-07-28 05:19:58 +10:00
|
|
|
socket:write(wf..file)
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|
2018-07-28 05:19:58 +10:00
|
|
|
socket:close()
|
|
|
|
sockets[sn] = nil
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|
|
|
|
end
|
2018-07-28 05:19:58 +10:00
|
|
|
if computer.uptime() > socket.opened + 60 then
|
|
|
|
socket:close()
|
|
|
|
sockets[sn] = nil
|
|
|
|
log("dropped client "..tostring(socket.addr)..":"..tostring(socket.port).." for inactivity",syslog.debug)
|
|
|
|
elseif socket.state ~= "open" then
|
|
|
|
sockets[sn] = nil
|
|
|
|
log("client "..tostring(socket.addr)..":"..tostring(socket.port).." closed socket",syslog.debug)
|
|
|
|
end
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-28 05:19:58 +10:00
|
|
|
local function socketHandler(socket)
|
|
|
|
log(tostring(socket.addr)..":"..tostring(socket.port).." opened",syslog.debug)
|
|
|
|
socket.opened = computer.uptime()
|
|
|
|
sockets[tostring(socket.addr)..":"..tostring(socket.port)] = socket
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|
|
|
|
|
2018-07-28 05:19:58 +10:00
|
|
|
function start()
|
|
|
|
loadConfig()
|
|
|
|
writeConfig()
|
|
|
|
net.flisten(70, socketHandler)
|
|
|
|
timer = event.timer(cfg.looptime, socketLoop, math.huge)
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|
2018-07-28 05:19:58 +10:00
|
|
|
|
|
|
|
function stop()
|
|
|
|
event.ignore("net_msg", socketLoop)
|
|
|
|
event.cancel(timer)
|
|
|
|
end
|
|
|
|
|
|
|
|
function set(k,v)
|
2018-07-28 05:52:37 +10:00
|
|
|
if type(cfg[k]) == "string" then
|
2018-07-28 05:19:58 +10:00
|
|
|
cfg[k] = v
|
2018-07-28 05:52:37 +10:00
|
|
|
elseif type(cfg[k]) == "number" then
|
|
|
|
cfg[k] = tonumber(v)
|
|
|
|
elseif type(cfg[k]) == "boolean" then
|
|
|
|
if v:lower():sub(1,1) == "t" then
|
|
|
|
cfg[k] = true
|
|
|
|
else
|
|
|
|
cfg[k] = false
|
|
|
|
end
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|
2018-07-28 05:52:37 +10:00
|
|
|
print("cfg."..k.." = "..tostring(cfg[k]))
|
2018-07-28 05:19:58 +10:00
|
|
|
writeConfig()
|
2018-02-19 21:04:22 +11:00
|
|
|
end
|