Merge pull request #1118 from dibdot/app-travelmate
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / ruleconfig.lua
1 -- ------ extra functions ------ --
2
3 function ruleCheck() -- determine if rule needs a protocol specified
4         local sourcePort = ut.trim(sys.exec("uci -p /var/state get mwan3." .. arg[1] .. ".src_port"))
5         local destPort = ut.trim(sys.exec("uci -p /var/state get mwan3." .. arg[1] .. ".dest_port"))
6         if sourcePort ~= "" or destPort ~= "" then -- ports configured
7                 local protocol = ut.trim(sys.exec("uci -p /var/state get mwan3." .. arg[1] .. ".proto"))
8                 if protocol == "" or protocol == "all" then -- no or improper protocol
9                         error_protocol = 1
10                 end
11         end
12 end
13
14 function ruleWarn() -- display warning message at the top of the page
15         if error_protocol == 1 then
16                 return "<font color=\"ff0000\"><strong>" .. translate("WARNING: this rule is incorrectly configured with no or improper protocol specified! Please configure a specific protocol!") .. "</strong></font>"
17         else
18                 return ""
19         end
20 end
21
22 function cbiAddPolicy(field)
23         uci.cursor():foreach("mwan3", "policy",
24                 function (section)
25                         field:value(section[".name"])
26                 end
27         )
28 end
29
30 function cbiAddProtocol(field)
31         local protocols = ut.trim(sys.exec("cat /etc/protocols | grep ' # ' | awk '{print $1}' | grep -vw -e 'ip' -e 'tcp' -e 'udp' -e 'icmp' -e 'esp' | grep -v 'ipv6' | sort | tr '\n' ' '"))
32         for p in string.gmatch(protocols, "%S+") do
33                 field:value(p)
34         end
35 end
36
37 -- ------ rule configuration ------ --
38
39 dsp = require "luci.dispatcher"
40 sys = require "luci.sys"
41 ut = require "luci.util"
42 arg[1] = arg[1] or ""
43
44 error_protocol = 0
45 ruleCheck()
46
47
48 m5 = Map("mwan3", translatef("MWAN Rule Configuration - %s", arg[1]),
49         ruleWarn())
50         m5.redirect = dsp.build_url("admin", "network", "mwan", "configuration", "rule")
51
52
53 mwan_rule = m5:section(NamedSection, arg[1], "rule", "")
54         mwan_rule.addremove = false
55         mwan_rule.dynamic = false
56
57
58 src_ip = mwan_rule:option(Value, "src_ip", translate("Source address"),
59         translate("Supports CIDR notation (eg \"192.168.100.0/24\") without quotes"))
60         src_ip.datatype = ipaddr
61
62 src_port = mwan_rule:option(Value, "src_port", translate("Source port"),
63         translate("May be entered as a single or multiple port(s) (eg \"22\" or \"80,443\") or as a portrange (eg \"1024:2048\") without quotes"))
64
65 dest_ip = mwan_rule:option(Value, "dest_ip", translate("Destination address"),
66         translate("Supports CIDR notation (eg \"192.168.100.0/24\") without quotes"))
67         dest_ip.datatype = ipaddr
68
69 dest_port = mwan_rule:option(Value, "dest_port", translate("Destination port"),
70         translate("May be entered as a single or multiple port(s) (eg \"22\" or \"80,443\") or as a portrange (eg \"1024:2048\") without quotes"))
71
72 proto = mwan_rule:option(Value, "proto", translate("Protocol"),
73         translate("View the contents of /etc/protocols for protocol descriptions"))
74         proto.default = "all"
75         proto.rmempty = false
76         proto:value("all")
77         proto:value("ip")
78         proto:value("tcp")
79         proto:value("udp")
80         proto:value("icmp")
81         proto:value("esp")
82         cbiAddProtocol(proto)
83
84 sticky = mwan_rule:option(ListValue, "sticky", translate("Sticky"),
85         translate("Traffic from the same source IP address that previously matched this rule within the sticky timeout period will use the same WAN interface"))
86         sticky.default = "0"
87         sticky:value("1", translate("Yes"))
88         sticky:value("0", translate("No"))
89
90 timeout = mwan_rule:option(Value, "timeout", translate("Sticky timeout"),
91         translate("Seconds. Acceptable values: 1-1000000. Defaults to 600 if not set"))
92         timeout.datatype = "range(1, 1000000)"
93
94 ipset = mwan_rule:option(Value, "ipset", translate("IPset"),
95         translate("Name of IPset rule. Requires IPset rule in /etc/dnsmasq.conf (eg \"ipset=/youtube.com/youtube\")"))
96
97 use_policy = mwan_rule:option(Value, "use_policy", translate("Policy assigned"))
98         cbiAddPolicy(use_policy)
99         use_policy:value("unreachable", translate("unreachable (reject)"))
100         use_policy:value("blackhole", translate("blackhole (drop)"))
101         use_policy:value("default", translate("default (use main routing table)"))
102
103
104 -- ------ currently configured policies ------ --
105
106 mwan_policy = m5:section(TypedSection, "policy", translate("Currently Configured Policies"))
107         mwan_policy.addremove = false
108         mwan_policy.dynamic = false
109         mwan_policy.sortable = false
110         mwan_policy.template = "cbi/tblsection"
111
112
113 return m5