bdabd38f348217b7ac23caf3b48bc7ebdd5c05eb
[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 - Rules"),
37 ruleWarn())
38
39 mwan_rule = m5:section(TypedSection, "rule", nil,
40 translate("Rules specify which traffic will use a particular MWAN policy based on IP address, port or protocol<br />" ..
41 "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 />" ..
42 "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 />" ..
43 "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
44 "Rules may not share the same name as configured interfaces, members or policies"))
45 mwan_rule.addremove = true
46 mwan_rule.anonymous = false
47 mwan_rule.dynamic = false
48 mwan_rule.sectionhead = translate("Rule")
49 mwan_rule.sortable = true
50 mwan_rule.template = "cbi/tblsection"
51 mwan_rule.extedit = dsp.build_url("admin", "network", "mwan", "rule", "%s")
52 function mwan_rule.create(self, section)
53 TypedSection.create(self, section)
54 m5.uci:save("mwan3")
55 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "rule", section))
56 end
57
58
59 src_ip = mwan_rule:option(DummyValue, "src_ip", translate("Source address"))
60 src_ip.rawhtml = true
61 function src_ip.cfgvalue(self, s)
62 return self.map:get(s, "src_ip") or "&#8212;"
63 end
64
65 src_port = mwan_rule:option(DummyValue, "src_port", translate("Source port"))
66 src_port.rawhtml = true
67 function src_port.cfgvalue(self, s)
68 return self.map:get(s, "src_port") or "&#8212;"
69 end
70
71 dest_ip = mwan_rule:option(DummyValue, "dest_ip", translate("Destination address"))
72 dest_ip.rawhtml = true
73 function dest_ip.cfgvalue(self, s)
74 return self.map:get(s, "dest_ip") or "&#8212;"
75 end
76
77 dest_port = mwan_rule:option(DummyValue, "dest_port", translate("Destination port"))
78 dest_port.rawhtml = true
79 function dest_port.cfgvalue(self, s)
80 return self.map:get(s, "dest_port") or "&#8212;"
81 end
82
83 proto = mwan_rule:option(DummyValue, "proto", translate("Protocol"))
84 proto.rawhtml = true
85 function proto.cfgvalue(self, s)
86 return self.map:get(s, "proto") or "all"
87 end
88
89 sticky = mwan_rule:option(DummyValue, "sticky", translate("Sticky"))
90 sticky.rawhtml = true
91 function sticky.cfgvalue(self, s)
92 if self.map:get(s, "sticky") == "1" then
93 stickied = 1
94 return translate("Yes")
95 else
96 stickied = nil
97 return translate("No")
98 end
99 end
100
101 timeout = mwan_rule:option(DummyValue, "timeout", translate("Sticky timeout"))
102 timeout.rawhtml = true
103 function timeout.cfgvalue(self, s)
104 if stickied then
105 local timeoutValue = self.map:get(s, "timeout")
106 if timeoutValue then
107 return timeoutValue .. "s"
108 else
109 return "600s"
110 end
111 else
112 return "&#8212;"
113 end
114 end
115
116 ipset = mwan_rule:option(DummyValue, "ipset", translate("IPset"))
117 ipset.rawhtml = true
118 function ipset.cfgvalue(self, s)
119 return self.map:get(s, "ipset") or "&#8212;"
120 end
121
122 use_policy = mwan_rule:option(DummyValue, "use_policy", translate("Policy assigned"))
123 use_policy.rawhtml = true
124 function use_policy.cfgvalue(self, s)
125 return self.map:get(s, "use_policy") or "&#8212;"
126 end
127
128 errors = mwan_rule:option(DummyValue, "errors", translate("Errors"))
129 errors.rawhtml = true
130 function errors.cfgvalue(self, s)
131 if not string.find(error_protocol_list, " " .. s .. " ") then
132 return ""
133 else
134 return "<span title=\"" .. translate("No protocol specified") .. "\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
135 end
136 end
137
138
139 return m5