From 73aff0f63b4aa93fc954ad4302e833df317bc8aa Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Wed, 11 Oct 2017 16:38:07 +0300 Subject: [PATCH] Rule: make convertchain a method --- awall/model.lua | 76 +++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/awall/model.lua b/awall/model.lua index 64aec6b..5fd214e 100644 --- a/awall/model.lua +++ b/awall/model.lua @@ -590,46 +590,8 @@ function M.Rule:trules() setdefault(ofrag, 'target', custom or self:target()) end - local tbl = self:table() - - local function convertchains(ofrags) - local res = {} - - for i, ofrag in ipairs(ofrags) do - - if contains(builtin[tbl], ofrag.chain) then table.insert(res, ofrag) - else - local ofs, recursive - if ofrag.chain == 'PREROUTING' then - ofs = {{chain='FORWARD'}, {chain='INPUT'}} - elseif ofrag.chain == 'POSTROUTING' then - ofs = {{chain='FORWARD'}, {chain='OUTPUT'}} - recursive = true - elseif ofrag.chain == 'INPUT' then - ofs = { - {match='-m addrtype --dst-type LOCAL', chain='PREROUTING'} - } - elseif ofrag.chain == 'FORWARD' then - ofs = { - {match='-m addrtype ! --dst-type LOCAL', chain='PREROUTING'} - } - end - - if ofs then - ofrag.chain = nil - ofs = combinations(ofs, {ofrag}) - if recursive then ofs = convertchains(ofs) end - extend(res, ofs) - - else table.insert(res, ofrag) end - end - end - - return res - end - - ofrags = convertchains(ffilter(ofrags)) - tag(ofrags, 'table', tbl, false) + ofrags = self:convertchains(ffilter(ofrags)) + tag(ofrags, 'table', self:table(), false) local function checkzof(ofrag, dir, chains) if ofrag[dir] and contains(chains, ofrag.chain) then @@ -667,6 +629,40 @@ function M.Rule:trulefilter(rule) return true end function M.Rule:extratrules(rules) return {} end +function M.Rule:convertchains(ofrags) + local res = {} + + for _, ofrag in ipairs(ofrags) do + + if contains(builtin[self:table()], ofrag.chain) then + table.insert(res, ofrag) + + else + local ofs, recursive + if ofrag.chain == 'PREROUTING' then + ofs = {{chain='FORWARD'}, {chain='INPUT'}} + elseif ofrag.chain == 'POSTROUTING' then + ofs = {{chain='FORWARD'}, {chain='OUTPUT'}} + recursive = true + elseif ofrag.chain == 'INPUT' then + ofs = {{match='-m addrtype --dst-type LOCAL', chain='PREROUTING'}} + elseif ofrag.chain == 'FORWARD' then + ofs = {{match='-m addrtype ! --dst-type LOCAL', chain='PREROUTING'}} + end + + if ofs then + ofrag.chain = nil + ofs = combinations(ofs, {ofrag}) + if recursive then ofs = self:convertchains(ofs) end + extend(res, ofs) + + else table.insert(res, ofrag) end + end + end + + return res +end + function M.Rule:extrarules(label, cls, options) local params = {} -- 2.22.0