97f28bd325eaf9ceb87c0230490479d312f7ad3a
[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<br />" ..
41         "Rules are based on IP address, port or protocol<br />" ..
42         "Rules are matched from top to bottom<br />" ..
43         "Rules below a matching rule are ignored<br />" ..
44         "Traffic not matching any rule is routed using the main routing table<br />" ..
45         "Traffic destined for known (other than default) networks is handled by the main routing table<br />" ..
46         "Traffic matching a rule, but all WAN interfaces for that policy are down will be blackholed<br />" ..
47         "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
48         "Rules may not share the same name as configured interfaces, members or policies"))
49         mwan_rule.addremove = true
50         mwan_rule.anonymous = false
51         mwan_rule.dynamic = false
52         mwan_rule.sectionhead = translate("Rule")
53         mwan_rule.sortable = true
54         mwan_rule.template = "cbi/tblsection"
55         mwan_rule.extedit = dsp.build_url("admin", "network", "mwan", "rule", "%s")
56         function mwan_rule.create(self, section)
57                 TypedSection.create(self, section)
58                 m5.uci:save("mwan3")
59                 luci.http.redirect(dsp.build_url("admin", "network", "mwan", "rule", section))
60         end
61
62
63 src_ip = mwan_rule:option(DummyValue, "src_ip", translate("Source address"))
64         src_ip.rawhtml = true
65         function src_ip.cfgvalue(self, s)
66                 return self.map:get(s, "src_ip") or "&#8212;"
67         end
68
69 src_port = mwan_rule:option(DummyValue, "src_port", translate("Source port"))
70         src_port.rawhtml = true
71         function src_port.cfgvalue(self, s)
72                 return self.map:get(s, "src_port") or "&#8212;"
73         end
74
75 dest_ip = mwan_rule:option(DummyValue, "dest_ip", translate("Destination address"))
76         dest_ip.rawhtml = true
77         function dest_ip.cfgvalue(self, s)
78                 return self.map:get(s, "dest_ip") or "&#8212;"
79         end
80
81 dest_port = mwan_rule:option(DummyValue, "dest_port", translate("Destination port"))
82         dest_port.rawhtml = true
83         function dest_port.cfgvalue(self, s)
84                 return self.map:get(s, "dest_port") or "&#8212;"
85         end
86
87 proto = mwan_rule:option(DummyValue, "proto", translate("Protocol"))
88         proto.rawhtml = true
89         function proto.cfgvalue(self, s)
90                 return self.map:get(s, "proto") or "all"
91         end
92
93 use_policy = mwan_rule:option(DummyValue, "use_policy", translate("Policy assigned"))
94         use_policy.rawhtml = true
95         function use_policy.cfgvalue(self, s)
96                 return self.map:get(s, "use_policy") or "&#8212;"
97         end
98
99 return m5