· 5 years ago · Nov 30, 2020, 09:24 PM
1local file = io.open("network.lock")
2local arr = {}
3local authorized = {}
4local j = 1
5
6for line in file:lines() do
7 if j <= 3 then
8 arr[j] = line
9 else
10 authorized[j - 3] = line
11 end
12
13 j = j + 1
14end
15
16local address = "ws://" .. arr[1] .. ":" .. arr[2]
17local name = arr[3]
18local destinataire = "allMaster"
19
20local ws, err = http.websocket(address)
21if not ws then
22 return printError(err)
23end
24
25ws.send("name¤null¤" .. name)
26
27function string:split(delimiter)
28 local result = {}
29 local from = 1
30 local delim_from, delim_to = string.find(self, delimiter, from)
31 while delim_from do
32 table.insert(result, string.sub(self, from, delim_from - 1))
33 from = delim_to + 1
34 delim_from, delim_to = string.find(self, delimiter, from)
35 end
36 table.insert(result, string.sub(self, from))
37 return result
38end
39
40function serializeTable(val, name, skipnewlines, depth)
41 skipnewlines = skipnewlines or false
42 depth = depth or 0
43
44 local tmp = string.rep(" ", depth)
45
46 if name then
47 tmp = tmp .. name .. " = "
48 end
49
50 if type(val) == "table" then
51 tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
52
53 for k, v in pairs(val) do
54 tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
55 end
56
57 tmp = tmp .. string.rep(" ", depth) .. "}"
58 elseif type(val) == "number" then
59 tmp = tmp .. tostring(val)
60 elseif type(val) == "string" then
61 tmp = tmp .. string.format("%q", val)
62 elseif type(val) == "boolean" then
63 tmp = tmp .. (val and "true" or "false")
64 else
65 tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
66 end
67
68 return tmp
69end
70
71function stringnify(val)
72 local ret = ""
73
74 local type = type(val)
75
76 if type == nil then
77 ret = val .. ""
78 else
79 if type == "table" then
80 ret = serializeTable(val)
81 else
82 ret = tostring(val)
83 end
84 end
85
86 return ret
87end
88
89while true do
90 local recv = ws.receive() .. " "
91
92 if recv ~= nil then
93 print("recv: " .. recv)
94
95 local type = "type"
96 local api = "api"
97 local cmd = "cmd"
98 local args = {}
99
100 local i = 1
101
102 -- string.gmatch(recv, "[^%s]+")
103 local split = recv:split("¤")
104
105 for _ in pairs(split) do
106 if i == 1 then
107 type = split[i] .. ""
108 elseif i == 2 then
109 destinataire = split[i] .. ""
110 elseif i == 3 then
111 api = split[i] .. ""
112 elseif i == 4 then
113 cmd = split[i] .. ""
114 else
115 args[i - 4] = split[i] .. ""
116 end
117
118 i = i + 1
119 end
120
121 i = i - 5
122
123 if type == "api" then
124 local canRun = false
125 local j = 1
126
127 for _ in pairs(authorized) do
128 canRun = canRun or (api == authorized[j])
129 j = j + 1
130
131 if canRun then
132 break
133 end
134 end
135
136 if canRun then
137 for key, value in pairs(getmetatable(load(api))) do
138 print(key, value)
139 end
140 end
141
142 print("send: " .. send)
143 ws.send("" .. send)
144 end
145 end
146end
147
148os.reboot()
149