ipset.lua 1.08 KB
Newer Older
1 2
--[[
Ipset file dumper for Alpine Wall
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
3
Copyright (C) 2012-2019 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 26 27
      if not lpc.wait(
	 util.run(
	    'ipset', '-!', 'create', name, table.unpack(ipset.options)
	 )
      ) ~= 0 then
28
	 util.printmsg('ipset creation failed: '..name)
29 30 31 32
      end
   end
end

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

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

return IPSet