and_cesbo

Astra Stream : HTTP connections list

Feb 5th, 2015
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. -- Astra Stream : HTTP connections list
  2. -- Usage:
  3. -- 1. Save script into the file: /etc/astra/stat.lua
  4. -- 2. Launch astra: astra --stream /etc/astra/stat.lua /etc/astra/config.lua
  5. -- 3. Access to the stat in browser: http://server-address:5000/
  6.  
  7. local http_stat_port = 5000
  8.  
  9. --
  10.  
  11. function render_stat_html()
  12.     local table_content = ""
  13.     local i = 1
  14.     local ct = os.time()
  15.  
  16.     for _, http_instance in pairs(http_output_instance_list) do
  17.         for channel_path, output_data in pairs(http_instance.__options.channel_list) do
  18.             for _, client_data in pairs(output_data.client_list) do
  19.                 local dt = ct - client_data.st
  20.                 local uptime = string.format("%02d:%02d", (dt / 3600), (dt / 60) % 60)
  21.                 table_content = table_content ..
  22.                                 "<tr>" ..
  23.                                 "<td>" .. i .. "</td>" ..
  24.                                 "<td>" .. client_data.addr .. "</td>" ..
  25.                                 "<td>" .. client_data.path .. "</td>" ..
  26.                                 "<td>" .. uptime .. "</td>" ..
  27.                                 "</tr>\r\n"
  28.                 i = i + 1
  29.             end
  30.         end
  31.     end
  32.  
  33.     return [[<!DOCTYPE html>
  34.  
  35. <html lang="en">
  36. <head>
  37.     <meta charset="utf-8" />
  38.     <title>Astra Stream : Statistics</title>
  39.     <style type="text/css">
  40. body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #333333; }
  41. table { width: 600px; margin: auto; }
  42. .brand { text-align: left; font-size: 18px; line-height: 20px; }
  43. .version { text-align: right; font-size: 14px; line-height: 20px; color: #888; }
  44.     </style>
  45. </head>
  46. <body>
  47.     <table border="0">
  48.         <tbody>
  49.             <tr>
  50.                 <td class="brand">Astra Stream</td>
  51.                 <td class="version">Astra v.]] .. astra.version .. [[</td>
  52.             </tr>
  53.         </tbody>
  54.     </table>
  55.     <table border="1">
  56.         <thead>
  57.             <tr>
  58.                 <th>#</th>
  59.                 <th>IP</th>
  60.                 <th>Source</th>
  61.                 <th>Uptime</th>
  62.             </tr>
  63.         </thead>
  64.         <tbody>
  65. ]] .. table_content .. [[
  66.         </tbody>
  67.     </table>
  68. </body>
  69. </html>
  70. ]]
  71. end
  72.  
  73. function on_request_stat(server, client, request)
  74.     if not request then
  75.         return nil
  76.     end
  77.  
  78.     server:send(client, {
  79.         code = 200,
  80.         headers = {
  81.             "Content-Type: text/html; charset=utf-8",
  82.             "Connection: close",
  83.         },
  84.         content = render_stat_html(),
  85.     })
  86. end
  87.  
  88. http_server({
  89.     addr = "0.0.0.0",
  90.     port = http_stat_port,
  91.     route = {
  92.         { "/", on_request_stat },
  93.     },
  94. })
Add Comment
Please, Sign In to add comment