1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apps/network/proxy.lua

61 lines
1.2 KiB
Lua
Raw Normal View History

2017-10-25 03:01:40 +00:00
local Event = require('event')
local Socket = require('socket')
2019-03-08 21:25:56 +00:00
local Util = require('util')
local function getProxy(path)
local x = Util.split(path, '(.-)/')
local proxy = _G
for _, v in pairs(x) do
proxy = proxy[v]
if not proxy then
break
end
end
2019-03-08 21:47:39 +00:00
return proxy
2019-03-08 21:25:56 +00:00
end
2017-10-25 03:01:40 +00:00
Event.addRoutine(function()
2018-01-24 22:39:38 +00:00
while true do
print('proxy: listening on port 188')
local socket = Socket.server(188)
2017-10-25 03:01:40 +00:00
2018-01-24 22:39:38 +00:00
print('proxy: connection from ' .. socket.dhost)
2017-10-25 03:01:40 +00:00
2018-01-24 22:39:38 +00:00
Event.addRoutine(function()
2019-03-08 21:25:56 +00:00
local path = socket:read(2)
if path then
local api = getProxy(path)
2017-10-25 03:01:40 +00:00
2019-03-08 21:25:56 +00:00
if not api then
2018-02-06 13:45:43 +00:00
print('proxy: invalid API')
2019-03-08 21:47:39 +00:00
socket:close()
2018-02-06 13:45:43 +00:00
return
end
2018-01-24 22:39:38 +00:00
local methods = { }
2019-03-08 21:25:56 +00:00
for k,v in pairs(api) do
2017-10-25 03:01:40 +00:00
if type(v) == 'function' then
table.insert(methods, k)
end
end
socket:write(methods)
2019-03-08 21:25:56 +00:00
local s, m = pcall(function()
while true do
local data = socket:read()
if not data then
print('proxy: lost connection from ' .. socket.dhost)
break
end
socket:write({ api[data[1]](table.unpack(data, 2)) })
2018-01-24 22:39:38 +00:00
end
2019-03-08 21:25:56 +00:00
end)
if not s and m then
_G.printError(m)
2018-01-24 22:39:38 +00:00
end
2017-10-25 03:01:40 +00:00
end
2019-03-08 21:47:39 +00:00
socket:close()
2017-10-25 03:01:40 +00:00
end)
end
end)