and_cesbo icon

Astra Stream : HTTP connections list

and_cesbo | PRO | 02/05/15 09:05:14 PM UTC | 0 ⭐ | 794 👁️ | Never ⏰ | []
Lua |

2.65 KB

|

None

|

0 👍

/

0 👎

-- Astra Stream : HTTP connections list
-- Usage:
-- 1. Save script into the file: /etc/astra/stat.lua
-- 2. Launch astra: astra --stream /etc/astra/stat.lua /etc/astra/config.lua
-- 3. Access to the stat in browser: http://server-address:5000/
 
local http_stat_port = 5000
 
--
 
function render_stat_html()
    local table_content = ""
    local i = 1
    local ct = os.time()
 
    for _, http_instance in pairs(http_output_instance_list) do
        for channel_path, output_data in pairs(http_instance.__options.channel_list) do
            for _, client_data in pairs(output_data.client_list) do
                local dt = ct - client_data.st
                local uptime = string.format("%02d:%02d", (dt / 3600), (dt / 60) % 60)
                table_content = table_content ..
                                "<tr>" ..
                                "<td>" .. i .. "</td>" ..
                                "<td>" .. client_data.addr .. "</td>" ..
                                "<td>" .. client_data.path .. "</td>" ..
                                "<td>" .. uptime .. "</td>" ..
                                "</tr>\r\n"
                i = i + 1
            end
        end
    end
 
    return [[<!DOCTYPE html>
 
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Astra Stream : Statistics</title>
    <style type="text/css">
body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #333333; }
table { width: 600px; margin: auto; }
.brand { text-align: left; font-size: 18px; line-height: 20px; }
.version { text-align: right; font-size: 14px; line-height: 20px; color: #888; }
    </style>
</head>
<body>
    <table border="0">
        <tbody>
            <tr>
                <td class="brand">Astra Stream</td>
                <td class="version">Astra v.]] .. astra.version .. [[</td>
            </tr>
        </tbody>
    </table>
    <table border="1">
        <thead>
            <tr>
                <th>#</th>
                <th>IP</th>
                <th>Source</th>
                <th>Uptime</th>
            </tr>
        </thead>
        <tbody>
]] .. table_content .. [[
        </tbody>
    </table>
</body>
</html>
]]
end
 
function on_request_stat(server, client, request)
    if not request then
        return nil
    end
 
    server:send(client, {
        code = 200,
        headers = {
            "Content-Type: text/html; charset=utf-8",
            "Connection: close",
        },
        content = render_stat_html(),
    })
end
 
http_server({
    addr = "0.0.0.0",
    port = http_stat_port,
    route = {
        { "/", on_request_stat },
    },
})

Comments