1
0
mirror of https://github.com/kepler155c/opus synced 2026-05-30 17:22:09 +00:00

protected network services

This commit is contained in:
kepler155c@gmail.com
2019-04-20 13:48:13 -04:00
parent 62a3bc1360
commit 7749e14cad
6 changed files with 83 additions and 54 deletions
+36 -32
View File
@@ -14,6 +14,36 @@ local function getProxy(path)
return proxy
end
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
Event.addRoutine(function()
print('proxy: listening on port 188')
while true do
@@ -22,39 +52,13 @@ Event.addRoutine(function()
print('proxy: connection from ' .. socket.dhost)
Event.addRoutine(function()
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)
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)) })
end
end)
if not s and m then
_G.printError(m)
end
end
local s, m = pcall(proxyConnection, socket)
print('proxy: closing connection to ' .. socket.dhost)
socket:close()
if not s and m then
print('Proxy error')
_G.printError(m)
end
end)
end
end)