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