-- xProxy. Web-UI mod -- http://cesbo.com/astra -- -- Copyright (C) 2014, Andrey Dyldin -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation, either version 3 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . -- -- Usage: -- Save into the /etc/astra/xproxy-mod.lua -- Execute: astra --xproxy /etc/astra/xproxy-mod.lua xproxy_config_path = "/etc/astra/xproxy-db.json" function auth_request(client_id, request, auth_callback) if not request then return nil end function check_auth() if not request.query or not request.query.user then return false end for _,a in ipairs(xproxy_config.accounts) do if a.user == request.query.user then return (a.pass == request.query.pass) end end return false end if check_auth() then auth_callback(true) else auth_callback(false) end end -- channels = {} xproxy_config = nil function xproxy_config_load() if utils.stat(xproxy_config_path).type == "file" then xproxy_config = json.load(xproxy_config_path) end if not xproxy_config then json.save(xproxy_config_path, { options = { no_udp = true, no_rtp = true, no_http = true, }, accounts = {}, channels = {}, }) xproxy_config = json.load(xproxy_config_path) if xproxy_config == nil then log.error("[xProxy] failed to load " .. xproxy_config_path) astra.exit() end end for _,c in pairs(xproxy_config.channels) do channels[c.path] = c.source end if xproxy_config.options.auth and #xproxy_config.options.auth > 0 then xproxy_pass = "Basic " .. base64.encode(xproxy_config.options.auth) end if xproxy_config.options.no_udp == true then xproxy_allow_udp = false end if xproxy_config.options.no_rtp == true then xproxy_allow_rtp = false end if xproxy_config.options.no_http == true then xproxy_allow_http = false end end xproxy_config_load() function render_stat_html() return [[ xProxy : Web-UI
Loading...
© 2014 Cesbo Ltd. All rights reserved.
]] end function on_http_stat(server, client, request) if not request then return nil end if not xproxy_config then server:abort(client, 404, "Access List is not defined") return nil end if xproxy_pass then if request.headers["authorization"] ~= xproxy_pass then server:send(client, { code = 401, headers = { "WWW-Authenticate: Basic realm=\"xProxy\"", "Content-Length: 0", "Connection: close", } }) return end end if request.method == "POST" then local data = json.decode(request.content) if not data then server:abort(client, 400) elseif data.cmd == "load" then local r = { version = astra.version, xproxy = {}, } r.xproxy.options = xproxy_config.options if #xproxy_config.accounts > 0 then r.xproxy.accounts = xproxy_config.accounts end if #xproxy_config.channels > 0 then r.xproxy.channels = xproxy_config.channels end server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode(r), }) elseif data.cmd == "stat" then local ct = os.time() local l = {} for i,c in pairs(client_list) do local dt = ct - c.st local uptime = string.format("%02d:%02d", (dt / 3600), (dt / 60) % 60) table.insert(l, { id = i, addr = c.addr, path = c.path, uptime = uptime, }) end local r = {} if #l > 0 then r.stat = l end server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode(r), }) elseif data.cmd == "disconnect" then local client_id = tonumber(data.id) if client_list[client_id] then server:close(client_list[client_id].client) end server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode({ success = true }) }) elseif data.cmd == "restart" then timer({ interval = 1, callback = function(self) self:close() astra.reload() end, }) server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode({ success = true }) }) elseif data.cmd == "save" then if data.scope == "options" then xproxy_config.options = data.options xproxy_pass = "Basic " .. base64.encode(xproxy_config.options.auth) json.save(xproxy_config_path, xproxy_config) server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode({ success = true }) }) elseif data.scope == "account" then local id = tonumber(data.id) if id == nil then server:abort(client, 400) return nil end if id == 0 then table.insert(xproxy_config.accounts, data.account) else xproxy_config.accounts[id] = data.account end json.save(xproxy_config_path, xproxy_config) server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode({ success = data.account }) }) elseif data.scope == "channel" then local id = tonumber(data.id) if id == nil then server:abort(client, 400) return nil end if id == 0 then table.insert(xproxy_config.channels, data.channel) channels[data.channel.path] = data.channel.source else local c = xproxy_config.channels[id] channels[c.path] = nil channels[data.channel.path] = data.channel.source xproxy_config.channels[id] = data.channel end json.save(xproxy_config_path, xproxy_config) server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode({ success = data.channel }) }) else server:abort(client, 400) end elseif data.cmd == "delete" then if data.scope == "account" then local id = tonumber(data.id) if id == nil then server:abort(client, 400) return nil end table.remove(xproxy_config.accounts, id) json.save(xproxy_config_path, xproxy_config) server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode({ success = true }) }) elseif data.scope == "channel" then local id = tonumber(data.id) if id == nil then server:abort(client, 400) return nil end local c = xproxy_config.channels[id] channels[c.path] = nil table.remove(xproxy_config.channels, id) json.save(xproxy_config_path, xproxy_config) server:send(client, { code = 200, headers = { "Content-Type: application/json", "Connection: close", }, content = json.encode({ success = true }) }) else server:abort(client, 400) end else server:abort(client, 400) end return nil end server:send(client, { code = 200, headers = { "Content-Type: text/html; charset=utf-8", "Connection: close", }, content = render_stat_html(), }) end