0f4c5950a0c7b481ab018cb0fc871b8530822ddc
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / rule.lua
1 -- ------ extra functions ------ --
2
3 function ruleCheck() -- determine if rules needs a proper protocol configured
4 uci.cursor():foreach("mwan3", "rule",
5 function (section)
6 local sourcePort = ut.trim(sys.exec("uci -p /var/state get mwan3." .. section[".name"] .. ".src_port"))
7 local destPort = ut.trim(sys.exec("uci -p /var/state get mwan3." .. section[".name"] .. ".dest_port"))
8 if sourcePort ~= "" or destPort ~= "" then -- ports configured
9 local protocol = ut.trim(sys.exec("uci -p /var/state get mwan3." .. section[".name"] .. ".proto"))
10 if protocol == "" or protocol == "all" then -- no or improper protocol
11 error_protocol_list = error_protocol_list .. section[".name"] .. " "
12 end
13 end
14 end
15 )
16 end
17
18 function ruleWarn() -- display warning messages at the top of the page
19 if error_protocol_list ~= " " then
20 return "<font color=\"ff0000\"><strong>" .. translate("WARNING: some rules have a port configured with no or improper protocol specified! Please configure a specific protocol!") .. "</strong></font>"
21 else
22 return ""
23 end
24 end
25
26 -- ------ rule configuration ------ --
27
28 dsp = require "luci.dispatcher"
29 sys = require "luci.sys"
30 ut = require "luci.util"
31
32 error_protocol_list = " "
33 ruleCheck()
34
35
36 m5 = Map("mwan3", translate("MWAN Rule Configuration"),
37 ruleWarn())
38 m5:append(Template("mwan/config_css"))
39
40
41 mwan_rule = m5:section(TypedSection, "rule", translate("Traffic Rules"),
42 translate("Rules specify which traffic will use a particular MWAN policy based on IP address, port or protocol<br />" ..
43 "Rules are matched from top to bottom. Rules below a matching rule are ignored. Traffic not matching any rule is routed using the main routing table<br />" ..
44 "Traffic destined for known (other than default) networks is handled by the main routing table. Traffic matching a rule, but all WAN interfaces for that policy are down will be blackholed<br />" ..
45 "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
46 "Rules may not share the same name as configured interfaces, members or policies"))
47 mwan_rule.addremove = true
48 mwan_rule.anonymous = false
49 mwan_rule.dynamic = false
50 mwan_rule.sectionhead = translate("Rule")
51 mwan_rule.sortable = true
52 mwan_rule.template = "cbi/tblsection"
53 mwan_rule.extedit = dsp.build_url("admin", "network", "mwan", "configuration", "rule", "%s")
54 function mwan_rule.create(self, section)
55 TypedSection.create(self, section)
56 m5.uci:save("mwan3")
57 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "configuration", "rule", section))
58 end
59
60
61 src_ip = mwan_rule:option(DummyValue, "src_ip", translate("Source address"))
62 src_ip.rawhtml = true
63 function src_ip.cfgvalue(self, s)
64 return self.map:get(s, "src_ip") or "&#8212;"
65 end
66
67 src_port = mwan_rule:option(DummyValue, "src_port", translate("Source port"))
68 src_port.rawhtml = true
69 function src_port.cfgvalue(self, s)
70 return self.map:get(s, "src_port") or "&#8212;"
71 end
72
73 dest_ip = mwan_rule:option(DummyValue, "dest_ip", translate("Destination address"))
74 dest_ip.rawhtml = true
75 function dest_ip.cfgvalue(self, s)
76 return self.map:get(s, "dest_ip") or "&#8212;"
77 end
78
79 dest_port = mwan_rule:option(DummyValue, "dest_port", translate("Destination port"))
80 dest_port.rawhtml = true
81 function dest_port.cfgvalue(self, s)
82 return self.map:get(s, "dest_port") or "&#8212;"
83 end
84
85 proto = mwan_rule:option(DummyValue, "proto", translate("Protocol"))
86 proto.rawhtml = true
87 function proto.cfgvalue(self, s)
88 return self.map:get(s, "proto") or "all"
89 end
90
91 sticky = mwan_rule:option(DummyValue, "sticky", translate("Sticky"))
92 sticky.rawhtml = true
93 function sticky.cfgvalue(self, s)
94 if self.map:get(s, "sticky") == "1" then
95 stickied = 1
96 return translate("Yes")
97 else
98 stickied = nil
99 return translate("No")
100 end
101 end
102
103 timeout = mwan_rule:option(DummyValue, "timeout", translate("Sticky timeout"))
104 timeout.rawhtml = true
105 function timeout.cfgvalue(self, s)
106 if stickied then
107 local timeoutValue = self.map:get(s, "timeout")
108 if timeoutValue then
109 return timeoutValue .. "s"
110 else
111 return "600s"
112 end
113 else
114 return "&#8212;"
115 end
116 end
117
118 ipset = mwan_rule:option(DummyValue, "ipset", translate("IPset"))
119 ipset.rawhtml = true
120 function ipset.cfgvalue(self, s)
121 return self.map:get(s, "ipset") or "&#8212;"
122 end
123
124 use_policy = mwan_rule:option(DummyValue, "use_policy", translate("Policy assigned"))
125 use_policy.rawhtml = true
126 function use_policy.cfgvalue(self, s)
127 return self.map:get(s, "use_policy") or "&#8212;"
128 end
129
130 errors = mwan_rule:option(DummyValue, "errors", translate("Errors"))
131 errors.rawhtml = true
132 function errors.cfgvalue(self, s)
133 if not string.find(error_protocol_list, " " .. s .. " ") then
134 return ""
135 else
136 return "<span title=\"" .. translate("No protocol specified") .. "\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
137 end
138 end
139
140
141 return m5