-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.lua
More file actions
121 lines (91 loc) · 2.92 KB
/
main.lua
File metadata and controls
121 lines (91 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
socket = require("socket")
dofile(getScriptPath() .. "\\config.lua")
json = dofile(getScriptPath() .. "\\json.lua")
dofile(getScriptPath() .. "\\helpers.lua")
dofile(getScriptPath() .. "\\callbacks.lua")
dofile(getScriptPath() .. "\\data_source.lua")
accepting = true
ds_tables = {}
auth = false
c = nil
function NewMessage(mes)
PrintDbgStr("New message: " .. mes)
local json_mes = json_decode(mes)
if json_mes == false then return end
if (not json_mes.id or not json_mes.method or not json_mes.args) then return end
if (auth == false and json_mes.method ~= "checkSecurity") then
return sendError(json_mes.id, "Not auth")
end
-- Создаем контекст для выполняемого метода
local context = tableMerge({
json_mes = json_mes,
}, _G)
-- Создаем строку аргументов
local args_string = ""
for key, value in pairs(json_mes.args) do
args_string = args_string .. "json_mes.args[" .. key .. "]"
if (#json_mes.args ~= key) then
args_string = args_string .. ", "
end
end
local code = "return {" .. json_mes.method .. "(" .. args_string .. ")}"
local f, error = loadstring(code)
local ok, result
if (f) then
setfenv(f, context)
ok, result = pcall(f)
if (not ok) then
return sendError(json_mes.id, result)
end
else
return sendError(json_mes.id, error)
end
if c == nil then return end
result = json_encode({
id = json_mes.id,
result = result,
})
PrintDbgStr("Message result: " .. result)
c:send(config.send_delimitter .. result)
end
function main()
s = assert(socket.bind(config.address, config.port))
s:settimeout(1)
while accepting do
c = s:accept()
if (c) then
c:settimeout(1)
PrintDbgStr("New connect")
local closed = false
while accepting and not closed do
local mes, i, s, error = "", 0, "", ""
while true do
s, error = c:receive(i, s)
if s ~= nil then
i = i + 1
mes = s
elseif error == "closed" then
closed = true
auth = false
ds_tables = {}
c:close()
c = nil
PrintDbgStr("Closed connect")
break
else break
end
end
if mes ~= "" then
local split_mes = split(mes, config.send_delimitter)
for key, value in pairs(split_mes) do
NewMessage(value)
end
end
end
end
end
end
function OnStop()
accepting = false
return 1
end