diff options
author | Sergey Nazaryev <sergey@nazaryev.ru> | 2018-11-06 16:33:31 +0000 |
---|---|---|
committer | Sergey Nazaryev <sergey@nazaryev.ru> | 2018-11-06 16:54:01 +0000 |
commit | 070561e040f666c7692a0baa80a7577bf423cbd5 (patch) | |
tree | 4d026f3907d68565e3c81a380a2e9c1a52ab73af | |
parent | 18369a7173001242c4fa611443b0c10ffac8bd9d (diff) | |
download | ifstat-070561e040f666c7692a0baa80a7577bf423cbd5.zip ifstat-070561e040f666c7692a0baa80a7577bf423cbd5.tar.gz ifstat-070561e040f666c7692a0baa80a7577bf423cbd5.tar.bz2 |
Вынес конфиг в отдельный файл (config.lua)
-rw-r--r-- | config.lua | 58 | ||||
-rwxr-xr-x | ifstatd.lua | 45 |
2 files changed, 85 insertions, 18 deletions
diff --git a/config.lua b/config.lua new file mode 100644 index 0000000..2bc31ab --- /dev/null +++ b/config.lua @@ -0,0 +1,58 @@ +local ANY = -1 +local IPPROTO_UDP = 17 +local IPPROTO_TCP = 6 + +local _config = { + delay_ms = 500, + iface = "enp0s8", + + filters = { + { + filter_num = 0, + enabled = 1, + ipproto = IPPROTO_TCP, + src_ip = "140.82.33.182", + dst_ip = ANY, + src_port = 22, + dst_port = ANY + }, + { + filter_num = 1, + enabled = 0, + ipproto = IPPROTO_TCP, + src_ip = ANY, + dst_ip = ANY, + src_port = ANY, + dst_port = ANY + }, + { + filter_num = 2, + enabled = 0, + ipproto = ANY, + src_ip = ANY, + dst_ip = ANY, + src_port = ANY, + dst_port = ANY + }, + { + filter_num = 3, + enabled = 0, + ipproto = ANY, + src_ip = ANY, + dst_ip = ANY, + src_port = ANY, + dst_port = ANY + }, + { + filter_num = 4, + enabled = 0, + ipproto = ANY, + src_ip = ANY, + dst_ip = ANY, + src_port = ANY, + dst_port = ANY + } + } +} + +return _config diff --git a/ifstatd.lua b/ifstatd.lua index ddb2551..b5098e4 100755 --- a/ifstatd.lua +++ b/ifstatd.lua @@ -1,17 +1,29 @@ local ANY = -1 -local IPPROTO_UDP = 17 -local IPPROTO_TCP = 6 +local MAX_FILTER_COUNT = 5 + +-- Взято из https://stackoverflow.com/questions/8200228/how-can-i-convert-an-ip-address-into-an-integer-with-lua +local ip2dec = function(ip) local i, dec = 3, 0; for d in string.gmatch(ip, "%d+") do dec = dec + 2 ^ (8 * i) * d; i = i - 1 end; return dec end local filter_to_defines = function(args) local num = args.filter_num local defines = {} if args.enabled == 1 then - defines["FILTER" .. num .. "_IPPROTO"] = tonumber(args.ipproto) - defines["FILTER" .. num .. "_SRC_IP"] = tonumber(args.src_ip) - defines["FILTER" .. num .. "_DST_IP"] = tonumber(args.dst_ip) - defines["FILTER" .. num .. "_SRC_PORT"] = tonumber(args.src_port) - defines["FILTER" .. num .. "_DST_PORT"] = tonumber(args.dst_port) + local src_ip = ANY + local dst_ip = ANY + + if args.src_ip ~= ANY then + src_ip = ip2dec(args.src_ip) + end + if args.dst_ip ~= ANY then + dst_ip = ip2dec(args.dst_ip) + end + + defines["FILTER" .. num .. "_IPPROTO"] = tonumber(args.ipproto or ANY) + defines["FILTER" .. num .. "_SRC_IP"] = src_ip + defines["FILTER" .. num .. "_DST_IP"] = dst_ip + defines["FILTER" .. num .. "_SRC_PORT"] = tonumber(args.src_port or ANY) + defines["FILTER" .. num .. "_DST_PORT"] = tonumber(args.dst_port or ANY) defines["FILTER" .. num .. "_ENABLED"] = 1 else defines["FILTER" .. num .. "_ENABLED"] = 0 @@ -70,10 +82,6 @@ local get_ifstat_data = function(bpf, filters) return ifstat_data end --- Взято из https://stackoverflow.com/questions/8200228/how-can-i-convert-an-ip-address-into-an-integer-with-lua -local ip2dec = function(ip) local i, dec = 3, 0; for d in string.gmatch(ip, "%d+") do dec = dec + 2 ^ (8 * i) * d; i = i - 1 end; return dec end -local dec2ip = function(decip) local divisor, quotient, ip; for i = 3, 0, -1 do divisor = 2 ^ (i * 8); quotient, decip = math.floor(decip / divisor), math.fmod(decip, divisor); if nil == ip then ip = quotient else ip = ip .. "." .. quotient end end return ip end - local inject_ifstat_bpf = function(BPF, iface, filters) local defines = filters_to_defines(filters) defines["ANY"] = ANY @@ -87,13 +95,14 @@ local inject_ifstat_bpf = function(BPF, iface, filters) end local parse_config = function() - return { ["delay_ms"] = 500, ["iface"] = "enp0s8", ["filters"] = { - {enabled=1, filter_num=0, ipproto=ANY, src_ip=ANY, dst_ip=ANY, src_port=ANY, dst_port=ANY}, - {enabled=0, filter_num=1, ipproto=ANY, src_ip=ANY, dst_ip=ANY, src_port=ANY, dst_port=ANY}, - {enabled=0, filter_num=2, ipproto=ANY, src_ip=ANY, dst_ip=ANY, src_port=ANY, dst_port=ANY}, - {enabled=0, filter_num=3, ipproto=ANY, src_ip=ANY, dst_ip=ANY, src_port=ANY, dst_port=ANY}, - {enabled=0, filter_num=4, ipproto=ANY, src_ip=ANY, dst_ip=ANY, src_port=ANY, dst_port=ANY} - }} + local config = require "config" + local filters_count = #config["filters"] + if filters_count > MAX_FILTER_COUNT then + error("ERROR: Max allowed amount of filters: %d" % (filters_count)) + elseif filters_count <= 0 then + error("ERROR: Please fill config (#TODO)") + end + return config end local ubus_objects = { ifstat = {} } |