luci-app-vpnbypass: better service control buttons 3223/head
authorStan Grishin <stangri@melmac.net>
Sun, 20 Oct 2019 22:42:08 +0000 (15:42 -0700)
committerStan Grishin <stangri@melmac.net>
Sun, 20 Oct 2019 22:42:08 +0000 (15:42 -0700)
Signed-off-by: Stan Grishin <stangri@melmac.net>
applications/luci-app-vpnbypass/Makefile
applications/luci-app-vpnbypass/luasrc/controller/vpnbypass.lua
applications/luci-app-vpnbypass/luasrc/model/cbi/vpnbypass.lua
applications/luci-app-vpnbypass/luasrc/view/vpnbypass/buttons.htm [new file with mode: 0644]
applications/luci-app-vpnbypass/luasrc/view/vpnbypass/css.htm [new file with mode: 0644]
applications/luci-app-vpnbypass/luasrc/view/vpnbypass/js.htm [new file with mode: 0644]
applications/luci-app-vpnbypass/luasrc/view/vpnbypass/status-textarea.htm [new file with mode: 0644]
applications/luci-app-vpnbypass/luasrc/view/vpnbypass/status.htm [new file with mode: 0644]

index 7c89090072b07813d7241da3a5bba9ff28759e5d..133c4bc33df75cf1262a3b920dceb650d53442d5 100644 (file)
@@ -10,7 +10,7 @@ LUCI_TITLE:=VPN Bypass Web UI
 LUCI_DESCRIPTION:=Provides Web UI for VPNBypass service.
 LUCI_DEPENDS:=+luci-mod-admin-full +vpnbypass
 LUCI_PKGARCH:=all
-PKG_RELEASE:=11
+PKG_RELEASE:=12
 
 include ../../luci.mk
 
index 56221e043ff86136371ba5fe5927adc0fd368c64..d034fce324be3fdb334a64b5c0f356670d1cc8d7 100644 (file)
@@ -6,5 +6,24 @@ function index()
                else
                        entry({"admin", "services", "vpnbypass"}, cbi("vpnbypass"), _("VPN Bypass"))
                end
+       entry({"admin", "services", "vpnbypass", "action"}, call("vpnbypass_action"), nil).leaf = true
        end
 end
+
+function vpnbypass_action(name)
+       local packageName = "vpnbypass"
+       if name == "start" then
+               luci.sys.init.start(packageName)
+       elseif name == "action" then
+               luci.util.exec("/etc/init.d/" .. packageName .. " reload >/dev/null 2>&1")
+               luci.util.exec("/etc/init.d/dnsmasq restart >/dev/null 2>&1")
+       elseif name == "stop" then
+               luci.sys.init.stop(packageName)
+       elseif name == "enable" then
+               luci.util.exec("uci set " .. packageName .. ".config.enabled=1; uci commit " .. packageName)
+       elseif name == "disable" then
+               luci.util.exec("uci set " .. packageName .. ".config.enabled=0; uci commit " .. packageName)
+       end
+       luci.http.prepare_content("text/plain")
+       luci.http.write("0")
+end
index e3c5753f4c28569afd416af62a6bd7acf38c4d02..8d3ef2b8eeb843c19eeb1a08363cad62d42b6d69 100644 (file)
@@ -1,45 +1,32 @@
 local readmeURL = "https://github.com/openwrt/packages/blob/master/net/vpnbypass/files/README.md"
-
-m = Map("vpnbypass", translate("VPN Bypass Settings"))
-
-h = m:section(NamedSection, "config", "vpnbypass", translate("Service Status"))
-local packageName = "vpnbypass"
 local uci = require "luci.model.uci".cursor()
 local sys = require "luci.sys"
-local http = require "luci.http"
-local dispatcher = require "luci.dispatcher"
 local util = require "luci.util"
-en = h:option(Button, "__toggle")
-if enabledFlag ~= "1" then
-       en.title      = translate("Service is disabled/stopped")
-       en.inputtitle = translate("Enable/Start")
-       en.inputstyle = "apply important"
-else
-       en.title      = translate("Service is enabled/started")
-       en.inputtitle = translate("Stop/Disable")
-       en.inputstyle = "reset important"
+local packageName = "vpnbypass"
+
+m = Map("vpnbypass", translate("VPN Bypass Settings"))
+
+local tmpfsVersion = tostring(util.trim(sys.exec("opkg list-installed " .. packageName .. " | awk '{print $3}'")))
+if not tmpfsVersion or tmpfsVersion == "" then
+  tmpfsStatusCode = -1
+  tmpfsVersion = ""
+  tmpfsStatus = packageName .. " " .. translate("is not installed or not found")
+else  
+  tmpfsVersion = " [" .. packageName .. " " .. tmpfsVersion .. "]"
 end
