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

7

8
local util = require('awall.util')
9 10


11
local IPSet = require('awall.class')()
12

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

15 16
function IPSet:dumpfile(name, ipsfile)
   ipsfile:write('# ipset '..name..'\n')
17
   ipsfile:write(table.concat(self.config[name].options, ' '))
18
   ipsfile:write('\n')
19 20
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
21
function IPSet:create()
22
   for name, ipset in pairs(self.config) do
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
23 24 25
      local pid = lpc.run(
	 'ipset', '-!', 'create', name, table.unpack(ipset.options)
      )
26
      if lpc.wait(pid) ~= 0 then
27
	 util.printmsg('ipset creation failed: '..name)
28 29 30 31
      end
   end
end

32
function IPSet:print()
33
   for _, name in util.sortedkeys(self.config) do
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
34 35
      self:dumpfile(name, io.output())
      io.write('\n')
36
   end
37 38
end

39
function IPSet:dump(prefix)
40
   for name, ipset in pairs(self.config) do
41
      local fname = prefix..name
42 43 44 45 46 47 48
      local file = io.open(fname)
      if not file then
	 file = io.open(fname, 'w')
	 self:dumpfile(name, file)
      end
      file:close()
   end
49
end
50 51

return IPSet