update 2026-09-26 17:00:06

This commit is contained in:
action committed 2026-09-26 17:00:06 +08:00
1 parent 97ae9f68da
commit bcc9ceb04a
4756 files changed
+1670713

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
module("luci.controller.oaf", package.seeall)
function index()
entry({"admin", "services", "oaf"}, firstchild(), _("Parental Control"), 20).dependent = true
end
@@ -0,0 +1,5 @@
module("luci.controller.oaf_about", package.seeall)
function index()
entry({"admin", "services", "oaf", "about"}, template("oaf/about"), _("About"), 100).leaf = true
end
@@ -0,0 +1,127 @@
module("luci.controller.oaf_advanced", package.seeall)
local util = require "luci.util"
function index()
entry({"admin", "services", "oaf", "advanced"}, template("oaf/advanced"), _("Settings"), 90).leaf = true
entry({"admin", "services", "oaf", "api", "system", "get_system_info"}, call("get_system_info"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "system", "set_system_info"}, call("set_system_info"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "system", "get_work_mode"}, call("get_work_mode"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "system", "set_work_mode"}, call("set_work_mode"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "system", "get_tcp_rst"}, call("get_tcp_rst"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "system", "set_tcp_rst"}, call("set_tcp_rst"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "system", "get_advanced_settings"}, call("get_advanced_settings"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "system", "set_advanced_settings"}, call("set_advanced_settings"), nil).leaf = true
end
local function ubus_call(api, payload)
payload = payload or {}
payload.api = api
return util.ubus("fwx", "common", payload) or {code = 1}
end
function get_system_info()
local http = require "luci.http"
local resp = ubus_call("get_system_info", {data = {}})
http.prepare_content("application/json")
http.write_json(resp)
end
function set_system_info()
local http = require "luci.http"
local lan_ifname = http.formvalue("lan_ifname") or ""
local theme_mode = tonumber(http.formvalue("theme_mode") or "0") or 0
if theme_mode ~= 0 and theme_mode ~= 1 then
theme_mode = 0
end
local resp = ubus_call("set_system_info", {
data = {
fwx = {
lan_ifname = lan_ifname,
theme_mode = theme_mode
}
}
})
http.prepare_content("application/json")
http.write_json(resp)
end
function get_work_mode()
local http = require "luci.http"
local resp = ubus_call("get_work_mode", {data = {}})
http.prepare_content("application/json")
http.write_json(resp)
end
function set_work_mode()
local http = require "luci.http"
local work_mode = tonumber(http.formvalue("work_mode") or "0") or 0
if work_mode ~= 0 and work_mode ~= 1 then
http.prepare_content("application/json")
http.write_json({code = 1})
return
end
local resp = ubus_call("set_work_mode", {
data = {
work_mode = work_mode
}
})
http.prepare_content("application/json")
http.write_json(resp)
end
function get_tcp_rst()
local http = require "luci.http"
local resp = ubus_call("get_tcp_rst", {data = {}})
http.prepare_content("application/json")
http.write_json(resp)
end
function set_tcp_rst()
local http = require "luci.http"
local tcp_rst = tonumber(http.formvalue("tcp_rst") or "1") or 1
tcp_rst = tcp_rst == 0 and 0 or 1
local resp = ubus_call("set_tcp_rst", {
data = {
tcp_rst = tcp_rst
}
})
http.prepare_content("application/json")
http.write_json(resp)
end
function get_advanced_settings()
local http = require "luci.http"
local resp = ubus_call("get_advanced_settings", {data = {}})
http.prepare_content("application/json")
http.write_json(resp)
end
function set_advanced_settings()
local http = require "luci.http"
local disable_hnat = tonumber(http.formvalue("disable_hnat") or "0") or 0
disable_hnat = disable_hnat == 1 and 1 or 0
local resp = ubus_call("set_advanced_settings", {
data = {
disable_hnat = disable_hnat
}
})
http.prepare_content("application/json")
http.write_json(resp)
end
@@ -0,0 +1,371 @@
module("luci.controller.oaf_app_filter", package.seeall)
local utl = require "luci.util"
function index()
local fs = require "nixio.fs"
if not fs.access("/etc/config/appfilter") then
return
end
entry({"admin", "services", "oaf", "app_filter"}, alias("admin", "services", "oaf", "app_filter", "rules"), _("App Filter"), 30).dependent = true
entry({"admin", "services", "oaf", "app_filter", "rules"}, cbi("oaf/app_filter/rules", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), _("Filter Rules"), 10).leaf=true
entry({"admin", "services", "oaf", "api", "app_filter", "class_list"}, call("get_class_list"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "get_all_users"}, call("get_all_users"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "get_filter_rules"}, call("get_filter_rules"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "add_filter_rule"}, call("add_filter_rule"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "update_filter_rule"}, call("update_filter_rule"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "delete_filter_rule"}, call("delete_filter_rule"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "get_appfilter_whitelist"}, call("get_appfilter_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "add_appfilter_whitelist"}, call("add_appfilter_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "del_appfilter_whitelist"}, call("del_appfilter_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "get_app_filter_adv"}, call("get_app_filter_adv"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_filter", "set_app_filter_adv"}, call("set_app_filter_adv"), nil).leaf = true
end
function get_class_list()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.CopyRight = "www.fanchmwrt.com"
req_obj.api = "class_list"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
llog("get class list");
luci.http.write_json(resp_obj.data)
else
llog("get class list failed");
luci.http.write_json({class_list = {}})
end
end
function get_all_users()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_all_users"
req_obj.data = {
flag = luci.http.formvalue("flag"),
page = luci.http.formvalue("page")
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json({data = resp_obj.data})
else
luci.http.write_json({data = resp_obj or {}})
end
end
function handle_file_upload()
local http = require "luci.http"
local fs = require "nixio.fs"
local upload_dir = "/tmp/uploads/"
local file_name = "uploaded_file"
llog("handle_file_upload started");
if not fs.access(upload_dir) then
fs.mkdir(upload_dir)
end
llog("Upload directory checked/created");
local file_path = upload_dir .. file_name
local fp
llog("file_path: " .. file_path);
http.setfilehandler(
function(meta, chunk, eof)
llog("File upload metadata: " .. (meta and meta.name or "nil") .. ", " .. (meta and meta.file or "nil"))
llog("File upload chunk size: " .. (chunk and #chunk or 0))
if not fp then
fp = io.open(file_path, "w")
llog("File opened for writing: " .. file_path)
end
if fp and chunk then
fp:write(chunk)
llog("Chunk written to file")
end
if fp and eof then
fp:close()
llog("File upload completed and file closed")
process_uploaded_file(file_path)
luci.http.prepare_content("application/json")
luci.http.write_json({ success = true, message = "File uploaded successfully" })
end
end
)
llog("handle_file_upload setup complete");
end
function process_uploaded_file(file_path)
llog("Processing uploaded file: " .. file_path)
local permanent_path = "/etc/config/" .. file_name
os.execute("mv " .. file_path .. " " .. permanent_path)
llog("File moved to: " .. permanent_path)
end
function llog(message)
local log_file = "/tmp/log/oaf_luci.log"
local fd = io.open(log_file, "a")
if fd then
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
fd:write(string.format("[%s] %s\n", timestamp, message))
fd:close()
end
end
function get_filter_rules()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_filter_rules"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data and resp_obj.data.list then
local rules_data = resp_obj.data.list
llog("get_filter_rules: returning " .. #rules_data .. " rules")
local json_str = json.stringify({code = 0, data = rules_data, message = "success"})
luci.http.write(json_str)
else
llog("get_filter_rules: failed, returning empty array")
local json_str = json.stringify({code = 0, data = {}, message = "success"})
luci.http.write(json_str)
end
end
function add_filter_rule()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local data_str = luci.http.formvalue("data")
if not data_str then
luci.http.write_json({code = 1, message = "Invalid request data"})
return
end
local rule_data = json.parse(data_str)
llog("add_filter_rule: " .. json.stringify(rule_data))
if not rule_data.name or not rule_data.mode or not rule_data.time_rules or not rule_data.app_ids then
luci.http.write_json({code = 1, message = "Missing required fields"})
return
end
local get_req_obj = {}
get_req_obj.api = "get_filter_rules"
get_req_obj.data = {}
local get_resp_obj = utl.ubus("fwx", "common", get_req_obj)
if get_resp_obj and get_resp_obj.code == 2000 and get_resp_obj.data and get_resp_obj.data.data then
local existing_rules = get_resp_obj.data.data
if #existing_rules >= 32 then
luci.http.write_json({code = 1, message = "Maximum 32 rules allowed"})
return
end
end
local req_obj = {}
req_obj.api = "add_filter_rule"
req_obj.data = rule_data
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "Rule added successfully"})
else
luci.http.write_json({code = 1, message = "Failed to add rule"})
end
end
function update_filter_rule()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local data_str = luci.http.formvalue("data")
if not data_str then
luci.http.write_json({code = 1, message = "Invalid request data"})
return
end
local rule_data = json.parse(data_str)
llog("update_filter_rule: " .. json.stringify(rule_data))
if not rule_data.id then
luci.http.write_json({code = 1, message = "Missing rule id"})
return
end
local req_obj = {}
req_obj.api = "update_filter_rule"
req_obj.data = rule_data
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "Rule updated successfully"})
else
luci.http.write_json({code = 1, message = "Failed to update rule"})
end
end
function delete_filter_rule()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local rule_id = luci.http.formvalue("rule_id")
if not rule_id then
luci.http.write_json({code = 1, message = "Invalid rule_id"})
return
end
llog("delete_filter_rule: " .. rule_id)
local req_obj = {}
req_obj.api = "delete_filter_rule"
req_obj.data = {
id = tonumber(rule_id)
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "Rule deleted successfully"})
else
luci.http.write_json({code = 1, message = "Failed to delete rule"})
end
end
function get_appfilter_whitelist()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_appfilter_whitelist"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json({code = 2000, data = resp_obj.data, message = "success"})
else
luci.http.write_json({code = 2000, data = {list = {}}, message = "success"})
end
end
function add_appfilter_whitelist()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local data_str = luci.http.formvalue("data")
if not data_str then
luci.http.write_json({code = 1, message = "Invalid request data"})
return
end
local whitelist_data = json.parse(data_str)
llog("add_appfilter_whitelist: " .. json.stringify(whitelist_data))
if not whitelist_data.mac_list or type(whitelist_data.mac_list) ~= "table" then
luci.http.write_json({code = 1, message = "Invalid mac_list"})
return
end
local req_obj = {}
req_obj.api = "add_appfilter_whitelist"
req_obj.data = whitelist_data
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 2000, message = "Whitelist added successfully"})
else
luci.http.write_json({code = 1, message = "Failed to add whitelist"})
end
end
function del_appfilter_whitelist()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local mac = luci.http.formvalue("mac")
if not mac then
luci.http.write_json({code = 1, message = "Invalid mac address"})
return
end
llog("del_appfilter_whitelist: " .. mac)
local req_obj = {}
req_obj.api = "del_appfilter_whitelist"
req_obj.data = {
mac = mac
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 2000, message = "Whitelist deleted successfully"})
else
luci.http.write_json({code = 1, message = "Failed to delete whitelist"})
end
end
function get_app_filter_adv()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_app_filter_adv"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json({code = 0, data = resp_obj.data, message = "success"})
end
end
function set_app_filter_adv()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local enable = tonumber(luci.http.formvalue("enable")) or 1
local req_obj = {}
req_obj.api = "set_app_filter_adv"
req_obj.data = {
enable = enable,
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "Saved successfully"})
else
luci.http.write_json({code = 1, message = "Failed to save"})
end
end
@@ -0,0 +1,83 @@
module("luci.controller.oaf_app_record", package.seeall)
local function normalize_page(value, default_value)
local n = tonumber(value or "") or default_value
if n < 1 then
n = default_value
end
return n
end
local function write_empty_list(page, page_size)
luci.http.write_json({
total_num = 0,
total_page = 1,
page = page,
page_size = page_size,
list = {}
})
end
function index()
entry({"admin", "services", "oaf", "app_record"}, template("oaf/app_record"), _("App Record"), 70).leaf = true
entry({"admin", "services", "oaf", "api", "app_record", "get_active_app_records"}, call("get_active_app_records")).leaf = true
entry({"admin", "services", "oaf", "api", "app_record", "get_app_history_records"}, call("get_app_history_records")).leaf = true
end
function get_active_app_records()
local util = require "luci.util"
local page = normalize_page(luci.http.formvalue("page"), 1)
local page_size = normalize_page(luci.http.formvalue("page_size"), 15)
luci.http.prepare_content("application/json")
local req_obj = {
api = "get_active_app_records",
data = {
page = page,
page_size = page_size
}
}
local resp_obj = util.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
write_empty_list(page, page_size)
end
end
function get_app_history_records()
local util = require "luci.util"
local mac = luci.http.formvalue("mac")
local start_time = tonumber(luci.http.formvalue("start_time") or "0") or 0
local end_time = tonumber(luci.http.formvalue("end_time") or "0") or 0
local appid = tonumber(luci.http.formvalue("appid") or "0") or 0
local page = normalize_page(luci.http.formvalue("page"), 1)
local page_size = normalize_page(luci.http.formvalue("page_size"), 15)
if appid < 0 then
appid = 0
end
luci.http.prepare_content("application/json")
local req_obj = {
api = "get_app_history_records",
data = {
mac = mac,
start_time = start_time,
end_time = end_time,
appid = appid,
page = page,
page_size = page_size
}
}
local resp_obj = util.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
write_empty_list(page, page_size)
end
end
@@ -0,0 +1,229 @@
module("luci.controller.oaf_dashboard", package.seeall)
function index()
entry({"admin", "services", "oaf", "dashboard"}, cbi("oaf/dashboard", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}),
_("Dashboard"), 10).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "get_dashboard_common"}, call("get_dashboard_common")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "get_init_status"}, call("get_init_status")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "set_init_status"}, call("set_init_status")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "set_notice_status"}, call("set_notice_status")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "get_daily_top_users"}, call("get_daily_top_users")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "get_history_traffic_stats"}, call("get_history_traffic_stats")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "get_global_traffic_stats"}, call("get_global_traffic_stats")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "get_active_users"}, call("get_active_users")).leaf = true
entry({"admin", "services", "oaf", "api", "dashboard", "get_app_type_stats"}, call("get_app_type_stats")).leaf = true
end
function get_dashboard_common()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_dashboard_common"
req_obj.CopyRight = "www.fanchmwrt.com"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
system_status = {},
network_status = {},
active_app = {total = 0, list = {}},
advanced = {disable_hnat = 0, notice_status = 0},
interface_traffic = {interface = "wan", traffic = {}}
})
end
end
function get_global_traffic_stats()
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_global_traffic_stats"
req_obj.data = {}
local date = luci.http.formvalue("date")
if date then
req_obj.data.date = tonumber(date) or 0
end
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
date = 0,
is_today = 1,
hourly_traffic = {}
})
end
end
function get_history_traffic_stats()
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_history_traffic_stats"
req_obj.data = {}
local days = luci.http.formvalue("days")
if days then
req_obj.data.days = tonumber(days) or 30
else
req_obj.data.days = 30
end
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
days = req_obj.data.days,
total_up_bytes = 0,
total_down_bytes = 0,
total_bytes = 0,
list = {}
})
end
end
function get_init_status()
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_init_status"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({init_status = 1})
end
end
function set_init_status()
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "set_init_status"
req_obj.data = { init_status = 1 }
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({init_status = 0})
end
end
function set_notice_status()
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "set_dashboard_notice_status"
req_obj.data = { notice_status = 1 }
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 2000, notice_status = 1})
else
luci.http.write_json({code = 1})
end
end
function get_daily_top_users()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_daily_top_users"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
date = 0,
total_count = 0,
users = {}
})
end
end
function get_active_users()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_active_users"
req_obj.data = {}
local count = luci.http.formvalue("count")
if count then
req_obj.data.count = tonumber(count) or 8
else
req_obj.data.count = 8
end
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
total_count = 0,
users = {}
})
end
end
function get_app_type_stats()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local stat_type = luci.http.formvalue("type") or "hourly"
local req_obj = {}
req_obj.api = "get_global_app_type_stats"
req_obj.data = {
type = stat_type,
limit = 10
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
type = stat_type,
limit = 10,
total_count = 0,
types = {}
})
end
end
@@ -0,0 +1,118 @@
module("luci.controller.oaf_feature", package.seeall)
function index()
entry({"admin", "services", "oaf", "feature"},
template("oaf/feature"),
_("Feature Library"), 80).dependent = true
entry({"admin", "services", "oaf", "feature", "info"}, call("get_feature_info"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "class_list"}, call("get_feature_class_list"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "online_config"}, call("get_feature_online_config"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "online_save"}, call("set_feature_online_config"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "online_list"}, call("get_feature_online_list"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "online_start"}, call("start_feature_online_update"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "online_status"}, call("get_feature_online_update_status"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "custom_list"}, call("get_custom_feature_list"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "custom_class_list"}, call("get_custom_feature_class_list"), nil).leaf = true
entry({"admin", "services", "oaf", "feature", "custom_save"}, call("set_custom_feature_list"), nil).leaf = true
end
function get_feature_info()
local json = require "luci.jsonc"
local util = require "luci.util"
local http = require "luci.http"
local resp = util.ubus("fwx", "common", {api = "get_feature_info", data = {}})
http.prepare_content("application/json")
http.write(json.stringify(resp or {code = 4000}))
end
local function write_fwx_response(api, data)
local json = require "luci.jsonc"
local util = require "luci.util"
local http = require "luci.http"
local resp = util.ubus("fwx", "common", {api = api, data = data or {}})
http.prepare_content("application/json")
http.write(json.stringify(resp or {code = 4000}))
end
function get_feature_online_config()
write_fwx_response("get_feature_online_config", {})
end
function set_feature_online_config()
local http = require "luci.http"
write_fwx_response("set_feature_online_config", {
token = http.formvalue("token") or ""
})
end
function get_feature_online_list()
local http = require "luci.http"
local lang = http.formvalue("lang") or "cn"
local refresh = tonumber(http.formvalue("refresh") or "0") or 0
if lang ~= "cn" and lang ~= "en" then
lang = "cn"
end
write_fwx_response("get_feature_online_list", {
lang = lang,
device_lang = http.formvalue("device_lang") or "",
refresh = refresh
})
end
function start_feature_online_update()
local http = require "luci.http"
local lang = http.formvalue("lang") or "cn"
if lang ~= "cn" and lang ~= "en" then
lang = "cn"
end
write_fwx_response("start_feature_online_update", {
id = http.formvalue("id") or "",
lang = lang,
md5 = http.formvalue("md5") or ""
})
end
function get_feature_online_update_status()
write_fwx_response("get_feature_online_update_status", {})
end
function get_custom_feature_list()
write_fwx_response("get_custom_feature", {})
end
function get_custom_feature_class_list()
write_fwx_response("get_custom_feature_class_list", {})
end
function set_custom_feature_list()
local json = require "luci.jsonc"
local http = require "luci.http"
local data_str = http.formvalue("data")
local ok, data_obj = pcall(json.parse, data_str or "")
if not ok then
data_obj = nil
end
if type(data_obj) ~= "table" or type(data_obj.app_list) ~= "table" then
http.prepare_content("application/json")
http.write(json.stringify({code = 4000, data = {error = "invalid request data"}}))
return
end
write_fwx_response("set_custom_feature", data_obj)
end
function get_feature_class_list()
local json = require "luci.jsonc"
local util = require "luci.util"
local http = require "luci.http"
local resp = util.ubus("fwx", "common", {CopyRight = "www.fanchmwrt.com", api = "class_list", data = {}})
http.prepare_content("application/json")
if resp and resp.code == 2000 and resp.data then
http.write(json.stringify(resp.data))
else
http.write(json.stringify({class_list = {}}))
end
end
@@ -0,0 +1,113 @@
module("luci.controller.oaf_internet_audit", package.seeall)
function index()
entry({"admin", "services", "oaf", "api", "internet_audit", "get_record_base"}, call("get_record_base"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "internet_audit", "set_record_base"}, call("set_record_base"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "internet_audit", "record_action"}, call("record_action"), nil).leaf = true
end
function ensure_record_section(cur)
local sid
cur:foreach("fwx", "record", function(s)
sid = s[".name"]
end)
if not sid then
sid = cur:add("fwx", "record")
end
return sid
end
function get_record_base()
local json = require "luci.jsonc"
local http = require "luci.http"
local utl = require "luci.util"
local req_obj = { api = "get_record_base", data = {} }
local resp = utl.ubus("fwx", "common", req_obj) or {code = 1}
http.prepare_content("application/json")
http.write(json.stringify(resp))
end
function set_record_base()
local json = require "luci.jsonc"
local http = require "luci.http"
local utl = require "luci.util"
local enable = tonumber(http.formvalue("enable") or 0) or 0
local record_time = tonumber(http.formvalue("record_time") or 0) or 0
local app_valid_time = tonumber(http.formvalue("app_valid_time") or 0) or 0
local history_data_size = http.formvalue("history_data_size") or ""
local history_data_path = http.formvalue("history_data_path") or ""
local base_data_path = http.formvalue("base_data_path") or http.formvalue("terminal_data_path") or ""
if record_time < 0 then record_time = 0 end
if app_valid_time < 0 then app_valid_time = 0 end
if history_data_size and history_data_size ~= "" then
local size_num = tonumber(history_data_size)
if not size_num or size_num < 1 or size_num > 1024 or size_num ~= math.floor(size_num) then
http.prepare_content("application/json")
http.write(json.stringify({code = 1, msg = "History data size must be an integer between 1 and 1024 MB"}))
return
end
end
if not history_data_path or history_data_path == "" or history_data_path == "/" then
http.prepare_content("application/json")
http.write(json.stringify({code = 1, msg = "History data path cannot be empty or /"}))
return
end
if not base_data_path or base_data_path == "" or base_data_path == "/" then
http.prepare_content("application/json")
http.write(json.stringify({code = 1, msg = "Base data path cannot be empty or /"}))
return
end
if #history_data_path > 64 then
http.prepare_content("application/json")
http.write(json.stringify({code = 1, msg = "History data path maximum length is 64 characters"}))
return
end
if #base_data_path > 64 then
http.prepare_content("application/json")
http.write(json.stringify({code = 1, msg = "Base data path maximum length is 64 characters"}))
return
end
local req_obj = {
api = "set_record_base",
data = {
enable = enable,
record_time = record_time,
app_valid_time = app_valid_time,
history_data_size = history_data_size,
history_data_path = history_data_path,
base_data_path = base_data_path
}
}
local resp = utl.ubus("fwx", "common", req_obj) or {code = 1}
http.prepare_content("application/json")
http.write(json.stringify(resp))
end
function record_action()
local json = require "luci.jsonc"
local http = require "luci.http"
local utl = require "luci.util"
local action = http.formvalue("action") or ""
local req_obj = {
api = "record_action",
data = {
action = action
}
}
local resp = utl.ubus("fwx", "common", req_obj) or {code = 1}
http.prepare_content("application/json")
http.write(json.stringify(resp))
end
@@ -0,0 +1,461 @@
module("luci.controller.oaf_mac_filter", package.seeall)
local utl = require "luci.util"
function index()
local fs = require "nixio.fs"
if not fs.access("/etc/config/macfilter") then
return
end
entry({"admin", "services", "oaf", "mac_filter"}, alias("admin", "services", "oaf", "mac_filter", "rules"), _("Access Control"), 40).dependent = true
entry({"admin", "services", "oaf", "mac_filter", "rules"}, cbi("oaf/mac_filter/rules", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), _("Filter Rules"), 10).leaf=true
entry({"admin", "services", "oaf", "api", "mac_filter", "get_mac_filter_base"}, call("get_mac_filter_base"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "set_mac_filter_base"}, call("set_mac_filter_base"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "set_mac_filter_time"}, call("set_mac_filter_time"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "get_mac_filter_time"}, call("get_mac_filter_time"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "get_mac_filter_user"}, call("get_mac_filter_user"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "set_mac_filter_user"}, call("set_mac_filter_user"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "del_mac_filter_user"}, call("del_mac_filter_user"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "add_mac_filter_user"}, call("add_mac_filter_user"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "get_mf_status"}, call("get_mf_status"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "get_mac_filter_whitelist"}, call("get_mac_filter_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "del_mac_filter_whitelist"}, call("del_mac_filter_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "add_mac_filter_whitelist"}, call("add_mac_filter_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "get_mac_filter_rules"}, call("get_mac_filter_rules"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "add_mac_filter_rule"}, call("add_mac_filter_rule"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "update_mac_filter_rule"}, call("update_mac_filter_rule"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "delete_mac_filter_rule"}, call("delete_mac_filter_rule"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "get_mac_filter_adv"}, call("get_mac_filter_adv"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "mac_filter", "set_mac_filter_adv"}, call("set_mac_filter_adv"), nil).leaf = true
end
function get_mf_status()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_mf_status"
req_obj.data = {}
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function get_mac_filter_user()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_mac_filter_user"
req_obj.data = {}
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function del_mac_filter_user()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
local mac = luci.http.formvalue("mac")
llog("del macfilter user "..mac);
req_obj.api = "del_mac_filter_user"
req_obj.data = {
mac = mac
}
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function add_mac_filter_user()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "add_mac_filter_user"
local data_str = luci.http.formvalue("data")
local data = json.parse(data_str)
req_obj.data = data
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function get_mac_filter_base()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_mac_filter_base"
req_obj.data = {}
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function set_mac_filter_user()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "set_mac_filter_user"
local mode = luci.http.formvalue("mode")
req_obj.data = {
mode = mode
}
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function set_mac_filter_base()
local json = require "luci.jsonc"
llog("set macfilter base");
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "set_mac_filter_base"
local enable = luci.http.formvalue("enable")
req_obj.data = {
enable = enable
}
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function set_mac_filter_time()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "set_mac_filter_time"
local data_str = luci.http.formvalue("data")
local data = json.parse(data_str)
req_obj.data = data
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function get_mac_filter_time()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_mac_filter_time"
req_obj.data = {}
local resp_obj=utl.ubus("fwx", "common", req_obj);
luci.http.write_json(resp_obj);
end
function get_mac_filter_whitelist()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_mac_filter_whitelist"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json({code = 2000, data = resp_obj.data, message = "success"})
else
luci.http.write_json({code = 2000, data = {list = {}}, message = "success"})
end
end
function add_mac_filter_whitelist()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local data_str = luci.http.formvalue("data")
if not data_str then
luci.http.write_json({code = 1, message = "Invalid request data"})
return
end
local whitelist_data = json.parse(data_str)
llog("add_mac_filter_whitelist: " .. json.stringify(whitelist_data))
if not whitelist_data.mac_list or type(whitelist_data.mac_list) ~= "table" then
luci.http.write_json({code = 1, message = "Invalid mac_list"})
return
end
local req_obj = {}
req_obj.api = "add_mac_filter_whitelist"
req_obj.data = whitelist_data
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 2000, message = "Whitelist added successfully"})
else
luci.http.write_json({code = 1, message = "Failed to add whitelist"})
end
end
function del_mac_filter_whitelist()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local mac = luci.http.formvalue("mac")
if not mac then
luci.http.write_json({code = 1, message = "Invalid mac address"})
return
end
llog("del_mac_filter_whitelist: " .. mac)
local req_obj = {}
req_obj.api = "del_mac_filter_whitelist"
req_obj.data = {
mac = mac
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 2000, message = "Whitelist deleted successfully"})
else
luci.http.write_json({code = 1, message = "Failed to delete whitelist"})
end
end
function llog(message)
local log_file = "/tmp/log/oaf_luci.log"
local fd = io.open(log_file, "a")
if fd then
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
fd:write(string.format("[%s] %s\n", timestamp, message))
fd:close()
end
end
function get_mac_filter_rules()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_mac_filter_rules"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data and resp_obj.data.list then
local rules_data = resp_obj.data.list
local json_str = json.stringify({code = 0, data = rules_data, message = "success"})
luci.http.write(json_str)
else
local json_str = json.stringify({code = 0, data = {}, message = "success"})
luci.http.write(json_str)
end
end
function add_mac_filter_rule()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local data_str = luci.http.formvalue("data")
if not data_str then
luci.http.write_json({code = 1, message = "Invalid request data"})
return
end
local rule_data = json.parse(data_str)
llog("add_mac_filter_rule: " .. json.stringify(rule_data))
if not rule_data.name or not rule_data.mode then
luci.http.write_json({code = 1, message = "Missing required fields"})
return
end
local time_mode = tonumber(rule_data.time_mode) or 1
if time_mode == 1 then
if (not rule_data.time_list or #rule_data.time_list == 0) and (not rule_data.time_rules or #rule_data.time_rules == 0) then
luci.http.write_json({code = 1, message = "Missing required time_list"})
return
end
elseif time_mode == 2 then
if (not rule_data.time_limit or rule_data.time_limit == "") and (not rule_data.time_rules or #rule_data.time_rules == 0) then
luci.http.write_json({code = 1, message = "Missing required time_limit"})
return
end
elseif time_mode == 3 then
if (not rule_data.flow_limit or rule_data.flow_limit == "") and (not rule_data.time_rules or #rule_data.time_rules == 0) then
luci.http.write_json({code = 1, message = "Missing required flow_limit"})
return
end
end
local get_req_obj = {}
get_req_obj.api = "get_mac_filter_rules"
get_req_obj.data = {}
local get_resp_obj = utl.ubus("fwx", "common", get_req_obj)
if get_resp_obj and get_resp_obj.code == 2000 and get_resp_obj.data and get_resp_obj.data.data then
local existing_rules = get_resp_obj.data.data
if #existing_rules >= 32 then
luci.http.write_json({code = 1, message = "Maximum 32 rules allowed"})
return
end
end
local req_obj = {}
req_obj.api = "add_mac_filter_rule"
req_obj.data = rule_data
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "Rule added successfully"})
else
luci.http.write_json({code = 1, message = "Failed to add rule"})
end
end
function update_mac_filter_rule()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local data_str = luci.http.formvalue("data")
if not data_str then
luci.http.write_json({code = 1, message = "Invalid request data"})
return
end
local rule_data = json.parse(data_str)
llog("update_mac_filter_rule: " .. json.stringify(rule_data))
if not rule_data.id then
luci.http.write_json({code = 1, message = "Missing rule id"})
return
end
local req_obj = {}
req_obj.api = "update_mac_filter_rule"
req_obj.data = rule_data
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "Rule updated successfully"})
else
luci.http.write_json({code = 1, message = "Failed to update rule"})
end
end
function delete_mac_filter_rule()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local rule_id = luci.http.formvalue("rule_id")
if not rule_id then
luci.http.write_json({code = 1, message = "Invalid rule_id"})
return
end
llog("delete_mac_filter_rule: " .. rule_id)
local req_obj = {}
req_obj.api = "delete_mac_filter_rule"
req_obj.data = {
id = tonumber(rule_id)
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "Rule deleted successfully"})
else
luci.http.write_json({code = 1, message = "Failed to delete rule"})
end
end
function get_mac_filter_adv()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_mac_filter_adv"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json({code = 0, data = resp_obj.data, message = "success"})
else
luci.http.write_json({code = 1, message = "Failed to load"})
end
end
function set_mac_filter_adv()
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local enable = tonumber(luci.http.formvalue("enable")) or 0
if enable ~= 0 and enable ~= 1 then
luci.http.write_json({code = 1, message = "Invalid enable value, must be 0 or 1"})
return
end
local req_obj = {}
req_obj.api = "set_mac_filter_adv"
req_obj.data = {
enable = enable
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 then
llog("Set macfilter enable: " .. enable)
luci.http.write_json({code = 0, message = "Saved successfully"})
else
luci.http.write_json({code = 1, message = "Failed to save"})
end
end
+452
View File
@@ -0,0 +1,452 @@
module("luci.controller.oaf_user", package.seeall)
local utl = require "luci.util"
function index()
local page
entry({"admin", "services", "oaf", "users"}, alias("admin", "services", "oaf", "users", "list"), _("User List"), 20).dependent = true
entry({"admin", "services", "oaf", "users", "list"}, cbi("oaf/user_list", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}),
nil).leaf = true
entry({"admin", "services", "oaf", "users", "detail"}, cbi("oaf/user_detail", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), nil).leaf = true
entry({"admin", "services", "oaf", "api", "user_status"}, call("user_status"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "dev_visit_list"}, call("get_dev_visit_list"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "dev_visit_time"}, call("get_dev_visit_time"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "app_class_visit_time"}, call("get_app_class_visit_time"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_class_list"}, call("get_class_list"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_all_users"}, call("get_all_users"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_system_base_info"}, call("get_system_base_info"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_mac_blacklist"}, call("get_mac_blacklist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "add_mac_blacklist"}, call("add_mac_blacklist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "del_mac_blacklist"}, call("del_mac_blacklist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_parental_control_detail"}, call("get_parental_control_detail"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "set_nickname"}, call("set_nickname"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_hourly_stats"}, call("get_hourly_stats"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_user_basic_info"}, call("get_user_basic_info"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_online_offline_records"}, call("get_online_offline_records"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "get_user_parental_control_rules"}, call("get_user_parental_control_rules"), nil).leaf = true
end
function get_hostname_by_mac(dst_mac)
leasefile="/tmp/dhcp.leases"
local fd = io.open(leasefile, "r")
if not fd then return end
while true do
local ln = fd:read("*l")
if not ln then
break
end
local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
if dst_mac == mac then
fd:close()
return name
end
end
fd:close()
return ""
end
function get_app_name_by_id(appid)
local class_fd = io.popen("find /tmp/appfilter/ -type f -name *.class |xargs cat |grep "..appid.."|awk '{print $2}'")
if class_fd then
local name = class_fd:read("*l")
class_fd:close()
if name and name ~= "" then
return name
end
end
if tonumber(appid) and tonumber(appid) > 0 then
return "App" .. tostring(appid)
end
return ""
end
function cmp_func(a,b)
return a.latest_time > b.latest_time
end
function normalize_mac(mac)
if not mac then
return ""
end
return tostring(mac):gsub("^%s+", ""):gsub("%s+$", ""):upper()
end
function call_fwx_common(api, data)
local req_obj = {}
req_obj.api = api
req_obj.data = data or {}
return utl.ubus("fwx", "common", req_obj)
end
function user_status()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local fd = io.open("/proc/net/af_client","r")
status_buf=fd:read('*a')
fd:close()
user_array=json.parse(status_buf)
local req_obj = {}
req_obj.api = "visit_list"
req_obj.data = {}
local visit_obj = utl.ubus("fwx", "common", req_obj)
local user_array = {}
if visit_obj and visit_obj.code == 2000 and visit_obj.data and visit_obj.data.dev_list then
user_array = visit_obj.data.dev_list
end
local history={}
for i, v in pairs(user_array) do
visit_array=user_array[i].visit_info
for j,s in pairs(visit_array) do
print(user_array[i].mac, user_array[i].ip,visit_array[j].appid, visit_array[j].latest_time)
total_time=visit_array[j].latest_time - visit_array[j].first_time;
history[#history+1]={
mac=user_array[i].mac,
ip=user_array[i].ip,
hostname=get_hostname_by_mac(user_array[i].mac),
appid=visit_array[j].appid,
appname=get_app_name_by_id(visit_array[j].appid),
total_num=0,
drop_num=0,
latest_action=visit_array[j].latest_action,
latest_time=os.date("%Y/%m/%d %H:%M:%S", visit_array[j].latest_time),
first_time=os.date("%Y/%m/%d %H:%M:%S", visit_array[j].first_time),
total_time=total_time
}
end
end
table.sort(history, cmp_func)
luci.http.write_json(history);
end
function get_class_list()
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.CopyRight = "www.fanchmwrt.com"
req_obj.api = "class_list"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({class_list = {}})
end
end
function get_all_users()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_all_users"
req_obj.data = {
flag = luci.http.formvalue("flag"),
page = luci.http.formvalue("page"),
page_size = luci.http.formvalue("page_size")
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json({data = resp_obj.data})
else
luci.http.write_json({data = resp_obj or {}})
end
end
function get_system_base_info()
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_system_base_info"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
user_session_enable = 0
})
end
end
function get_mac_blacklist()
luci.http.prepare_content("application/json")
local resp_obj = call_fwx_common("get_mac_blacklist", {})
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json({code = 0, data = resp_obj.data, message = "success"})
else
luci.http.write_json({code = 1, data = {list = {}}, message = "failed"})
end
end
function add_mac_blacklist()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local mac_list = {}
local mac = normalize_mac(luci.http.formvalue("mac"))
if mac ~= "" then
table.insert(mac_list, mac)
else
local data_str = luci.http.formvalue("data")
if data_str and data_str ~= "" then
local data = json.parse(data_str)
if data and type(data.mac_list) == "table" then
for _, item in ipairs(data.mac_list) do
local normalized_mac = normalize_mac(item)
if normalized_mac ~= "" then
table.insert(mac_list, normalized_mac)
end
end
end
end
end
if #mac_list == 0 then
luci.http.write_json({code = 1, message = "Invalid mac"})
return
end
local resp_obj = call_fwx_common("add_mac_blacklist", {mac_list = mac_list})
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "success"})
else
luci.http.write_json({code = 1, message = "failed"})
end
end
function del_mac_blacklist()
luci.http.prepare_content("application/json")
local mac = normalize_mac(luci.http.formvalue("mac"))
if mac == "" then
luci.http.write_json({code = 1, message = "Invalid mac"})
return
end
local resp_obj = call_fwx_common("del_mac_blacklist", {mac = mac})
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json({code = 0, message = "success"})
else
luci.http.write_json({code = 1, message = "failed"})
end
end
function get_parental_control_detail()
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_parental_control_detail"
req_obj.data = {
mac = luci.http.formvalue("mac")
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
pc_status = "unlimited",
pc_status_key = "unlimited",
af_whitelist = 0,
mf_whitelist = 0,
appfilter_rules = {},
macfilter_rules = {}
})
end
end
function get_user_parental_control_rules()
luci.http.prepare_content("application/json")
local target_mac = normalize_mac(luci.http.formvalue("mac"))
if target_mac == "" then
luci.http.write_json({
mac = "",
af_whitelist = 0,
mf_whitelist = 0,
list = {}
})
return
end
local req_obj = {
api = "get_user_parental_control_rules",
data = {
mac = target_mac
}
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and type(resp_obj.data) == "table" then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
mac = target_mac,
af_whitelist = 0,
mf_whitelist = 0,
list = {}
})
end
end
function get_oaf_status()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_oaf_status"
req_obj.data = {}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json(resp_obj or {})
end
end
function set_nickname()
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "set_nickname"
req_obj.data = {
mac = luci.http.formvalue("mac"),
nickname = luci.http.formvalue("nickname")
}
local resp_obj = utl.ubus("fwx", "common", req_obj);
if resp_obj and resp_obj.code == 2000 then
luci.http.write_json(resp_obj.data or {})
else
luci.http.write_json(resp_obj or {})
end
end
function get_dev_visit_time(mac)
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "dev_visit_time"
req_obj.data = {
mac = mac
}
local visit_obj = utl.ubus("fwx", "common", req_obj)
local visit_list = {}
if visit_obj and visit_obj.code == 2000 and visit_obj.data and visit_obj.data.list then
visit_list = visit_obj.data.list
end
luci.http.write_json(visit_list)
end
function get_app_class_visit_time(mac)
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "app_class_visit_time"
req_obj.data = {
mac = mac
}
local visit_obj = utl.ubus("fwx", "common", req_obj)
local class_array = {}
if visit_obj and visit_obj.code == 2000 and visit_obj.data and visit_obj.data.class_list then
class_array = visit_obj.data.class_list
end
luci.http.write_json(class_array)
end
function get_dev_visit_list(mac)
local json = require "luci.jsonc"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "dev_visit_list"
req_obj.data = {
mac = mac
}
local page = luci.http.formvalue("page")
local page_size = luci.http.formvalue("page_size")
if page then
req_obj.data.page = tonumber(page) or 1
end
if page_size then
req_obj.data.page_size = tonumber(page_size) or 15
end
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json(resp_obj or {})
end
end
function get_hourly_stats(mac)
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_hourly_top_apps"
req_obj.data = {
mac = mac
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({
mac = mac,
date = 0,
is_today = 1,
hourly_stats = {}
})
end
end
function get_user_basic_info(mac)
local json = require "luci.jsonc"
local utl = require "luci.util"
luci.http.prepare_content("application/json")
local req_obj = {}
req_obj.api = "get_user_basic_info"
req_obj.data = {
mac = mac
}
local resp_obj = utl.ubus("fwx", "common", req_obj)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
luci.http.write_json(resp_obj.data)
else
luci.http.write_json({})
end
end
@@ -0,0 +1,181 @@
module("luci.controller.oaf_whitelist", package.seeall)
local util = require "luci.util"
local jsonc = require "luci.jsonc"
local function normalize_mac(mac)
return string.upper((mac or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end
local function is_valid_mac(mac)
return mac and mac:match("^%x%x:%x%x:%x%x:%x%x:%x%x:%x%x$") ~= nil
end
local function write_json(data)
luci.http.prepare_content("application/json")
luci.http.write_json(data)
end
local function get_record_whitelist_page(page, page_size)
local req_obj = {
api = "get_record_whitelist",
data = {
page = page,
page_size = page_size
}
}
return util.ubus("fwx", "common", req_obj)
end
function index()
local fs = require "nixio.fs"
local has_app_filter = fs.access("/etc/config/appfilter")
local has_access_control = fs.access("/etc/config/macfilter")
local has_record = fs.access("/etc/config/fwx_record")
if not has_app_filter and not has_access_control and not has_record then
return
end
entry({"admin", "services", "oaf", "whitelist"}, cbi("oaf/whitelist", {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), _("Whitelist"), 50).leaf = true
entry({"admin", "services", "oaf", "api", "record_whitelist", "get_record_whitelist"}, call("get_record_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "record_whitelist", "get_record_whitelist_all"}, call("get_record_whitelist_all"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "record_whitelist", "add_record_whitelist"}, call("add_record_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "record_whitelist", "del_record_whitelist"}, call("del_record_whitelist"), nil).leaf = true
entry({"admin", "services", "oaf", "api", "record_whitelist", "get_all_users"}, call("get_all_users"), nil).leaf = true
end
function get_record_whitelist()
local page = tonumber(luci.http.formvalue("page") or "1") or 1
local page_size = tonumber(luci.http.formvalue("page_size") or "200") or 200
if page < 1 then page = 1 end
if page_size < 1 then page_size = 200 end
if page_size > 200 then page_size = 200 end
local resp_obj = get_record_whitelist_page(page, page_size)
if resp_obj and resp_obj.code == 2000 and resp_obj.data then
write_json({code = 2000, data = resp_obj.data, message = "success"})
else
write_json({code = 2000, data = {total_num = 0, total_page = 1, page = page, page_size = page_size, list = {}}, message = "success"})
end
end
function get_record_whitelist_all()
local all_list = {}
local total_page = 1
local page = 1
local page_size = 200
while page <= total_page do
local resp_obj = get_record_whitelist_page(page, page_size)
if not (resp_obj and resp_obj.code == 2000 and resp_obj.data and type(resp_obj.data.list) == "table") then
break
end
local i
for i = 1, #resp_obj.data.list do
all_list[#all_list + 1] = resp_obj.data.list[i]
end
total_page = tonumber(resp_obj.data.total_page or 1) or 1
if total_page < 1 then total_page = 1 end
page = page + 1
end
write_json({code = 2000, data = {list = all_list}, message = "success"})
end
function add_record_whitelist()
local data_str = luci.http.formvalue("data")
local mac = normalize_mac(luci.http.formvalue("mac"))
local mac_list = {}
if data_str and data_str ~= "" then
local parsed = jsonc.parse(data_str)
if parsed and type(parsed.mac_list) == "table" then
local i
for i = 1, #parsed.mac_list do
local one_mac = normalize_mac(parsed.mac_list[i])
if is_valid_mac(one_mac) then
mac_list[#mac_list + 1] = one_mac
end
end
end
end
if #mac_list == 0 and is_valid_mac(mac) then
mac_list = {mac}
end
if #mac_list == 0 then
write_json({code = 1, message = "Invalid mac_list"})
return
end
local resp_obj = util.ubus("fwx", "common", {
api = "add_record_whitelist",
data = {mac_list = mac_list}
})
if resp_obj and resp_obj.code == 2000 then
write_json({code = 2000, message = "Whitelist added successfully"})
else
write_json({code = 1, message = "Failed to add whitelist"})
end
end
function del_record_whitelist()
local mac = normalize_mac(luci.http.formvalue("mac"))
if not is_valid_mac(mac) then
write_json({code = 1, message = "Invalid mac address"})
return
end
local resp_obj = util.ubus("fwx", "common", {
api = "del_record_whitelist",
data = {mac = mac}
})
if resp_obj and resp_obj.code == 2000 then
write_json({code = 2000, message = "Whitelist deleted successfully"})
else
write_json({code = 1, message = "Failed to delete whitelist"})
end
end
function get_all_users()
local all_list = {}
local total_page = 1
local page = 1
local page_size = 200
while page <= total_page do
local req_obj = {
api = "get_all_users",
data = {
flag = 2,
page = page,
page_size = page_size
}
}
local resp_obj = util.ubus("fwx", "common", req_obj)
if not (resp_obj and resp_obj.code == 2000 and resp_obj.data and type(resp_obj.data.list) == "table") then
break
end
local i
for i = 1, #resp_obj.data.list do
all_list[#all_list + 1] = resp_obj.data.list[i]
end
total_page = tonumber(resp_obj.data.total_page or 1) or 1
if total_page < 1 then total_page = 1 end
page = page + 1
end
write_json({code = 2000, data = {list = all_list}, message = "success"})
end