a2e3bce3484e6d52e75d9021858648f674ef8fd4
[project/luci.git] / applications / luci-firewall / luasrc / tools / firewall.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011 Jo-Philipp Wich <xm@subsignal.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 module("luci.tools.firewall", package.seeall)
16
17 local ut = require "luci.util"
18 local ip = require "luci.ip"
19 local nx = require "nixio"
20
21 local tr, trf = luci.i18n.translate, luci.i18n.translatef
22
23 function fmt_neg(x)
24         if type(x) == "string" then
25                 local v, neg = x:gsub("^ *! *", "")
26                 if neg > 0 then
27                         return v, "%s " % tr("not")
28                 else
29                         return x, ""
30                 end
31         end
32         return x, ""
33 end
34
35 function fmt_mac(x)
36         if x and #x > 0 then
37                 local m, n
38                 local l = { tr("MAC"), " " }
39                 for m in ut.imatch(x) do
40                         m, n = fmt_neg(m)
41                         l[#l+1] = "<var>%s%s</var>" %{ n, m }
42                         l[#l+1] = ", "
43                 end
44                 if #l > 1 then
45                         l[#l] = nil
46                         if #l > 3 then
47                                 l[1] = tr("MACs")
48                         end
49                         return table.concat(l, "")
50                 end
51         end
52 end
53
54 function fmt_port(x)
55         if x and #x > 0 then
56                 local p, n
57                 local l = { tr("port"), " " }
58                 for p in ut.imatch(x) do
59                         p, n = fmt_neg(p)
60                         local a, b = p:match("(%d+)%D+(%d+)")
61                         if a and b then
62                                 l[1] = tr("ports")
63                                 l[#l+1] = "<var>%s%d-%d</var>" %{ n, a, b }
64                         else
65                                 l[#l+1] = "<var>%s%d</var>" %{ n, p }
66                         end
67                         l[#l+1] = ", "
68                 end
69                 if #l > 1 then
70                         l[#l] = nil
71                         if #l > 3 then
72                                 l[1] = tr("ports")
73                         end
74                         return table.concat(l, "")
75                 end
76         end
77 end
78
79 function fmt_ip(x)
80         if x and #x > 0 then
81                 local l = { tr("IP"), " " }
82                 local v, a, n
83                 for v in ut.imatch(x) do
84                         v, n = fmt_neg(v)
85                         a, m = v:match("(%S+)/(%d+%.%S+)")
86                         a = a or v
87                         a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m)
88                         if a and (a:is6() or a:prefix() < 32) then
89                                 l[1] = tr("IP range")
90                                 l[#l+1] = "<var title='%s - %s'>%s%s</var>" %{
91                                         a:minhost():string(),
92                                         a:maxhost():string(),
93                                         n, a:string()
94                                 }
95                         else
96                                 l[#l+1] = "<var>%s%s</var>" %{
97                                         n,
98                                         a and a:string() or v
99                                 }
100                         end
101                         l[#l+1] = ", "
102                 end
103                 if #l > 1 then
104                         l[#l] = nil
105                         if #l > 3 then
106                                 l[1] = tr("IPs")
107                         end
108                         return table.concat(l, "")
109                 end
110         end
111 end
112
113 function fmt_zone(x)
114         if x == "*" then
115                 return "<var>%s</var>" % tr("any zone")
116         elseif x and #x > 0 then
117                 return "<var>%s</var>" % x
118         end
119 end
120
121 function fmt_icmp_type(x)
122         if x and #x > 0 then
123                 local t, v, n
124                 local l = { tr("type"), " " }
125                 for v in ut.imatch(x) do
126                         v, n = fmt_neg(v)
127                         l[#l+1] = "<var>%s%s</var>" %{ n, v }
128                         l[#l+1] = ", "
129                 end
130                 if #l > 1 then
131                         l[#l] = nil
132                         if #l > 3 then
133                                 l[1] = tr("types")
134                         end
135                         return table.concat(l, "")
136                 end
137         end
138 end
139
140 function fmt_proto(x, icmp_types)
141         if x and #x > 0 then
142                 local v, n
143                 local l = { }
144                 local t = fmt_icmp_type(icmp_types)
145                 for v in ut.imatch(x) do
146                         v, n = fmt_neg(v)
147                         if v == "tcpudp" then
148                                 l[#l+1] = "TCP"
149                                 l[#l+1] = "UDP"
150                                 l[#l+1] = ", "
151                         elseif v ~= "all" then
152                                 local p = nx.getproto(v)
153                                 if p then
154                                         -- ICMP
155                                         if (p.proto == 1 or p.proto == 58) and t then
156                                                 l[#l+1] = trf(
157                                                         "%s%s with %s",
158                                                         n, p.aliases[1] or p.name, t
159                                                 )
160                                         else
161                                                 l[#l+1] = "%s%s" %{
162                                                         n,
163                                                         p.aliases[1] or p.name
164                                                 }
165                                         end
166                                         l[#l+1] = ", "
167                                 end
168                         end
169                 end
170                 if #l > 0 then
171                         l[#l] = nil
172                         return table.concat(l, "")
173                 end
174         end
175 end
176
177 function fmt_limit(limit, burst)
178         burst = tonumber(burst)
179         if limit and #limit > 0 then
180                 local l, u = limit:match("(%d+)/(%w+)")
181                 l = tonumber(l or limit)
182                 u = u or "second"
183                 if l then
184                         if u:match("^s") then
185                                 u = tr("second")
186                         elseif u:match("^m") then
187                                 u = tr("minute")
188                         elseif u:match("^h") then
189                                 u = tr("hour")
190                         elseif u:match("^d") then
191                                 u = tr("day")
192                         end
193                         if burst and burst > 0 then
194                                 return trf("<var>%d</var> pkts. per <var>%s</var>, \
195                                     burst <var>%d</var> pkts.", l, u, burst)
196                         else
197                                 return trf("<var>%d</var> pkts. per <var>%s</var>", l, u)
198                         end
199                 end
200         end
201 end
202
203 function fmt_target(x)
204         if x == "ACCEPT" then
205                 return tr("Accept")
206         elseif x == "REJECT" then
207                 return tr("Refuse")
208         elseif x == "NOTRACK" then
209                 return tr("Do not track")
210         else --if x == "DROP" then
211                 return tr("Discard")
212         end
213 end