applications/luci-firewall: fix display bug in snat rule overview, proto all rules...
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / firewall / rules.lua
index 05e98a9..9bfa66b 100644 (file)
@@ -2,6 +2,7 @@
 LuCI - Lua Configuration Interface
 
 Copyright 2008 Steven Barth <steven@midlink.org>
+Copyright 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -9,7 +10,6 @@ You may obtain a copy of the License at
 
        http://www.apache.org/licenses/LICENSE-2.0
 
-$Id$
 ]]--
 
 local ds = require "luci.dispatcher"
@@ -59,7 +59,7 @@ function s.parse(self, ...)
                self.map:set(created, "src",       "wan")
                self.map:set(created, "proto",     (i_p ~= "other") and i_p or "all")
                self.map:set(created, "dest_port", i_e)
-               self.map:set(created, "_name",     i_n)
+               self.map:set(created, "name",      i_n)
 
                if i_p ~= "other" and i_e and #i_e > 0 then
                        created = nil
@@ -71,106 +71,116 @@ function s.parse(self, ...)
                self.map:set(created, "target", "ACCEPT")
                self.map:set(created, "src",    f_s)
                self.map:set(created, "dest",   f_d)
-               self.map:set(created, "_name",  f_n)
+               self.map:set(created, "name",   f_n)
        end
 
        if created then
                m.uci:save("firewall")
                luci.http.redirect(ds.build_url(
-                       "admin", "network", "firewall", "rules", created
+                       "admin/network/firewall/rules", created
                ))
        end
 end
 
-name = s:option(DummyValue, "_name", translate("Name"))
-function name.cfgvalue(self, s)
-       return self.map:get(s, "_name") or "-"
-end
+ft.opt_name(s, DummyValue, translate("Name"))
 
-family = s:option(DummyValue, "family", translate("Family"))
-function family.cfgvalue(self, s)
+local function rule_proto_txt(self, s)
        local f = self.map:get(s, "family")
+       local p = ft.fmt_proto(self.map:get(s, "proto"),
+                              self.map:get(s, "icmp_type")) or translate("traffic")
+
        if f and f:match("4") then
-               return translate("IPv4")
+               return "%s-%s" %{ translate("IPv4"), p }
        elseif f and f:match("6") then
-               return translate("IPv6")
+               return "%s-%s" %{ translate("IPv6"), p }
        else
-               return translate("Any")
+               return "%s %s" %{ translate("Any"), p }
        end
 end
 
-proto = s:option(DummyValue, "proto", translate("Protocol"))
-proto.rawhtml = true
-proto.width   = "20%"
-function proto.cfgvalue(self, s)
-       return ft.fmt_proto(self.map:get(s, "proto"), self.map:get(s, "icmp_type"))
-               or "TCP+UDP"
-end
-
-src = s:option(DummyValue, "src", translate("Source"))
-src.rawhtml = true
-src.width   = "20%"
-function src.cfgvalue(self, s)
-       local z = ft.fmt_zone(self.map:get(s, "src"))
-       local a = ft.fmt_ip(self.map:get(s, "src_ip"))
+local function rule_src_txt(self, s)
+       local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
+       local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
        local p = ft.fmt_port(self.map:get(s, "src_port"))
        local m = ft.fmt_mac(self.map:get(s, "src_mac"))
 
-       local s = "From %s in %s " %{
-               (a or "<var>any host</var>"),
-               (z or "<var>any zone</var>")
-       }
-
        if p and m then
-               s = s .. "with source %s and %s" %{ p, m }
+               return translatef("From %s in %s with source %s and %s", a, z, p, m)
        elseif p or m then
-               s = s .. "with source %s" %( p or m )
+               return translatef("From %s in %s with source %s", a, z, p or m)
+       else
+               return translatef("From %s in %s", a, z)
        end
-
-       return s
 end
 
-dest = s:option(DummyValue, "dest", translate("Destination"))
-dest.rawhtml = true
-dest.width   = "20%"
-function dest.cfgvalue(self, s)
+local function rule_dest_txt(self, s)
        local z = ft.fmt_zone(self.map:get(s, "dest"))
-       local a = ft.fmt_ip(self.map:get(s, "dest_ip"))
        local p = ft.fmt_port(self.map:get(s, "dest_port"))
 
        -- Forward
        if z then
-               return "To %s%s in %s" %{
-                       (a or "<var>any host</var>"),
-                       (p and ", %s" % p or ""),
-                       z
-               }
+               local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
+               if p then
+                       return translatef("To %s, %s in %s", a, p, z)
+               else
+                       return translatef("To %s in %s", a, z)
+               end
 
        -- Input
        else
-               return "To %s%s on <var>this device</var>" %{
-                       (a or "<var>any router IP</var>"),
-                       (p and " at %s" % p or "")
-               }
+               local a = ft.fmt_ip(self.map:get(s, "dest_ip"),
+                       translate("any router IP"))
+
+               if p then
+                       return translatef("To %s at %s on <var>this device</var>", a, p)
+               else
+                       return translatef("To %s on <var>this device</var>", a)
+               end
+       end
+end
+
+local function snat_dest_txt(self, s)
+       local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
+       local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
+       local p = ft.fmt_port(self.map:get(s, "dest_port")) or
+               ft.fmt_port(self.map:get(s, "src_dport"))
+
+       if p then
+               return translatef("To %s, %s in %s", a, p, z)
+       else
+               return translatef("To %s in %s", a, z)
        end
 end
 
 
