1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 13:14:49 +00:00
opus/sys/network/proxy.lua

41 lines
829 B
Lua
Raw Normal View History

2017-10-25 03:01:40 +00:00
local Event = require('event')
local Socket = require('socket')
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()
local api = socket:read(2)
if api then
2017-10-25 03:01:40 +00:00
local proxy = _G[api]
2018-02-06 13:45:43 +00:00
if not proxy then
print('proxy: invalid API')
return
end
2018-01-24 22:39:38 +00:00
local methods = { }
2017-10-25 03:01:40 +00:00
for k,v in pairs(proxy) do
if type(v) == 'function' then
table.insert(methods, k)
end
end
socket:write(methods)
2018-01-24 22:39:38 +00:00
while true do
local data = socket:read()
if not data then
2017-10-26 22:56:55 +00:00
print('proxy: lost connection from ' .. socket.dhost)
2018-01-24 22:39:38 +00:00
break
end
socket:write({ proxy[data.fn](table.unpack(data.args)) })
end
2017-10-25 03:01:40 +00:00
end
end)
end
end)