From: SiYao Mo Date: Thu, 24 Jun 2021 08:01:18 +0000 (+0800) Subject: luci-app-frpc/frps:add service status display X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=ad08c8e912e09f6aadfbca9b54aa4c974cf377ab;p=project%2Fluci.git luci-app-frpc/frps:add service status display reference from luci-app-smartdns Special thanks to Nick Peng Signed-off-by: SiYao Mo --- diff --git a/applications/luci-app-frpc/htdocs/luci-static/resources/view/frpc.js b/applications/luci-app-frpc/htdocs/luci-static/resources/view/frpc.js index 6c16ab800f..fb8f679b32 100644 --- a/applications/luci-app-frpc/htdocs/luci-static/resources/view/frpc.js +++ b/applications/luci-app-frpc/htdocs/luci-static/resources/view/frpc.js @@ -2,6 +2,7 @@ 'require view'; 'require ui'; 'require form'; +'require rpc'; 'require tools.widgets as widgets'; // [Widget, Option, Title, Description, {Param: 'Value'}], @@ -115,12 +116,60 @@ function defOpts(s, opts, params) { } } +var callServiceList = rpc.declare({ + object: 'service', + method: 'list', + params: ['name'], + expect: { '': {} } +}); + +function getServiceStatus() { + return L.resolveDefault(callServiceList('frpc'), {}).then(function (res) { + var isRunning = false; + try { + isRunning = res['frpc']['instances']['instance1']['running']; + } catch (e) { } + return isRunning; + }); +} + +function renderStatus(isRunning) { + var renderHTML = ""; + var spanTemp = "%s - %s"; + + if (isRunning) { + renderHTML += String.format(spanTemp, 'green', _("frp Client"), _("RUNNING")); + } else { + renderHTML += String.format(spanTemp, 'red', _("frp Client"), _("NOT RUNNING")); + } + + return renderHTML; +} + return view.extend({ render: function() { var m, s, o; m = new form.Map('frpc', _('frp Client')); + s = m.section(form.NamedSection, '_status'); + s.anonymous = true; + s.render = function (section_id) { + L.Poll.add(function () { + return L.resolveDefault(getServiceStatus()).then(function(res) { + var view = document.getElementById("service_status"); + view.innerHTML = renderStatus(res); + }); + }); + + return E('div', { class: 'cbi-map' }, + E('div', { class: 'cbi-section'}, [ + E('div', { id: 'service_status' }, + _('Collecting data ...')) + ]) + ); + } + s = m.section(form.NamedSection, 'common', 'conf'); s.dynamic = true; diff --git a/applications/luci-app-frpc/root/usr/share/rpcd/acl.d/luci-app-frpc.json b/applications/luci-app-frpc/root/usr/share/rpcd/acl.d/luci-app-frpc.json index b2bb22325a..b76c163d21 100644 --- a/applications/luci-app-frpc/root/usr/share/rpcd/acl.d/luci-app-frpc.json +++ b/applications/luci-app-frpc/root/usr/share/rpcd/acl.d/luci-app-frpc.json @@ -6,6 +6,9 @@ "/etc/passwd": ["read"], "/etc/group": ["read"] }, + "ubus": { + "service": [ "list" ] + }, "uci": ["frpc"] }, "write": { diff --git a/applications/luci-app-frps/htdocs/luci-static/resources/view/frps.js b/applications/luci-app-frps/htdocs/luci-static/resources/view/frps.js index cb092ce6bb..25f0714d83 100644 --- a/applications/luci-app-frps/htdocs/luci-static/resources/view/frps.js +++ b/applications/luci-app-frps/htdocs/luci-static/resources/view/frps.js @@ -1,6 +1,7 @@ 'use strict'; 'require view'; 'require form'; +'require rpc'; 'require tools.widgets as widgets'; // [Widget, Option, Title, Description, {Param: 'Value'}], @@ -90,12 +91,60 @@ function defOpts(s, opts, params) { } } +var callServiceList = rpc.declare({ + object: 'service', + method: 'list', + params: ['name'], + expect: { '': {} } +}); + +function getServiceStatus() { + return L.resolveDefault(callServiceList('frps'), {}).then(function (res) { + var isRunning = false; + try { + isRunning = res['frps']['instances']['instance1']['running']; + } catch (e) { } + return isRunning; + }); +} + +function renderStatus(isRunning) { + var renderHTML = ""; + var spanTemp = "%s - %s"; + + if (isRunning) { + renderHTML += String.format(spanTemp, 'green', _("frp Server"), _("RUNNING")); + } else { + renderHTML += String.format(spanTemp, 'red', _("frp Server"), _("NOT RUNNING")); + } + + return renderHTML; +} + return view.extend({ render: function() { var m, s, o; m = new form.Map('frps', _('frp Server')); + s = m.section(form.NamedSection, '_status'); + s.anonymous = true; + s.render = function (section_id) { + L.Poll.add(function () { + return L.resolveDefault(getServiceStatus()).then(function(res) { + var view = document.getElementById("service_status"); + view.innerHTML = renderStatus(res); + }); + }); + + return E('div', { class: 'cbi-map' }, + E('div', { class: 'cbi-section'}, [ + E('div', { id: 'service_status' }, + _('Collecting data ...')) + ]) + ); + } + s = m.section(form.NamedSection, 'common', 'conf'); s.dynamic = true; diff --git a/applications/luci-app-frps/root/usr/share/rpcd/acl.d/luci-app-frps.json b/applications/luci-app-frps/root/usr/share/rpcd/acl.d/luci-app-frps.json index b065945b35..2981b5def0 100644 --- a/applications/luci-app-frps/root/usr/share/rpcd/acl.d/luci-app-frps.json +++ b/applications/luci-app-frps/root/usr/share/rpcd/acl.d/luci-app-frps.json @@ -6,6 +6,9 @@ "/etc/group": [ "read" ], "/etc/passwd": [ "read" ] }, + "ubus": { + "service": [ "list" ] + }, "uci": ["frps"] }, "write": {