applications/luci-firewall: complete rework firewall ui
[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 name = s:option(DummyValue, "_name", translate("Name"))
75 function name.cfgvalue(self, s)
76         return self.map:get(s, "_name") or "-"
77 end
78
79 proto = s:option(DummyValue, "proto", translate("Protocol"))
80 proto.rawhtml = true
81 function proto.cfgvalue(self, s)
82         return ft.fmt_proto(self.map:get(s, "proto")) or "Any"
83 end
84
85
86 src = s:option(DummyValue, "src", translate("Source"))
87 src.rawhtml = true
88 src.width   = "20%"
89 function src.cfgvalue(self, s)
90         local z = ft.fmt_zone(self.map:get(s, "src"))
91         local a = ft.fmt_ip(self.map:get(s, "src_ip"))
92         local p = ft.fmt_port(self.map:get(s, "src_port"))
93         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
94
95         local s = "From %s in %s " %{
96                 (a or "<var>any host</var>"),
97                 (z or "<var>any zone</var>")
98         }
99
100         if p and m then
101                 s = s .. "with source %s and %s" %{ p, m }
102         elseif p or m then
103                 s = s .. "with source %s" %( p or m )
104         end
105
106         return s
107 end
108
109 via = s:option(DummyValue, "via", translate("Via"))
110 via.rawhtml = true
111 via.width   = "20%"
112 function via.cfgvalue(self, s)
113         local a = ft.fmt_ip(self.map:get(s, "src_dip"))
114         local p = ft.fmt_port(self.map:get(s, "src_dport"))
115
116         --local z = self.map:get(s, "src")
117         --local s = "To %s " %(a or "<var>any %s IP</var>" %( z or "router" ))
118
119         return "To %s%s" %{
120                 (a or "<var>any router IP</var>"),
121                 (p and " at %s" % p or "")
122         }
123 end
124
125 dest = s:option(DummyValue, "dest", translate("Destination"))
126 dest.rawhtml = true
127 dest.width   = "30%"
128 function dest.cfgvalue(self, s)
129         local z = ft.fmt_zone(self.map:get(s, "dest"))
130         local a = ft.fmt_ip(self.map:get(s, "dest_ip"))
131         local p = ft.fmt_port(self.map:get(s, "dest_port")) or
132                 ft.fmt_port(self.map:get(s, "src_dport"))
133
134         return "Forward to %s%s in %s " %{
135                 (a or "<var>any host</var>"),
136                 (p and ", %s" % p or ""),
137                 (z or "<var>any zone</var>")
138         }
139 end
140
141 return m