Merge pull request #2019 from dibdot/adblock
[oweals/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / overview_tab.lua
1 -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs       = require("nixio.fs")
5 local uci      = require("luci.model.uci").cursor()
6 local util     = require("luci.util")
7 local nw       = require("luci.model.network").init()
8 local fw       = require("luci.model.firewall").init()
9 local dump     = util.ubus("network.interface", "dump", {})
10 local trmiface = uci:get("travelmate", "global", "trm_iface") or "trm_wwan"
11 local uplink   = uci:get("network", trmiface) or ""
12
13 m = Map("travelmate", translate("Travelmate"),
14         translate("Configuration of the travelmate package to to enable travel router functionality. ")
15         .. translatef("For further information "
16         .. "<a href=\"%s\" target=\"_blank\">"
17         .. "see online documentation</a>", "https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md"))
18 m:chain("network")
19 m:chain("firewall")
20
21 function m.on_apply(self)
22         luci.sys.call("env -i /etc/init.d/travelmate restart >/dev/null 2>&1")
23 end
24
25 -- Interface Wizard
26
27 if uplink == "" then
28         ds = m:section(NamedSection, "global", "travelmate", translate("Interface Wizard"))
29         o = ds:option(Value, "trm_iface", translate("Create Uplink interface"),
30                 translate("Create a new wireless wan uplink interface, configure it to use dhcp and ")
31                 .. translate("add it to the wan zone of the firewall. ")
32                 .. translate("This step has only to be done once."))
33         o.datatype = "and(uciname,rangelength(3,15))"
34         o.default = trmiface
35         o.rmempty = false
36
37         function o.validate(self, value)
38                 if value then
39                         local nwnet = nw:get_network(value)
40                         local zone  = fw:get_zone("wan")
41                         local fwnet = fw:get_zone_by_network(value)
42                         if not nwnet then
43                                 nwnet = nw:add_network(value, { proto = "dhcp" })
44                         end
45                         if zone and not fwnet then
46                                 fwnet = zone:add_network(value)
47                         end
48                 end
49                 return value
50         end
51         return m
52 end
53
54 -- Main travelmate options
55
56 s = m:section(NamedSection, "global", "travelmate")
57
58 o1 = s:option(Flag, "trm_enabled", translate("Enable travelmate"))
59 o1.default = o1.disabled
60 o1.rmempty = false
61
62 o2 = s:option(Flag, "trm_captive", translate("Captive Portal Detection"),
63         translate("Check the internet availability, log captive portal redirections and keep the uplink connection 'alive'."))
64 o2.default = o2.enabled
65 o2.rmempty = false
66
67 o3 = s:option(ListValue, "trm_iface", translate("Uplink / Trigger interface"),
68         translate("Name of the used uplink interface."))
69 if dump then
70         local i, v
71         for i, v in ipairs(dump.interface) do
72                 if v.interface ~= "loopback" and v.interface ~= "lan" then
73                         o3:value(v.interface)
74                 end
75         end
76 end
77 o3.default = trmiface
78 o3.rmempty = false
79
80 if fs.access("/usr/bin/qrencode") then
81         btn = s:option(Button, "btn", translate("View AP QR-Codes"),
82                 translate("Connect your Android or iOS devices to your router's WiFi using the shown QR code."))
83         btn.inputtitle = translate("QR-Codes")
84         btn.inputstyle = "apply"
85         btn.disabled = false
86
87         function btn.write()
88                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "travelmate", "apqr"))
89         end
90 end
91
92 -- Runtime information
93
94 ds = s:option(DummyValue, "_dummy")
95 ds.template = "travelmate/runtime"
96
97 -- Extra options
98
99 e = m:section(NamedSection, "global", "travelmate", translate("Extra options"),
100 translate("Options for further tweaking in case the defaults are not suitable for you."))
101
102 e1 = e:option(Flag, "trm_debug", translate("Enable verbose debug logging"))
103 e1.default = e1.disabled
104 e1.rmempty = false
105
106 e2 = e:option(Value, "trm_radio", translate("Radio selection"),
107         translate("Restrict travelmate to a dedicated radio, e.g. 'radio0'."))
108 e2.datatype = "and(uciname,rangelength(6,6))"
109 e2.rmempty = true
110
111 e3 = e:option(Value, "trm_triggerdelay", translate("Trigger Delay"),
112         translate("Additional trigger delay in seconds before travelmate processing begins."))
113 e3.datatype = "range(1,60)"
114 e3.default = 2
115 e3.rmempty = false
116
117 e4 = e:option(Value, "trm_maxretry", translate("Connection Limit"),
118         translate("Retry limit to connect to an uplink."))
119 e4.default = 3
120 e4.datatype = "range(1,10)"
121 e4.rmempty = false
122
123 e5 = e:option(Value, "trm_minquality", translate("Signal Quality Threshold"),
124         translate("Minimum signal quality threshold as percent for conditional uplink (dis-) connections."))
125 e5.default = 35
126 e5.datatype = "range(20,80)"
127 e5.rmempty = false
128
129 e6 = e:option(Value, "trm_maxwait", translate("Interface Timeout"),
130         translate("How long should travelmate wait for a successful wlan uplink connection."))
131 e6.default = 30
132 e6.datatype = "range(20,40)"
133 e6.rmempty = false
134
135 e7 = e:option(Value, "trm_timeout", translate("Overall Timeout"),
136         translate("Overall retry timeout in seconds."))
137 e7.default = 60
138 e7.datatype = "range(30,300)"
139 e7.rmempty = false
140
141 return m