Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- xProxy. Web-UI mod
- -- http://cesbo.com/astra
- --
- -- Copyright (C) 2014-2015, Andrey Dyldin <and@cesbo.com>
- --
- -- 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 <http://www.gnu.org/licenses/>.
- --
- -- Usage:
- -- Save into the /etc/astra/xproxy-ui.lua
- -- Execute: astra --relay /etc/astra/xproxy-ui.lua
- xproxy_config_path = "/etc/astra/xproxy-db.json"
- function xproxy_init_client(server, client, request, path)
- local client_data = server:data(client)
- local client_id = client_data.client_id
- if client_id then return nil end
- repeat
- client_id = math.random(10000000, 99000000)
- until not client_list[client_id]
- local p = path:sub(2)
- client_list[client_id] = {
- server = server,
- client = client,
- addr = request.addr,
- path = p,
- name = channel_names[p],
- st = os.time(),
- }
- client_data.client_id = client_id
- end
- client_addr_list = {}
- client_name_list = {}
- function check_account(user, pass, callback)
- if not user or not pass then
- callback(false)
- return nil
- end
- local as = xproxy_config.options.authserver
- if as ~= nil and as ~= "" then
- local opts = parse_url(as)
- opts.content = json.encode({ cmd = "auth", user = user, pass = pass })
- opts.method = "POST"
- opts.headers = {
- "User-Agent: Astra [xproxy-ui]",
- "Host: " .. opts.host .. ":" .. opts.port,
- "Content-Type: application/json",
- "Content-Length: " .. #opts.content,
- "Connection: close"
- }
- if opts.login and opts.password then
- local token = (opts.login .. ":" .. opts.password):b64e()
- table.insert(opts.headers, "Authorization: Basic " .. token)
- end
- opts.callback = function(request, response)
- callback(response.code == 200)
- end
- http_request(opts)
- return nil
- end
- local c = os.time()
- for _,a in ipairs(xproxy_config.accounts) do
- if a.user == user then
- if a.pass ~= pass then
- break
- end
- if a.enable == false then
- break
- end
- if not a.exp then
- break
- end
- local y, m, d = a.exp:match("(%d+)-(%d+)-(%d+)")
- local e = os.time({ year = tonumber(y), month = tonumber(m), day = tonumber(d) })
- if e <= c then
- break
- end
- callback(true)
- return nil
- end
- end
- callback(false)
- end
- function auth_request(client_id, request, auth_callback)
- if not request then
- if xproxy_config.options.check_ip then
- local a = client_list[client_id].addr
- if a then client_addr_list[a] = nil end
- end
- if xproxy_config.options.check_name then
- local a = client_list[client_id].user
- if a then client_name_list[a] = nil end
- end
- return nil
- end
- local path = request.path:sub(2) -- skip '/'
- if path == xproxy_config.options.access_denied_id then
- auth_callback(true)
- return nil
- end
- local user = nil
- local pass = nil
- if request.query then
- if request.query.auth then
- local b = request.query.auth:find(" ")
- if b then
- user = request.query.auth:sub(1, b - 1)
- pass = request.query.auth:sub(b + 1)
- end
- else
- user = request.query.user
- if request.query.login then user = request.query.login end
- pass = request.query.pass
- end
- end
- local function check_ip()
- if xproxy_config.options.check_ip then
- local cc = client_addr_list[request.addr]
- if cc then
- local c = client_list[cc]
- c.server:close(c.client)
- end
- client_addr_list[request.addr] = client_id
- end
- end
- local function check_name()
- if xproxy_config.options.check_name then
- local cc = client_name_list[user]
- if cc then
- local c = client_list[cc]
- c.server:close(c.client)
- end
- client_name_list[user] = client_id
- end
- client_list[client_id].user = user
- end
- local function check_result(result)
- if not result then
- if xproxy_config.options.access_denied_id then
- request.redirect = xproxy_config.options.access_denied_id
- auth_callback(true)
- else
- auth_callback(false)
- end
- else
- check_ip()
- check_name()
- auth_callback(true)
- end
- end
- check_account(user, pass, check_result)
- end
- function on_request_channel(server, client, request)
- local client_data = server:data(client)
- if not request then -- on_close
- kill_input(client_data.input)
- xproxy_kill_client(server, client)
- collectgarbage()
- return nil
- end
- local path = request.path:sub(2) -- skip '/'
- local channel = channels[path]
- if not channel then channel = channels["*"] end
- if not channel then
- server:abort(client, 404)
- return nil
- end
- local conf = parse_url(channel)
- if not conf then
- server:abort(client, 404)
- return nil
- end
- xproxy_init_client(server, client, request, request.path)
- local allow_channel = function()
- if request.redirect then
- channel = channels[request.redirect]
- if not channel then
- server:abort(client, 404)
- return nil
- end
- conf = parse_url(channel)
- if not conf then
- server:abort(client, 404)
- return nil
- end
- end
- conf.name = "Relay " .. client_data.client_id
- client_data.input = init_input(conf)
- server:send(client, {
- upstream = client_data.input.tail:stream(),
- buffer_size = relay_buffer_size,
- buffer_fill = relay_buffer_fill,
- })
- end
- do_auth_request(server, client, request, allow_channel)
- end
- --
- channels = {}
- channel_names = {}
- 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_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 _,a in pairs(xproxy_config.accounts) do
- if a.enable == nil then
- a.enable = true
- end
- end
- for _,c in pairs(xproxy_config.channels) do
- if c.status then
- c.enable = (c.status == "active")
- c.status = nil
- end
- if c.enable then
- channels[c.path] = c.source
- channel_names[c.path] = c.name
- end
- end
- if xproxy_config.options.auth then
- local a = split(xproxy_config.options.auth, ":")
- xproxy_config.options.login = a[1]
- xproxy_config.options.pass = a[2]
- xproxy_config.options.auth = nil
- end
- if xproxy_config.options.login and #xproxy_config.options.login > 0 then
- xproxy_pass = "Basic " .. base64.encode(xproxy_config.options.login .. ":" .. xproxy_config.options.pass)
- end
- if xproxy_config.options.no_udp == true then
- xproxy_allow_udp = false
- relay_allow_udp = false
- end
- if xproxy_config.options.no_http == true then
- xproxy_allow_http = false
- relay_allow_http = false
- end
- end
- xproxy_config_load()
- function render_stat_html()
- return [[<!DOCTYPE html>
- <html lang="en" ng-app="App">
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>xProxy : Web-UI</title>
- <link lazy-href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
- <link lazy-href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" />
- <style type="text/css">
- * { box-sizing: border-box; }
- html { height: 100%; }
- body { height: 100%; margin: 0; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; color: #333333; }
- a:not([href]) { cursor: pointer; }
- .xproxy-nav { padding: 10px 15px; }
- .wrap { min-height: 100%; }
- .content { overflow: visible; padding-top: 5px; padding-bottom: 25px; }
- .footer { position: relative; height: 20px; margin-top: -20px; width: 100%; padding: 0 15px; }
- .footer span { font-size: 0.8em; color: #bbb; display: inline-block; width: 50%; padding: 0 15px; }
- .footer .version { text-align: right; }
- .app-status { position: fixed; z-index: 10000; top: 0; right: 0; bottom: 0; left: 0; padding: 20px; background: #ffffff; }
- .options-group>div { padding-left: 5px; padding-right: 5px; }
- .options-group>div:first-child { padding-left: 15px; }
- .options-group>div:last-child { padding-right: 15px; }
- .quickdate { display: block; position: relative; vertical-align: bottom; font-size: 15px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; }
- .quickdate-button { background: #ffffff; color: #333333; font-size: 14px; border: solid 1px #cccccc; box-shadow: outset 0 1px 1px rgba(0, 0, 0, 0.075); border-radius: 4px; padding: 6px 12px; display: block; text-decoration: none; width: 100%; height: 34px; }
- .quickdate-button:hover i { text-decoration: none; }
- .quickdate-button i { padding-right: 4px; }
- .quickdate-button div, .quickdate-action-link div { display: inline; }
- .quickdate-popup { z-index: 10; border: solid 1px #000; text-align: center; width: 250px; display: none; position: absolute; padding: 5px; color: #333333; font-size: 15px; background-color: #fafafa; border: solid 1px #dddddd; border-radius: 3px; -webkit-box-shadow: 0px 10px 30px rgba(25, 25, 25, 0.92); -moz-box-shadow: 0px 10px 30px rgba(25, 25, 25, 0.92); box-shadow: 0px 10px 30px rgba(25, 25, 25, 0.92); }
- .quickdate-popup.open { display: block; }
- .quickdate-close { display: none; }
- .quickdate-calendar-header { display: block; padding: 2px 0; margin-bottom: 5px; text-align: center; }
- .quickdate-month { display: inline-block; }
- a.quickdate-prev-month { float: left; }
- a.quickdate-next-month { float: right; }
- .quickdate-text-inputs { display: none; }
- input.quickdate-date-input, input.quickdate-time-input { width: 100px; margin: 0; height: auto; padding: 2px 3px; }
- table.quickdate-calendar { border-collapse: collapse; border-spacing: 0; width: 100%; margin-top: 5px; }
- table.quickdate-calendar th, table.quickdate-calendar td { padding: 5px; }
- table.quickdate-calendar td:hover { cursor: pointer; }
- .quickdate input.ng-invalid { border: 1px solid #dd3b30; }
- .quickdate input.ng-invalid:focus { outline-color: #dd3b30; }
- .quickdate-action-link:visited, .quickdate-action-link:hover { color: #333333; }
- .quickdate-next-month i { padding-left: 10px; }
- .quickdate-prev-month i { padding-right: 10px; }
- table.quickdate-calendar { border: solid 1px #ccc; background-color: #ffffff; }
- table.quickdate-calendar th, table.quickdate-calendar td { border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; }
- table.quickdate-calendar th { font-size: 10px; }
- table.quickdate-calendar td:hover { background-color: #e6e6e6; }
- table.quickdate-calendar td.other-month { background-color: #dbdbdb; color: #808080; }
- table.quickdate-calendar td.other-month:hover { background-color: #c7c7c7; }
- table.quickdate-calendar td.disabled-date { background-color: inherit; color: #ffffff; }
- table.quickdate-calendar td.disabled-date:hover { background-color: inherit; cursor: default; }
- table.quickdate-calendar td.selected { background-color: #b0ccde; font-weight: bold; }
- table.quickdate-calendar td.is-today { color: #b58922; font-weight: bold; }
- table.quickdate-calendar td.is-today.disabled-date { color: #929292; font-weight: normal; }
- table.inline-form input[type=checkbox] { margin: 0; height: 34px; width: 100%; }
- .expired input, .expired .quickdate-button { border: 1px solid #d9534f; }
- </style>
- </head>
- <body>
- <div class="app-status" ng-hide="version">Loading...</div>
- <div class="wrap">
- <nav class="xproxy-nav container-fluid"><div class="row">
- <div class="col-sm-3">
- </div>
- <div class="col-sm-offset-3 col-sm-6" style="white-space:nowrap;text-align:right;">
- <a href="#/stat" class="btn btn-default" style="width:120px;" ng-class="{'active':controller==='StatController'}">Connections</a>
- <a href="#/options" class="btn btn-default" style="width:120px;" ng-class="{'active':controller==='OptionsController'}">Options</a>
- <a href="#/accounts" class="btn btn-default" style="width:120px;" ng-class="{'active':controller==='AccountsController'}">Accounts</a>
- <a href="#/channels" class="btn btn-default" style="width:120px;" ng-class="{'active':controller==='ChannelsController'}">Channels</a>
- </div>
- </div></nav>
- <div class="container-fluid content" ng-view></div>
- </div> <!-- wrap -->
- <footer class="footer">
- <span class="copyright">© 2014 Cesbo Ltd. All rights reserved.</span><span class="version" ng-bind="version"></span>
- </footer>
- </body>
- <script type="text/ng-template" id="stat.html">
- <div class="row"><div class="col-sm-offset-2 col-sm-8">
- <table class="table"><thead><tr>
- <th width="100px">Session ID</th>
- <th width="100px">User Name</th>
- <th width="150px">IP</th>
- <th>Channel</th>
- <th width="100px">Uptime</th>
- <th width="100px"></th>
- </tr></thead><tbody>
- <tr ng-repeat="c in stat">
- <td ng-bind="c.id"></td>
- <td ng-bind="c.user"></td>
- <td ng-bind="c.addr"></td>
- <td ng-bind="c.name"></td>
- <td ng-bind="c.uptime"></td>
- <td align="right"><a ng-click="disconnect(c.id)">Disconnect</a></td>
- </tr>
- <tr><td colspan="6"><a ng-click="refresh()">Refresh Connections List</a></td></tr>
- </tbody></table>
- </div></div>
- </script>
- <script type="text/ng-template" id="options.html">
- <div class="row"><div class="col-sm-offset-2 col-sm-8">
- <div class="form-horizontal">
- <div class="form-group">
- <label class="col-sm-3 control-label">Basic Authorization</label>
- <div class="col-sm-6"><div class="row options-group">
- <div class="col-sm-6"><input type="text" class="form-control" ng-model="xproxy.options.login" placeholder="Login" /></div>
- <div class="col-sm-6"><input type="password" class="form-control" ng-model="xproxy.options.pass" placeholder="Password" /></div>
- </div></div>
- </div>
- <div class="form-group">
- <label class="col-sm-3 control-label">Disable UDP proxy</label>
- <div class="col-sm-6"><label class="checkbox"><input type="checkbox" ng-model="xproxy.options.no_udp" /> <span style="color:#737373;font-weight:normal;">http://server/udp/*</span></label></div>
- </div>
- <div class="form-group">
- <label class="col-sm-3 control-label">Disable HTTP proxy</label>
- <div class="col-sm-6"><label class="checkbox"><input type="checkbox" ng-model="xproxy.options.no_http" /> <span style="color:#737373;font-weight:normal;">http://server/http/*</span></label></div>
- </div>
- <div class="form-group">
- <label class="col-sm-3 control-label">Check client IP</label>
- <div class="col-sm-6"><label class="checkbox"><input type="checkbox" ng-model="xproxy.options.check_ip" /> <span style="color:#737373;font-weight:normal;">Prevent to use one account from different IP</span></label></div>
- </div>
- <div class="form-group">
- <label class="col-sm-3 control-label">Check client Name</label>
- <div class="col-sm-6"><label class="checkbox"><input type="checkbox" ng-model="xproxy.options.check_name" /> <span style="color:#737373;font-weight:normal;">Prevent to use one account twice</span></label></div>
- </div>
- <div class="form-group">
- <label class="col-sm-3 control-label">Auth Server</label>
- <div class="col-sm-6">
- <input type="text" class="form-control" ng-model="xproxy.options.authserver" placeholder="http://..." />
- <p class="help-block">Send authentication reuqest to the external server</p>
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-3 control-label">Access Denied Channel ID</label>
- <div class="col-sm-6">
- <input type="text" class="form-control" ng-model="xproxy.options.access_denied_id" placeholder="" />
- <p class="help-block">Show this channel if authentication has failed</p>
- </div>
- </div>
- <hr />
- <div class="form-group">
- <div class="col-sm-offset-3 col-sm-6">
- <button class="btn btn-primary" style="width:100px;" ng-click="options_save()">Save</button>
- <button class="btn btn-danger" style="width:100px;" ng-click="restart()">Restart</button>
- </div>
- </div>
- </div>
- </div></div>
- </script>
- <script type="text/ng-template" id="accounts.html">
- <div class="row"><div class="col-sm-offset-2 col-sm-8"><table class="table inline-form">
- <thead>
- <tr>
- <th width="35px"> </th>
- <th>User Name</th>
- <th>Password</th>
- <th width="200px">Expired Date</th>
- <th width="120px"></th>
- </tr>
- </thead><tbody>
- <tr>
- <td><input type="checkbox" class="checkbox" ng-model="account_new.enable" /></td>
- <td><input type="text" class="form-control" ng-model="account_new.user" /></td>
- <td><input type="text" class="form-control" ng-model="account_new.pass" /></td>
- <td><quick-datepicker ng-model="account_new.exp"></quick-datepicker></td>
- <td><button class="btn btn-success" ng-click="account_save(0)"><i class="fa fa-plus fa-lg fa-fw"></i></button></td>
- </tr>
- <tr ng-repeat="(i,a) in xproxy.accounts" ng-class="{'expired':is_expired(a)}">
- <td><input type="checkbox" class="checkbox" ng-model="a.enable" /></td>
- <td><input type="text" class="form-control" ng-model="a.user" /></td>
- <td><input type="text" class="form-control" ng-model="a.pass" /></td>
- <td><quick-datepicker ng-model="a.exp"></quick-datepicker></td>
- <td>
- <button class="btn btn-success" ng-click="account_save(i + 1)"><i class="fa fa-check fa-lg fa-fw"></i></button>
- <button class="btn btn-danger" ng-click="account_delete(i + 1)"><i class="fa fa-times fa-lg fa-fw"></i></button>
- </td>
- </tr>
- </tbody>
- </table></div></div>
- </script>
- <script type="text/ng-template" id="channels.html">
- <div class="row"><div class="col-sm-offset-2 col-sm-8"><table class="table inline-form">
- <thead>
- <tr>
- <th width="35px"> </th>
- <th>Name</th>
- <th>Source</th>
- <th>Channel ID</th>
- <th width="120px"></th>
- </tr>
- </thead><tbody>
- <tr>
- <td><input type="checkbox" class="checkbox" ng-model="channel_new.enable" /></td>
- <td><input type="text" class="form-control" ng-model="channel_new.name" /></td>
- <td><input type="text" class="form-control" ng-model="channel_new.source" /></td>
- <td><input type="text" class="form-control" ng-model="channel_new.path" /></td>
- <td><button class="btn btn-success" ng-click="channel_save(0)"><i class="fa fa-plus fa-lg fa-fw"></i></button></td>
- </tr>
- <tr ng-repeat="(i,c) in xproxy.channels">
- <td><input type="checkbox" class="checkbox" ng-model="c.enable" /></td>
- <td><input type="text" class="form-control" ng-model="c.name" /></td>
- <td><input type="text" class="form-control" ng-model="c.source" /></td>
- <td><input type="text" class="form-control" ng-model="c.path" /></td>
- <td>
- <button class="btn btn-success" ng-click="channel_save(i + 1)"><i class="fa fa-check fa-lg fa-fw"></i></button>
- <button class="btn btn-danger" ng-click="channel_delete(i + 1)"><i class="fa fa-times fa-lg fa-fw"></i></button>
- </td>
- </tr>
- </tbody>
- </table></div></div>
- </script>
- <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
- <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script>
- <script type="text/javascript">
- (function(){var a;a=angular.module("ngQuickDate",[]),a.provider("ngQuickDateDefaults",function(){return{options:{dateFormat:"dd/MM/yyyy",timeFormat:"h:mm a",labelFormat:null,placeholder:"Click to Set Date",hoverText:null,buttonIconHtml:null,closeButtonHtml:"×",nextLinkHtml:"Next →",prevLinkHtml:"← Prev",disableTimepicker:1,disableClearButton:1,defaultTime:null,dayAbbreviations:["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],dateFilter:null,parseDateFunction:function(a){var b;return b=Date.parse(a),isNaN(b)?null:new Date(b)}},$get:function(){return this.options},set:function(a,b){var c,d,e;if("object"==typeof a){e=[];for(c in a)d=a[c],e.push(this.options[c]=d);return e}return this.options[a]=b}}}),a.directive("quickDatepicker",["ngQuickDateDefaults","$filter","$sce",function(a,b,c){return{restrict:"E",require:"?ngModel",scope:{dateFilter:"=?",onChange:"&",required:"@"},replace:!0,link:function(d,e,f,g){var h,i,j,k,l,m,n,o,p,q,r,s,t;return m=function(){return q(),d.toggleCalendar(!1),d.weeks=[],d.inputDate=null,d.inputTime=null,d.invalid=!0,"string"==typeof f.initValue&&g.$setViewValue(f.initValue),p(),o()},q=function(){var b,e;for(b in a)e=a[b],b.match(/[Hh]tml/)?d[b]=c.trustAsHtml(a[b]||""):!d[b]&&f[b]?d[b]=f[b]:d[b]||(d[b]=a[b]);return d.labelFormat||(d.labelFormat=d.dateFormat,d.disableTimepicker||(d.labelFormat+=" "+d.timeFormat)),f.iconClass&&f.iconClass.length?d.buttonIconHtml=c.trustAsHtml("<i ng-show='iconClass' class='"+f.iconClass+"'></i>"):void 0},i=!1,window.document.addEventListener("click",function(){return d.calendarShown&&!i&&(d.toggleCalendar(!1),d.$apply()),i=!1}),angular.element(e[0])[0].addEventListener("click",function(){return i=!0}),o=function(){var a;return a=g.$modelValue?new Date(g.$modelValue):null,s(),r(a),d.mainButtonStr=a?b("date")(a,d.labelFormat):d.placeholder,d.invalid=g.$invalid},r=function(a){return null!=a?(d.inputDate=b("date")(a,d.dateFormat),d.inputTime=b("date")(a,d.timeFormat)):(d.inputDate=null,d.inputTime=null)},p=function(a){var b;return null==a&&(a=null),b=null!=a?new Date(a):new Date,"Invalid Date"===b.toString()&&(b=new Date),b.setDate(1),d.calendarDate=new Date(b)},s=function(){var a,b,c,e,f,h,i,k,m,n,o,p,q,r;for(h=d.calendarDate.getDay()-1,e=l(d.calendarDate.getFullYear(),d.calendarDate.getMonth()),f=Math.ceil((h+e)/7),o=[],a=new Date(d.calendarDate),a.setDate(a.getDate()+-1*h),i=p=0,r=f-1;r>=0?r>=p:p>=r;i=r>=0?++p:--p)for(o.push([]),c=q=0;6>=q;c=++q)b=new Date(a),d.defaultTime&&(m=d.defaultTime.split(":"),b.setHours(m[0]||0),b.setMinutes(m[1]||0),b.setSeconds(m[2]||0)),k=g.$modelValue&&b&&j(b,g.$modelValue),n=j(b,new Date),o[i].push({date:b,selected:k,disabled:"function"==typeof d.dateFilter?!d.dateFilter(b):!1,other:b.getMonth()!==d.calendarDate.getMonth(),today:n}),a.setDate(a.getDate()+1);return d.weeks=o},g.$parsers.push(function(a){return d.required&&null==a?(g.$setValidity("required",!1),null):angular.isDate(a)?(g.$setValidity("required",!0),a):angular.isString(a)?(g.$setValidity("required",!0),d.parseDateFunction(a)):null}),g.$formatters.push(function(a){return angular.isDate(a)?a:angular.isString(a)?d.parseDateFunction(a):void 0}),h=function(a,c){return b("date")(a,c)},t=function(a){return"string"==typeof a?n(a):a},n=a.parseDateFunction,j=function(a,b,c){return null==c&&(c=!1),c?a-b===0:(a=t(a),b=t(b),a&&b&&a.getYear()===b.getYear()&&a.getMonth()===b.getMonth()&&a.getDate()===b.getDate())},k=function(a,b){return a&&b?parseInt(a.getTime()/6e4)===parseInt(b.getTime()/6e4):!1},l=function(a,b){return[31,a%4===0&&a%100!==0||a%400===0?29:28,31,30,31,30,31,31,30,31,30,31][b]},g.$render=function(){return p(g.$viewValue),o()},g.$viewChangeListeners.unshift(function(){return p(g.$viewValue),o(),d.onChange?d.onChange():void 0}),d.$watch("calendarShown",function(a){var b;return a?(b=angular.element(e[0].querySelector(".quickdate-date-input"))[0],b.select()):void 0}),d.toggleCalendar=function(a){return d.calendarShown=isFinite(a)?a:!d.calendarShown},d.selectDate=function(a,b){var c;return null==b&&(b=!0),c=!g.$viewValue&&a||g.$viewValue&&!a||a&&g.$viewValue&&a.getTime()!==g.$viewValue.getTime(),"function"!=typeof d.dateFilter||d.dateFilter(a)?(g.$setViewValue(a),b&&d.toggleCalendar(!1),!0):!1},d.selectDateFromInput=function(a){var b,c,e,f;null==a&&(a=!1);try{if(c=n(d.inputDate),!c)throw"Invalid Date";if(!d.disableTimepicker&&d.inputTime&&d.inputTime.length&&c){if(f=d.disableTimepicker?"00:00:00":d.inputTime,e=n(""+d.inputDate+" "+f),!e)throw"Invalid Time";c=e}if(!k(g.$viewValue,c)&&!d.selectDate(c,!1))throw"Invalid Date";return a&&d.toggleCalendar(!1),d.inputDateErr=!1,d.inputTimeErr=!1}catch(h){if(b=h,"Invalid Date"===b)return d.inputDateErr=!0;if("Invalid Time"===b)return d.inputTimeErr=!0}},d.onDateInputTab=function(){return d.disableTimepicker&&d.toggleCalendar(!1),!0},d.onTimeInputTab=function(){return d.toggleCalendar(!1),!0},d.nextMonth=function(){return p(new Date(new Date(d.calendarDate).setMonth(d.calendarDate.getMonth()+1))),o()},d.prevMonth=function(){return p(new Date(new Date(d.calendarDate).setMonth(d.calendarDate.getMonth()-1))),o()},d.clear=function(){return d.selectDate(null,!0)},m()},template:"<div class='quickdate'>\n <a href='' ng-focus='toggleCalendar()' ng-click='toggleCalendar()' class='quickdate-button' title='{{hoverText}}'><div ng-hide='iconClass' ng-bind-html='buttonIconHtml'></div>{{mainButtonStr}}</a>\n <div class='quickdate-popup' ng-class='{open: calendarShown}'>\n <a href='' tabindex='-1' class='quickdate-close' ng-click='toggleCalendar()'><div ng-bind-html='closeButtonHtml'></div></a>\n <div class='quickdate-text-inputs'>\n <div class='quickdate-input-wrapper'>\n <label>Date</label>\n <input class='quickdate-date-input' ng-class=\"{'ng-invalid': inputDateErr}\" name='inputDate' type='text' ng-model='inputDate' placeholder='01/01/2014' ng-enter=\"selectDateFromInput(true)\" ng-blur=\"selectDateFromInput(false)\" on-tab='onDateInputTab()' />\n </div>\n <div class='quickdate-input-wrapper' ng-hide='disableTimepicker'>\n <label>Time</label>\n <input class='quickdate-time-input' ng-class=\"{'ng-invalid': inputTimeErr}\" name='inputTime' type='text' ng-model='inputTime' placeholder='12:00 PM' ng-enter=\"selectDateFromInput(true)\" ng-blur=\"selectDateFromInput(false)\" on-tab='onTimeInputTab()'>\n </div>\n </div>\n <div class='quickdate-calendar-header'>\n <a href='' class='quickdate-prev-month quickdate-action-link' tabindex='-1' ng-click='prevMonth()'><div ng-bind-html='prevLinkHtml'></div></a>\n <span class='quickdate-month'>{{calendarDate | date:'MMMM yyyy'}}</span>\n <a href='' class='quickdate-next-month quickdate-action-link' ng-click='nextMonth()' tabindex='-1' ><div ng-bind-html='nextLinkHtml'></div></a>\n </div>\n <table class='quickdate-calendar'>\n <thead>\n <tr>\n <th ng-repeat='day in dayAbbreviations'>{{day}}</th>\n </tr>\n </thead>\n <tbody>\n <tr ng-repeat='week in weeks'>\n <td ng-mousedown='selectDate(day.date, true, true)' ng-click='$event.preventDefault()' ng-class='{\"other-month\": day.other, \"disabled-date\": day.disabled, \"selected\": day.selected, \"is-today\": day.today}' ng-repeat='day in week'>{{day.date | date:'d'}}</td>\n </tr>\n </tbody>\n </table>\n <div class='quickdate-popup-footer'>\n <a href='' class='quickdate-clear' tabindex='-1' ng-hide='disableClearButton' ng-click='clear()'>Clear</a>\n </div>\n </div>\n</div>"}}]),a.directive("ngEnter",function(){return function(a,b,c){return b.bind("keydown keypress",function(b){return 13===b.which?(a.$apply(c.ngEnter),b.preventDefault()):void 0})}}),a.directive("onTab",function(){return{restrict:"A",link:function(a,b,c){return b.bind("keydown keypress",function(b){return 9!==b.which||b.shiftKey?void 0:a.$apply(c.onTab)})}}})}).call(this);
- </script>
- <script type="text/javascript">
- var App = angular.module("App", ["ngRoute", "ngQuickDate"]);
- App.directive("lazyHref", function() {
- return {
- restrict: "A",
- transclude: true,
- link: function (scope, element, attrs) {
- element.attr("type", "text/css");
- element.attr("rel", "stylesheet");
- element.attr("href", attrs.lazyHref);
- element.removeAttr("lazy-href");
- }
- };
- });
- App.config(["$routeProvider", function($routeProvider) {
- $routeProvider
- .when("/stat", {
- templateUrl: "stat.html",
- controller: "StatController"
- })
- .when("/options", {
- templateUrl: "options.html",
- controller: "OptionsController"
- })
- .when("/accounts", {
- templateUrl: "accounts.html",
- controller: "AccountsController"
- })
- .when("/channels", {
- templateUrl: "channels.html",
- controller: "ChannelsController"
- })
- .otherwise({
- redirectTo: '/stat'
- });;
- }]);
- App.run(["$rootScope",
- "$http",
- function($rootScope, $http)
- {
- angular.element(document).ready(function() {
- $http
- .post('/stat/', { 'cmd': 'load' })
- .success(function(data) {
- $rootScope.version = "Astra v." + data.version;
- $rootScope.xproxy = data.xproxy;
- if($rootScope.xproxy.accounts === undefined) $rootScope.xproxy.accounts = [];
- if($rootScope.xproxy.channels === undefined) $rootScope.xproxy.channels = [];
- });
- });
- }]);
- App.controller('AccountsController', ['$rootScope',
- '$scope',
- '$http',
- function($rootScope, $scope, $http)
- {
- $rootScope.controller = 'AccountsController';
- $scope.account_new = {};
- $scope.account_save = function(i) {
- var a = (i === 0) ? $scope.account_new : $rootScope.xproxy.accounts[i - 1];
- var e = false;
- if(a.user.length === 0) {
- a.user = "ERROR";
- e = true;
- }
- if(a.pass.length === 0) {
- a.pass = "ERROR";
- e = true;
- }
- if(e === true) return;
- $http
- .post('/stat/', {
- 'cmd': 'save',
- 'scope': 'account',
- 'id': i,
- 'account': a
- })
- .success(function(data) {
- if(i === 0 && data.success) $rootScope.xproxy.accounts.push(data.success);
- });
- $scope.account_new = {};
- };
- $scope.is_expired = function(a) {
- var cdate = new Date();
- cdate.setHours(0);
- cdate.setMinutes(0);
- cdate.setSeconds(0);
- var edate = new Date(a.exp);
- return (edate <= cdate);
- };
- $scope.account_delete = function(i) {
- $http
- .post('/stat/', {
- 'cmd': 'delete',
- 'scope': 'account',
- 'id': i
- })
- .success(function(data) {
- if(data.success) $rootScope.xproxy.accounts.splice(i - 1, 1);
- });
- };
- }]);
- App.controller('ChannelsController', ['$rootScope',
- '$scope',
- '$http',
- function($rootScope, $scope, $http)
- {
- $rootScope.controller = 'ChannelsController';
- $scope.channel_new = { 'enable': true };
- $scope.channel_save = function(i) {
- var c = (i === 0) ? $scope.channel_new : $rootScope.xproxy.channels[i - 1];
- var e = false;
- if(c.source.length === 0) {
- c.source = "ERROR";
- e = true;
- }
- if(c.path.length === 0) {
- c.path = "ERROR";
- e = true;
- }
- if(e === true) return;
- $http
- .post('/stat/', {
- 'cmd': 'save',
- 'scope': 'channel',
- 'id': i,
- 'channel': c
- })
- .success(function(data) {
- if(i === 0 && data.success) $rootScope.xproxy.channels.push(data.success);
- });
- $scope.channel_new = { 'enable': true };
- };
- $scope.channel_delete = function(i) {
- $http
- .post('/stat/', {
- 'cmd': 'delete',
- 'scope': 'channel',
- 'id': i
- })
- .success(function(data) {
- if(data.success) $rootScope.xproxy.channels.splice(i - 1, 1);
- });
- };
- }]);
- App.controller('OptionsController', ['$rootScope',
- '$scope',
- '$http',
- function($rootScope, $scope, $http)
- {
- $rootScope.controller = 'OptionsController';
- $scope.options_save = function() {
- var opts = $rootScope.xproxy.options;
- for(var k in opts) {
- if(!opts[k]) delete(opts[k]);
- }
- $http
- .post('/stat/', {
- 'cmd': 'save',
- 'scope': 'options',
- 'options': opts
- })
- .success(function(data) {
- //
- });
- };
- $scope.restart = function() {
- $http
- .post('/stat/', {
- 'cmd': 'restart',
- 'scope': 'options',
- })
- .success(function(data) {
- //
- });
- };
- }]);
- App.controller('StatController', ['$rootScope',
- '$scope',
- '$http',
- function($rootScope, $scope, $http)
- {
- $rootScope.controller = 'StatController';
- $scope.refresh = function() {
- $http
- .post('/stat/', { 'cmd': 'stat' })
- .success(function(data) {
- $scope.stat = (data.stat === undefined) ? [] : data.stat;
- });
- };
- $scope.disconnect = function(id) {
- $http
- .post('/stat/', { 'cmd': 'disconnect', 'id': id })
- .success(function(data) {
- for(var i = 0; i < $scope.stat.length; ++i) {
- if($scope.stat[i].id === id) {
- $scope.stat.splice(i, 1);
- return;
- }
- }
- });
- };
- $scope.refresh();
- }]);
- </script>
- </html>
- ]]
- end
- function on_request_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,
- user = c.user,
- addr = c.addr,
- path = c.path,
- name = c.name,
- 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
- if xproxy_config.options.login and #xproxy_config.options.login > 0 then
- xproxy_pass = "Basic " .. base64.encode(xproxy_config.options.login .. ":" .. xproxy_config.options.pass)
- else
- xproxy_config.options.login = nil
- xproxy_config.options.pass = nil
- end
- 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)
- if data.channel.enable then
- channels[data.channel.path] = data.channel.source
- channel_names[data.channel.path] = data.channel.name
- end
- else
- local c = xproxy_config.channels[id]
- channels[c.path] = nil
- channel_names[c.path] = nil
- if data.channel.enable then
- channels[data.channel.path] = data.channel.source
- channel_names[data.channel.path] = data.channel.name
- end
- 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
- channel_names[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
- elseif data.cmd == "auth" then
- check_account(data.user, data.pass, function(ok)
- if ok then
- server:send(client, {
- code = 200,
- headers = { "Content-Type: text/plain", "Connection: close", },
- content = "Ok"
- })
- else
- server:abort(client, 403)
- end
- 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
- xproxy_route = {
- { "/", on_request_stat },
- }
Add Comment
Please, Sign In to add comment