mirror of
https://git.shadowkat.net/izaya/OC-PsychOS2.git
synced 2024-11-24 20:27:39 -05:00
cleanup RPC library, make it possible to detect the client hostname when running RPC requests
This commit is contained in:
parent
0aa574ea7a
commit
5f56c74e6f
24
lib/rpc.lua
24
lib/rpc.lua
@ -9,6 +9,8 @@ local function setacl(self, fname, host)
|
||||
self[fname] = self[fname] or {}
|
||||
self[fname][host] = true
|
||||
end
|
||||
-- function rpc.allow(fn, host) -- string string -- -- Enable the allow list for function *fname* and add *host* to it.
|
||||
-- function rpc.deny(fn, host) -- string string -- -- Enable the deny list for function *fname* and add *host* to it.
|
||||
rpc.allow = setmetatable({},{__call=setacl})
|
||||
rpc.deny = setmetatable({},{__call=setacl})
|
||||
|
||||
@ -23,15 +25,15 @@ local function isPermitted(host,fn)
|
||||
end
|
||||
|
||||
local function rpcexec(_, from, port, data)
|
||||
if port ~= rpc.port then return false end
|
||||
os.spawn(function()
|
||||
if port == rpc.port then
|
||||
local rpcrq = serial.unserialize(data) or {}
|
||||
if rpcf[rpcrq[1]] and isPermitted(from,rpcrq[1]) then
|
||||
minitel.send(from,port,serial.serialize({rpcrq[2],pcall(rpcf[rpcrq[1]],table.unpack(rpcrq,3))}))
|
||||
elseif type(rpcrq[2]) == "string" then
|
||||
minitel.send(from,port,serial.serialize({rpcrq[2],false,"function unavailable"}))
|
||||
end
|
||||
end
|
||||
local rpcrq = serial.unserialize(data) or {}
|
||||
if rpcf[rpcrq[1]] and isPermitted(from,rpcrq[1]) then
|
||||
os.setenv("RPC_CLIENT",from)
|
||||
minitel.send(from,port,serial.serialize({rpcrq[2],pcall(rpcf[rpcrq[1]],table.unpack(rpcrq,3))}))
|
||||
elseif type(rpcrq[2]) == "string" then
|
||||
minitel.send(from,port,serial.serialize({rpcrq[2],false,"function unavailable"}))
|
||||
end
|
||||
end,"rpc worker for "..tostring(from))
|
||||
end
|
||||
function rpcf.list()
|
||||
@ -94,4 +96,10 @@ function rpc.register(name,fn) -- string function -- -- Registers a function to
|
||||
rpcf[name] = fn
|
||||
end
|
||||
|
||||
function rpc.unregister(name) -- string -- -- Removes a function from the RPC function registry, clearing any ACL rules.
|
||||
rpcf[name] = nil
|
||||
rpc.allow[name] = nil
|
||||
rpc.deny[name] = nil
|
||||
end
|
||||
|
||||
return rpc
|
||||
|
Loading…
Reference in New Issue
Block a user