-function en.write()
-       enabledFlag = enabledFlag == "1" and "0" or "1"
-       uci:set(packageName, "config", "enabled", enabledFlag)
-       uci:save(packageName)
-       uci:commit(packageName)
-       if enabledFlag == "0" then
-               sys.init.stop(packageName)
-               sys.init.disable(packageName)
-       else
-               sys.init.enable(packageName)
-               sys.init.start(packageName)
-       end
-       if dispatcher.lookup("admin/vpn") then
-               http.redirect(dispatcher.build_url("admin", "vpn", packageName))
-       else
-               http.redirect(dispatcher.build_url("admin", "services", packageName))
-       end
+local tmpfsStatus = "Stopped"
+if sys.call("iptables -t mangle -L | grep -q VPNBYPASS") == 0 then
+       tmpfsStatus = "Running"
 end
 
-s = m:section(NamedSection, "config", "vpnbypass", translate("VPN Bypass Rules"))
+h = m:section(NamedSection, "config", packageName, translate("Service Status") .. tmpfsVersion)
+ss = h:option(DummyValue, "_dummy", translate("Service Status"))
+ss.template = packageName .. "/status"
+ss.value = tmpfsStatus
+buttons = h:option(DummyValue, "_dummy")
+buttons.template = packageName .. "/buttons"
 
+s = m:section(NamedSection, "config", "vpnbypass", translate("VPN Bypass Rules"))
 -- Local Ports
 p1 = s:option(DynamicList, "localport", translate("Local Ports to Bypass"), translate("Local ports to trigger VPN Bypass"))
 p1.datatype    = "portrange"
