From f6d56838c740fd4dd0f3a579ccd7ba902b3171aa Mon Sep 17 00:00:00 2001 From: xiaorouji <60100640+xiaorouji@users.noreply.github.com> Date: Wed, 29 Mar 2023 11:21:20 +0800 Subject: [PATCH] luci: reduce duplicate code --- luci-app-passwall/Makefile | 2 +- luci-app-passwall/luasrc/passwall/api.lua | 9 +++++++++ luci-app-passwall/root/usr/share/passwall/haproxy.lua | 7 +------ .../root/usr/share/passwall/helper_dnsmasq_add.lua | 8 +------- .../root/usr/share/passwall/rule_update.lua | 8 ++------ luci-app-passwall/root/usr/share/passwall/subscribe.lua | 8 ++------ 6 files changed, 16 insertions(+), 26 deletions(-) diff --git a/luci-app-passwall/Makefile b/luci-app-passwall/Makefile index f3ebac1a2..a4547aabc 100644 --- a/luci-app-passwall/Makefile +++ b/luci-app-passwall/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-passwall PKG_VERSION:=4.61 -PKG_RELEASE:=3 +PKG_RELEASE:=4 PKG_CONFIG_DEPENDS:= \ CONFIG_PACKAGE_$(PKG_NAME)_Iptables_Transparent_Proxy \ diff --git a/luci-app-passwall/luasrc/passwall/api.lua b/luci-app-passwall/luasrc/passwall/api.lua index ad51f5597..ba57a276e 100644 --- a/luci-app-passwall/luasrc/passwall/api.lua +++ b/luci-app-passwall/luasrc/passwall/api.lua @@ -17,6 +17,15 @@ DISTRIB_ARCH = nil LOG_FILE = "/tmp/log/" .. appname .. ".log" CACHE_PATH = "/tmp/etc/" .. appname .. "_tmp" +function log(...) + local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ") + local f, err = io.open(LOG_FILE, "a") + if f and err == nil then + f:write(result .. "\n") + f:close() + end +end + function exec_call(cmd) local process = io.popen(cmd .. '; echo -e "\n$?"') local lines = {} diff --git a/luci-app-passwall/root/usr/share/passwall/haproxy.lua b/luci-app-passwall/root/usr/share/passwall/haproxy.lua index d5fd69cca..5e9d22554 100644 --- a/luci-app-passwall/root/usr/share/passwall/haproxy.lua +++ b/luci-app-passwall/root/usr/share/passwall/haproxy.lua @@ -8,12 +8,7 @@ local uci = api.uci local sys = api.sys local log = function(...) - local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ") - local f, err = io.open("/tmp/log/passwall.log", "a") - if f and err == nil then - f:write(result .. "\n") - f:close() - end + api.log(...) end function get_ip_port_from(str) diff --git a/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua b/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua index fe12491dc..02e913206 100644 --- a/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua +++ b/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq_add.lua @@ -16,7 +16,6 @@ local PROXY_MODE = var["-PROXY_MODE"] local NO_PROXY_IPV6 = var["-NO_PROXY_IPV6"] local NO_LOGIC_LOG = var["-NO_LOGIC_LOG"] local NFTFLAG = var["-NFTFLAG"] -local LOG_FILE = api.LOG_FILE local CACHE_PATH = api.CACHE_PATH local CACHE_FLAG = "dnsmasq_" .. FLAG local CACHE_DNS_PATH = CACHE_PATH .. "/" .. CACHE_FLAG @@ -36,12 +35,7 @@ local function log(...) if NO_LOGIC_LOG == "1" then return end - local f, err = io.open(LOG_FILE, "a") - if f and err == nil then - local str = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ") - f:write(str .. "\n") - f:close() - end + api.log(...) end local function check_dns(domain, dns) diff --git a/luci-app-passwall/root/usr/share/passwall/rule_update.lua b/luci-app-passwall/root/usr/share/passwall/rule_update.lua index 67b5cc3b0..63e0e2ec9 100755 --- a/luci-app-passwall/root/usr/share/passwall/rule_update.lua +++ b/luci-app-passwall/root/usr/share/passwall/rule_update.lua @@ -36,14 +36,10 @@ local v2ray_asset_location = ucic:get_first(name, 'global_rules', "v2ray_locatio local log = function(...) if arg1 then - local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ") if arg1 == "log" then - local f, err = io.open("/tmp/log/passwall.log", "a") - if f and err == nil then - f:write(result .. "\n") - f:close() - end + api.log(...) elseif arg1 == "print" then + local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ") print(result) end end diff --git a/luci-app-passwall/root/usr/share/passwall/subscribe.lua b/luci-app-passwall/root/usr/share/passwall/subscribe.lua index 36fe38e59..536cd4fb0 100755 --- a/luci-app-passwall/root/usr/share/passwall/subscribe.lua +++ b/luci-app-passwall/root/usr/share/passwall/subscribe.lua @@ -83,15 +83,11 @@ local nodeResult = {} -- update result local debug = false local log = function(...) - local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ") if debug == true then + local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ") print(result) else - local f, err = io.open("/tmp/log/" .. appname .. ".log", "a") - if f and err == nil then - f:write(result .. "\n") - f:close() - end + api.log(...) end end