forked from yangzhong-freebsd/lua-httpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd
executable file
·513 lines (404 loc) · 16.1 KB
/
httpd
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/libexec/flua
-- vim: set et:
-- Minimal web server written in Lua
--
-- Use with inetd, no other dependencies:
-- http stream tcp nowait root /usr/local/sbin/httpd httpd
--
-- Copyright (c) 2016 - 2020 Ryan Moeller <[email protected]>
--
-- Permission to use, copy, modify, and distribute this software for any
-- purpose with or without fee is hereby granted, provided that the above
-- copyright notice and this permission notice appear in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
local SRC_DIR = "/home/yang/src/lua-httpd/"
package.path = SRC_DIR .. "?.lua;" .. package.path
local httpd = require("httpd")
local template = require("template")
local boot = require("boot")
local db = require("db")
local disk = require("disk")
local filesystem = require("filesystem")
local hardening = require("hardening")
local keymap = require("keymap")
local lang = require("lang")
local network = require("network")
local pkgsets = require("pkgsets")
local partition = require("partition")
local service = require("service")
local shell = require("shell")
local json = require("json")
local misc = require("misc")
--local manifest = "/usr/local/share/freebsd/MANIFESTS/amd64-amd64-13.0-RELEASE"
local manifest = SRC_DIR .. "MANIFEST"
local stylesheet_file = assert(io.open(SRC_DIR .. "style.css", "r"))
local stylesheet = stylesheet_file:read("*all")
stylesheet_file:close()
local main_page_file = assert(io.open(SRC_DIR .. "main_page.html", "r"))
local main_page = main_page_file:read("*all")
main_page_file:close()
local add_user_file= assert(io.open(SRC_DIR .. "add_user.html", "r"))
local add_user_page = add_user_file:read("*all")
add_user_file:close()
local network_file = assert(io.open(SRC_DIR .. "network.html", "r"))
local network_page = network_file:read("*all")
network_file:close()
local keymap_file = assert(io.open(SRC_DIR .. "keymap.html", "r"))
local keymap_page = keymap_file:read("*all")
keymap_file:close()
local zfs_file = assert(io.open(SRC_DIR .. "zfs.html", "r"))
local zfs_page = zfs_file:read("*all")
zfs_file:close()
local language_file = assert(io.open(SRC_DIR .. "lang.html", "r"))
local language_page = language_file:read("*all")
language_file:close()
function pairsByKeys(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function() -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
function selected(cond)
return cond and "selected" or ""
end
function checked(cond)
return cond and "checked" or ""
end
function readonly(cond)
return cond and "readonly" or ""
end
function wireless(cond)
return cond and "[Wireless]" or "[Wired]"
end
function disabled(cond)
return cond and "disabled" or ""
end
function orEmpty(string)
if (string) then
return string
else
return ""
end
-- return string and string or ""
end
function orWheel(string)
if (string) then
return string
else
return "wheel"
end
-- return string and string or ""
end
--TODO: properly credit
function unescape (str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end)
return str
end
function splitString(str, char)
local split_list = {}
for match in str:gmatch("[^" .. char .. "]+") do
table.insert(split_list, match)
end
return split_list
end
function parseRequest(body)
local lines = splitString(body, "&")
local req = {}
for i, line in ipairs(lines) do
local mapping = unescape(line)
local key, val = mapping:match("^([^=]+)=(.*)")
--requests could possibly have multiple vals per key.
--Since I know that these will always be for inputs
--that will never have space in them (currently: disks)
--I'll just space-separate them.
if (type(req[key]) == "string") then
req[key] = req[key].." "..val
else
req[key] = val
end
end
return req
end
function getStylesheet()
return { status=200, reason="ok", body=stylesheet }
end
function prefillDB()
db.updateIfUnset("installer_language", "en")
db.updateIfUnset("keymap_layout", keymap.getCurrentLayout())
db.updateIfUnset("keymap_variant", keymap.getCurrentVariant())
db.updateIfUnset("hostname", "freebsd")
db.updateIfUnset("packages", "base kernel") --TODO make this depend on data in package.lua
end
function getSelectedDisks(parsed_db)
local db_disks = parsed_db.zfs_disks or ""
local selected_disk_list = misc.splitString(db_disks, " ")
local selected_disks = {}
for _, disk in ipairs(selected_disk_list) do
selected_disks[disk] = true
end
return selected_disks
end
function mainPage(request)
prefillDB()
local parsed_db = db.parse()
local cur_lang = parsed_db.installer_language
local keymap_layout = parsed_db.keymap_layout
local keymap_variant = parsed_db.keymap_variant
local selected_packages_list = misc.splitString(parsed_db.packages, " ")
local selected_packages = {}
for _, package in ipairs(selected_packages_list) do
selected_packages[package] = true
end
local installReady = (parsed_db.network and parsed_db.zfs_disks) --TODO: make this check smarter
local body = template.process(main_page,
{
boothowto = boot.howto(),
bootmethod = boot.method(),
cur_lang = cur_lang,
disks = disk.info(),
filesystem_formats = filesystem.formats,
hardening_menu = hardening.menu,
keymap_string = keymap.prettyPrint(keymap_layout, keymap_variant),
lang = lang.translations,
network_string = parsed_db.network,
packages = pkgsets.pkgsets,
partition_styles = partition.styles,
ready = installReady,
shells = shell.list,
selected_disks = getSelectedDisks(parsed_db),
selected_packages = selected_packages,
service_menu = service.menu,
users = db.getUsersAsList(parsed_db),
})
return { status=200, reason="ok", body=body }
end
function writePackages(request) --TODO make 'packages' 'pkgsets' naming more consistent
local req = json.decode(request.body)
db.update("packages", table.concat(req, " "))
return { status=200, reason="ok", body="TODO"}
end
function languagePage(request)
local parsed_db = db.parse()
local cur_lang = parsed_db.installer_language
local body = template.process(language_page,
{
cur_lang = cur_lang,
lang = lang.translations,
languages = lang.languages,
})
return { status=200, reason="ok", body=body }
end
function zfsPage(request)
local parsed_db = db.parse()
local selected_filesystem = parsed_db.zfs_filesystem or "stripe"
local cur_lang = parsed_db.installer_language
local body = template.process(zfs_page,
{
cur_lang = cur_lang,
disks = disk.info(),
filesystem_formats = filesystem.zfs_formats,
lang = lang.translations,
selected_disks = getSelectedDisks(parsed_db),
selected_filesystem = selected_filesystem,
})
return { status=200, reason="ok", body = body }
end
function writeZFS(request)
local req = parseRequest(request.body)
local disks = req.disk
db.update("zfs_filesystem", req.filesystem)
db.update("zfs_disks", disks)
return { status=303, headers = {["Location"]="/"}, reason="ok", body = "TODO" }
end
function setLanguage(request)
local req = parseRequest(request.body)
db.update("installer_language", req.language)
--TODO: set more defaults based on language selection
local keymap_layout = lang.languages[req.language].keymap_layout
local keymap_variant = lang.languages[req.language].keymap_variant
local parsed_db = db.parse()
if (not parsed_db.keymap_layout or parsed_db.keymap_layout == "") then
keymap.setKeymap(keymap_layout, keymap_variant)
db.update("keymap_layout", keymap_layout)
db.update("keymap_variant", keymap_variant)
end
return { status=303, headers = {["Location"]="/"}, reason="ok", body = "TODO" }
end
function keymapPage(request)
local parsed_db = db.parse()
local selected_layout = parsed_db.keymap_layout
local selected_variant = parsed_db.keymap_variant
local variants = keymap.XMap[selected_layout] or {}
local cur_lang = parsed_db.installer_language
local body = template.process(keymap_page,
{
cur_lang = cur_lang,
lang = lang.translations,
selected_layout = selected_layout,
selected_variant = selected_variant,
x_list = keymap.XList,
x_variants = variants,
})
return { status=200, reason="ok", body = body }
end
function getVariants(request)
local variants = keymap.XMap[request.matches[1]]
return { status=200, reason="ok", body=json.encode(variants)}
end
function setKeymap(request)
local map = json.decode(request.body)
keymap.setKeymap(map.layout, map.variant)
return { status=200, reason="ok", body = map.layout}
end
function writeKeymap(request)
local req = parseRequest(request.body)
db.update("keymap_layout", req.keymap)
db.update("keymap_variant", req.variant)
return { status=303, headers = {["Location"]="/"}, reason="ok", body = "TODO" }
end
function addUserPage(request)
local parsed_db = db.parse()
local cur_lang = parsed_db.installer_language
local body = template.process(add_user_page,
{
cur_lang = cur_lang,
lang = lang.translations,
shells = shell.list,
})
return { status=200, reason="ok", body = body }
end
function editUserPage(request)
local parsed_db = db.parse()
local cur_lang = parsed_db.installer_language
local username = request.matches[1]
local user_data = db.parse().users[username]
local body = template.process(add_user_page,
{
cur_lang = cur_lang,
editing = true,
full_name = user_data.full_name,
groups = user_data.groups,
lang = lang.translations,
shells = shell.list,
username = username,
user_shell = user_data.shell,
})
return { status=200, reason="ok", body = body }
end
function addUser(request)
local req = parseRequest(request.body)
--TODO verify input
db.update("user:"..req.username..":full_name", req.full_name)
db.update("user:"..req.username..":password", req.password)
db.update("user:"..req.username..":groups", req.groups)
db.update("user:"..req.username..":shell", req.shell)
return { status=303, headers = {["Location"]="/#users"}, reason="ok", body = "TODO" }
end
function deleteUser(request)
local username = request.matches[1]
--TODO verify input
db.removeMatches("^user:"..username..":")
return { status=303, headers = {["Location"]="/#users"}, reason="ok", body = "TODO" }
end
function networkPage(request)
local parsed_db = db.parse()
local cur_lang = parsed_db.installer_language
local body = template.process(network_page,
{
cur_lang = cur_lang,
lang = lang.translations,
network_interfaces = network.getInterfaces(),
})
return { status=200, reason="ok", body = body }
end
function checkIsWireless(request)
local isWireless = network.isWireless(request.matches[1])
return {status=200, reason="ok", body=json.encode(isWireless)}
end
function getNetworkStatus(request)
local status = network.status()
return { status=200, reason="ok", body=status}
end
function scanWireless(request)
local networks = network.scanWireless()
return { status=200, reason="ok", body=json.encode(networks)}
end
function connectToNetwork(request)
local req = json.decode(request.body)
network.connectWireless(req.network, req.password)
return { status=200, reason="ok", body = "okay"}
end
function writeNetwork(request)
local req = json.decode(request.body)
db.update("network_interface", req.network_interface)
db.update("network", req.network)
db.update("network_password", req.password)
local resolv_file = io.open("/etc/resolv.conf", "r")
while (resolv_file == nil) do --TODO: there must be a better way to do this
os.execute("sleep 1")
resolv_file = io.open("/etc/resolv.conf", "r")
end
for line in resolv_file:lines() do
local key, val = line:match("^([^ ]+) (.*)")
if (key == "search") then
db.update("resolv_search", val)
elseif (key == "nameserver") then
db.update("resolv_nameserver", val)
end
end
resolv_file:close()
return { status=303, headers = {["Location"]="/"}, reason="ok", body = "TODO" }
end
function doInstall(request)
local req = parseRequest(request.body)
for key, val in pairs(req) do
print(key, val)
end
db.update("hostname", req.hostname)
db.update("root_password", req.root_password)
db.update("packages", req.packages)
db.close()
--For testing, I don't run the actual install command. Check the 'liveuser' branch
return { status=501, reason="Not implemented", body="TODO" }
end
local server = httpd.create_server("/var/log/httpd.log")
server:add_route("GET", "^/style$", getStylesheet)
--Fetch API stuff
server:add_route("GET", "^/keymap/variants/(.*)$", getVariants)
server:add_route("GET", "^/network/iswireless/(.*)$", checkIsWireless)
server:add_route("GET", "^/scanwireless$", scanWireless)
server:add_route("GET", "^/networkstatus$", getNetworkStatus)
server:add_route("POST", "^/setkeymap$", setKeymap)
server:add_route("POST", "^/network$", connectToNetwork)
server:add_route("POST", "^/networkconfirm$", writeNetwork)
server:add_route("POST", "^/pkgsets$", writePackages)
--Pages
server:add_route("GET", "^/language$", languagePage)
server:add_route("GET", "^/zfs$", zfsPage)
server:add_route("GET", "^/adduser$", addUserPage)
server:add_route("GET", "^/edituser/(.*)$", editUserPage)
server:add_route("GET", "^/network$", networkPage)
server:add_route("GET", "^/keymap$", keymapPage)
server:add_route("GET", "^/$", mainPage)
server:add_route("POST", "^/zfs$", writeZFS)
server:add_route("POST", "^/setlanguage", setLanguage)
server:add_route("POST", "^/install$", doInstall)
server:add_route("POST", "^/adduser$", addUser)
server:add_route("POST", "^/deleteuser/(.*)$", deleteUser)
server:add_route("POST", "^/writekeymap$", writeKeymap)
server:run(true)