diff options
-rwxr-xr-x | awall-cli | 16 | ||||
-rw-r--r-- | awall/init.lua | 32 |
2 files changed, 32 insertions, 16 deletions
@@ -6,7 +6,23 @@ Copyright (C) 2012 Kaarle Ritvanen Licensed under the terms of GPL2 ]]-- +require 'lfs' +require 'stringy' + +testmode = stringy.endswith(arg[0], '/awall-cli') + +if testmode then + path = string.sub(arg[0], 1, -11) + lfs.chdir(path) +end require 'awall' +awall.loadmodules(testmode and '.') + +if testmode then + awall.confdirs = {'json', 'input'} + awall.iptdir = 'output' + awall.ipsfile = 'output/ipset' +end awall.translate() diff --git a/awall/init.lua b/awall/init.lua index 2f87b37..b2d2680 100644 --- a/awall/init.lua +++ b/awall/init.lua @@ -15,17 +15,21 @@ require 'awall.model' require 'awall.util' -local testmode = arg[0] ~= '/usr/sbin/awall' +confdirs = {'/usr/share/awall', '/etc/awall'} +iptdir = '/etc/iptables' +ipsfile = '/etc/ipset.d/awall' local modules = {package.loaded['awall.model']} - -local modpath = testmode and '.' or '/usr/share/lua/5.1' -for modfile in lfs.dir(modpath..'/awall/modules') do - if stringy.endswith(modfile, '.lua') then - local name = 'awall.modules.'..string.sub(modfile, 1, -5) - require(name) - table.insert(modules, package.loaded[name]) +local loaded = false + +function loadmodules(path) + for modfile in lfs.dir((path or '/usr/share/lua/5.1')..'/awall/modules') do + if stringy.endswith(modfile, '.lua') then + local name = 'awall.modules.'..string.sub(modfile, 1, -5) + require(name) + table.insert(modules, package.loaded[name]) + end end end @@ -34,10 +38,6 @@ function translate() config = {} - local confdirs = testmode and {'json', - 'config'} or {'/usr/share/awall', - '/etc/awall'} - for i, dir in ipairs(confdirs) do local fnames = {} for fname in lfs.dir(dir) do table.insert(fnames, fname) end @@ -119,17 +119,17 @@ function translate() end end - awall.iptables.dump(testmode and 'output' or '/etc/iptables') + awall.iptables.dump(iptdir) if config.ipset then - ipsfile = io.output(testmode and 'output/ipset' or '/etc/ipset.d/awall') + local ips = io.output(ipsfile) for name, params in pairs(config.ipset) do 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 - ipsfile:write(line..'\n') + ips:write(line..'\n') end - ipsfile:close() + ips:close() end end |