applications/luci-firewall: use option "name" instead of deprecated "_name", expose...
[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
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
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 proto = s:option(DummyValue, "proto", translate("Protocol"))
79 proto.rawhtml = true
80 function proto.cfgvalue(self, s)
81         return ft.fmt_proto(self.map:get(s, "proto")) or "Any"
82 end
83
84
85 src = s:option(DummyValue, "src", translate("Source"))
86 src.rawhtml = true
87 src.width   = "20%"
88 function src.cfgvalue(self, s)
89         local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
90         local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
91         local p = ft.fmt_port(self.map:get(s, "src_port"))
92         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
93
94         if p and m then
95                 return translatef("From %s in %s with source %s and %s", a, z, p, m)
96         elseif p or m then
97                 return translatef("From %s in %s with source %s", a, z, p or m)
98         else
99                 return translatef("From %s in %s", a, z)
100         end
101 end
102
103 via = s:option(DummyValue, "via", translate("Via"))
104 via.rawhtml = true
105 via.width   = "20%"
106 function via.cfgvalue(self, s)
107         local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
108         local p = ft.fmt_port(self.map:get(s, "src_dport"))
109
110         if p then
111                 return translatef("To %s at %s", a, p)
112         else
113                 return translatef("To %s", a)
114         end
115 end
116
117 dest = s:option(DummyValue, "dest", translate("Destination"))
118 dest.rawhtml = true
119 dest.width   = "30%"
120 function dest.cfgvalue(self, s)
121         local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
122         local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
123         local p = ft.fmt_port(self.map:get(s, "dest_port")) or
124                 ft.fmt_port(self.map:get(s, "src_dport"))
125
126         if p then
127                 return translatef("Forward to %s, %s in %s", a, p, z)
128         else
129                 return translatef("Forward to %s in %s", a, z)
130         end
131 end
132
133 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
134
135 return m