--[[
LuCI - Lua Configuration Interface
-Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
+Copyright 2011-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.
http://www.apache.org/licenses/LICENSE-2.0
-$Id$
]]--
module("luci.tools.firewall", package.seeall)
local ip = require "luci.ip"
local nx = require "nixio"
-local tr, trf = luci.i18n.translate, luci.i18n.translatef
+local translate, translatef = luci.i18n.translate, luci.i18n.translatef
+
+local function tr(...)
+ return tostring(translate(...))
+end
function fmt_neg(x)
if type(x) == "string" then
end
end
-function fmt_port(x)
+function fmt_port(x, d)
if x and #x > 0 then
local p, n
local l = { tr("port"), " " }
return table.concat(l, "")
end
end
+ return d and "<var>%s</var>" % d
end
-function fmt_ip(x)
+function fmt_ip(x, d)
if x and #x > 0 then
local l = { tr("IP"), " " }
local v, a, n
a, m = v:match("(%S+)/(%d+%.%S+)")
a = a or v
a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m)
- if a and (a:is6() or a:prefix() < 32) then
+ if a and (a:is6() and a:prefix() < 128 or a:prefix() < 32) then
l[1] = tr("IP range")
l[#l+1] = "<var title='%s - %s'>%s%s</var>" %{
a:minhost():string(),
return table.concat(l, "")
end
end
+ return d and "<var>%s</var>" % d
end
-function fmt_zone(x)
+function fmt_zone(x, d)
if x == "*" then
return "<var>%s</var>" % tr("any zone")
elseif x and #x > 0 then
return "<var>%s</var>" % x
+ elseif d then
+ return "<var>%s</var>" % d
end
end
v, n = fmt_neg(v)
if v == "tcpudp" then
l[#l+1] = "TCP"
+ l[#l+1] = ", "
l[#l+1] = "UDP"
l[#l+1] = ", "
elseif v ~= "all" then
if p then
-- ICMP
if (p.proto == 1 or p.proto == 58) and t then
- l[#l+1] = trf(
+ l[#l+1] = translatef(
"%s%s with %s",
n, p.aliases[1] or p.name, t
)
u = tr("day")
end
if burst and burst > 0 then
- return trf("<var>%d</var> pkts. per <var>%s</var>, \
+ return translatef("<var>%d</var> pkts. per <var>%s</var>, \
burst <var>%d</var> pkts.", l, u, burst)
else
- return trf("<var>%d</var> pkts. per <var>%s</var>", l, u)
+ return translatef("<var>%d</var> pkts. per <var>%s</var>", l, u)
+ end
+ end
+ end
+end
+
+function fmt_target(x, dest)
+ if dest and #dest > 0 then
+ if x == "ACCEPT" then
+ return tr("Accept forward")
+ elseif x == "REJECT" then
+ return tr("Refuse forward")
+ elseif x == "NOTRACK" then
+ return tr("Do not track forward")
+ else --if x == "DROP" then
+ return tr("Discard forward")
+ end
+ else
+ if x == "ACCEPT" then
+ return tr("Accept input")
+ elseif x == "REJECT" then
+ return tr("Refuse input")
+ elseif x == "NOTRACK" then
+ return tr("Do not track input")
+ else --if x == "DROP" then
+ return tr("Discard input")
+ end
+ end
+end
+
+
+function opt_enabled(s, t, ...)
+ if t == luci.cbi.Button then
+ local o = s:option(t, "__enabled")
+ function o.render(self, section)
+ if self.map:get(section, "enabled") ~= "0" then
+ self.title = tr("Rule is enabled")
+ self.inputtitle = tr("Disable")
+ self.inputstyle = "reset"
+ else
+ self.title = tr("Rule is disabled")
+ self.inputtitle = tr("Enable")
+ self.inputstyle = "apply"
end
+ t.render(self, section)
end
+ function o.write(self, section, value)
+ if self.map:get(section, "enabled") ~= "0" then
+ self.map:set(section, "enabled", "0")
+ else
+ self.map:del(section, "enabled")
+ end
+ end
+ return o
+ else
+ local o = s:option(t, "enabled", ...)
+ o.default = "1"
+ return o
end
end
-function fmt_target(x)
- if x == "ACCEPT" then
- return tr("Accept")
- elseif x == "REJECT" then
- return tr("Refuse")
- elseif x == "NOTRACK" then
- return tr("Do not track")
- else --if x == "DROP" then
- return tr("Discard")
+function opt_name(s, t, ...)
+ local o = s:option(t, "name", ...)
+
+ function o.cfgvalue(self, section)
+ return self.map:get(section, "name") or
+ self.map:get(section, "_name") or "-"
end
+
+ function o.write(self, section, value)
+ if value ~= "-" then
+ self.map:set(section, "name", value)
+ self.map:del(section, "_name")
+ else
+ self:remove(section)
+ end
+ end
+
+ function o.remove(self, section)
+ self.map:del(section, "name")
+ self.map:del(section, "_name")
+ end
+
+ return o
end