+match = s:option(DummyValue, "match", translate("Match"))
+match.rawhtml = true
+match.width   = "70%"
+function match.cfgvalue(self, s)
+       return "<small>%s<br />%s<br />%s</small>" % {
+               rule_proto_txt(self, s),
+               rule_src_txt(self, s),
+               rule_dest_txt(self, s)
+       }
+end
+
 target = s:option(DummyValue, "target", translate("Action"))
 target.rawhtml = true
 target.width   = "20%"
 function target.cfgvalue(self, s)
-       local z = ft.fmt_zone(self.map:get(s, "dest"))
-       local l = ft.fmt_limit(self.map:get(s, "limit"), self.map:get(s, "limit_burst"))
-       local t = ft.fmt_target(self.map:get(s, "target"))
+       local t = ft.fmt_target(self.map:get(s, "target"), self.map:get(s, "dest"))
+       local l = ft.fmt_limit(self.map:get(s, "limit"),
+               self.map:get(s, "limit_burst"))
 
-       return "<var>%s</var> %s%s" %{
-               t,
-               (z and "forward" or "input"),
-               (l and " and limit to %s" % l or "")
-       }
+       if l then
+               return translatef("<var>%s</var> and limit to %s", t, l)
+       else
+               return "<var>%s</var>" % t
+       end
 end
 
+ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
+
 
 --
 -- SNAT
@@ -211,7 +221,7 @@ function s.parse(self, ...)
                self.map:set(created, "proto",     "all")
                self.map:set(created, "src_dip",   a)
                self.map:set(created, "src_dport", p)
-               self.map:set(created, "_name",     n)
+               self.map:set(created, "name",      n)
        end
 
        if created then
@@ -226,75 +236,34 @@ function s.filter(self, sid)
        return (self.map:get(sid, "target") == "SNAT")
 end
 
-name = s:option(DummyValue, "_name", translate("Name"))
-function name.cfgvalue(self, s)
-       return self.map:get(s, "_name") or "-"
-end
-
-proto = s:option(DummyValue, "proto", translate("Protocol"))
-proto.rawhtml = true
-function proto.cfgvalue(self, s)
-       return ft.fmt_proto(self.map:get(s, "proto")) or "TCP+UDP"
-end
-
-
-src = s:option(DummyValue, "src", translate("Source"))
-src.rawhtml = true
-src.width   = "20%"
-function src.cfgvalue(self, s)
-       local z = ft.fmt_zone(self.map:get(s, "src"))
-       local a = ft.fmt_ip(self.map:get(s, "src_ip"))
-       local p = ft.fmt_port(self.map:get(s, "src_port"))
-       local m = ft.fmt_mac(self.map:get(s, "src_mac"))
-
-       local s = "From %s in %s " %{
-               (a or "<var>any host</var>"),
-               (z or "<var>any zone</var>")
-       }
-
-       if p and m then
-               s = s .. "with source %s and %s" %{ p, m }
-       elseif p or m then
-               s = s .. "with source %s" %( p or m )
-       end
-
-       return s
-end
-
-dest = s:option(DummyValue, "dest", translate("Destination"))
-dest.rawhtml = true
-dest.width   = "30%"
-function dest.cfgvalue(self, s)
-       local z = ft.fmt_zone(self.map:get(s, "dest"))
-       local a = ft.fmt_ip(self.map:get(s, "dest_ip"))
-       local p = ft.fmt_port(self.map:get(s, "dest_port")) or
-               ft.fmt_port(self.map:get(s, "src_dport"))
+ft.opt_name(s, DummyValue, translate("Name"))
 
-       return "To %s%s in %s " %{
-               (a or "<var>any host</var>"),
-               (p and ", %s" % p or ""),
-               (z or "<var>any zone</var>")
+match = s:option(DummyValue, "match", translate("Match"))
+match.rawhtml = true
+match.width   = "70%"
+function match.cfgvalue(self, s)
+       return "<small>%s<br />%s<br />%s</small>" % {
+               rule_proto_txt(self, s),
+               rule_src_txt(self, s),
+               snat_dest_txt(self, s)
        }
 end
 
-snat = s:option(DummyValue, "via", translate("SNAT"))
+snat = s:option(DummyValue, "via", translate("Action"))
 snat.rawhtml = true
 snat.width   = "20%"
 function snat.cfgvalue(self, s)
        local a = ft.fmt_ip(self.map:get(s, "src_dip"))
        local p = ft.fmt_port(self.map:get(s, "src_dport"))
 
-       --local z = self.map:get(s, "src")
-       --local s = "To %s " %(a or "<var>any %s IP</var>" %( z or "router" ))
-
        if a and p then
-               return "Rewrite to source %s, %s" %{ a, p }
-       elseif a or p then
-               return "Rewrite to source %s" %( a or p )
+               return translatef("Rewrite to source %s, %s", a, p)
        else
-               return "Bug"
+               return translatef("Rewrite to source %s", a or p)
        end
 end
 
+ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
+
 
 return m