poppy-server/rpc.lua

107 lines
2.5 KiB
Lua
Raw Permalink Normal View History

2021-12-31 21:19:26 +00:00
local rpc = {
2022-07-03 18:32:45 +00:00
PROTOCOL_VERSION = "003"
2021-12-31 21:19:26 +00:00
}
2022-01-02 16:16:35 +00:00
function rpc.clean(record)
assert(type(record) == "string", "cleaned record is not a string! :" .. type(record))
2022-01-02 16:16:35 +00:00
return record:gsub("\31", "")
end
2021-12-31 21:19:26 +00:00
2022-01-02 17:40:30 +00:00
function rpc.print(record)
--local cleaned, _ = record:gsub("\31","|")
2022-01-02 18:19:54 +00:00
--print(cleaned)
2022-01-02 17:40:30 +00:00
end
2021-12-31 21:19:26 +00:00
function rpc.assert(...)
assert(...)
end
2022-01-02 16:16:35 +00:00
function rpc.validate(record)
2022-01-02 18:19:54 +00:00
rpc.print("Starting validation")
2022-01-02 16:16:35 +00:00
local errorMsg = false
local count
record, count = record:gsub("^poppyV" .. rpc.PROTOCOL_VERSION .."\31","")
if not count == 1 then
errorMsg = "unsupported version or message!"
record = nil
end
2022-01-02 17:40:30 +00:00
rpc.print("record is now ".. record)
2022-06-19 09:29:10 +00:00
local clientID, count = record:match("^%d-\31")
2022-01-02 16:16:35 +00:00
clientID = rpc.clean(clientID)
if not clientID then
errorMsg = "No clientID found!"
record = nil
else
record, count = record:gsub("^%d-\31","")
if count ~= 1 then
error("Failed to remove clientiD?")
end
end
2022-01-02 17:40:30 +00:00
rpc.print("record is now " .. record)
rpc.print("match clientID: " .. clientID)
rpc.print("ended validation")
2022-01-02 16:16:35 +00:00
return {
errorMsg = errorMsg,
clientID = tonumber(clientID),
2022-01-02 16:16:35 +00:00
version = rpc.PROTOCOL_VERSION,
record = record }
2021-12-31 21:19:26 +00:00
end
function rpc.unprivligedExecute(commands, record, ip, port)
assert(type(commands) == "table")
assert(type(record) == "string")
assert(ip)
assert(port)
rpc.print("exexute: " .. record)
local command = record:match("^.-\31")
if command then command = rpc.clean(command) end
record = record:gsub("^.-\31","")
if not command then
command = record
record = nil
end
rpc.assert(type(commands[command]) == "function", "Unknown command encountered!" .. command)
if record then
rpc.print("executing " .. command .. " with " .. record)
return commands[command](record, ip, port)
else
rpc.print("executing no arguments:" .. command)
return commands[command](ip, port)
end
end
2022-01-02 17:40:30 +00:00
function rpc.execute(commands, clientID, record)
2022-01-02 16:16:35 +00:00
assert(type(commands) == "table")
2022-01-02 17:40:30 +00:00
assert(type(record) == "string")
2022-01-02 18:19:54 +00:00
rpc.print("exexute: " .. record)
2022-01-02 16:16:35 +00:00
local command = record:match("^.-\31")
2022-01-02 17:40:30 +00:00
if command then command = rpc.clean(command) end
2022-01-02 16:16:35 +00:00
record = record:gsub("^.-\31","")
2022-06-19 09:29:10 +00:00
if not command then
command = record
record = nil
end
rpc.assert(type(commands[command]) == "function", "Unknown command encountered!" .. command)
if record then
rpc.print("executing " .. command .. " with " .. record)
return commands[command](clientID, record)
else
rpc.print("executing no arguments:" .. command)
return commands[command](clientID)
2021-12-31 21:19:26 +00:00
end
end
2022-06-19 09:29:10 +00:00
2021-12-31 21:19:26 +00:00
return rpc