diff --git a/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/buttons.htm b/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/buttons.htm
new file mode 100644 (file)
index 0000000..9f79b9b
--- /dev/null
@@ -0,0 +1,53 @@
+<%#
+  Copyright 2019 Stan Grishin <stangri@melmac.net>
+-%>
+
+<%-
+       local packageName = "vpnbypass"
+       local tmpfsStatus = "Stopped"
+       local enabledFlag = luci.model.uci.cursor():get(packageName, "config", "enabled")
+       if luci.sys.call("iptables -t mangle -L | grep -q " .. packageName:upper()) == 0 then
+               tmpfsStatus = "Running"
+       end
+
+       if tmpfsStatus == "Stopped" then
+               btn_start_style = "cbi-button cbi-button-apply important"
+               btn_action_style = "cbi-button cbi-button-apply important"
+               btn_stop_style = "cbi-button cbi-button-reset -disabled"
+       else
+               btn_start_style = "cbi-button cbi-button-apply -disabled"
+               btn_action_style = "cbi-button cbi-button-apply important"
+               btn_stop_style = "cbi-button cbi-button-reset important"
+       end
+       if enabledFlag ~= "1" then
+               btn_start_style = "cbi-button cbi-button-apply -disabled"
+               btn_action_style = "cbi-button cbi-button-apply -disabled"
+               btn_enable_style = "cbi-button cbi-button-apply important"
+               btn_disable_style = "cbi-button cbi-button-reset -disabled"
+       else
+               btn_enable_style = "cbi-button cbi-button-apply -disabled"
+               btn_disable_style = "cbi-button cbi-button-reset important"
+       end
+-%>
+
+<%+vpnbypass/css%>
+<%+vpnbypass/js%>
+
+<div class="cbi-value"><label class="cbi-value-title">Service Control</label>
+       <div class="cbi-value-field">
+               <input type="button" class="<%=btn_start_style%>" id="btn_start" name="start" value="<%:Start%>" onclick="button_action(this)" />
+               <span id="btn_start_spinner" class="btn_spinner"></span>
+               <input type="button" class="<%=btn_action_style%>" id="btn_action" name="action" value="<%:Reload%>" onclick="button_action(this)" />
+               <span id="btn_action_spinner" class="btn_spinner"></span>
+               <input type="button" class="<%=btn_stop_style%>" id="btn_stop" name="stop" value="<%:Stop%>" onclick="button_action(this)"  />
+               <span id="btn_stop_spinner" class="btn_spinner"></span>
+               &nbsp;
+               &nbsp;
+               &nbsp;
+               &nbsp;
+               <input type="button" class="<%=btn_enable_style%>" id="btn_enable" name="enable" value="<%:Enable%>" onclick="button_action(this)"  />
+               <span id="btn_enable_spinner" class="btn_spinner"></span>
+               <input type="button" class="<%=btn_disable_style%>" id="btn_disable" name="disable" value="<%:Disable%>" onclick="button_action(this)"  />
+               <span id="btn_disable_spinner" class="btn_spinner"></span>
+       </div>
+</div>
diff --git a/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/css.htm b/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/css.htm
new file mode 100644 (file)
index 0000000..6fb3d51
--- /dev/null
@@ -0,0 +1,9 @@
+<style type="text/css">
+       .btn_spinner
+       {
+               display: inline-block;
+               width: 0px;
+               height: 16px;
+               margin: 0 0px;
+       }
+</style>
diff --git a/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/js.htm b/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/js.htm
new file mode 100644 (file)
index 0000000..76809e9
--- /dev/null
@@ -0,0 +1,60 @@
+
+<script type="text/javascript">
+//<![CDATA[
+ function button_action(action) {
+  var xhr = new XHR(false);
+  var btn_start = document.getElementById("btn_start");
+  var btn_action = document.getElementById("btn_action");
+  var btn_stop = document.getElementById("btn_stop");
+  var btn_enable = document.getElementById("btn_enable");
+  var btn_disable = document.getElementById("btn_disable");
+       var btn_spinner;
+       switch (action.name) {
+               case "start":
+                       btn_spinner = document.getElementById("btn_start_spinner");
+                       break;
+               case "action":
+                       btn_spinner = document.getElementById("btn_action_spinner");
+                       break;
+               case "stop":
+                       btn_spinner = document.getElementById("btn_stop_spinner");
+                       break;
+               case "enable":
+                       btn_spinner = document.getElementById("btn_enable_spinner");
+                       break;
+               case "disable":
+                       btn_spinner = document.getElementById("btn_disable_spinner");
+                       break;
+       }
+       btn_start.disabled = true;
+       btn_action.disabled = true;
+       btn_stop.disabled = true;
+       btn_enable.disabled = true;
+       btn_disable.disabled = true;
+  spinner(btn_spinner, 1);
+       xhr.get('<%=luci.dispatcher.build_url("admin", "services", "vpnbypass", "action")%>/' + action.name, null,
+               function (x) {
+                       if (!x) {
+                               return;
+                       }
+      btn_start.disabled = false;
+      btn_action.disabled = false;
+      btn_stop.disabled = false;
+      btn_enable.disabled = false;
+      btn_disable.disabled = false;
+                       spinner(btn_spinner, 0);
+      location.reload();
+     });
+}
+function spinner(element, state) {
+       if (state === 1) {
+    element.style.width = "16px";
+               element.innerHTML = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" />';
+       }
+       else {
+    element.style.width = "0px";
+               element.innerHTML = '';
+       }
+}
+//]]>
+</script>
diff --git a/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/status-textarea.htm b/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/status-textarea.htm
new file mode 100644 (file)
index 0000000..75243e3
--- /dev/null
@@ -0,0 +1,13 @@
+<%#
+Copyright 2017-2019 Stan Grishin (stangri@melmac.net)
+This is free software, licensed under the Apache License, Version 2.0
+-%>
+
+<%+cbi/valueheader%>
+
+<textarea rows="<%=select(2, self:cfgvalue(section):gsub('\n', '\n'))%>"
+  style="border:none;box-shadow:none;background:transparent;font-weight:bold;line-height:20px;width:50em;padding:none;margin:6px;resize:none;overflow:hidden;"
+  disabled="disabled"><%=self:cfgvalue(section):gsub('\n', '\n')%>
+</textarea>
+
+<%+cbi/valuefooter%>
diff --git a/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/status.htm b/applications/luci-app-vpnbypass/luasrc/view/vpnbypass/status.htm
new file mode 100644 (file)
index 0000000..c453428
--- /dev/null
@@ -0,0 +1,10 @@
+<%#
+Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
+This is free software, licensed under the Apache License, Version 2.0
+-%>
+
+<%+cbi/valueheader%>
+
+<input name="status" id="status" type="text" class="cbi-input-text" style="outline:none;border:none;box-shadow:none;background:transparent;font-weight:bold;line-height:30px;height:30px;width:50em;" value="<%=self:cfgvalue(section)%>" disabled="disabled" />
+
+<%+cbi/valuefooter%>