This instance of Gitlab will be upgraded to 13.9 at 13.00 CEST / 11.00 UTC. It will be unavailable for some moment. Thanks for your understanding.

ipset.lua 1.04 KB
Newer Older
1 2 3 4 5 6 7 8 9
--[[
Ipset file dumper for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--


module(..., package.seeall)

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
10 11 12 13 14 15 16 17 18
local IPSet = {}

function new(config)
   local res = {config=config}
   setmetatable(res, {__index=IPSet})
   return res
end

function IPSet:commands()
19
   local res = {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
20 21
   if self.config then
      for name, params in pairs(self.config) do
22 23 24 25 26 27 28 29 30
	 if not params.type then error('Type not defined for set '..name) end
	 local line = 'create '..name..' '..params.type
	 if params.family then line = line..' family '..params.family end
	 table.insert(res, line..'\n')
      end
   end
   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
31 32
function IPSet:create()
   for i, line in ipairs(self:commands()) do
33 34 35 36 37 38 39 40 41
      local pid, stdin = lpc.run('ipset', '-!', 'restore')
      stdin:write(line)
      stdin:close()
      if lpc.wait(pid) ~= 0 then
	 io.stderr:write('ipset command failed: '..line)
      end
   end
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
42
function IPSet:dump(ipsfile)
43
   local file = io.output(ipsfile)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
44
   for i, line in ipairs(self:commands()) do file:write(line) end
45 46
   file:close()
end