-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_utils.lua
More file actions
232 lines (183 loc) · 5.54 KB
/
server_utils.lua
File metadata and controls
232 lines (183 loc) · 5.54 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---@diagnostic disable: invisible
local PATH = (...):gsub("%.server_utils$", "")
local Performance = require(PATH .. ".plugins.performance")
local startsWith = require(PATH .. ".utils").startsWith
local performance = Performance()
local json = require(PATH .. ".lib.json")
local server = {}
function server.allowedHeaders()
local response = [[
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT
Access-Control-Allow-Headers: x-api-key
Access-Control-Max-Age: 86400
Content-Length: 0
]]
return response
end
---@param body string
function server.buildResponse(body)
local response = table.concat({
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Headers: Content-Type, x-api-key, X-Requested-With, Access-Control-Request-Headers",
"Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS",
"Content-Length: " .. #body,
"",
body,
}, "\r\n")
return response
end
--- @class FeatherRequest
--- @field method string
--- @field path string
--- @field params table
--- @field headers table
--- Builds a request object from a raw request string
--- @param client table
--- @return FeatherRequest
function server.buildRequest(client)
local request = client:receive()
local method, pathWithQuery = request:match("^(%u+)%s+([^%s]+)")
local path, queryString = pathWithQuery:match("^([^?]+)%??(.*)$")
local function parseQuery(qs)
local params = {}
for key, val in qs:gmatch("([^&=?]+)=([^&=?]+)") do
params[key] = val
end
return params
end
local params = parseQuery(queryString)
local line = client:receive()
local raw_headers = line
while line ~= "" do
raw_headers = raw_headers .. "\n" .. line
line = client:receive()
end
local headers = {}
for header in raw_headers:gmatch("[^\r\n]+") do
local key, value = header:match("^([^:]+):%s*(.*)$")
if key and value then
headers[key] = value
end
end
return {
method = method,
path = path,
params = params,
headers = headers,
}
end
--- check if the given address is in the whitelist
---@param addr string
---@param whitelist table
function server.isInWhitelist(addr, whitelist)
for _, a in pairs(whitelist) do
local ptn = "^" .. a:gsub("%.", "%%."):gsub("%*", "%%d*") .. "$"
if addr:match(ptn) then
return true
end
end
return false
end
function server.unauthorizedResponse()
local response = table.concat({
"HTTP/1.1 401 Unauthorized",
"Content-Type: application/json",
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS",
"Content-Length: 0",
"",
}, "\r\n")
return response
end
function server.createResponse(body)
return server.buildResponse(json.encode(body))
end
--- Handle get request
local function handleGetRequest(request, feather, dt)
local data = {}
if request.path == "/config" then
data = feather:__getConfig()
end
if request.path == "/performance" then
data = performance:getResponseBody(dt)
end
if request.path == "/observers" then
data = feather.featherObserver:getResponseBody()
end
if request.path ~= nil and startsWith(request.path, "/plugins") then
local pluginResponse = feather.pluginManager:handleRequest(request, feather)
data = pluginResponse
end
return data
end
--- Handle params update request
local function handlePutRequest(request, feather)
local data = {}
if request.path == "/config" then
feather:__setConfig(request.params)
end
if request.path ~= nil and startsWith(request.path, "/plugins") then
local pluginResponse = feather.pluginManager:handleParamsUpdate(request, feather)
data = pluginResponse
end
return data
end
--- Handle actions request
local function handlePostRequest(request, feather)
local data = {}
if request.path == "/logs" then
if request.params.action == "toggle-screenshots" then
feather:toggleScreenshots(not feather.captureScreenshot)
end
end
if request.path ~= nil and startsWith(request.path, "/plugins") then
local pluginResponse = feather.pluginManager:handleActionRequest(request, feather)
data = pluginResponse
end
return data
end
-- Handle a request from a client
---@param client table
---@param feather Feather
function server.handleRequest(client, feather, dt)
if client then
client:settimeout(1)
local request = server.buildRequest(client)
local addr = client:getsockname()
if not server.isInWhitelist(addr, feather.whitelist) then
feather:trace("non-whitelisted connection attempt: ", addr)
client:close()
end
local canProcess = true
if request.method ~= "OPTIONS" and feather.apiKey ~= "" and request.headers["x-api-key"] ~= feather.apiKey then
canProcess = false
client:send(server.unauthorizedResponse())
client:close()
end
if request and canProcess then
local response = {}
if request.method == "OPTIONS" then
local optionsResponse = server.allowedHeaders()
client:send(optionsResponse)
client:close()
return
end
if request.method == "GET" then
response.data = handleGetRequest(request, feather, dt)
end
if request.method == "PUT" then
response.data = handlePutRequest(request, feather)
end
if request.method == "POST" then
response.data = handlePostRequest(request, feather)
end
client:send(server.createResponse(response.data or {}))
end
client:close()
end
end
return server