lovebird/lovebird.lua

491 lines
12 KiB
Lua
Raw Normal View History

--
-- lovebird
--
-- Copyright (c) 2014, rxi
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
local socket = require "socket"
2014-04-09 22:29:50 +00:00
local lovebird = { _version = "0.0.1" }
lovebird.inited = false
lovebird.host = "*"
lovebird.buffer = ""
lovebird.lines = {}
lovebird.pages = {}
2014-04-10 17:55:20 +00:00
lovebird.wrapprint = true
lovebird.timestamp = true
lovebird.allowhtml = true
2014-04-09 22:29:50 +00:00
lovebird.port = 8000
lovebird.whitelist = { "127.0.0.1", "192.168.*.*" }
lovebird.maxlines = 200
lovebird.updateinterval = .5
2014-04-09 22:29:50 +00:00
lovebird.pages["index"] = [[
<?lua
-- Handle console input
if req.parsedbody.input then
local str = req.parsedbody.input
xpcall(function() assert(loadstring(str))() end, lovebird.onerror)
end
?>
2014-04-10 11:33:18 +00:00
<!doctype html>
2014-04-09 22:29:50 +00:00
<html>
<head>
2014-04-10 11:33:18 +00:00
<meta http-equiv="x-ua-compatible" content="IE=Edge"/>
2014-04-09 22:29:50 +00:00
<title>lovebird</title>
<style>
2014-04-10 11:33:18 +00:00
body {
margin: 0px;
2014-04-12 09:08:39 +00:00
font-size: 14px;
2014-04-10 11:33:18 +00:00
font-family: helvetica, verdana, sans;
background: #FFFFFF;
}
form {
margin-bottom: 0px;
}
2014-04-10 17:55:20 +00:00
.timestamp {
color: #909090;
}
.greybordered {
margin: 12px;
background: #F0F0F0;
border: 1px solid #E0E0E0;
border-radius: 3px;
}
2014-04-10 11:33:18 +00:00
#header {
background: #101010;
height: 25px;
color: #F0F0F0;
2014-04-10 17:13:24 +00:00
padding: 9px
2014-04-10 11:33:18 +00:00
}
#title {
float: left;
font-size: 20px;
}
#title a {
color: #F0F0F0;
text-decoration: none;
}
#title a:hover {
color: #FFFFFF;
}
#version {
font-size: 10px;
}
2014-04-10 11:33:18 +00:00
#status {
float: right;
font-size: 14px;
padding-top: 4px;
}
#main a {
color: #000000;
text-decoration: none;
background: #E0E0E0;
border: 1px solid #D0D0D0;
border-radius: 3px;
padding-left: 2px;
padding-right: 2px;
display: inline-block;
}
#main a:hover {
background: #D0D0D0;
border: 1px solid #C0C0C0;
}
#console {
2014-04-10 11:33:18 +00:00
position: absolute;
2014-04-12 09:08:39 +00:00
top: 40px; bottom: 0px; left: 0px; right: 312px;
2014-04-10 11:33:18 +00:00
}
#input {
position: absolute;
margin: 10px;
bottom: 0px; left: 0px; right: 0px;
}
#inputbox {
width: 100%;
}
#output {
overflow-y: scroll;
position: absolute;
margin: 10px;
top: 0px; bottom: 36px; left: 0px; right: 0px;
}
#env {
position: absolute;
top: 40px; bottom: 0px; right: 0px;
2014-04-12 09:08:39 +00:00
width: 300px;
}
#envheader {
padding: 5px;
background: #E0E0E0;
}
#envvars {
position: absolute;
left: 0px; right: 0px; top: 25px; bottom: 0px;
margin: 10px;
overflow-y: scroll;
font-size: 12px;
}
2014-04-09 22:29:50 +00:00
</style>
</head>
<body>
2014-04-10 11:33:18 +00:00
<div id="header">
<div id="title">
<a href="https://github.com/rxi/lovebird">lovebird</a>
<span id="version"><?lua echo(lovebird._version) ?></span>
</div>
<div id="status"></div>
2014-04-10 11:33:18 +00:00
</div>
<div id="main">
<div id="console" class="greybordered">
<div id="output"> <?lua echo(lovebird.buffer) ?> </div>
<div id="input">
<form method="post" onsubmit="onInputSubmit(); return false">
<input id="inputbox" name="input" type="text"></input>
</form>
</div>
</div>
<div id="env" class="greybordered">
<div id="envheader"></div>
<div id="envvars"></div>
2014-04-10 11:33:18 +00:00
</div>
2014-04-09 22:29:50 +00:00
</div>
<script>
document.getElementById("inputbox").focus();
var updateDivContent = function(id, content) {
var div = document.getElementById(id);
if (div.innerHTML != content) {
div.innerHTML = content;
return true;
}
return false;
}
var geturl = function(url, onComplete, onFail) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState != 4) return;
if (req.status == 200) {
if (onComplete) onComplete(req.responseText);
} else {
if (onFail) onFail(req.responseText);
}
}
url += (url.indexOf("?") > -1 ? "&_=" : "?_=") + Math.random();
req.open("GET", url, true);
req.send();
}
var onInputSubmit = function() {
var b = document.getElementById("inputbox");
var req = new XMLHttpRequest();
req.open("POST", "/", true);
req.send("input=" + encodeURIComponent(b.value));
b.value = "";
refreshOutput();
2014-04-09 22:29:50 +00:00
}
/* Output buffer and status */
var refreshOutput = function() {
geturl("/buffer", function(text) {
updateDivContent("status", "connected &#9679;");
if (updateDivContent("output", text)) {
var div = document.getElementById("output");
div.scrollTop = div.scrollHeight;
2014-04-09 22:29:50 +00:00
}
},
function(text) {
updateDivContent("status", "disconnected &#9675;");
});
2014-04-09 22:29:50 +00:00
}
setInterval(refreshOutput, <?lua echo(lovebird.updateinterval) ?> * 1000);
/* Environment variable view */
var envPath = "";
var refreshEnv = function() {
geturl("/env.json?p=" + envPath, function(text) {
var json = eval("(" + text + ")");
/* Header */
var html = "<a href='#' onclick=\"setEnvPath('')\">env</a>";
var acc = "";
var p = json.path != "" ? json.path.split(".") : [];
for (var i = 0; i < p.length; i++) {
acc += "." + p[i];
html += " <a href='#' onclick=\"setEnvPath('" + acc + "')\">" +
p[i] + "</a>";
}
updateDivContent("envheader", html);
/* Handle invalid table path */
if (!json.valid) {
updateDivContent("envvars", "Bad path");
return;
}
/* Variables */
var html = "<table>";
for (var i = 0; json.vars[i]; i++) {
var x = json.vars[i];
var fullpath = (json.path + "." + x.key).replace(/^\./, "");
var k = x.key;
if (x.type == "table") {
k = "<a href='#' onclick=\"setEnvPath('" + fullpath + "')\">" +
k + "</a>";
}
var v = "<a href='#' onclick=\"insertVar('" + fullpath + "');\">" +
x.value + "</a>"
html += "<tr><td>" + k + "</td><td>" + v + "</td></tr>";
}
html += "</table>";
updateDivContent("envvars", html);
});
}
var setEnvPath = function(p) {
envPath = p;
refreshEnv();
}
var insertVar = function(p) {
var b = document.getElementById("inputbox");
b.value += p;
b.focus();
}
setInterval(refreshEnv, <?lua echo(lovebird.updateinterval) ?> * 1000);
2014-04-09 22:29:50 +00:00
</script>
</body>
</html>
]]
2014-04-11 22:24:14 +00:00
lovebird.pages["buffer"] = [[ <?lua echo(lovebird.buffer) ?> ]]
lovebird.pages["env.json"] = [[
<?lua
local t = _G
local p = req.parsedurl.query.p or ""
p = p:gsub("%.+", "%."):match("^[%.]*(.*)[%.]*$")
if p ~= "" then
for x in p:gmatch("[^%.]+") do
t = t[x]
-- Return early if path does not exist
if type(t) ~= "table" then
echo('{ "valid": false, "path": ' .. string.format("%q", p) .. ' }')
return
2014-04-11 23:01:55 +00:00
end
end
end
?>
{
"valid": true,
"path": "<?lua echo(p) ?>",
"vars": [
<?lua
local keys = {}
for k in pairs(t) do table.insert(keys, k) end
table.sort(keys)
for _, k in pairs(keys) do
local v = t[k]
?>
{
"key": "<?lua echo(k) ?>",
"value": <?lua echo(
string.format("%q",
lovebird.truncate(
lovebird.htmlescape(
tostring(v)), 26))) ?>,
"type": "<?lua echo(type(v)) ?>",
},
<?lua end ?>
]
}
2014-04-11 23:01:55 +00:00
]]
local loadstring = loadstring or load
local map = function(t, fn)
local res = {}
for k, v in pairs(t) do res[k] = fn(v) end
return res
end
local trace = function(...)
print("[lovebird] " .. table.concat(map({...}, tostring), " "))
end
local unescape = function(str)
local f = function(x) return string.char(tonumber("0x"..x)) end
return (str:gsub("%+", " "):gsub("%%(..)", f))
end
2014-04-09 22:29:50 +00:00
function lovebird.init()
lovebird.server = assert(socket.bind(lovebird.host, lovebird.port))
lovebird.addr, lovebird.port = lovebird.server:getsockname()
lovebird.server:settimeout(0)
2014-04-10 11:34:30 +00:00
if lovebird.wrapprint then
2014-04-09 22:29:50 +00:00
local oldprint = print
print = function(...)
oldprint(...)
lovebird.print(...)
end
end
lovebird.inited = true
end
function lovebird.template(str, env)
env = env or {}
local keys, vals = {}, {}
for k, v in pairs(env) do
table.insert(keys, k)
table.insert(vals, v)
end
local f = function(x) return string.format(" echo(%q)", x) end
str = ("?>"..str.."<?lua"):gsub("%?>(.-)<%?lua", f)
str = "local echo, " .. table.concat(keys, ",") .. " = ..." .. str
local output = {}
local echo = function(str) table.insert(output, str) end
assert(loadstring(str))(echo, unpack(vals))
return table.concat(map(output, tostring))
end
function lovebird.parseurl(url)
local res = {}
res.path, res.search = url:match("/([^%?]*)%??(.*)")
res.query = {}
for k, v in res.search:gmatch("([^&^?]-)=([^&^#]*)") do
res.query[k] = unescape(v)
end
return res
end
2014-04-11 22:24:14 +00:00
function lovebird.htmlescape(str)
return str:gsub("<", "&lt;")
end
2014-04-11 23:01:55 +00:00
function lovebird.truncate(str, len)
if #str < len then
return str
end
return str:sub(1, len - 3) .. "..."
end
function lovebird.checkwhitelist(addr)
if lovebird.whitelist == nil then return true end
for _, a in pairs(lovebird.whitelist) do
local ptn = "^" .. a:gsub("%.", "%%."):gsub("%*", "%%d*") .. "$"
if addr:match(ptn) then return true end
end
return false
end
2014-04-09 22:29:50 +00:00
function lovebird.print(...)
local str = table.concat(map({...}, tostring), " ")
if not lovebird.allowhtml then
2014-04-11 22:24:14 +00:00
str = lovebird.htmlescape(str)
end
2014-04-10 17:55:20 +00:00
if lovebird.timestamp then
str = os.date('<span class="timestamp">[%H:%M:%S]</span> ') .. str
end
table.insert(lovebird.lines, str)
if #lovebird.lines > lovebird.maxlines then
table.remove(lovebird.lines, 1)
2014-04-09 22:29:50 +00:00
end
lovebird.buffer = table.concat(lovebird.lines, "<br>")
2014-04-09 22:29:50 +00:00
end
function lovebird.onerror(err)
2014-04-09 22:29:50 +00:00
trace("ERROR:", err)
end
function lovebird.onrequest(req, client)
local page = req.parsedurl.path
page = page ~= "" and page or "index"
-- Handle "page not found"
if not lovebird.pages[page] then
return "HTTP/1.1 404\r\nContent-Type: text/html\r\n\r\nBad page"
2014-04-09 22:29:50 +00:00
end
-- Handle page
local str
xpcall(function()
str = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" ..
lovebird.template(lovebird.pages[page],
{ lovebird = lovebird, req = req })
end, lovebird.onerror)
return str
2014-04-09 22:29:50 +00:00
end
function lovebird.onconnect(client)
2014-04-09 22:29:50 +00:00
-- Create request table
local requestptn = "(%S*)%s*(%S*)%s*(%S*)"
local req = {}
req.socket = client
req.addr, req.port = client:getsockname()
req.request = client:receive()
req.method, req.url, req.proto = req.request:match(requestptn)
req.headers = {}
2014-04-09 22:29:50 +00:00
while 1 do
local line = client:receive()
if not line or #line == 0 then break end
local k, v = line:match("(.-):%s*(.*)$")
req.headers[k] = v
2014-04-09 22:29:50 +00:00
end
if req.headers["Content-Length"] then
req.body = client:receive(req.headers["Content-Length"])
2014-04-09 22:29:50 +00:00
end
-- Parse body
req.parsedbody = {}
if req.body then
for k, v in req.body:gmatch("([^&]-)=([^&^#]*)") do
req.parsedbody[k] = unescape(v)
end
end
-- Parse request line's url
req.parsedurl = lovebird.parseurl(req.url)
2014-04-09 22:29:50 +00:00
-- Handle request; get data to send
local data, index = lovebird.onrequest(req), 0
2014-04-09 22:29:50 +00:00
-- Send data
while index < #data do
index = index + client:send(data, index)
end
-- Clear up
client:close()
end
function lovebird.update()
2014-04-09 22:29:50 +00:00
if not lovebird.inited then lovebird.init() end
local client = lovebird.server:accept()
if client then
client:settimeout(2)
local addr = client:getsockname()
if lovebird.checkwhitelist(addr) then
xpcall(function() lovebird.onconnect(client) end, function() end)
2014-04-09 22:29:50 +00:00
else
trace("got non-whitelisted connection attempt: ", addr)
client:close()
end
end
end
return lovebird