luci-app-frpc/frps:add service status display 5143/head
authorSiYao Mo <msylgj@vip.qq.com>
Wed, 23 Jun 2021 16:55:28 +0000 (00:55 +0800)
committerSiYao Mo <msylgj@vip.qq.com>
Thu, 24 Jun 2021 09:56:32 +0000 (17:56 +0800)
reference from luci-app-smartdns
Special thanks to Nick Peng <pymumu@gmail.com>

Signed-off-by: SiYao Mo <msylgj@vip.qq.com>
applications/luci-app-frpc/htdocs/luci-static/resources/view/frpc.js
applications/luci-app-frpc/root/usr/share/rpcd/acl.d/luci-app-frpc.json
applications/luci-app-frps/htdocs/luci-static/resources/view/frps.js
applications/luci-app-frps/root/usr/share/rpcd/acl.d/luci-app-frps.json

index 6c16ab800f01881827289c06b5dc32c1dea5c4fa..fb8f679b32a00737d5f65dc6e6fda1808c5496c9 100644 (file)
@@ -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 = "<span style=\"color:%s;font-weight:bold;margin-left:15px\">%s - %s</span>";
+
+       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;
 
index b2bb22325ab78073e57c76cbe82087e3e4d7e4a9..b76c163d2172609101772e3c12d1dbef483ab63a 100644 (file)
@@ -6,6 +6,9 @@
                                "/etc/passwd": ["read"],
                                "/etc/group": ["read"]
                        },
+                       "ubus": {
+                               "service": [ "list" ]
+                       },
                        "uci": ["frpc"]
                },
                "write": {
index cb092ce6bb0072b5e44e668f27acf4e295a4b5e1..25f0714d83bbb07c71b2b347549189ab080ffe0f 100644 (file)
@@ -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 = "<span style=\"color:%s;font-weight:bold;margin-left:15px\">%s - %s</span>";
+
+       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;
 
index b065945b351dd7a23ab77fa5bf9e255fae7f0bb0..2981b5def0c9f61b1844fe4e5febf0c910fb7a7e 100644 (file)
@@ -6,6 +6,9 @@
                                "/etc/group": [ "read" ],
                                "/etc/passwd": [ "read" ]
                        },
+                       "ubus": {
+                               "service": [ "list" ]
+                       },
                        "uci": ["frps"]
                },
                "write": {