1
0
mirror of https://github.com/ShadowKatStudios/OC-Minitel.git synced 2024-11-23 10:38:05 +11:00

updated fserv to use threads rather than a coroutine scheduler, added internet proxy support

This commit is contained in:
Izaya 2020-02-19 20:36:23 +11:00
parent 92a740389e
commit 645fcf8270

View File

@ -3,6 +3,8 @@ local syslog = require "syslog"
local fs = require "filesystem" local fs = require "filesystem"
local event = require "event" local event = require "event"
local serial = require "serialization" local serial = require "serialization"
local thread = require "thread"
local internet = require "internet"
local coro = {} -- table of coroutines, one per socket local coro = {} -- table of coroutines, one per socket
local cfg = {} local cfg = {}
@ -12,6 +14,7 @@ local isRunning = false -- simple lock
cfg.path = "/srv" -- default config cfg.path = "/srv" -- default config
cfg.port = 70 cfg.port = 70
cfg.looptimer = 0.5 cfg.looptimer = 0.5
cfg.iproxy = false
local function log(msg,level) local function log(msg,level)
syslog(msg,level,"frequestd") syslog(msg,level,"frequestd")
@ -39,7 +42,7 @@ local function writeConfig()
end end
local function handleSocket(sock) -- create a coroutine for a new socket local function handleSocket(sock) -- create a coroutine for a new socket
coro[#coro+1] = coroutine.create(function() coro[#coro+1] = thread.create(function()
local line local line
repeat repeat
coroutine.yield() coroutine.yield()
@ -51,8 +54,26 @@ local function handleSocket(sock) -- create a coroutine for a new socket
sock:close() sock:close()
return false return false
end end
path = fs.canonical(cfg.path.."/"..fs.canonical(path))
sock.cname = sock.addr..":"..tostring(sock.port) sock.cname = sock.addr..":"..tostring(sock.port)
if path:match("^/?(http[s]?)/") and cfg.iproxy then
local scheme, rpath = path:match("^/?(http[s]?)/(.+)")
log("["..sock.cname.."] "..scheme.."://"..rpath,6)
local r = internet.request(scheme.."://"..rpath)
if r then
log("["..sock.cname.."] Transferring remote file.",7)
sock:write("y")
for s in r do
coroutine.yield()
sock:write(s)
end
else
log("["..sock.cname.."] Unable to open remote file.",7)
sock:write("fUnable to open remote file.")
end
sock:close()
return
end
path = fs.canonical(cfg.path.."/"..fs.canonical(path))
log("["..sock.cname.."] "..ttype.." "..path,6) log("["..sock.cname.."] "..ttype.." "..path,6)
if ttype == "t" then -- transfer request if ttype == "t" then -- transfer request
if not fs.exists(path) then if not fs.exists(path) then
@ -81,8 +102,8 @@ local function handleSocket(sock) -- create a coroutine for a new socket
log("["..sock.cname.."] Transferring file.",7) log("["..sock.cname.."] Transferring file.",7)
local chunk = f:read(net.mtu) local chunk = f:read(net.mtu)
repeat repeat
sock:write(chunk)
coroutine.yield() coroutine.yield()
sock:write(chunk)
chunk = f:read(net.mtu) chunk = f:read(net.mtu)
until not chunk until not chunk
sock:close() sock:close()
@ -107,30 +128,15 @@ local function handleSocket(sock) -- create a coroutine for a new socket
log("New connection: "..sock.addr..":"..tostring(sock.port),7) log("New connection: "..sock.addr..":"..tostring(sock.port),7)
end end
local function sched() -- run coroutines
if isRunning then return end
isRunning = true
for i = #coro, 1, -1 do -- prune dead coroutines
if coroutine.status(coro[i]) == "dead" then
table.remove(coro,i)
end
end
for k,v in pairs(coro) do
coroutine.resume(v)
end
isRunning = false
end
function start() function start()
if timer or listener then return end if timer or listener then return end
loadConfig() loadConfig()
writeConfig() writeConfig()
timer = event.timer(cfg.looptimer, sched, math.huge)
listener = net.flisten(cfg.port, handleSocket) listener = net.flisten(cfg.port, handleSocket)
end end
function stop() function stop()
if not timer or not listener then return end if not timer or not listener then return end
event.cancel(timer) thread.waitForAll(coro)
event.ignore("net_msg",listener) event.ignore("net_msg",listener)
end end
function restart() function restart()