From 475782665a4008af0aa17bd060d636390b97628f Mon Sep 17 00:00:00 2001 From: CN_SZTL Date: Mon, 24 Feb 2020 00:32:55 +0800 Subject: [PATCH] luci-app-passwall: sync with upstream source --- package/lienol/luci-app-passwall/Makefile | 4 +- .../luasrc/controller/passwall.lua | 5 +- .../root/etc/uci-defaults/luci-app-passwall | 6 + .../root/usr/share/passwall/app.sh | 31 ++- .../root/usr/share/passwall/rule_update.lua | 151 +++++++++++++++ .../root/usr/share/passwall/rule_update.sh | 181 ------------------ 6 files changed, 189 insertions(+), 189 deletions(-) create mode 100644 package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.lua delete mode 100755 package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.sh diff --git a/package/lienol/luci-app-passwall/Makefile b/package/lienol/luci-app-passwall/Makefile index d55bb148cd..a01d91050d 100644 --- a/package/lienol/luci-app-passwall/Makefile +++ b/package/lienol/luci-app-passwall/Makefile @@ -7,8 +7,8 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-passwall PKG_VERSION:=3.5 -PKG_RELEASE:=19 -PKG_DATE:=20200222 +PKG_RELEASE:=20 +PKG_DATE:=20200223 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) diff --git a/package/lienol/luci-app-passwall/luasrc/controller/passwall.lua b/package/lienol/luci-app-passwall/luasrc/controller/passwall.lua index 31b7664530..18e03afcf1 100644 --- a/package/lienol/luci-app-passwall/luasrc/controller/passwall.lua +++ b/package/lienol/luci-app-passwall/luasrc/controller/passwall.lua @@ -39,7 +39,7 @@ function index() entry({"admin", "vpn", "passwall", "rule_list"}, cbi("passwall/rule_list", {autoapply = true}), _("Set Blacklist And Whitelist"), 98).leaf = true - entry({"admin", "vpn", "passwall", "log"}, cbi("passwall/log"), + entry({"admin", "vpn", "passwall", "log"}, form("passwall/log"), _("Watch Logs"), 99).leaf = true entry({"admin", "vpn", "passwall", "node_config"}, cbi("passwall/node_config")).leaf = true @@ -274,8 +274,7 @@ end function update_rules() local update = luci.http.formvalue("update") - luci.sys.call("nohup /usr/share/passwall/rule_update.sh '" .. update .. - "' 2>&1 &") + luci.sys.call("lua /usr/share/passwall/rule_update.lua log '" .. update .. "' > /dev/null 2>&1 &") end function kcptun_check() diff --git a/package/lienol/luci-app-passwall/root/etc/uci-defaults/luci-app-passwall b/package/lienol/luci-app-passwall/root/etc/uci-defaults/luci-app-passwall index 89932c7dd0..efcd5ee0a7 100755 --- a/package/lienol/luci-app-passwall/root/etc/uci-defaults/luci-app-passwall +++ b/package/lienol/luci-app-passwall/root/etc/uci-defaults/luci-app-passwall @@ -5,6 +5,12 @@ uci -q batch <<-EOF >/dev/null add ucitrack passwall set ucitrack.@passwall[-1].init=passwall commit ucitrack + delete firewall.passwall + set firewall.passwall=include + set firewall.passwall.type=script + set firewall.passwall.path=/var/etc/passwall.include + set firewall.passwall.reload=1 + commit firewall EOF echo "" > /etc/dnsmasq.conf diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh b/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh index c62f4fdafe..2be227ed30 100755 --- a/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh +++ b/package/lienol/luci-app-passwall/root/usr/share/passwall/app.sh @@ -19,6 +19,7 @@ DNS_PORT=7913 LUA_API_PATH=/usr/lib/lua/luci/model/cbi/$CONFIG/api API_GEN_V2RAY=$LUA_API_PATH/gen_v2ray_client_config.lua API_GEN_TROJAN=$LUA_API_PATH/gen_trojan_client_config.lua +FWI=$(uci get firewall.passwall.path 2>/dev/null) echolog() { local d="$(date "+%Y-%m-%d %H:%M:%S")" @@ -584,7 +585,7 @@ start_crontab() { if [ "$autoupdate" = "1" ]; then local t="0 $dayupdate * * $weekupdate" [ "$weekupdate" = "7" ] && t="0 $dayupdate * * *" - echo "$t $APP_PATH/rule_update.sh" >>/etc/crontabs/root + echo "$t lua $APP_PATH/rule_update.lua nil log > /dev/null 2>&1 &" >>/etc/crontabs/root echolog "配置定时任务:自动更新规则。" fi @@ -846,7 +847,7 @@ stop_dnsmasq() { rm -rf /var/dnsmasq.d/dnsmasq-$CONFIG.conf rm -rf $DNSMASQ_PATH/dnsmasq-$CONFIG.conf rm -rf $TMP_DNSMASQ_PATH - /etc/init.d/dnsmasq reload >/dev/null 2>&1 & + /etc/init.d/dnsmasq restart >/dev/null 2>&1 & } start_haproxy() { @@ -957,6 +958,28 @@ start_haproxy() { } } +flush_include() { + echo '#!/bin/sh' >$FWI +} + +gen_include() { + flush_include + extract_rules() { + echo "*$1" + iptables-save -t $1 | grep PSW | \ + sed -e "s/^-A \(OUTPUT\|PREROUTING\)/-I \1 1/" + echo 'COMMIT' + } + cat <<-EOF >>$FWI + iptables-save -c | grep -v "PSW" | iptables-restore -c + iptables-restore -n <<-EOT + $(extract_rules nat) + $(extract_rules mangle) + EOT + EOF + return 0 +} + kill_all() { kill -9 $(pidof $@) >/dev/null 2>&1 & } @@ -990,8 +1013,9 @@ start() { start_dns add_dnsmasq source $APP_PATH/iptables.sh start + gen_include start_crontab - /etc/init.d/dnsmasq reload >/dev/null 2>&1 & + /etc/init.d/dnsmasq restart >/dev/null 2>&1 & echolog "运行完成!\n" rm -f "$LOCK_FILE" return 0 @@ -1010,6 +1034,7 @@ stop() { done clean_log source $APP_PATH/iptables.sh stop + flush_include kill_all v2ray-plugin obfs-local ps -w | grep -E "$CONFIG_PATH" | grep -v "grep" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 & rm -rf $TMP_DNSMASQ_PATH $CONFIG_PATH diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.lua b/package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.lua new file mode 100644 index 0000000000..844b8c9b92 --- /dev/null +++ b/package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.lua @@ -0,0 +1,151 @@ +#!/usr/bin/lua + +require 'luci.sys' +local luci = luci +local ucic = luci.model.uci.cursor() +local name = 'passwall' +local arg1 = arg[1] + +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("/var/log/passwall.log", "a") + if f and err == nil then + f:write(result .. "\n") + f:close() + end + elseif arg1 == "print" then + print(result) + end + end +end + +-- trim +local function trim(text) + if not text or text == "" then return "" end + return (string.gsub(text, "^%s*(.-)%s*$", "%1")) +end +-- wget +local function wget(url, file) + local cmd = "/usr/bin/wget --no-check-certificate -t 3 -T 10 -O" + if file then + cmd = cmd .. " " .. file .. " " .. url + else + cmd = cmd .. "- " .. url + end + local stdout = luci.sys.exec(cmd) + return trim(stdout) +end + +local rule_path = "/etc/config/" .. name .. "_rule" +local url_main = "https://raw.githubusercontent.com/hq450/fancyss/master/rules" +local reboot = 0 +local gfwlist_update = 0 +local chnroute_update = 0 +local chnlist_update = 0 +if arg[2] then + if arg[2]:find("gfwlist") then + gfwlist_update = 1 + end + if arg[2]:find("chnroute") then + chnroute_update = 1 + end + if arg[2]:find("chnlist") then + chnlist_update = 1 + end +else + gfwlist_update = ucic:get_first(name, 'global_rules', "gfwlist_update", 1) + chnroute_update = ucic:get_first(name, 'global_rules', "chnroute_update", 1) + chnlist_update = ucic:get_first(name, 'global_rules', "chnlist_update", 1) +end +if gfwlist_update == 0 and chnroute_update == 0 and chnlist_update == 0 then + os.exit(0) +end +local version = wget(url_main .. "/version1") +if version then + if tonumber(gfwlist_update) == 1 then + local gfwlist = luci.sys.exec("echo -n $(echo '" .. version .. "' | sed -n 1p)") + local new_version = luci.sys.exec("echo -n $(echo '" .. gfwlist .. "' | awk '{print $1}')") + local new_md5 = luci.sys.exec("echo -n $(echo '" .. gfwlist .. "' | awk '{print $3}')") + if new_version ~= "" and new_md5 ~= "" then + local old_md5 = luci.sys.exec("echo -n $(md5sum " .. rule_path .. "/gfwlist.conf | awk '{print $1}')") + if old_md5 ~= new_md5 then + log("开始更新gfwlist...") + wget(url_main .. "/gfwlist.conf", "/tmp/gfwlist_tmp") + local download_md5 = luci.sys.exec("echo -n $([ -f '/tmp/gfwlist_tmp' ] && md5sum /tmp/gfwlist_tmp | awk '{print $1}')") + if download_md5 == new_md5 then + luci.sys.exec("mv -f /tmp/gfwlist_tmp " .. rule_path .. "/gfwlist.conf") + uci.set(name, ucic:get_first(name, 'global_rules'), "gfwlist_version", new_version) + ucic:commit(name) + reboot = 1 + log("更新gfwlist成功...") + end + else + log("gfwlist版本一致,不用更新。") + end + else + log("gfwlist文件下载失败!") + end + end + + if tonumber(chnroute_update) == 1 then + local chnroute = luci.sys.exec("echo -n $(echo '" .. version .. "' | sed -n 2p)") + local new_version = luci.sys.exec("echo -n $(echo '" .. chnroute .. "' | awk '{print $1}')") + local new_md5 = luci.sys.exec("echo -n $(echo '" .. chnroute .. "' | awk '{print $3}')") + if new_version ~= "" and new_md5 ~= "" then + local old_md5 = luci.sys.exec("echo -n $(md5sum " .. rule_path .. "/chnroute | awk '{print $1}')") + if old_md5 ~= new_md5 then + log("开始更新chnroute...") + wget(url_main .. "/chnroute.txt", "/tmp/chnroute_tmp") + local download_md5 = luci.sys.exec("echo -n $([ -f '/tmp/chnroute_tmp' ] && md5sum /tmp/chnroute_tmp | awk '{print $1}')") + if download_md5 == new_md5 then + luci.sys.exec("mv -f /tmp/chnroute_tmp " .. rule_path .. "/chnroute") + uci.set(name, ucic:get_first(name, 'global_rules'), "chnroute_version", new_version) + ucic:commit(name) + reboot = 1 + log("更新chnroute成功...") + end + else + log("chnroute版本一致,不用更新。") + end + else + log("chnroute文件下载失败!") + end + end + + if tonumber(chnlist_update) == 1 then + local chnlist = luci.sys.exec("echo -n $(echo '" .. version .. "' | sed -n 4p)") + local new_version = luci.sys.exec("echo -n $(echo '" .. chnlist .. "' | awk '{print $1}')") + local new_md5 = luci.sys.exec("echo -n $(echo '" .. chnlist .. "' | awk '{print $3}')") + if new_version ~= "" and new_md5 ~= "" then + local old_md5 = luci.sys.exec("echo -n $(md5sum " .. rule_path .. "/chnlist | awk '{print $1}')") + if old_md5 ~= new_md5 then + log("开始更新chnlist...") + wget(url_main .. "/cdn.txt", "/tmp/chnlist_tmp") + local download_md5 = luci.sys.exec("echo -n $([ -f '/tmp/chnlist_tmp' ] && md5sum /tmp/chnlist_tmp | awk '{print $1}')") + if download_md5 == new_md5 then + luci.sys.exec("mv -f /tmp/chnlist_tmp " .. rule_path .. "/chnlist") + uci.set(name, ucic:get_first(name, 'global_rules'), "chnlist_version", new_version) + ucic:commit(name) + reboot = 1 + log("更新chnlist成功...") + end + else + log("chnlist版本一致,不用更新。") + end + else + log("chnlist文件下载失败!") + end + end +end +uci.set(name, ucic:get_first(name, 'global_rules'), "gfwlist_update", gfwlist_update) +uci.set(name, ucic:get_first(name, 'global_rules'), "chnroute_update", chnroute_update) +uci.set(name, ucic:get_first(name, 'global_rules'), "chnlist_update", chnlist_update) +ucic:commit(name) + +if reboot == 1 then + log("重启服务,应用新的规则。") + luci.sys.call("/usr/share/" .. name .. "/iptables.sh flush_ipset && /etc/init.d/" .. name .. " restart") +end \ No newline at end of file diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.sh b/package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.sh deleted file mode 100755 index 4117ed9cec..0000000000 --- a/package/lienol/luci-app-passwall/root/usr/share/passwall/rule_update.sh +++ /dev/null @@ -1,181 +0,0 @@ -#!/bin/sh - -CONFIG=passwall -LOG_FILE=/var/log/$CONFIG.log -RULE_PATH=/etc/config/${CONFIG}_rule -Date=$(date "+%Y-%m-%d %H:%M:%S") - -url_main="https://raw.githubusercontent.com/hq450/fancyss/master/rules" - -update=$1 -gfwlist_update=0 -chnroute_update=0 -if [ -n "$update" ]; then - [ -n "$(echo $update | grep "gfwlist_update")" ] && gfwlist_update=1 - [ -n "$(echo $update | grep "chnroute_update")" ] && chnroute_update=1 - [ -n "$(echo $update | grep "chnlist_update")" ] && chnlist_update=1 -else - gfwlist_update=$(uci -q get $CONFIG.@global_rules[0].gfwlist_update) - chnroute_update=$(uci -q get $CONFIG.@global_rules[0].chnroute_update) - chnlist_update=$(uci -q get $CONFIG.@global_rules[0].chnlist_update) -fi - -if [ "$gfwlist_update" == 0 -a "$chnroute_update" == 0 -a "$chnlist_update" == 0 ]; then - exit -fi - -uci_get_by_type() { - local index=0 - if [ -n $4 ]; then - index=$4 - fi - local ret=$(uci -q get $CONFIG.@$1[$index].$2 2>/dev/null) - echo ${ret:=$3} -} - -get_url() { - local url=$1 - local save_path=$2 - status=$(/usr/bin/curl -w %{http_code} --connect-timeout 5 --retry 1 $url --silent -o $save_path) - [ "$?" != 0 ] && { - status=$(/usr/bin/wget -q --no-check-certificate --timeout=5 --tries 1 $url -O $save_path) - [ "$?" == 0 ] && status=200 - } - echo $status -} - -# rule update -echo $Date: 开始更新规则,请等待... > $LOG_FILE -status=$(get_url $url_main/version1 /tmp/version1) -[ -z "$status" ] && { - echo $Date: 无法访问更新接口,请更新接口! >> $LOG_FILE - exit -} -online_content=$(cat /tmp/version1 2>/dev/null) -if [ -z "$online_content" ]; then - rm -rf /tmp/version1 - echo $Date: 没有检测到在线版本,可能是访问github有问题! >> $LOG_FILE - exit -fi - -# update gfwlist -if [ "$gfwlist_update" == 1 ]; then - gfwlist=$(cat /tmp/version1 | sed -n 1p) - version_gfwlist2=$(echo $gfwlist | sed 's/ /\n/g' | sed -n 1p) - md5sum_gfwlist2=$(echo $gfwlist | sed 's/ /\n/g' | tail -n 2 | head -n 1) - local_md5sum_gfwlist=$(md5sum $RULE_PATH/gfwlist.conf | awk '{print $1}') - if [ ! -z "$version_gfwlist2" ]; then - version_gfwlist1=$(uci_get_by_type global_rules gfwlist_version) - if [ "$version_gfwlist1" != "$version_gfwlist2" -o "$md5sum_gfwlist2" != "$local_md5sum_gfwlist" ]; then - echo $Date: 检测到新版本gfwlist,开始更新... >> $LOG_FILE - echo $Date: 下载gfwlist到临时文件... >> $LOG_FILE - status=$(get_url $url_main/gfwlist.conf /tmp/gfwlist.conf) - [ -z "$status" ] && { - echo $Date: 无法访问更新接口,请更新接口! >> $LOG_FILE - exit - } - md5sum_gfwlist1=$(md5sum /tmp/gfwlist.conf | sed 's/ /\n/g' | sed -n 1p) - if [ "$md5sum_gfwlist1"x = "$md5sum_gfwlist2"x ]; then - echo $Date: 下载完成,校验通过,将临时文件覆盖到原始gfwlist文件 >> $LOG_FILE - mv /tmp/gfwlist.conf $RULE_PATH/gfwlist.conf - uci set $CONFIG.@global_rules[0].gfwlist_version=$version_gfwlist2 - rm -rf /tmp/dnsmasq.d/gfwlist.conf - reboot="1" - echo $Date: 你的gfwlist已经更新到最新了哦~ >> $LOG_FILE - else - echo $Date: 下载完成,但是校验没有通过! >> $LOG_FILE - fi - else - echo $Date: 检测到gfwlist本地版本号和在线版本号相同,不用更新! >> $LOG_FILE - fi - else - echo $Date: gfwlist文件下载失败! >> $LOG_FILE - fi - rm -rf /tmp/gfwlist.conf -fi - -# update chnroute -if [ "$chnroute_update" == 1 ]; then - chnroute=$(cat /tmp/version1 | sed -n 2p) - version_chnroute2=$(echo $chnroute | sed 's/ /\n/g' | sed -n 1p) - md5sum_chnroute2=$(echo $chnroute | sed 's/ /\n/g' | tail -n 2 | head -n 1) - local_md5sum_chnroute=$(md5sum $RULE_PATH/chnroute | awk '{print $1}') - if [ ! -z "$version_chnroute2" ]; then - version_chnroute1=$(uci_get_by_type global_rules chnroute_version) - if [ "$version_chnroute1" != "$version_chnroute2" -o "$md5sum_chnroute2" != "$local_md5sum_chnroute" ]; then - echo $Date: 检测到新版本chnroute,开始更新... >> $LOG_FILE - echo $Date: 下载chnroute到临时文件... >> $LOG_FILE - status=$(get_url $url_main/chnroute.txt /tmp/chnroute) - [ -z "$status" ] && { - echo $Date: 无法访问更新接口,请更新接口! >> $LOG_FILE - exit - } - md5sum_chnroute1=$(md5sum /tmp/chnroute | sed 's/ /\n/g' | sed -n 1p) - if [ "$md5sum_chnroute1"x = "$md5sum_chnroute2"x ]; then - echo $Date: 下载完成,校验通过,将临时文件覆盖到原始chnroute文件 >> $LOG_FILE - mv /tmp/chnroute $RULE_PATH/chnroute - uci set $CONFIG.@global_rules[0].chnroute_version=$version_chnroute2 - ipset flush chnroute - ipset destroy chnroute - reboot="1" - echo $Date: 你的chnroute已经更新到最新了哦~ >> $LOG_FILE - else - echo $Date: 下载完成,但是校验没有通过! >> $LOG_FILE - fi - else - echo $Date: 检测到chnroute本地版本号和在线版本号相同,不用更新! >> $LOG_FILE - fi - else - echo $Date: chnroute文件下载失败! >> $LOG_FILE - fi - rm -rf /tmp/chnroute -fi - -# update chnlist -if [ "$chnlist_update" == 1 ]; then - chnlist=$(cat /tmp/version1 | sed -n 4p) - version_chnlist2=$(echo $chnlist | sed 's/ /\n/g' | sed -n 1p) - md5sum_chnlist2=$(echo $chnlist | sed 's/ /\n/g' | tail -n 2 | head -n 1) - local_md5sum_chnlist=$(md5sum $RULE_PATH/chnlist | awk '{print $1}') - if [ ! -z "$version_chnlist2" ]; then - version_chnlist1=$(uci_get_by_type global_rules chnlist_version) - if [ "$version_chnlist1" != "$version_chnlist2" -o "$md5sum_chnlist2" != "$local_md5sum_chnlist" ]; then - echo $Date: 检测到新版本chnlist,开始更新... >> $LOG_FILE - echo $Date: 下载chnlist到临时文件... >> $LOG_FILE - status=$(get_url $url_main/cdn.txt /tmp/chnlist) - [ -z "$status" ] && { - echo $Date: 无法访问更新接口,请更新接口! >> $LOG_FILE - exit - } - md5sum_chnlist1=$(md5sum /tmp/chnlist | sed 's/ /\n/g' | sed -n 1p) - if [ "$md5sum_chnlist1"x = "$md5sum_chnlist2"x ]; then - echo $Date: 下载完成,校验通过,将临时文件覆盖到原始chnlist文件 >> $LOG_FILE - mv /tmp/chnlist $RULE_PATH/chnlist - uci set $CONFIG.@global_rules[0].chnlist_version=$version_chnlist2 - reboot="1" - echo $Date: 你的chnlist已经更新到最新了哦~ >> $LOG_FILE - else - echo $Date: 下载完成,但是校验没有通过! >> $LOG_FILE - fi - else - echo $Date: 检测到chnlist本地版本号和在线版本号相同,不用更新! >> $LOG_FILE - fi - else - echo $Date: chnlist文件下载失败! >> $LOG_FILE - fi - rm -rf /tmp/chnlist -fi - -echo $Date: 更新进程运行完毕! >> $LOG_FILE -rm -rf /tmp/version1 -# write number -uci set $CONFIG.@global_rules[0].gfwlist_update=$gfwlist_update -uci set $CONFIG.@global_rules[0].chnroute_update=$chnroute_update -uci set $CONFIG.@global_rules[0].chnlist_update=$chnlist_update -uci commit -# reboot -if [ "$reboot" == "1" ]; then - echo $Date: 重启软件,以应用新的规则文件!请稍后! >> $LOG_FILE - /etc/init.d/$CONFIG restart -fi -exit