Compare commits
2 Commits
2aca6df1b4
...
dc03ad94bc
Author | SHA1 | Date | |
---|---|---|---|
dc03ad94bc | |||
8ca414e312 |
40
exec/fget.lua
Normal file
40
exec/fget.lua
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
local minitel = require "minitel"
|
||||||
|
|
||||||
|
local tA = {...}
|
||||||
|
|
||||||
|
local function parseURL(url)
|
||||||
|
local proto,addr = url:match("(.-)://(.+)")
|
||||||
|
addr = addr or url
|
||||||
|
local hp, path = addr:match("(.-)(/.*)")
|
||||||
|
hp, path = hp or addr, path or "/"
|
||||||
|
local host, port = hp:match("(.+):(.+)")
|
||||||
|
host = host or hp
|
||||||
|
return proto, host, port, path
|
||||||
|
end
|
||||||
|
|
||||||
|
local proto, host, port, path = parseURL(tA[1])
|
||||||
|
proto,port = proto or "fget", port or 70
|
||||||
|
local fname, rtype = tA[2] or "-", tA[3] or "t"
|
||||||
|
|
||||||
|
local sock = minitel.open(host,port)
|
||||||
|
local f = nil
|
||||||
|
if fname ~= "-" then
|
||||||
|
f = io.open(fname,"w")
|
||||||
|
if not f then error("couldn't open file for writing") end
|
||||||
|
else
|
||||||
|
f = io.open(os.getenv("t"))
|
||||||
|
f.close = function() end
|
||||||
|
end
|
||||||
|
if not sock then error("couldn't open connection to host") end
|
||||||
|
sock:write(string.format("%s%s\n",rtype,path))
|
||||||
|
local rtype, buf = "", ""
|
||||||
|
repeat
|
||||||
|
coroutine.yield()
|
||||||
|
rtype = sock:read(1)
|
||||||
|
until rtype ~= ""
|
||||||
|
repeat
|
||||||
|
coroutine.yield()
|
||||||
|
buf = sock:read("*a")
|
||||||
|
f:write(buf)
|
||||||
|
until sock.state == "closed" and buf == ""
|
||||||
|
f:close()
|
78
service/fserv.lua
Normal file
78
service/fserv.lua
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
local minitel = require "minitel"
|
||||||
|
local serial = require "serialization"
|
||||||
|
|
||||||
|
local cfg = {["path"]="/boot/srv/frequest",["port"]=70}
|
||||||
|
|
||||||
|
f=io.open("/boot/cfg/fserv.cfg","rb")
|
||||||
|
if f then
|
||||||
|
local ncfg = serial.unserialize(f:read("*a"))
|
||||||
|
f:close()
|
||||||
|
for k,v in pairs(ncfg) do
|
||||||
|
cfg[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function fileHandler(socket,rtype,path)
|
||||||
|
syslog(string.format("[%s:%d] %s %s",socket.addr,socket.port,rtype,path),syslog.info,"fserv")
|
||||||
|
if rtype == "t" then
|
||||||
|
if fs.exists(path) and fs.isDirectory(path) then
|
||||||
|
socket:write("d")
|
||||||
|
for _,file in ipairs(fs.list(path)) do
|
||||||
|
socket:write(file.."\n")
|
||||||
|
end
|
||||||
|
elseif fs.exists(path) and not fs.isDirectory(path) then
|
||||||
|
local f,err = io.open(path,"rb")
|
||||||
|
if f then
|
||||||
|
socket:write("y")
|
||||||
|
while true do
|
||||||
|
local c = f:read(4096)
|
||||||
|
if not c or c == "" then break end
|
||||||
|
socket:write(c)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
socket:write("fFailed to open file: "..err)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
socket:write("nFile not found")
|
||||||
|
end
|
||||||
|
elseif rtype == "s" then
|
||||||
|
if fs.exists(path) then
|
||||||
|
local ftype = "f"
|
||||||
|
if fs.isDirectory(path) then
|
||||||
|
ftype = "d"
|
||||||
|
end
|
||||||
|
socket:write(string.format("y%s\n%d",ftype,fs.size(path)))
|
||||||
|
else
|
||||||
|
socket:write("nFile not found.")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
socket:write("fUnknown request type")
|
||||||
|
end
|
||||||
|
socket:close()
|
||||||
|
end
|
||||||
|
local function httpHandler(socket,rtype,path)
|
||||||
|
socket:write("fHTTP requests are not yet implemented.")
|
||||||
|
socket:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function socketHandler(socket)
|
||||||
|
return function()
|
||||||
|
local line = nil
|
||||||
|
repeat
|
||||||
|
coroutine.yield()
|
||||||
|
line = socket:read()
|
||||||
|
until line
|
||||||
|
local rtype, path = line:match("(.)(.+)")
|
||||||
|
if path:sub(1,6) == "/http/" or path:sub(1,5) == "http/" then
|
||||||
|
httpHandler(socket,rtype,path)
|
||||||
|
else
|
||||||
|
path = (cfg.path .. "/" .. path:gsub("../","")):gsub("/+","/")
|
||||||
|
fileHandler(socket,rtype,path)
|
||||||
|
end
|
||||||
|
socket:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
while true do
|
||||||
|
os.spawn(socketHandler(minitel.listen(70)))
|
||||||
|
end
|
@ -78,7 +78,6 @@ end
|
|||||||
local function loadconfig()
|
local function loadconfig()
|
||||||
hostname = os.getenv("HOSTNAME") or computer.address():sub(1,8)
|
hostname = os.getenv("HOSTNAME") or computer.address():sub(1,8)
|
||||||
if OPENOS or PSYCHOS then
|
if OPENOS or PSYCHOS then
|
||||||
print("Opening hostname file.",hnpath)
|
|
||||||
local f,g=io.open(hnpath,"rb")
|
local f,g=io.open(hnpath,"rb")
|
||||||
if f then
|
if f then
|
||||||
hostname = f:read("*a"):match("(.-)\n")
|
hostname = f:read("*a"):match("(.-)\n")
|
||||||
@ -166,7 +165,7 @@ function start()
|
|||||||
return npID
|
return npID
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sendPacket(packetID,packetType,dest,sender,vPort,data)
|
local function sendPacket(packetID,packetType,dest,sender,vPort,data,repeatingFrom)
|
||||||
if rcache[dest] then
|
if rcache[dest] then
|
||||||
dprint("Cached", rcache[dest][1],"send",rcache[dest][2],cfg.port,packetID,packetType,dest,sender,vPort,data)
|
dprint("Cached", rcache[dest][1],"send",rcache[dest][2],cfg.port,packetID,packetType,dest,sender,vPort,data)
|
||||||
if component.type(rcache[dest][1]) == "modem" then
|
if component.type(rcache[dest][1]) == "modem" then
|
||||||
@ -176,6 +175,7 @@ function start()
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
dprint("Not cached", cfg.port,packetID,packetType,dest,sender,vPort,data)
|
dprint("Not cached", cfg.port,packetID,packetType,dest,sender,vPort,data)
|
||||||
|
if v.address ~= repeatingFrom or (v.type ~= "tunnel" and v.isWireless()) then
|
||||||
for k,v in pairs(modems) do
|
for k,v in pairs(modems) do
|
||||||
if v.type == "modem" then
|
if v.type == "modem" then
|
||||||
v.broadcast(cfg.port,packetID,packetType,dest,sender,vPort,data)
|
v.broadcast(cfg.port,packetID,packetType,dest,sender,vPort,data)
|
||||||
@ -185,6 +185,7 @@ function start()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function pruneCache()
|
local function pruneCache()
|
||||||
for k,v in pairs(rcache) do
|
for k,v in pairs(rcache) do
|
||||||
@ -231,7 +232,7 @@ function start()
|
|||||||
elseif dest:sub(1,1) == "~" then -- broadcasts start with ~
|
elseif dest:sub(1,1) == "~" then -- broadcasts start with ~
|
||||||
computer.pushSignal("net_broadcast",sender,vPort,data)
|
computer.pushSignal("net_broadcast",sender,vPort,data)
|
||||||
elseif cfg.route then -- repeat packets if route is enabled
|
elseif cfg.route then -- repeat packets if route is enabled
|
||||||
sendPacket(packetID,packetType,dest,sender,vPort,data)
|
sendPacket(packetID,packetType,dest,sender,vPort,data,localModem)
|
||||||
end
|
end
|
||||||
if not rcache[sender] then -- add the sender to the rcache
|
if not rcache[sender] then -- add the sender to the rcache
|
||||||
dprint("rcache: "..sender..":", localModem,from,computer.uptime())
|
dprint("rcache: "..sender..":", localModem,from,computer.uptime())
|
||||||
|
Loading…
Reference in New Issue
Block a user