From 5a09275b10c6ee01865d18199c18a2e52731b9bb Mon Sep 17 00:00:00 2001 From: actions Date: Sat, 25 May 2024 17:30:05 +0800 Subject: [PATCH] luci-app-passwall: sync upstream last commit: https://github.com/xiaorouji/openwrt-passwall/commit/3c2d4f2d4fb0b27d1146a2bb849a8609e1d375f6 --- .../passwall/client/node_subscribe_config.lua | 40 +++++++--- .../luasrc/model/cbi/passwall/client/rule.lua | 36 +++++++-- luci-app-passwall/po/zh-cn/passwall.po | 41 +++++++--- .../root/usr/share/passwall/app.sh | 40 ++++++++-- .../root/usr/share/passwall/rule_update.lua | 19 ++++- .../root/usr/share/passwall/subscribe.lua | 10 +++ .../root/usr/share/passwall/tasks.sh | 75 +++++++++++++++++++ patch-luci-app-passwall.patch | 4 +- 8 files changed, 229 insertions(+), 36 deletions(-) create mode 100755 luci-app-passwall/root/usr/share/passwall/tasks.sh diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua b/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua index e13625af4..d4d8c2c48 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua @@ -130,19 +130,41 @@ o = s:option(Flag, "auto_update", translate("Enable auto update subscribe")) o.default = 0 o.rmempty = false ----- Week update rules -o = s:option(ListValue, "week_update", translate("Week update rules")) +---- Week Update +o = s:option(ListValue, "week_update", translate("Update Mode")) +o:value(8, translate("Loop Mode")) o:value(7, translate("Every day")) -for e = 1, 6 do o:value(e, translate("Week") .. e) end -o:value(0, translate("Week") .. translate("day")) -o.default = 0 +o:value(1, translate("Every Monday")) +o:value(2, translate("Every Tuesday")) +o:value(3, translate("Every Wednesday")) +o:value(4, translate("Every Thursday")) +o:value(5, translate("Every Friday")) +o:value(6, translate("Every Saturday")) +o:value(0, translate("Every Sunday")) +o.default = 7 o:depends("auto_update", true) +o.rmempty = true ----- Day update rules -o = s:option(ListValue, "time_update", translate("Day update rules")) -for e = 0, 23 do o:value(e, e .. translate("oclock")) end +---- Time Update +o = s:option(ListValue, "time_update", translate("Update Time(every day)")) +for t = 0, 23 do o:value(t, t .. ":00") end o.default = 0 -o:depends("auto_update", true) +o:depends("week_update", "0") +o:depends("week_update", "1") +o:depends("week_update", "2") +o:depends("week_update", "3") +o:depends("week_update", "4") +o:depends("week_update", "5") +o:depends("week_update", "6") +o:depends("week_update", "7") +o.rmempty = true + +---- Interval Update +o = s:option(ListValue, "interval_update", translate("Update Interval(hour)")) +for t = 1, 24 do o:value(t, t .. " " .. translate("hour")) end +o.default = 2 +o:depends("week_update", "8") +o.rmempty = true o = s:option(Value, "user_agent", translate("User-Agent")) o.default = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0" diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua b/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua index 5823ce3bf..505f6890b 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua @@ -48,18 +48,40 @@ o.default = 0 o.rmempty = false ---- Week Update -o = s:option(ListValue, "week_update", translate("Week update rules")) +o = s:option(ListValue, "week_update", translate("Update Mode")) +o:value(8, translate("Loop Mode")) o:value(7, translate("Every day")) -for e = 1, 6 do o:value(e, translate("Week") .. e) end -o:value(0, translate("Week") .. translate("day")) -o.default = 0 +o:value(1, translate("Every Monday")) +o:value(2, translate("Every Tuesday")) +o:value(3, translate("Every Wednesday")) +o:value(4, translate("Every Thursday")) +o:value(5, translate("Every Friday")) +o:value(6, translate("Every Saturday")) +o:value(0, translate("Every Sunday")) +o.default = 7 o:depends("auto_update", true) +o.rmempty = true ---- Time Update -o = s:option(ListValue, "time_update", translate("Day update rules")) -for e = 0, 23 do o:value(e, e .. translate("oclock")) end +o = s:option(ListValue, "time_update", translate("Update Time(every day)")) +for t = 0, 23 do o:value(t, t .. ":00") end o.default = 0 -o:depends("auto_update", true) +o:depends("week_update", "0") +o:depends("week_update", "1") +o:depends("week_update", "2") +o:depends("week_update", "3") +o:depends("week_update", "4") +o:depends("week_update", "5") +o:depends("week_update", "6") +o:depends("week_update", "7") +o.rmempty = true + +---- Interval Update +o = s:option(ListValue, "interval_update", translate("Update Interval(hour)")) +for t = 1, 24 do o:value(t, t .. " " .. translate("hour")) end +o.default = 2 +o:depends("week_update", "8") +o.rmempty = true if has_xray or has_singbox then o = s:option(Value, "v2ray_location_asset", translate("Location of V2ray/Xray asset"), translate("This variable specifies a directory where geoip.dat and geosite.dat files are.")) diff --git a/luci-app-passwall/po/zh-cn/passwall.po b/luci-app-passwall/po/zh-cn/passwall.po index 11e0f5c34..4cd5cdfe5 100644 --- a/luci-app-passwall/po/zh-cn/passwall.po +++ b/luci-app-passwall/po/zh-cn/passwall.po @@ -838,23 +838,44 @@ msgstr "规则版本" msgid "Enable auto update rules" msgstr "开启自动更新规则" -msgid "Week update rules" -msgstr "更新时间星期" +msgid "Update Time(every day)" +msgstr "更新时间(每天)" -msgid "Day update rules" -msgstr "更新时间小时" +msgid "Update Interval(hour)" +msgstr "更新间隔(小时)" + +msgid "Update Mode" +msgstr "更新模式" + +msgid "Loop Mode" +msgstr "循环" msgid "Every day" msgstr "每天" -msgid "day" -msgstr "日" +msgid "Every Monday" +msgstr "每周一" -msgid "Week" -msgstr "周" +msgid "Every Tuesday" +msgstr "每周二" -msgid "oclock" -msgstr "点" +msgid "Every Wednesday" +msgstr "每周三" + +msgid "Every Thursday" +msgstr "每周四" + +msgid "Every Friday" +msgstr "每周五" + +msgid "Every Saturday" +msgstr "每周六" + +msgid "Every Sunday" +msgstr "每周日" + +msgid "hour" +msgstr "小时" msgid "Location of V2ray/Xray asset" msgstr "V2ray/Xray 资源文件目录" diff --git a/luci-app-passwall/root/usr/share/passwall/app.sh b/luci-app-passwall/root/usr/share/passwall/app.sh index 4a890d546..9b6319de5 100755 --- a/luci-app-passwall/root/usr/share/passwall/app.sh +++ b/luci-app-passwall/root/usr/share/passwall/app.sh @@ -1143,19 +1143,36 @@ clean_log() { } clean_crontab() { + [ -f "/tmp/lock/${CONFIG}_cron.lock" ] && return touch /etc/crontabs/root #sed -i "/${CONFIG}/d" /etc/crontabs/root >/dev/null 2>&1 sed -i "/$(echo "/etc/init.d/${CONFIG}" | sed 's#\/#\\\/#g')/d" /etc/crontabs/root >/dev/null 2>&1 sed -i "/$(echo "lua ${APP_PATH}/rule_update.lua log" | sed 's#\/#\\\/#g')/d" /etc/crontabs/root >/dev/null 2>&1 sed -i "/$(echo "lua ${APP_PATH}/subscribe.lua start" | sed 's#\/#\\\/#g')/d" /etc/crontabs/root >/dev/null 2>&1 + + pgrep -af "${CONFIG}/" | awk '/tasks\.sh/{print $1}' | xargs kill -9 >/dev/null 2>&1 + rm -rf /tmp/lock/${CONFIG}_tasks.lock } start_crontab() { + if [ "$ENABLED_DEFAULT_ACL" == 1 ] || [ "$ENABLED_ACLS" == 1 ]; then + start_daemon=$(config_t_get global_delay start_daemon 0) + [ "$start_daemon" = "1" ] && $APP_PATH/monitor.sh > /dev/null 2>&1 & + fi + + [ -f "/tmp/lock/${CONFIG}_cron.lock" ] && { + rm -rf "/tmp/lock/${CONFIG}_cron.lock" + echolog "当前为计划任务自动运行,不重新配置定时任务。" + return + } + clean_crontab + [ "$ENABLED" != 1 ] && { /etc/init.d/cron restart return } + auto_on=$(config_t_get global_delay auto_on 0) if [ "$auto_on" = "1" ]; then time_off=$(config_t_get global_delay time_off) @@ -1181,7 +1198,11 @@ start_crontab() { if [ "$autoupdate" = "1" ]; then local t="0 $dayupdate * * $weekupdate" [ "$weekupdate" = "7" ] && t="0 $dayupdate * * *" - echo "$t lua $APP_PATH/rule_update.lua log > /dev/null 2>&1 &" >>/etc/crontabs/root + if [ "$weekupdate" = "8" ]; then + update_loop=1 + else + echo "$t lua $APP_PATH/rule_update.lua log all cron > /dev/null 2>&1 &" >>/etc/crontabs/root + fi echolog "配置定时任务:自动更新规则。" fi @@ -1202,17 +1223,23 @@ start_crontab() { for name in $(ls ${TMP_SUB_PATH}); do week_update=$(echo $name | awk -F '_' '{print $1}') time_update=$(echo $name | awk -F '_' '{print $2}') + cfgids=$(echo -n $(cat ${TMP_SUB_PATH}/${name}) | sed 's# #,#g') local t="0 $time_update * * $week_update" [ "$week_update" = "7" ] && t="0 $time_update * * *" - cfgids=$(echo -n $(cat ${TMP_SUB_PATH}/${name}) | sed 's# #,#g') - echo "$t lua $APP_PATH/subscribe.lua start $cfgids > /dev/null 2>&1 &" >>/etc/crontabs/root + if [ "$week_update" = "8" ]; then + update_loop=1 + else + echo "$t lua $APP_PATH/subscribe.lua start $cfgids cron > /dev/null 2>&1 &" >>/etc/crontabs/root + fi done rm -rf $TMP_SUB_PATH } if [ "$ENABLED_DEFAULT_ACL" == 1 ] || [ "$ENABLED_ACLS" == 1 ]; then - start_daemon=$(config_t_get global_delay start_daemon 0) - [ "$start_daemon" = "1" ] && $APP_PATH/monitor.sh > /dev/null 2>&1 & + [ "$update_loop" = "1" ] && { + $APP_PATH/tasks.sh > /dev/null 2>&1 & + echolog "自动更新:启动循环更新进程。" + } else echolog "运行于非代理模式,仅允许服务启停的定时任务。" fi @@ -1221,6 +1248,7 @@ start_crontab() { } stop_crontab() { + [ -f "/tmp/lock/${CONFIG}_cron.lock" ] && return clean_crontab /etc/init.d/cron restart #echolog "清除定时执行命令。" @@ -1717,7 +1745,7 @@ stop() { delete_ip2route kill_all v2ray-plugin obfs-local pgrep -f "sleep.*(6s|9s|58s)" | xargs kill -9 >/dev/null 2>&1 - pgrep -af "${CONFIG}/" | awk '! /app\.sh|subscribe\.lua|rule_update\.lua/{print $1}' | xargs kill -9 >/dev/null 2>&1 + pgrep -af "${CONFIG}/" | awk '! /app\.sh|subscribe\.lua|rule_update\.lua|tasks\.sh/{print $1}' | xargs kill -9 >/dev/null 2>&1 unset V2RAY_LOCATION_ASSET unset XRAY_LOCATION_ASSET stop_crontab 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 8750d96ce..4db7e26ae 100755 --- a/luci-app-passwall/root/usr/share/passwall/rule_update.lua +++ b/luci-app-passwall/root/usr/share/passwall/rule_update.lua @@ -8,6 +8,8 @@ local jsonc = require "luci.jsonc" local name = 'passwall' local api = require ("luci.passwall.api") local arg1 = arg[1] +local arg2 = arg[2] +local arg3 = arg[3] local rule_path = "/usr/share/" .. name .. "/rules" local reboot = 0 @@ -35,6 +37,10 @@ local geosite_api = "https://api.github.com/repos/Loyalsoldier/v2ray-rules-dat/ local asset_location = ucic:get_first(name, 'global_rules', "v2ray_location_asset", "/usr/share/v2ray/") local use_nft = ucic:get(name, "@global_forwarding[0]", "use_nft") or "0" +if arg3 == "cron" then + arg2 = nil +end + local log = function(...) if arg1 then if arg1 == "log" then @@ -369,8 +375,8 @@ local function fetch_geosite() return 0 end -if arg[2] then - string.gsub(arg[2], '[^' .. "," .. ']+', function(w) +if arg2 then + string.gsub(arg2, '[^' .. "," .. ']+', function(w) if w == "gfwlist" then gfwlist_update = 1 end @@ -459,6 +465,15 @@ ucic:save(name) luci.sys.call("uci commit " .. name) if reboot == 1 then + if arg3 == "cron" then + local f = io.open("/var/lock/" .. name .. ".lock", "r") + if f == nil then + luci.sys.call("touch /tmp/lock/" .. name .. "_cron.lock") + else + f:close() + end + end + log("重启服务,应用新的规则。") if use_nft == "1" then luci.sys.call("sh /usr/share/" .. name .. "/nftables.sh flush_nftset_reload > /dev/null 2>&1 &") diff --git a/luci-app-passwall/root/usr/share/passwall/subscribe.lua b/luci-app-passwall/root/usr/share/passwall/subscribe.lua index 82af8a438..8fe660179 100755 --- a/luci-app-passwall/root/usr/share/passwall/subscribe.lua +++ b/luci-app-passwall/root/usr/share/passwall/subscribe.lua @@ -1164,6 +1164,16 @@ local function update_node(manual) uci:commit(appname) end + + if arg[3] == "cron" then + local f = io.open("/var/lock/" .. appname .. ".lock", "r") + if f == nil then + luci.sys.call("touch /tmp/lock/" .. appname .. "_cron.lock") + else + f:close() + end + end + luci.sys.call("/etc/init.d/" .. appname .. " restart > /dev/null 2>&1 &") end diff --git a/luci-app-passwall/root/usr/share/passwall/tasks.sh b/luci-app-passwall/root/usr/share/passwall/tasks.sh new file mode 100755 index 000000000..2597a4170 --- /dev/null +++ b/luci-app-passwall/root/usr/share/passwall/tasks.sh @@ -0,0 +1,75 @@ +#!/bin/sh + +## 循环更新脚本 + +CONFIG=passwall +APP_PATH=/usr/share/$CONFIG +TMP_PATH=/tmp/etc/$CONFIG +LOCK_FILE=/tmp/lock/${CONFIG}_tasks.lock +CFG_UPDATE_INT=0 + +config_n_get() { + local ret=$(uci -q get "${CONFIG}.${1}.${2}" 2>/dev/null) + echo "${ret:=$3}" +} + +config_t_get() { + local index=${4:-0} + local ret=$(uci -q get "${CONFIG}.@${1}[${index}].${2}" 2>/dev/null) + echo "${ret:=${3}}" +} + +exec 99>"$LOCK_FILE" +flock -n 99 +if [ "$?" != 0 ]; then + exit 0 +fi + +while true +do + + if [ "$CFG_UPDATE_INT" -ne 0 ]; then + + autoupdate=$(config_t_get global_rules auto_update) + weekupdate=$(config_t_get global_rules week_update) + hourupdate=$(config_t_get global_rules interval_update) + hourupdate=$(expr "$hourupdate" \* 60) + if [ "$autoupdate" = "1" ]; then + [ "$weekupdate" = "8" ] && { + [ "$(expr "$CFG_UPDATE_INT" % "$hourupdate")" -eq 0 ] && lua $APP_PATH/rule_update.lua log all cron > /dev/null 2>&1 & + } + fi + + TMP_SUB_PATH=$TMP_PATH/sub_tasks + mkdir -p $TMP_SUB_PATH + for item in $(uci show ${CONFIG} | grep "=subscribe_list" | cut -d '.' -sf 2 | cut -d '=' -sf 1); do + if [ "$(config_n_get $item auto_update 0)" = "1" ]; then + cfgid=$(uci show ${CONFIG}.$item | head -n 1 | cut -d '.' -sf 2 | cut -d '=' -sf 1) + remark=$(config_n_get $item remark) + week_update=$(config_n_get $item week_update) + hour_update=$(config_n_get $item interval_update) + echo "$cfgid" >> $TMP_SUB_PATH/${week_update}_${hour_update} + fi + done + + [ -d "${TMP_SUB_PATH}" ] && { + for name in $(ls ${TMP_SUB_PATH}); do + week_update=$(echo $name | awk -F '_' '{print $1}') + hour_update=$(echo $name | awk -F '_' '{print $2}') + hour_update=$(expr "$hour_update" \* 60) + cfgids=$(echo -n $(cat ${TMP_SUB_PATH}/${name}) | sed 's# #,#g') + [ "$week_update" = "8" ] && { + [ "$(expr "$CFG_UPDATE_INT" % "$hour_update")" -eq 0 ] && lua $APP_PATH/subscribe.lua start $cfgids cron > /dev/null 2>&1 & + } + + done + rm -rf $TMP_SUB_PATH + } + + fi + + CFG_UPDATE_INT=$(expr "$CFG_UPDATE_INT" + 10) + + sleep 600 + +done 2>/dev/null diff --git a/patch-luci-app-passwall.patch b/patch-luci-app-passwall.patch index e5c2559cf..7d14184e0 100644 --- a/patch-luci-app-passwall.patch +++ b/patch-luci-app-passwall.patch @@ -133,7 +133,7 @@ index 9682502..c8688e2 100644 -- [[ Subscribe Settings ]]-- s = m:section(TypedSection, "global_subscribe", "") diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua b/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua -index 55bb691..e13625a 100644 +index 4d07ef4..d4d8c2c 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/client/node_subscribe_config.lua @@ -45,7 +45,6 @@ end @@ -157,7 +157,7 @@ index 72997c3..04b70f3 100644 -- [[ Delay Settings ]]-- s = m:section(TypedSection, "global_delay", translate("Delay Settings")) diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua b/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua -index 375cf9c..5823ce3 100644 +index 8e6947d..505f689 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/client/rule.lua @@ -4,8 +4,6 @@ local has_xray = api.finded_com("xray")