ipset.lua 1.04 KB
Newer Older
1 2
--[[
Ipset file dumper for Alpine Wall
3
Copyright (C) 2012-2014 Kaarle Ritvanen
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
4
See LICENSE file for license details
5 6
]]--

7
local IPSet = require('awall.class')()
8

9
function IPSet:init(config) self.config = config or {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
10

11 12
function IPSet:dumpfile(name, ipsfile)
   ipsfile:write('# ipset '..name..'\n')
13
   ipsfile:write(table.concat(self.config[name].options, ' '))
14
   ipsfile:write('\n')
15 16
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
17
function IPSet:create()
18 19
   for name, ipset in pairs(self.config) do
      local pid = lpc.run('ipset', '-!', 'create', name,
20
			  unpack(ipset.options))
21
      if lpc.wait(pid) ~= 0 then
22
	 io.stderr:write('ipset creation failed: '..name)
23 24 25 26
      end
   end
end

27 28 29 30 31
function IPSet:print()
   for name, ipset in pairs(self.config) do
      self:dumpfile(name, io.stdout)
      io.stdout:write('\n')
   end
32 33
end

34
function IPSet:dump(prefix)
35
   for name, ipset in pairs(self.config) do
36
      local fname = prefix..name
37 38 39 40 41 42 43
      local file = io.open(fname)
      if not file then
	 file = io.open(fname, 'w')
	 self:dumpfile(name, file)
      end
      file:close()
   end
44
end
45 46

return IPSet