Update my email addresses in the license headers
[project/luci.git] / applications / luci-app-firewall / luasrc / model / cbi / firewall / forwards.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2012 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 local ds = require "luci.dispatcher"
6 local ft = require "luci.tools.firewall"
7
8 m = Map("firewall", translate("Firewall - Port Forwards"),
9         translate("Port forwarding allows remote computers on the Internet to \
10                    connect to a specific computer or service within the \
11                    private LAN."))
12
13 --
14 -- Port Forwards
15 --
16
17 s = m:section(TypedSection, "redirect", translate("Port Forwards"))
18 s.template  = "cbi/tblsection"
19 s.addremove = true
20 s.anonymous = true
21 s.sortable  = true
22 s.extedit   = ds.build_url("admin/network/firewall/forwards/%s")
23 s.template_addremove = "firewall/cbi_addforward"
24
25 function s.create(self, section)
26         local n = m:formvalue("_newfwd.name")
27         local p = m:formvalue("_newfwd.proto")
28         local E = m:formvalue("_newfwd.extzone")
29         local e = m:formvalue("_newfwd.extport")
30         local I = m:formvalue("_newfwd.intzone")
31         local a = m:formvalue("_newfwd.intaddr")
32         local i = m:formvalue("_newfwd.intport")
33
34         if p == "other" or (p and a) then
35                 created = TypedSection.create(self, section)
36
37                 self.map:set(created, "target",    "DNAT")
38                 self.map:set(created, "src",       E or "wan")
39                 self.map:set(created, "dest",      I or "lan")
40                 self.map:set(created, "proto",     (p ~= "other") and p or "all")
41                 self.map:set(created, "src_dport", e)
42                 self.map:set(created, "dest_ip",   a)
43                 self.map:set(created, "dest_port", i)
44                 self.map:set(created, "name",      n)
45         end
46
47         if p ~= "other" then
48                 created = nil
49         end
50 end
51
52 function s.parse(self, ...)
53         TypedSection.parse(self, ...)
54         if created then
55                 m.uci:save("firewall")
56                 luci.http.redirect(ds.build_url(
57                         "admin/network/firewall/redirect", created
58                 ))
59         end
60 end
61
62 function s.filter(self, sid)
63         return (self.map:get(sid, "target") ~= "SNAT")
64 end
65
66
67 ft.opt_name(s, DummyValue, translate("Name"))
68
69
70 local function forward_proto_txt(self, s)
71         return "%s-%s" %{
72                 translate("IPv4"),
73                 ft.fmt_proto(self.map:get(s, "proto"),
74                          self.map:get(s, "icmp_type")) or "TCP+UDP"
75         }
76 end
77
78 local function forward_src_txt(self, s)
79         local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
80         local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
81         local p = ft.fmt_port(self.map:get(s, "src_port"))
82         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
83
84         if p and m then
85                 return translatef("From %s in %s with source %s and %s", a, z, p, m)
86         elseif p or m then
87                 return translatef("From %s in %s with source %s", a, z, p or m)
88         else
89                 return translatef("From %s in %s", a, z)
90         end
91 end
92
93 local function forward_via_txt(self, s)
94         local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
95         local p = ft.fmt_port(self.map:get(s, "src_dport"))
96
97         if p then
98                 return translatef("Via %s at %s", a, p)
99         else
100                 return translatef("Via %s", a)
101         end
102 end
103
104 match = s:option(DummyValue, "match", translate("Match"))
105 match.rawhtml = true
106 match.width   = "50%"
107 function match.cfgvalue(self, s)
108         return "<small>%s<br />%s<br />%s</small>" % {
109                 forward_proto_txt(self, s),
110                 forward_src_txt(self, s),
111                 forward_via_txt(self, s)
112         }
113 end
114
115
116 dest = s:option(DummyValue, "dest", translate("Forward to"))
117 dest.rawhtml = true
118 dest.width   = "40%"
119 function dest.cfgvalue(self, s)
120         local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
121         local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
122         local p = ft.fmt_port(self.map:get(s, "dest_port")) or
123                 ft.fmt_port(self.map:get(s, "src_dport"))
124
125         if p then
126                 return translatef("%s, %s in %s", a, p, z)
127         else
128                 return translatef("%s in %s", a, z)
129         end
130 end
131
132 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
133
134 return m