Merge pull request #1703 from dibdot/travelmate
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / rule.lua
1 -- Copyright 2014 Aedan Renner <chipdankly@gmail.com>
2 -- Copyright 2018 Florian Eckert <fe@dev.tdt.de>
3 -- Licensed to the public under the GNU General Public License v2.
4
5 dsp = require "luci.dispatcher"
6
7
8 function ruleCheck()
9         local rule_error = {}
10         uci.cursor():foreach("mwan3", "rule",
11                 function (section)
12                         rule_error[section[".name"]] = false
13                         local uci = uci.cursor(nil, "/var/state")
14                         local sourcePort = uci:get("mwan3", section[".name"], "src_port")
15                         local destPort = uci:get("mwan3", section[".name"], "dest_port")
16                         if sourcePort ~= nil or destPort ~= nil then
17                                 local protocol = uci:get("mwan3", section[".name"], "proto")
18                                 if protocol == nil or protocol == "all" then
19                                         rule_error[section[".name"]] = true
20                                 end
21                         end
22                 end
23         )
24         return rule_error
25 end
26
27 function ruleWarn(rule_error)
28         local warnings = ""
29         for i, k in pairs(rule_error) do
30                 if rule_error[i] == true then
31                         warnings = warnings .. string.format("<strong>%s</strong><br />",
32                                 translatef("WARNING: Rule %s have a port configured with no or improper protocol specified!", i)
33                                 )
34                 end
35         end
36
37         return warnings
38 end
39
40 m5 = Map("mwan3", translate("MWAN - Rules"),
41         ruleWarn(ruleCheck())
42         )
43
44 mwan_rule = m5:section(TypedSection, "rule", nil,
45         translate("Rules specify which traffic will use a particular MWAN policy<br />" ..
46         "Rules are based on IP address, port or protocol<br />" ..
47         "Rules are matched from top to bottom<br />" ..
48         "Rules below a matching rule are ignored<br />" ..
49         "Traffic not matching any rule is routed using the main routing table<br />" ..
50         "Traffic destined for known (other than default) networks is handled by the main routing table<br />" ..
51         "Traffic matching a rule, but all WAN interfaces for that policy are down will be blackholed<br />" ..
52         "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
53         "Rules may not share the same name as configured interfaces, members or policies"))
54 mwan_rule.addremove = true
55 mwan_rule.anonymous = false
56 mwan_rule.dynamic = false
57 mwan_rule.sectionhead = translate("Rule")
58 mwan_rule.sortable = true
59 mwan_rule.template = "cbi/tblsection"
60 mwan_rule.extedit = dsp.build_url("admin", "network", "mwan", "rule", "%s")
61 function mwan_rule.create(self, section)
62         TypedSection.create(self, section)
63         m5.uci:save("mwan3")
64         luci.http.redirect(dsp.build_url("admin", "network", "mwan", "rule", section))
65 end
66
67 src_ip = mwan_rule:option(DummyValue, "src_ip", translate("Source address"))
68 src_ip.rawhtml = true
69 function src_ip.cfgvalue(self, s)
70         return self.map:get(s, "src_ip") or "&#8212;"
71 end
72
73 src_port = mwan_rule:option(DummyValue, "src_port", translate("Source port"))
74 src_port.rawhtml = true
75 function src_port.cfgvalue(self, s)
76         return self.map:get(s, "src_port") or "&#8212;"
77 end
78
79 dest_ip = mwan_rule:option(DummyValue, "dest_ip", translate("Destination address"))
80 dest_ip.rawhtml = true
81 function dest_ip.cfgvalue(self, s)
82         return self.map:get(s, "dest_ip") or "&#8212;"
83 end
84
85 dest_port = mwan_rule:option(DummyValue, "dest_port", translate("Destination port"))
86 dest_port.rawhtml = true
87 function dest_port.cfgvalue(self, s)
88         return self.map:get(s, "dest_port") or "&#8212;"
89 end
90
91 proto = mwan_rule:option(DummyValue, "proto", translate("Protocol"))
92 proto.rawhtml = true
93 function proto.cfgvalue(self, s)
94         return self.map:get(s, "proto") or "all"
95 end
96
97 use_policy = mwan_rule:option(DummyValue, "use_policy", translate("Policy assigned"))
98 use_policy.rawhtml = true
99 function use_policy.cfgvalue(self, s)
100         return self.map:get(s, "use_policy") or "&#8212;"
101 end
102
103 return m5