Merge pull request #278 from nmav/ocserv
[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.extzone")
39         local e = m:formvalue("_newfwd.extport")
40         local I = m:formvalue("_newfwd.intzone")
41         local a = m:formvalue("_newfwd.intaddr")
42         local i = m:formvalue("_newfwd.intport")
43
44         if p == "other" or (p and a) then
45                 created = TypedSection.create(self, section)
46
47                 self.map:set(created, "target",    "DNAT")
48                 self.map:set(created, "src",       E or "wan")
49                 self.map:set(created, "dest",      I or "lan")
50                 self.map:set(created, "proto",     (p ~= "other") and p or "all")
51                 self.map:set(created, "src_dport", e)
52                 self.map:set(created, "dest_ip",   a)
53                 self.map:set(created, "dest_port", i)
54                 self.map:set(created, "name",      n)
55         end
56
57         if p ~= "other" then
58                 created = nil
59         end
60 end
61
62 function s.parse(self, ...)
63         TypedSection.parse(self, ...)
64         if created then
65                 m.uci:save("firewall")
66                 luci.http.redirect(ds.build_url(
67                         "admin/network/firewall/redirect", created
68                 ))
69         end
70 end
71
72 function s.filter(self, sid)
73         return (self.map:get(sid, "target") ~= "SNAT")
74 end
75
76
77 ft.opt_name(s, DummyValue, translate("Name"))
78
79
80 local function forward_proto_txt(self, s)
81         return "%s-%s" %{
82                 translate("IPv4"),
83                 ft.fmt_proto(self.map:get(s, "proto"),
84                          self.map:get(s, "icmp_type")) or "TCP+UDP"
85         }
86 end
87
88 local function forward_src_txt(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 local function forward_via_txt(self, s)
104         local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
105         local p = ft.fmt_port(self.map:get(s, "src_dport"))
106
107         if p then
108                 return translatef("Via %s at %s", a, p)
109         else
110                 return translatef("Via %s", a)
111         end
112 end
113
114 match = s:option(DummyValue, "match", translate("Match"))
115 match.rawhtml = true
116 match.width   = "50%"
117 function match.cfgvalue(self, s)
118         return "<small>%s<br />%s<br />%s</small>" % {
119                 forward_proto_txt(self, s),
120                 forward_src_txt(self, s),
121                 forward_via_txt(self, s)
122         }
123 end
124
125
126 dest = s:option(DummyValue, "dest", translate("Forward to"))
127 dest.rawhtml = true
128 dest.width   = "40%"
129 function dest.cfgvalue(self, s)
130         local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
131         local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
132         local p = ft.fmt_port(self.map:get(s, "dest_port")) or
133                 ft.fmt_port(self.map:get(s, "src_dport"))
134
135         if p then
136                 return translatef("%s, %s in %s", a, p, z)
137         else
138                 return translatef("%s in %s", a, z)
139         end
140 end
141
142 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
143
144 return m