From 7c0f4a79df46c3f6d063ee80215ce27222455b5c Mon Sep 17 00:00:00 2001 From: jack chen Date: Tue, 17 Nov 2020 05:32:58 -0900 Subject: [PATCH 1/4] add ubus interface Signed-off-by: jack chen --- open-app-filter/files/appfilter.lua | 183 ++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 open-app-filter/files/appfilter.lua diff --git a/open-app-filter/files/appfilter.lua b/open-app-filter/files/appfilter.lua new file mode 100644 index 0000000..8715c3e --- /dev/null +++ b/open-app-filter/files/appfilter.lua @@ -0,0 +1,183 @@ +#!/usr/bin/lua + +local libubus = require "ubus" +local uloop = require "uloop" + +local UBUS_STATUS_OK = 0 +local UBUS_STATUS_INVALID_COMMAND = 1 +local UBUS_STATUS_INVALID_ARGUMENT = 2 +local UBUS_STATUS_METHOD_NOT_FOUND = 3 +local UBUS_STATUS_NOT_FOUND = 4 +local UBUS_STATUS_NO_DATA = 5 +local UBUS_STATUS_PERMISSION_DENIED = 6 +local UBUS_STATUS_TIMEOUT = 7 +local UBUS_STATUS_NOT_SUPPORTED = 8 +local UBUS_STATUS_UNKNOWN_ERROR = 9 +local UBUS_STATUS_CONNECTION_FAILED = 10 +local UBUS_STATUS_ALREADY_EXISTS = 11 + +local cfg = "/etc/appfilter/feature.cfg" + +local ubus + +local function init_table() + local f = io.open(cfg, "r") + local t = {} + if f then + for l in f:lines() do + table.insert(t, l) + end + end + f:close() + return t +end + +local function lookup(t, o) + if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end + if not o then return UBUS_STATUS_INVALID_ARGUMENT end + + for _, v in ipairs(t) do + if v:match(o) then + if v:match("#class") then + local tt = {} + local found + + for _, v in ipairs(t) do + repeat + if v:match(o) then + found = true + table.insert(tt, v) + break + end + + if v:match("#class") then + found = false + break + end + + if found then + table.insert(tt, v) + end + until true + end + return tt + else + return v + end + else + return nil + end + return nil +end + +local function lookup_class(t, c) + if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end + if not c then return UBUS_STATUS_INVALID_ARGUMENT end + + local ret = lookup(t, c) + if type(ret) ~= "table" then return UBUS_STATUS_NOT_FOUND then + return ret +end + +local function lookup_app(t, c) + if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end + if not c then return UBUS_STATUS_INVALID_ARGUMENT end + + local ret = lookup(t, c) + + if type(ret) ~= "string" then return UBUS_STATUS_NOT_FOUND end + return ret +end + +local function add_class(t, c) + if not c then return UBUS_STATUS_INVALID_ARGUMENT end + local f = io.open(cfg, "r+") + if f then + io.output(f) + for v in f:lines() do + io.write(v) + io.write("\n") + end + io.write("#class "..c) + end + f:flush() + f:close() +end + +local function list_class(t) + if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end + + local tt = {} + + for _, v in ipairs(t) do + if v:match("#class (%S+)") then + table.insert(tt, v) + end + end + return tt +end + +local methods = { + ["appfilter"] = { + add_class = { + function(req, msg) + if not msg.class return UBUS_STATUS_INVALID_ARGUMENT end + local t = init_table() + local ret + local tmp = lookup_class(t, msg.class) + if type(tmp) ~= "table" then + add_class(t, msg.class) + else + ret = UBUS_STATUS_ALREADY_EXISTS + end + ubus.reply(req, {status = ret}) + end,{class = libubus.STRING} + }, + list_class = { + function(req, msg) + local t = init_table() + local class = list_class(t) + if not ret then ubus.reply(req, { class = class}) then + end,{} + }, + list_app = { + function(req,msg) + if not msg.class return UBUS_STATUS_INVALID_ARGUMENT end + local t = init_table() + local tt = lookup_class(t, msg.class) + local ret = {} + for i, v in ipairs(tt) do + local id, name = v:match("(%d+) (%S+)") + ret[i] = {id = id, name = name} + end + ubus.reply(req, {app = ret}) + end,{class = libubus.STRING} + } + } +} + +function ubus_init() + local conn = libubus.connect() + if not conn then + error("Failed to connect to ubus") + end + + conn:add(methods) + + return { + call = function(object, method, params) + return conn:call(object, method, params or {}) + end, + reply = function(req, msg) + conn:reply(req, msg) + end + } +end + +local function main() + uloop.init() + ubus = ubus_init() + uloop.run() +end + +main() \ No newline at end of file From 30838ede59b1d59036140d15e3bef76b89ce28e2 Mon Sep 17 00:00:00 2001 From: juno Date: Tue, 17 Nov 2020 22:10:56 -0900 Subject: [PATCH 2/4] add ubus interface Signed-off-by: juno --- open-app-filter/Makefile | 2 + open-app-filter/files/appfilter.init | 1 + open-app-filter/files/appfilter.lua | 264 +++++++++++++++++---------- 3 files changed, 170 insertions(+), 97 deletions(-) diff --git a/open-app-filter/Makefile b/open-app-filter/Makefile index e8f9d31..b7d790c 100755 --- a/open-app-filter/Makefile +++ b/open-app-filter/Makefile @@ -14,6 +14,7 @@ include $(INCLUDE_DIR)/package.mk define Package/appfilter SECTION:=Derry Apps CATEGORY:=Derry Apps + DEPENDS:=+libubox-lua TITLE:=App filter userspace module endef @@ -39,6 +40,7 @@ define Package/appfilter/install $(INSTALL_BIN) ./files/appfilter.init $(1)/etc/init.d/appfilter $(INSTALL_BIN) ./files/appfilter.sh $(1)/usr/bin $(INSTALL_BIN) ./files/gen_class.sh $(1)/usr/bin + $(INSTALL_BIN) ./files/appfilter.lua $(1)/usr/bin/appfilter $(INSTALL_BIN) ./files/appfilter.config $(1)/etc/config/appfilter endef diff --git a/open-app-filter/files/appfilter.init b/open-app-filter/files/appfilter.init index a1aac6e..f2024e7 100755 --- a/open-app-filter/files/appfilter.init +++ b/open-app-filter/files/appfilter.init @@ -4,6 +4,7 @@ START=96 start() { gen_class.sh /etc/appfilter/feature.cfg appfilter.sh + /usr/bin/appfilter & } stop() { diff --git a/open-app-filter/files/appfilter.lua b/open-app-filter/files/appfilter.lua index 8715c3e..1fce329 100644 --- a/open-app-filter/files/appfilter.lua +++ b/open-app-filter/files/appfilter.lua @@ -16,144 +16,214 @@ local UBUS_STATUS_UNKNOWN_ERROR = 9 local UBUS_STATUS_CONNECTION_FAILED = 10 local UBUS_STATUS_ALREADY_EXISTS = 11 -local cfg = "/etc/appfilter/feature.cfg" +local cfg_file = "/etc/appfilter/feature.cfg" +local cfg = {} +local class = {} local ubus -local function init_table() - local f = io.open(cfg, "r") +cfg.__index = cfg +class.__index = class +function cfg:init_table(file) + local f = io.open(file, "r") local t = {} + local t2 = {} if f then - for l in f:lines() do - table.insert(t, l) - end - end - f:close() - return t -end - -local function lookup(t, o) - if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end - if not o then return UBUS_STATUS_INVALID_ARGUMENT end - - for _, v in ipairs(t) do - if v:match(o) then - if v:match("#class") then - local tt = {} - local found - - for _, v in ipairs(t) do - repeat - if v:match(o) then - found = true - table.insert(tt, v) - break - end - - if v:match("#class") then - found = false - break - end - - if found then - table.insert(tt, v) - end - until true - end - return tt - else - return v + for line in f:lines() do + table.insert(t, line) + local tt = line:match("#class (%S+)") + if tt then + table.insert(t2, tt) end - else - return nil + end + setmetatable(t, self) + setmetatable(t2, self) + return t,t2 end return nil end -local function lookup_class(t, c) - if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end - if not c then return UBUS_STATUS_INVALID_ARGUMENT end +function cfg:lookup(o) + if not o then return UBUS_STATUS_INVALID_ARGUMENT end + local tab = self + for _, v in ipairs(tab) do + if v:match(o) then + if v:match("#class") then + local tt = {} + local t2 = {} + local found + for _, t in ipairs(tab) do + repeat + if t:match(o) then + found = true + table.insert(tt, t) + break + end - local ret = lookup(t, c) - if type(ret) ~= "table" then return UBUS_STATUS_NOT_FOUND then - return ret + if t:match("#class") then + found = false + table.insert(t2, t) + break + end + + if found then + table.insert(tt, t) + else + table.insert(t2, t) + end + until true + end + setmetatable(tt, self) + setmetatable(t2, self) + return tt, t2 + else + return v + end + end + end + return nil end -local function lookup_app(t, c) - if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end - if not c then return UBUS_STATUS_INVALID_ARGUMENT end - - local ret = lookup(t, c) - - if type(ret) ~= "string" then return UBUS_STATUS_NOT_FOUND end - return ret +function cfg:lookup_class(class) + if not class then return UBUS_STATUS_INVALID_ARGUMENT end + local t1, t2 = self:lookup(class) + if type(t1) ~= "table" then return nil end + return t1, t2 end -local function add_class(t, c) - if not c then return UBUS_STATUS_INVALID_ARGUMENT end - local f = io.open(cfg, "r+") +function cfg:add_class(class) + if not class then return UBUS_STATUS_INVALID_ARGUMENT end + local f = io.open(cfg_file, "r+") + local tab = self if f then io.output(f) - for v in f:lines() do + for _, v in ipairs(tab) do io.write(v) io.write("\n") end - io.write("#class "..c) + io.write("#class "..class) + f:flush() + f:close() + return UBUS_STATUS_OK + else + return UBUS_STATUS_NOT_FOUND end - f:flush() - f:close() end -local function list_class(t) - if type(t) ~= "table" then return UBUS_STATUS_INVALID_ARGUMENT end +function cfg:add_app(class, name, proto, sport, dport, url, request, dict) + if not name then return UBUS_STATUS_INVALID_ARGUMENT end + local f = io.open(cfg_file, "r+") + io.output(f) + local t1,t2 = self:lookup_class(class) + local id = math.modf(string.match(t1[#t1], "(%d+) %S+:") +1) + local str = string.format("%d %s:[%s;%s;%s;%s;%s;%s]", id, name, proto, sport or "", dport or "", url or "", request or "", dict or "") + table.insert(t1, str) + if f then + for _, v in ipairs(t2) do + if v then + io.write(v) + io.write("\n") + end + end + for _, v in ipairs(t1) do + if v then + io.write(v) + io.write("\n") + end + end + f:flush() + f:close() + end + return id +end - local tt = {} +function cfg:del_app(id, name) + local t = self + local f = io.open(cfg_file, "r+") + local ret + if id then + for i, v in ipairs(t) do + if v:match(id) then + table.remove(t, i) + ret = i + end + end - for _, v in ipairs(t) do - if v:match("#class (%S+)") then - table.insert(tt, v) + end + + if name then + for i, v in ipairs(t) do + if v:match(name) then + table.remove(t, i) + ret = i + end end end - return tt + + if f then + io.output(f) + for _, v in ipairs(t) do + io.write(v) + io.write("\n") + end + f:flush() + f:close() + end + return ret end local methods = { - ["appfilter"] = { - add_class = { + ["appfilter"] = { + add_class = { function(req, msg) - if not msg.class return UBUS_STATUS_INVALID_ARGUMENT end - local t = init_table() + if not msg.class then return UBUS_STATUS_INVALID_ARGUMENT end + local class = msg.class + local t = cfg:init_table(cfg_file) local ret - local tmp = lookup_class(t, msg.class) - if type(tmp) ~= "table" then - add_class(t, msg.class) - else - ret = UBUS_STATUS_ALREADY_EXISTS - end - ubus.reply(req, {status = ret}) - end,{class = libubus.STRING} + if t:lookup_class(class) then return ubus.reply(req, {ret = UBUS_STATUS_ALREADY_EXISTS}) end + ret = t:add_class(class) + ubus.reply(req, {msg = ret}) + end, {class = libubus.STRING} + }, + add_app = { + function (req, msg) + if not msg.class then return UBUS_STATUS_INVALID_ARGUMENT end + if not msg.name then return UBUS_STATUS_INVALID_ARGUMENT end + local t = cfg:init_table(cfg_file) + local ret + if t:lookup(msg.name) then return ubus.reply(req, {ret = UBUS_STATUS_ALREADY_EXISTS}) end + ret = t:add_app(msg.class, msg.name, msg.proto, msg.sport, msg.dport, msg.url, msg.request, msg.dict) + ubus.reply(req, {ret = ret}) + end,{class = libubus.STRING, name = libubus.STRING, proto = libubus.STRING, sport = libubus.INT32, dport = libubus.INT32, url = libubus.STRING, request = libubus.STRING, dict = libubus.STRING} + }, + del_app = { + function(req, msg) + local t = cfg:init_table(cfg_file) + ret = t:del_app(msg.id, msg.name) + ubus.reply(req, {ret = ret}) + end,{id = libubus.INT32, name = libubus.STRING} }, list_class = { - function(req, msg) - local t = init_table() - local class = list_class(t) - if not ret then ubus.reply(req, { class = class}) then + function (req, msg) + local _, c = cfg:init_table(cfg_file) + ubus.reply(req, {result = c}) end,{} }, list_app = { - function(req,msg) - if not msg.class return UBUS_STATUS_INVALID_ARGUMENT end - local t = init_table() - local tt = lookup_class(t, msg.class) + function (req, msg) + if not msg.class then return UBUS_STATUS_INVALID_ARGUMENT end + local t = cfg:init_table(cfg_file) local ret = {} - for i, v in ipairs(tt) do - local id, name = v:match("(%d+) (%S+)") - ret[i] = {id = id, name = name} + for i, v in ipairs(t:lookup_class(msg.class)) do + if not v:match("#class") then + local id, name = v:match("(%d+) (%S+):%[") + ret[i-1] = {id = id, name = name} + end end - ubus.reply(req, {app = ret}) + ubus.reply(req, {result = ret}) end,{class = libubus.STRING} } - } + } } function ubus_init() From d775e3e4e63a2d71b4b2a8346fa31b59470dc9c7 Mon Sep 17 00:00:00 2001 From: juno Date: Tue, 17 Nov 2020 23:16:27 -0900 Subject: [PATCH 3/4] fix some bug Signed-off-by: juno --- open-app-filter/files/appfilter.lua | 38 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/open-app-filter/files/appfilter.lua b/open-app-filter/files/appfilter.lua index 1fce329..f8bd7db 100644 --- a/open-app-filter/files/appfilter.lua +++ b/open-app-filter/files/appfilter.lua @@ -24,7 +24,7 @@ local ubus cfg.__index = cfg class.__index = class -function cfg:init_table(file) +function cfg:init(file) local f = io.open(file, "r") local t = {} local t2 = {} @@ -84,15 +84,15 @@ function cfg:lookup(o) return nil end -function cfg:lookup_class(class) - if not class then return UBUS_STATUS_INVALID_ARGUMENT end - local t1, t2 = self:lookup(class) +function cfg:lookup_class(m) + if not m then return UBUS_STATUS_INVALID_ARGUMENT end + local t1, t2 = self:lookup(m) if type(t1) ~= "table" then return nil end return t1, t2 end -function cfg:add_class(class) - if not class then return UBUS_STATUS_INVALID_ARGUMENT end +function cfg:add_class(m) + if not m then return UBUS_STATUS_INVALID_ARGUMENT end local f = io.open(cfg_file, "r+") local tab = self if f then @@ -101,7 +101,7 @@ function cfg:add_class(class) io.write(v) io.write("\n") end - io.write("#class "..class) + io.write("#class "..m) f:flush() f:close() return UBUS_STATUS_OK @@ -110,12 +110,12 @@ function cfg:add_class(class) end end -function cfg:add_app(class, name, proto, sport, dport, url, request, dict) +function cfg:add_app(m, name, proto, sport, dport, url, request, dict) if not name then return UBUS_STATUS_INVALID_ARGUMENT end local f = io.open(cfg_file, "r+") io.output(f) - local t1,t2 = self:lookup_class(class) - local id = math.modf(string.match(t1[#t1], "(%d+) %S+:") +1) + local t1,t2 = self:lookup_class(m) + local id = math.modf(string.match(t1[#t1-1], "(%d+) %S+:") +1) local str = string.format("%d %s:[%s;%s;%s;%s;%s;%s]", id, name, proto, sport or "", dport or "", url or "", request or "", dict or "") table.insert(t1, str) if f then @@ -177,11 +177,10 @@ local methods = { add_class = { function(req, msg) if not msg.class then return UBUS_STATUS_INVALID_ARGUMENT end - local class = msg.class - local t = cfg:init_table(cfg_file) + local t = cfg:init(cfg_file) local ret - if t:lookup_class(class) then return ubus.reply(req, {ret = UBUS_STATUS_ALREADY_EXISTS}) end - ret = t:add_class(class) + if t:lookup_class(msg.class) then return ubus.reply(req, {ret = UBUS_STATUS_ALREADY_EXISTS}) end + ret = t:add_class(msg.class) ubus.reply(req, {msg = ret}) end, {class = libubus.STRING} }, @@ -189,7 +188,8 @@ local methods = { function (req, msg) if not msg.class then return UBUS_STATUS_INVALID_ARGUMENT end if not msg.name then return UBUS_STATUS_INVALID_ARGUMENT end - local t = cfg:init_table(cfg_file) + if not msg.proto then return UBUS_STATUS_INVALID_ARGUMENT end + local t = cfg:init(cfg_file) local ret if t:lookup(msg.name) then return ubus.reply(req, {ret = UBUS_STATUS_ALREADY_EXISTS}) end ret = t:add_app(msg.class, msg.name, msg.proto, msg.sport, msg.dport, msg.url, msg.request, msg.dict) @@ -198,21 +198,21 @@ local methods = { }, del_app = { function(req, msg) - local t = cfg:init_table(cfg_file) - ret = t:del_app(msg.id, msg.name) + local t = cfg:init(cfg_file) + local ret = t:del_app(msg.id, msg.name) ubus.reply(req, {ret = ret}) end,{id = libubus.INT32, name = libubus.STRING} }, list_class = { function (req, msg) - local _, c = cfg:init_table(cfg_file) + local _, c = cfg:init(cfg_file) ubus.reply(req, {result = c}) end,{} }, list_app = { function (req, msg) if not msg.class then return UBUS_STATUS_INVALID_ARGUMENT end - local t = cfg:init_table(cfg_file) + local t = cfg:init(cfg_file) local ret = {} for i, v in ipairs(t:lookup_class(msg.class)) do if not v:match("#class") then From 62b1478d935fc0cc3bd75a95940891294962747c Mon Sep 17 00:00:00 2001 From: juno Date: Wed, 18 Nov 2020 00:13:58 -0900 Subject: [PATCH 4/4] fix add new app bug Signed-off-by: juno --- open-app-filter/files/appfilter.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/open-app-filter/files/appfilter.lua b/open-app-filter/files/appfilter.lua index f8bd7db..8cea896 100644 --- a/open-app-filter/files/appfilter.lua +++ b/open-app-filter/files/appfilter.lua @@ -112,12 +112,21 @@ end function cfg:add_app(m, name, proto, sport, dport, url, request, dict) if not name then return UBUS_STATUS_INVALID_ARGUMENT end + local id + local offset local f = io.open(cfg_file, "r+") io.output(f) local t1,t2 = self:lookup_class(m) - local id = math.modf(string.match(t1[#t1-1], "(%d+) %S+:") +1) + if t1[#t1] == nil or "" then + offset = 0 + id = math.modf(string.match(t1[#t1-1], "(%d+) %S+:") +1) + else + offset = 1 + id = math.modf(string.match(t1[#t1], "(%d+) %S+:") +1) + end + local str = string.format("%d %s:[%s;%s;%s;%s;%s;%s]", id, name, proto, sport or "", dport or "", url or "", request or "", dict or "") - table.insert(t1, str) + table.insert(t1, #t1+offset, str) if f then for _, v in ipairs(t2) do if v then @@ -126,7 +135,7 @@ function cfg:add_app(m, name, proto, sport, dport, url, request, dict) end end for _, v in ipairs(t1) do - if v then + if v then io.write(v) io.write("\n") end