From 3ed1cb6ec675b8703b4687cc66a644341ef092f7 Mon Sep 17 00:00:00 2001 From: CN_SZTL Date: Mon, 9 Mar 2020 03:57:33 +0800 Subject: [PATCH] luci-app-passwall: sync with upstream source --- package/lienol/luci-app-passwall/Makefile | 2 +- .../root/usr/share/passwall/app.sh | 27 +----- .../root/usr/share/passwall/iptables.sh | 25 ++++++ .../root/usr/share/passwall/subscribe.lua | 83 +++++++++---------- 4 files changed, 68 insertions(+), 69 deletions(-) diff --git a/package/lienol/luci-app-passwall/Makefile b/package/lienol/luci-app-passwall/Makefile index 2edbec6653..19a33e225f 100644 --- a/package/lienol/luci-app-passwall/Makefile +++ b/package/lienol/luci-app-passwall/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-passwall PKG_VERSION:=3.6 -PKG_RELEASE:=19 +PKG_RELEASE:=20 PKG_DATE:=20200308 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) 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 66bf3f4a07..3e6d8c2d7a 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 @@ -21,7 +21,6 @@ LUA_API_PATH=/usr/lib/lua/luci/model/cbi/$CONFIG/api API_GEN_TROJAN=$LUA_API_PATH/gen_trojan.lua API_GEN_V2RAY=$LUA_API_PATH/gen_v2ray.lua API_GEN_V2RAY_SHUNT=$LUA_API_PATH/gen_v2ray_shunt.lua -FWI=$(uci get firewall.passwall.path 2>/dev/null) echolog() { local d="$(date "+%Y-%m-%d %H:%M:%S")" @@ -866,28 +865,6 @@ 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 & } @@ -921,10 +898,9 @@ start() { start_dns add_dnsmasq source $APP_PATH/iptables.sh start - gen_include /etc/init.d/dnsmasq restart >/dev/null 2>&1 & - echolog "运行完成!\n" start_crontab + echolog "运行完成!\n" rm -f "$LOCK_FILE" return 0 } @@ -942,7 +918,6 @@ stop() { done clean_log source $APP_PATH/iptables.sh stop - flush_include kill_all v2ray-plugin obfs-local ps -w | grep -v "grep" | grep $CONFIG/test.sh | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 & ps -w | grep -v "grep" | grep $CONFIG/monitor.sh | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 & diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/iptables.sh b/package/lienol/luci-app-passwall/root/usr/share/passwall/iptables.sh index 300c1b5dfd..68a42aab70 100755 --- a/package/lienol/luci-app-passwall/root/usr/share/passwall/iptables.sh +++ b/package/lienol/luci-app-passwall/root/usr/share/passwall/iptables.sh @@ -10,6 +10,7 @@ IPSET_WHITELIST="whitelist" ipt_n="iptables -t nat" ipt_m="iptables -t mangle" ip6t_n="ip6tables -t nat" +FWI=$(uci -q get firewall.passwall.path 2>/dev/null) factor() { if [ -z "$1" ] || [ -z "$2" ]; then @@ -658,12 +659,36 @@ flush_ipset() { ipset -F $IPSET_WHITELIST >/dev/null 2>&1 && ipset -X $IPSET_WHITELIST >/dev/null 2>&1 & } +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 +} + start() { add_firewall_rule + gen_include } stop() { del_firewall_rule + flush_include } case $1 in diff --git a/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua b/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua index 9d28dc8f5a..98e0dfd2b2 100644 --- a/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua +++ b/package/lienol/luci-app-passwall/root/usr/share/passwall/subscribe.lua @@ -6,6 +6,7 @@ -- @author William Chan ------------------------------------------------ require 'nixio' +require 'uci' require 'luci.util' require 'luci.jsonc' require 'luci.sys' @@ -20,8 +21,9 @@ local b64decode = nixio.bin.b64decode local nodeResult = {} -- update result local application = 'passwall' local uciType = 'nodes' -local ucic = luci.model.uci.cursor() +local ucic2 = uci.cursor() local arg2 = arg[2] +ucic2:revert(application) local log = function(...) if arg2 then @@ -42,17 +44,17 @@ end local CONFIG = {} do local function import_config(protocol) - local node_num = ucic:get_first(application, "global_other", protocol .. "_node_num", 1) + local node_num = ucic2:get(application, "@global_other[0]", protocol .. "_node_num") or 1 for i = 1, node_num, 1 do local name = string.upper(protocol) local option = protocol .. "_node" .. i - local szType = "global" + local szType = "@global[0]" local json = { remarks = name .. "节点" .. i, type = szType, option = option, set = function(server) - ucic:set(application, ucic:get_first(application, szType), option, server) + ucic2:set(application, szType, option, server) end } CONFIG[name .. "_NODE" .. i] = json @@ -67,8 +69,10 @@ do if v.get then currentNode = v.get() else - local cfgid = ucic:get_first(application, v.type, v.option) - if cfgid then currentNode = ucic:get_all(application, cfgid) end + local cfgid = ucic2:get(application, v.type, v.option) + if cfgid then + currentNode = ucic2:get_all(application, cfgid) + end end if currentNode then CONFIG[k].currentNode = currentNode @@ -101,13 +105,13 @@ local function split(full, sep) return {} end -- urlencode -local function get_urlencode(c) return sformat("%%%02X", sbyte(c)) end +-- local function get_urlencode(c) return sformat("%%%02X", sbyte(c)) end -local function urlEncode(szText) - local str = szText:gsub("([^0-9a-zA-Z ])", get_urlencode) - str = str:gsub(" ", "+") - return str -end +-- local function urlEncode(szText) +-- local str = szText:gsub("([^0-9a-zA-Z ])", get_urlencode) +-- str = str:gsub(" ", "+") +-- return str +-- end local function get_urldecode(h) return schar(tonumber(h, 16)) end local function UrlDecode(szText) @@ -316,15 +320,14 @@ end local function truncate_nodes() local is_stop = 0 local function clear(type) - local node_num = - ucic:get_first(application, "global_other", type .. "_node_num", 1) + local node_num = ucic2:get(application, "@global_other[0]", type .. "_node_num") or 1 for i = 1, node_num, 1 do - local node = ucic:get_first(application, "global", type.."_node" .. i, nil) - if node and node ~= "nil" then - local is_sub_node = ucic:get(application, node, "is_sub", "0") + local node = ucic2:get(application, "@global[0]", type.."_node" .. i) + if node then + local is_sub_node = ucic2:get(application, node, "is_sub") or 0 if is_sub_node == "1" then is_stop = 1 - ucic:set(application, ucic:get_first(application, 'global'), type.."_node" .. i, "nil") + ucic2:set(application, '@global[0]', type.."_node" .. i, "nil") end end end @@ -333,15 +336,12 @@ local function truncate_nodes() clear("udp") clear("socks5") - ucic:delete_all(application, uciType, function(old) - if old.is_sub and old.is_sub == "1" then - return true - else - return false + ucic2:foreach(application, uciType, function(node) + if (node.is_sub or node.hashkey) and node.add_mode ~= '导入' then + ucic2:delete(application, node['.name']) end end) - ucic:save(application) - luci.sys.call("uci commit " .. application) + ucic2:commit(application) if is_stop == 1 then luci.sys.call("/etc/init.d/" .. application .. " restart > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早 @@ -391,9 +391,9 @@ local function select_node(nodes, config) end -- 还不行 随便找一个 if not server then - server = ucic:get_first(application, uciType) + server = ucic2:get(application, '@' .. uciType .. '[0]') if server then - log('无法找到最匹配的节点,当前已更换为' .. ucic:get_all(application, server).remarks) + log('无法找到最匹配的节点,当前已更换为' .. ucic2:get_all(application, server).remarks) end end if server then @@ -407,23 +407,25 @@ local function update_node() return end -- delet all for subscribe nodes - ucic:foreach(application, uciType, function(node) + ucic2:foreach(application, uciType, function(node) if (node.is_sub or node.hashkey) and node.add_mode ~= '导入' then - ucic:delete(application, node['.name']) + ucic2:delete(application, node['.name']) end end) - for k, v in ipairs(nodeResult) do - for kk, vv in ipairs(v) do - local section = ucic:add(application, uciType) - ucic:tset(application, section, vv) + for _, v in ipairs(nodeResult) do + for _, vv in ipairs(v) do + local cfgid = ucic2:add(application, uciType) + for kkk, vvv in pairs(vv) do + ucic2:set(application, cfgid, kkk, vvv) + end end end - ucic:save(application) - luci.sys.call("uci commit " .. application) + ucic2:commit(application) if next(CONFIG) then local nodes = {} - ucic:foreach(application, uciType, function(node) + local ucic3 = uci.cursor() + ucic3:foreach(application, uciType, function(node) if node.port and node.address and node.remarks then nodes[node['.name']] = node end @@ -431,11 +433,9 @@ local function update_node() for _, config in pairs(CONFIG) do select_node(nodes, config) end - ucic:save(application) - luci.sys.call("uci commit " .. application) + ucic3:commit(application) luci.sys.call("/etc/init.d/" .. application .. " restart > /dev/null 2>&1 &") -- 不加&的话日志会出现的更早 end - end local function parse_link(raw, remark, manual) @@ -460,7 +460,7 @@ local function parse_link(raw, remark, manual) local servers = {} -- SS里面包着 干脆直接这样 for _, server in ipairs(nodes.servers) do - tinsert(servers, setmetatable(server, {__index = extra})) + tinsert(servers, setmetatable(server, { __index = extra })) end nodes = servers else @@ -520,7 +520,7 @@ end local execute = function() -- exec do - ucic:foreach(application, "subscribe_list", function(obj) + ucic2:foreach(application, "subscribe_list", function(obj) local enabled = obj.enabled or nil if enabled and enabled == "1" then local remark = obj.remark @@ -566,4 +566,3 @@ if arg[1] then end end end -