2019-06-28 17:50:02 +00:00
|
|
|
local Event = require('opus.event')
|
|
|
|
local Socket = require('opus.socket')
|
|
|
|
local Util = require('opus.util')
|
2019-03-08 21:25:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-04-20 17:48:13 +00:00
|
|
|
local function proxyConnection(socket)
|
|
|
|
local path = socket:read(2)
|
|
|
|
if path then
|
|
|
|
local api = getProxy(path)
|
|
|
|
|
|
|
|
if not api then
|
|
|
|
print('proxy: invalid API')
|
|
|
|
socket:close()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local methods = { }
|
|
|
|
for k,v in pairs(api) do
|
|
|
|
if type(v) == 'function' then
|
|
|
|
table.insert(methods, k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
socket:write(methods)
|
|
|
|
|
|
|
|
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)) })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-25 03:01:40 +00:00
|
|
|
Event.addRoutine(function()
|
2019-04-14 21:44:29 +00:00
|
|
|
print('proxy: listening on port 188')
|
2018-01-24 22:39:38 +00:00
|
|
|
while true do
|
|
|
|
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-04-20 17:48:13 +00:00
|
|
|
local s, m = pcall(proxyConnection, socket)
|
|
|
|
print('proxy: closing connection to ' .. socket.dhost)
|
2019-03-08 21:47:39 +00:00
|
|
|
socket:close()
|
2019-04-20 17:48:13 +00:00
|
|
|
if not s and m then
|
|
|
|
print('Proxy error')
|
|
|
|
_G.printError(m)
|
|
|
|
end
|
2017-10-25 03:01:40 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|